Skip to content

Commit 7862ef8

Browse files
authored
Merge pull request #4 from ZDF-OSS/feature/add_aws_managed_rules
feat(aws-managed): add_aws_managed_rules
2 parents 03cf33e + 323cc03 commit 7862ef8

File tree

10 files changed

+720
-107
lines changed

10 files changed

+720
-107
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projenrc.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { awscdk } from 'projen';
22

33
const PROJECT_NAME = 'cdk-aws-wafv2-geofence-lib';
44
const PROJECT_DESCRIPTION =
5-
'The cdk-aws-wafv2-geofence-lib is an AWS CDK construct library that adds a AWS WAFv2 with GeoBlocking enabled for an AppSync, API Gateway or an ALB.';
5+
'The cdk-aws-wafv2-geofence-lib is an AWS CDK construct library that adds a AWS WAFv2 with GeoBlocking and AWS Managed Rules for AppSync, API Gateway or an ALB.';
66

77
const project = new awscdk.AwsCdkConstructLibrary({
88
author: 'ZeroDotFive',
@@ -17,6 +17,9 @@ const project = new awscdk.AwsCdkConstructLibrary({
1717
homepage: 'https://zerodotfive.com',
1818
description: PROJECT_DESCRIPTION,
1919
keywords: ['aws', 'cdk', 'awscdk', 'aws-cdk', 'wafv2', 'aws-waf', 'aws-wafv2', 'geoblock'],
20+
gitignore: [
21+
'cdk.out/',
22+
],
2023
});
2124

2225
project.addBundledDeps('@types/aws-lambda');

API.md

Lines changed: 231 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1-
# AWS WAFv2 GeoBlocking CDK construct for Cloud Development Kit (AWS CDK)
1+
# AWS WAFv2 cdk construct for Cloud Development Kit (AWS CDK)
22

33

44

5-
The WAFv2 GeoBlocking construct is free for everyone to use. It supports blocking of requests to AWS ressources based on IP orign (Country).
5+
The WAFv2 construct is free for everyone to use and it leverages the massive improvements made by AWS compared to V1.
66

7-
It offers a high-level abstraction and integrates neatly with your existing AWS CDK project. It encapsulates AWS best practices in your
8-
infrastructure definition and hides boilerplate logic for your.
7+
**Add an extra layer of security to protect your services from common attacks**
8+
9+
It offers a high-level abstraction and integrates neatly with your existing AWS CDK project. It brings AWS best practices into your infrastructure and hides boilerplate logic in your project.
910

1011
The Construct is available in the following languages:
1112

1213
* JavaScript, TypeScript ([Node.js ≥ 14.15.0](https://nodejs.org/download/release/latest-v14.x/))
1314
* We recommend using a version in [Active LTS](https://nodejs.org/en/about/releases/)
1415

15-
1616
Third-party Language Deprecation: language version is only supported until its EOL (End Of Life) shared by the vendor or community and is subject to change with prior notice.
1717

18+
**Features**
19+
* Blocking of requests to your AWS ressources based on IP orign (Country) - If you application is national, restrict the web traffic to the county.
20+
21+
* AWS Managed Rules for AWS WAF is a managed service that provides protection against common application vulnerabilities or other unwanted traffic (https://docs.aws.amazon.com/waf/latest/developerguide/aws-managed-rule-groups-list.html)
22+
23+
24+
***AWS Managed Rules***
25+
AWS Managed Rules for AWS WAF is a managed service that provides protection against common application vulnerabilities or other unwanted traffic. You have the option of selecting one or more rule groups from AWS Managed Rules for each web ACL, up to the maximum web ACL capacity unit (WCU) limit.
26+
27+
28+
29+
1830
\
1931
Jump To:
2032
[Getting Started](#getting-started)
@@ -46,14 +58,18 @@ When you use a geo match statement just for the region and country labels that i
4658
```ts
4759
// AWS WAFv2 GeoBlocking CDK Component
4860
const allowedCountiesToAccessService = ["DE"]
49-
const geoblockingWaf = new CdkWafGeoLib(this, 'GeoblockingWaf',
50-
{
51-
allowedCountiesToAccessService: ['DE'],
61+
new CdkWafGeoLib(this, 'Cdk-Waf-Geo-Lib', {
62+
// Geo blocking
63+
allowedCountiesToAccessService: allowedCountiesToAccessService,
64+
enableGeoBlocking: false,
65+
// AWS Default WAF Rules
66+
enableAWSManagedRulesBlocking: true,
67+
enableAWSManagedRuleCRS: true,
68+
69+
priority: 100,
5270
resourceArn: lb.loadBalancerArn,
53-
block: true,
54-
priority: 105,
55-
enableCloudWatchLogs: true
56-
})
71+
enableCloudWatchLogs: true,
72+
});
5773
```
5874

5975
#### Properties <a name="Properties" id="Properties"></a>
@@ -186,12 +202,17 @@ export class EcsBpMicroserviceWaf extends cdk.Stack {
186202
requestsPerTarget: 500,
187203
targetGroup: tg,
188204
});
205+
189206
new CdkWafGeoLib(this, 'Cdk-Waf-Geo-Lib', {
190-
allowedCountiesToAccessService: ['US'],
207+
// Geo blocking
208+
allowedCountiesToAccessService: ['DE'],
209+
enableGeoBlocking: false,
191210
resourceArn: lb.loadBalancerArn,
192-
block: true,
193-
priority: 105,
194-
enableCloudWatchLogs: false,
211+
priority: 233,
212+
enableCloudWatchLogs: true,
213+
// AWS Default WAF Rules
214+
enableAWSManagedRulesBlocking: true,
215+
enableAWSManagedRuleCRS: true,
195216
});
196217
}
197218
}

package.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/waf-rule-geoblock.ts

Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import * as cdk from 'aws-cdk-lib';
22

33
export interface IWafRuleProps {
4-
/** If you define more than one Rule in a WebACL , AWS WAF evaluates each request against the Rules in order based on the value of Priority . AWS WAF processes rules with lower priority first. The priorities don’t need to be consecutive, but they must all be different. */
4+
/** If you enable more than one Rule, AWS WAF evaluates each request against the Rules in order based on the value of Priority . AWS WAF processes rules with lower priority first. The priorities don’t need to be consecutive, but they must all be different. */
55
priority: number;
66

7-
/** Instructs AWS WAF to count the web request and then continue evaluating the request using the remaining rules in the web ACL. Link: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-wafv2-webacl-ruleaction.html#cfn-wafv2-webacl-ruleaction-count */
8-
count: boolean;
9-
107
/** Instructs AWS WAF to block the web request. Link: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-wafv2-webacl-ruleaction.html#cfn-wafv2-webacl-ruleaction-block */
118
block: boolean;
129

@@ -15,67 +12,46 @@ export interface IWafRuleProps {
1512
allowed_countries: Array<string>;
1613
}
1714

18-
export class WafRuleGeoBlockGreenList
19-
implements cdk.aws_wafv2.CfnWebACL.RuleProperty, IWafRuleProps {
20-
allowed_countries: string[];
21-
count: boolean;
15+
export class WafRulesGeoBlock implements IWafRuleProps {
2216
block: boolean;
23-
action?:
24-
| cdk.aws_wafv2.CfnWebACL.RuleActionProperty
25-
| cdk.IResolvable
26-
| undefined;
27-
captchaConfig?:
28-
| cdk.IResolvable
29-
| cdk.aws_wafv2.CfnWebACL.CaptchaConfigProperty
30-
| undefined;
31-
challengeConfig?:
32-
| cdk.IResolvable
33-
| cdk.aws_wafv2.CfnWebACL.ChallengeConfigProperty
34-
| undefined;
35-
name: string;
36-
overrideAction?:
37-
| cdk.IResolvable
38-
| cdk.aws_wafv2.CfnWebACL.OverrideActionProperty
39-
| undefined;
4017
priority: number;
41-
ruleLabels?:
42-
| cdk.IResolvable
43-
| (cdk.IResolvable | cdk.aws_wafv2.CfnWebACL.LabelProperty)[]
44-
| undefined;
45-
statement: cdk.IResolvable | cdk.aws_wafv2.CfnWebACL.StatementProperty;
46-
visibilityConfig:
47-
| cdk.IResolvable
48-
| cdk.aws_wafv2.CfnWebACL.VisibilityConfigProperty;
18+
19+
waf_rule: cdk.aws_wafv2.CfnWebACL.RuleProperty;
20+
21+
allowed_countries: string[];
4922

5023
constructor(props: IWafRuleProps) {
51-
this.name = 'WafGeoBlockGreenList';
52-
if (props.block) {
53-
this.block = true;
54-
this.action = {
55-
block: {},
56-
};
57-
} else {
58-
this.action = {
59-
count: {},
60-
};
61-
}
62-
this.block = props.block;
63-
this.allowed_countries = props.allowed_countries;
6424
this.priority = props.priority;
65-
this.count = props.count;
66-
this.visibilityConfig = {
67-
sampledRequestsEnabled: true,
68-
cloudWatchMetricsEnabled: true,
69-
metricName: 'WafGeoBlockGreenList',
70-
};
71-
this.statement = {
72-
notStatement: {
73-
statement: {
74-
geoMatchStatement: {
75-
countryCodes: props.allowed_countries,
25+
this.allowed_countries = props.allowed_countries;
26+
this.block = props.block;
27+
28+
this.waf_rule = {
29+
name: 'WafGeoBlockGreenList',
30+
priority: this.priority,
31+
action: {
32+
...(this.block ? {
33+
block: {
34+
},
35+
} : { count: {} }),
36+
},
37+
statement: {
38+
notStatement: {
39+
statement: {
40+
geoMatchStatement: {
41+
countryCodes: props.allowed_countries,
42+
},
7643
},
7744
},
7845
},
46+
visibilityConfig: {
47+
sampledRequestsEnabled: true,
48+
cloudWatchMetricsEnabled: true,
49+
metricName: 'WafGeoBlockGreenList',
50+
},
7951
};
8052
}
53+
54+
public rule(): cdk.aws_wafv2.CfnWebACL.RuleProperty {
55+
return this.waf_rule;
56+
}
8157
}

0 commit comments

Comments
 (0)