Skip to content

Commit

Permalink
feat(waf): change WAF Classic web_acls from list to dict (#5380)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergio <sergio@prowler.com>
  • Loading branch information
HugoPBrito and sergargar authored Oct 11, 2024
1 parent a6db526 commit 304bb27
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def execute(self):
if lb_arn in acl.albs:
report.status = "PASS"
report.status_extended = f"ELBv2 ALB {lb.name} is protected by WAFv2 Web ACL {acl.name}."
for acl in waf_client.web_acls:
for acl in waf_client.web_acls.values():
if lb_arn in acl.albs:
report.status = "PASS"
report.status_extended = f"ELBv2 ALB {lb.name} is protected by WAFv1 Web ACL {acl.name}."
Expand Down
20 changes: 10 additions & 10 deletions prowler/providers/aws/services/waf/waf_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
from prowler.providers.aws.lib.service.service import AWSService


################### WAF
class WAF(AWSService):
def __init__(self, provider):
# Call AWSService's __init__
super().__init__("waf-regional", provider)
self.web_acls = []
self.web_acls = {}
self.__threading_call__(self._list_web_acls)
self.__threading_call__(self._list_resources_for_web_acl)

Expand All @@ -21,13 +20,13 @@ def _list_web_acls(self, regional_client):
if not self.audit_resources or (
is_resource_filtered(waf["WebACLId"], self.audit_resources)
):
self.web_acls.append(
WebAcl(
name=waf["Name"],
id=waf["WebACLId"],
albs=[],
region=regional_client.region,
)
arn = f"arn:aws:waf-regional:{regional_client.region}:{self.audited_account}:webacl/{waf['WebACLId']}"
self.web_acls[arn] = WebAcl(
arn=arn,
name=waf["Name"],
id=waf["WebACLId"],
albs=[],
region=regional_client.region,
)
except Exception as error:
logger.error(
Expand All @@ -37,7 +36,7 @@ def _list_web_acls(self, regional_client):
def _list_resources_for_web_acl(self, regional_client):
logger.info("WAF - Describing resources...")
try:
for acl in self.web_acls:
for acl in self.web_acls.values():
if acl.region == regional_client.region:
for resource in regional_client.list_resources_for_web_acl(
WebACLId=acl.id, ResourceType="APPLICATION_LOAD_BALANCER"
Expand All @@ -51,6 +50,7 @@ def _list_resources_for_web_acl(self, regional_client):


class WebAcl(BaseModel):
arn: str
name: str
id: str
albs: list[str]
Expand Down
12 changes: 7 additions & 5 deletions tests/providers/aws/services/waf/waf_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,18 @@ def test_list_web_acls(self):
# WAF client for this test class
aws_provider = set_mocked_aws_provider([AWS_REGION_EU_WEST_1])
waf = WAF(aws_provider)
waf_arn = "arn:aws:waf-regional:eu-west-1:123456789012:webacl/my-web-acl-id"
assert len(waf.web_acls) == 1
assert waf.web_acls[0].name == "my-web-acl"
assert waf.web_acls[0].region == AWS_REGION_EU_WEST_1
assert waf.web_acls[0].id == "my-web-acl-id"
assert waf.web_acls[waf_arn].name == "my-web-acl"
assert waf.web_acls[waf_arn].region == AWS_REGION_EU_WEST_1
assert waf.web_acls[waf_arn].id == "my-web-acl-id"

# Test WAF Describe Web ACLs Resources
def test_list_resources_for_web_acl(self):
# WAF client for this test class
aws_provider = set_mocked_aws_provider([AWS_REGION_EU_WEST_1])
waf = WAF(aws_provider)
waf_arn = "arn:aws:waf-regional:eu-west-1:123456789012:webacl/my-web-acl-id"
assert len(waf.web_acls) == 1
assert len(waf.web_acls[0].albs) == 1
assert "alb-arn" in waf.web_acls[0].albs
assert len(waf.web_acls[waf_arn].albs) == 1
assert "alb-arn" in waf.web_acls[waf_arn].albs

0 comments on commit 304bb27

Please sign in to comment.