Skip to content

Commit f1c7b5f

Browse files
MatthewCanezhelyan
authored andcommitted
Fix logging
1 parent 71ed522 commit f1c7b5f

2 files changed

Lines changed: 18 additions & 16 deletions

File tree

ca_cdk_constructs/edge_services/waf_v2_builder.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from constructs import Construct
22
from aws_cdk import aws_wafv2 as waf, aws_logs as cf_logs, Tags
3-
3+
from typing import Optional
44
from ca_cdk_constructs.edge_services.waf_rule_templates import (
55
managed_rule_group_property,
66
ip_rule_property,
@@ -16,7 +16,7 @@ class WafV2Builder:
1616
:param description: The description of the WAF ACL.
1717
:param tags: The tags of the WAF ACL. {"Component": "WAF"} is added in addition to this.
1818
:param waf_scope: The scope of the WAF ACL. Defaults to CLOUDFRONT.
19-
:param log_group: The CloudWatch log group to use for logging. If not included logging will be disabled.
19+
:param log_group: The CloudWatch log group to use for logging. Log group name MUST start with 'aws-waf-logs-'. If not included logging will be disabled.
2020
:param default_action: The default action of the WAF ACL. Defaults to Allow.
2121
2222
Functions:
@@ -53,10 +53,10 @@ def __init__(
5353
scope: Construct,
5454
name: str,
5555
description: str,
56-
tags: dict = dict(),
57-
waf_scope: str = "CLOUDFRONT",
58-
log_group: cf_logs.LogGroup = None,
59-
default_action: waf.CfnWebACL.DefaultActionProperty = None,
56+
tags: Optional[dict] = dict(),
57+
waf_scope: Optional[str] = "CLOUDFRONT",
58+
log_group: Optional[cf_logs.LogGroup] = None,
59+
default_action: Optional[waf.CfnWebACL.DefaultActionProperty] = None,
6060
) -> None:
6161
self.rules = list()
6262
self.scope = scope
@@ -70,7 +70,7 @@ def __init__(
7070
allow=waf.CfnWebACL.AllowActionProperty()
7171
)
7272

73-
if log_group:
73+
if self.log_group:
7474
self.visibility_config = waf.CfnWebACL.VisibilityConfigProperty(
7575
cloud_watch_metrics_enabled=True,
7676
metric_name=self.name,
@@ -99,9 +99,9 @@ def add_managed_rule(
9999
priority: int,
100100
managed_rule_name: str,
101101
managed_rule_vendor: str,
102-
count_only: bool = False,
103-
rules_to_exclude: list[str] = [],
104-
cloud_watch_metrics_enabled: bool = False,
102+
count_only: Optional[bool] = False,
103+
rules_to_exclude: Optional[list[str]] = [],
104+
cloud_watch_metrics_enabled: Optional[bool] = False,
105105
) -> None:
106106
"""
107107
Adds a managed rule to the WAFv2 WebACL.
@@ -131,9 +131,9 @@ def add_ip_rule(
131131
name: str,
132132
priority: int,
133133
addresses: dict[str, list[str]],
134-
allow: bool = False,
135-
count_only: bool = False,
136-
cloud_watch_metrics_enabled: bool = False,
134+
allow: Optional[bool] = False,
135+
count_only: Optional[bool] = False,
136+
cloud_watch_metrics_enabled: Optional[bool] = False,
137137
) -> None:
138138
"""
139139
Adds an IP rule to the WAFv2 WebACL.
@@ -170,7 +170,7 @@ def build(self) -> waf.CfnWebACL:
170170

171171
web_acl = waf.CfnWebACL(
172172
self.scope,
173-
self.name,
173+
"Default",
174174
name=self.name,
175175
description=self.description,
176176
default_action=self.default_action,
@@ -185,7 +185,7 @@ def build(self) -> waf.CfnWebACL:
185185
self.logging_config = waf.CfnLoggingConfiguration(
186186
self.scope,
187187
f"{self.name}LogConfig",
188-
log_destination_configs=[web_acl.attr_arn],
188+
log_destination_configs=[self.log_group.log_group_arn],
189189
resource_arn=web_acl.attr_arn,
190190
)
191191

tests/test_waf_v2_builder.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ def test_waf_v2_logging_disabled(waf_builder):
9393

9494
def test_waf_v2_logging_enabled():
9595
stack = Stack(App(), "TestStack")
96-
test_log_group = aws_logs.LogGroup(stack, "TestLogGroup")
96+
test_log_group = aws_logs.LogGroup(
97+
stack, "TestLogGroup", log_group_name="aws-waf-logs-test-group"
98+
)
9799
waf_builder = WafV2Builder(
98100
stack,
99101
name="TestWaf",

0 commit comments

Comments
 (0)