Skip to content

Commit 1c3f43d

Browse files
committed
Add get_rules function to WAF builder
1 parent 4215f09 commit 1c3f43d

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

ca_cdk_constructs/edge_services/waf_rule_templates.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
# A COLLECTION OF HELPER FUNCTIONS FOR GENERATING AWS WAFV2 RULES #
55
###################################################################
66

7+
# Note for developers: These rule builder functions are used within
8+
# the WAF builder class. If you change any interfaces or make any
9+
# major changes to these functions, please update the WAF builder
10+
# class to match, including the docstrings.
11+
712

813
def managed_rule_group_property(
914
name: str,
@@ -126,10 +131,8 @@ def ip_rule_property(
126131

127132
# keys of addresses dict can only be "IPV4" or "IPV6" - easiest way to
128133
# check is by using set differences
129-
if set(addresses.keys()) - set(list(['IPV4', 'IPV6'])) != set(list([])):
130-
raise AttributeError(
131-
"keys for addresses dict must only be 'IPV4' or 'IPV6'!"
132-
)
134+
if set(addresses.keys()) - set(list(["IPV4", "IPV6"])) != set(list([])):
135+
raise AttributeError("keys for addresses dict must only be 'IPV4' or 'IPV6'!")
133136

134137
# Need IPv4 and IPv6 IP sets
135138
ipv4_arn = waf.CfnIPSet(
@@ -171,8 +174,8 @@ def ip_rule_property(
171174
ip_set_reference_statement=waf.CfnWebACL.IPSetReferenceStatementProperty(
172175
arn=ipv6_arn
173176
),
174-
)
177+
),
175178
]
176179
)
177-
)
180+
),
178181
)

ca_cdk_constructs/edge_services/waf_v2_builder.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class WafV2Builder:
2222
Functions:
2323
add_custom_rule: Adds a custom rule to the WAFv2 WebACL.
2424
add_managed_rule: Adds a managed rule to the WAFv2 WebACL.
25+
add_ip_rule: Adds an IP rule to the WAFv2 WebACL.
26+
get_rules: Returns the list of rules added to the WAFv2 WebACL.
2527
build: Builds the WAFv2 WebACL.
2628
2729
Example:
@@ -84,6 +86,11 @@ def __init__(
8486
self.tags = tags | {"Component": "WAF"}
8587

8688
def add_custom_rule(self, rule: waf.CfnWebACL.RuleProperty) -> None:
89+
"""
90+
Adds a custom rule to the WAFv2 WebACL.
91+
92+
:param rule: The rule to add.
93+
"""
8794
self.rules.append(rule)
8895

8996
def add_managed_rule(
@@ -150,6 +157,12 @@ def add_ip_rule(
150157
)
151158
)
152159

160+
def get_rules(self) -> list[waf.CfnWebACL.RuleProperty]:
161+
"""
162+
Returns the list of rules that have been added to the builder.
163+
"""
164+
return self.rules
165+
153166
def build(self) -> waf.CfnWebACL:
154167
"""
155168
Builds the WAFv2 WebACL.

tests/test_waf_v2_builder.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def waf_builder():
1313
description="A dummy WAF for testing",
1414
tags={"Foo": "Bar"},
1515
)
16-
1716
yield waf_builder
1817

1918

@@ -78,3 +77,16 @@ def test_waf_v2_logging_enabled():
7877
)
7978
waf = waf_builder.build()
8079
assert waf.visibility_config.cloud_watch_metrics_enabled == True
80+
81+
82+
def test_waf_v2_get_rules(waf_builder):
83+
waf_builder.add_managed_rule(
84+
name="TestManaged",
85+
priority=1,
86+
managed_rule_name="AWSManagedRulesCommonRuleSet",
87+
managed_rule_vendor="AWS",
88+
count_only=True,
89+
)
90+
rules = waf_builder.get_rules()
91+
assert len(rules) == 1
92+
assert type(rules[0]) == aws_wafv2.CfnWebACL.RuleProperty

0 commit comments

Comments
 (0)