Skip to content

Commit 831a83f

Browse files
authored
Merge pull request #28 from citizensadvice/add-waf-rule-templates
Add WAFv2 rule templates and builder class
2 parents 7fabaa3 + 1c3f43d commit 831a83f

15 files changed

Lines changed: 468 additions & 12275 deletions

README.md

Lines changed: 2 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -56,128 +56,9 @@ CrossAccountDomainDelegation(
5656
### Edge services
5757

5858
<details>
59-
<summary>Web Application Firewall</summary>
59+
<summary>WAF Rule Templates</summary>
6060

61-
Deploys AWS WAF using a vendored
62-
[AWS WAF Security Automations v3.2.0](https://github.com/awslabs/aws-waf-security-automations/tree/v3.2.0)
63-
template, with the addition of the AWS Managed `KnownBadInputs` Rule (to protect specifically
64-
against the `log4j` vulnerability). It also allows for the addition of any additional user-defined
65-
custom rules, by supplying a list of one or more `CfnWebACL.RuleProperty`. TO NOTE: these may
66-
incur additional costs, if they take the total number of `WCUs` for the WAF above `1500`.
67-
68-
In able to accommodate custom rules, and because of the limitations on working with imported
69-
nested templates with the CDK, the WAF provides a fixed set of standard rules - which is NOT
70-
parameterised in the `ProtectedCloudfront` construct. In order to vary the rules (e.g. add
71-
more of the standard rules, override any rules to COUNT, etc) you will need to copy the
72-
construct code to your config repo and make the amendments directly in the construct and
73-
template(s):
74-
75-
`SQL injection rule` - `BLOCK`
76-
77-
`Cross-site scripting rule` - `BLOCK`
78-
79-
`Flood protection rule` - `BLOCK`. A simple rate based rule, which blocks an individual IP
80-
address if average requests over a 5-minute period from that IP address exceed a user-supplied
81-
`RequestThreshold` and unblocks once they fall below this threshold again. CARE!! Given
82-
that many LCAs work from a fixed single IP address, this should not be set to too low a value.
83-
84-
`Reputation lists rule` - `BLOCK`
85-
86-
For the original WAF configuration options see the "Parameters" section in the
87-
[original template](ca_cdk_constructs/edge_services/assets/aws-waf-security-automations.json).
88-
89-
Usage:
90-
91-
```python
92-
from ca_cdk_constructs.edge_services.waf_stack import WafStack
93-
94-
WafStack(app,
95-
"Waf",
96-
# The waf **MUST** be instantiated with the rule combination here. Only the
97-
# flood_protection_threshold and custom_rules can be varied.
98-
params={
99-
"ActivateAWSManagedRulesParam": "yes",
100-
"ActivateSqlInjectionProtectionParam": "yes",
101-
"ActivateCrossSiteScriptingProtectionParam": "yes",
102-
"ActivateHttpFloodProtectionParam": "yes - AWS WAF rate based rule",
103-
"ActivateScannersProbesProtectionParam": "no",
104-
"ActivateReputationListsProtectionParam": "yes",
105-
"ActivateBadBotProtectionParam": "no",
106-
# threshold requests in 5-minute period from any single IP before that
107-
# IP is blocked.
108-
"RequestThreshold": flood_protection_threshold, # default = 100
109-
},
110-
custom_rules: <list of aws_cdk.aws_wafv2.CfnWebACL.RuleProperty]> default = [],
111-
})
112-
113-
```
114-
115-
</details>
116-
117-
<details>
118-
<summary>ProtectedCloudfront</summary>
119-
120-
[protected_cloudfront](ca_cdk_constructs/edge_services/protected_cloudfront.py)
121-
122-
Creates a Cloudfront distribution protected by the AWS WAF. The distribution forwards a
123-
custom header that can be requested by downstream load balancers in order to prevent traffic
124-
from hitting them directly.
125-
126-
When using this library construct, the only properties of the WAF that can be specified
127-
are any custom rules to be added on top of the WAF (list of aws_cdk.aws_wafv2.CfnWebACL.RuleProperty)
128-
129-
Usage:
130-
131-
```python
132-
from aws_cdk import App, Stack
133-
from aws_cdk.aws_eks import HelmChart
134-
from ca_cdk_constructs.edge_services.protected_cloudfront import ProtectedCloudfrontStack
135-
import json
136-
137-
app = App()
138-
139-
hosted_zone = # create or import a hosted zone
140-
141-
custom_rules = # optionally specify a list of aws_cdk.aws_wafv2.CfnWebACL.RuleProperty
142-
143-
# creates Cloudfront protected by WAF at myapp.<hosted_zone_domain>
144-
cdn = ProtectedCloudfrontStack(app, "ca-referrals",
145-
hosted_zone=hosted_zone,
146-
sub_domain="myapp",
147-
origin_domain="my-loadbalancer-url"
148-
custom_rules=custom_rules,
149-
flood_protection_threshold="2500" # any value >= 100
150-
)
151-
152-
# retrieve the secret header which must be added to the load balancer in order
153-
# to prevent users bypassing the CDN ( and the WAF )
154-
cdn.secret_header
155-
# or
156-
cdn.SECRET_HEADER_NAME
157-
# and
158-
cdn.secret_header_value
159-
160-
# To add the header to e.g. Kubernetes ALB ingress use:
161-
162-
k8s_deployment_stack = Stack(app, "K8sDeployment")
163-
# add the header to the ALB ingress
164-
chart_overrides = {
165-
"web": {
166-
"ingress": {
167-
"annotations": {
168-
"alb.ingress.kubernetes.io/conditions.main": json.dumps(
169-
[
170-
# other config can go here
171-
cdn.alb_ingress_header_config_annotation
172-
]
173-
)
174-
}
175-
}
176-
}
177-
}
178-
179-
HelmChart(k8s_deployment_stack, "myapp", cluster=cluster, namespace="myapp-namespace", values=chart_overrides)
180-
```
61+
TODO TODO TODO
18162

18263
</details>
18364

ca_cdk_constructs/edge_services/protected_cloudfront.py

Lines changed: 0 additions & 192 deletions
This file was deleted.

0 commit comments

Comments
 (0)