Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to specify the rules for an optional WAF to attach to the app #23

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const descriptionWithTag = (tag?: string) =>
// to point to other PR branches etc
const DEV_DEPLOYED_IMAGE_TAG = "0.4.4";

let rulePriorityCounter = 0;

/**
* Stack for dev
*/
Expand Down Expand Up @@ -64,5 +66,91 @@ new ElsaDataStack(
enableAccessPoints: true,
},
databaseName: "elsa_data",
wafRules: [
{
name: "LimitRequests100",
priority: rulePriorityCounter++,
action: {
block: {},
},
statement: {
rateBasedStatement: {
// given our site is not one where you'd expect high traffic - we set this to
// the minimum, and we will see how that plays out
limit: 100,
aggregateKeyType: "IP",
},
},
visibilityConfig: {
sampledRequestsEnabled: true,
cloudWatchMetricsEnabled: true,
metricName: "LimitRequests100",
},
},
{
name: "AllowedCountriesOnly",
priority: rulePriorityCounter++,
action: {
block: {},
},
statement: {
notStatement: {
statement: {
geoMatchStatement: {
// block traffic if not from below
countryCodes: ["AU"],
},
},
},
},
visibilityConfig: {
sampledRequestsEnabled: true,
cloudWatchMetricsEnabled: true,
metricName: "AllowedCountriesOnly",
},
},
{
name: "AWS-AWSManagedRulesCommonRuleSet",
priority: rulePriorityCounter++,
statement: {
managedRuleGroupStatement: {
name: "AWSManagedRulesCommonRuleSet",
vendorName: "AWS",
// an example of how we might want to exclude rules
// excludedRules: [
// {
// name: "SizeRestrictions_BODY",
// },
//],
},
},
overrideAction: {
none: {},
},
visibilityConfig: {
sampledRequestsEnabled: true,
cloudWatchMetricsEnabled: true,
metricName: "AWS-AWSManagedRulesCommonRuleSet",
},
},
{
name: "AWS-AWSManagedRulesAmazonIpReputationList",
priority: rulePriorityCounter++,
statement: {
managedRuleGroupStatement: {
vendorName: "AWS",
name: "AWSManagedRulesAmazonIpReputationList",
},
},
overrideAction: {
none: {},
},
visibilityConfig: {
sampledRequestsEnabled: true,
cloudWatchMetricsEnabled: true,
metricName: "AWS-AWSManagedRulesAmazonIpReputationList",
},
},
],
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { IBucket } from "aws-cdk-lib/aws-s3";
import { ContainerConstruct } from "../construct/container-construct";
import { IHostedZone } from "aws-cdk-lib/aws-route53";
import { ICertificate } from "aws-cdk-lib/aws-certificatemanager";
import { CfnWebACL, CfnWebACLAssociation } from "aws-cdk-lib/aws-wafv2";

interface Props extends ElsaDataApplicationSettings {
readonly vpc: ec2.IVpc;
Expand Down Expand Up @@ -109,6 +110,26 @@ export class ElsaDataApplicationAppRunnerConstruct extends Construct {
vpcConnector: vpcConnector,
});

if (props.wafRules && props.wafRules.length > 0) {
const cfnWebAcl = new CfnWebACL(this, "WebAcl", {
defaultAction: {
allow: {},
},
scope: "REGIONAL",
visibilityConfig: {
cloudWatchMetricsEnabled: true,
metricName: "MetricForWebACLCDK",
sampledRequestsEnabled: true,
},
rules: props.wafRules,
});

new CfnWebACLAssociation(this, "WebAclAssociation", {
resourceArn: appService.serviceArn,
webAclArn: cfnWebAcl.attrArn,
});
}

// register a cloudMapService for the Application in our namespace
// chose a sensible default - but allow an alteration in case I guess someone might
// want to run two Elsa *in the same infrastructure*
Expand Down
8 changes: 8 additions & 0 deletions packages/aws-application/elsa-data-application-settings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CfnWebACL } from "aws-cdk-lib/aws-wafv2";

/**
* The user settable settings for the Elsa Data application service.
*/
Expand Down Expand Up @@ -83,6 +85,12 @@ export interface ElsaDataApplicationSettings {
* The cpu assigned to the Elsa Data application container - defaults to something sensible
*/
readonly cpu?: number;

/**
* If present and non-empty - tells us to use these rules for establishing a WAF.
* If not present, then no WAF is installed.
*/
readonly wafRules?: CfnWebACL.RuleProperty[];
}

export interface ElsaDataApplicationBuildLocal {
Expand Down
Loading