Skip to content

Commit

Permalink
fix(list_by_service): execute lambda if requested (#5930)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfagoagas authored Nov 27, 2024
1 parent f9864ee commit f576b24
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
5 changes: 3 additions & 2 deletions prowler/lib/check/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,9 @@ def list_by_service(bulk_checks_metadata: dict, service: str = None) -> set:
checks = set()

if service:
if service == "lambda":
service = "awslambda"
# This is a special case for the AWS provider since `lambda` is a reserved keyword in Python
if service == "awslambda":
service = "lambda"
checks = {
check_name
for check_name, check_metadata in bulk_checks_metadata.items()
Expand Down
69 changes: 69 additions & 0 deletions tests/lib/check/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,35 @@
Compliance=[],
)

mock_metadata_lambda = CheckMetadata(
Provider="aws",
CheckID="awslambda_function_url_public",
CheckTitle="Check 1",
CheckType=["type1"],
ServiceName="lambda",
SubServiceName="subservice1",
ResourceIdTemplate="template1",
Severity="high",
ResourceType="resource1",
Description="Description 1",
Risk="risk1",
RelatedUrl="url1",
Remediation={
"Code": {
"CLI": "cli1",
"NativeIaC": "native1",
"Other": "other1",
"Terraform": "terraform1",
},
"Recommendation": {"Text": "text1", "Url": "url1"},
},
Categories=["categoryone"],
DependsOn=["dependency1"],
RelatedTo=["related1"],
Notes="notes1",
Compliance=[],
)


class TestCheckMetada:

Expand Down Expand Up @@ -188,6 +217,46 @@ def test_list_by_service(self, mock_recover_checks, mock_load_metadata):
# Assertions
assert result == {"accessanalyzer_enabled"}

@mock.patch("prowler.lib.check.models.load_check_metadata")
@mock.patch("prowler.lib.check.models.recover_checks_from_provider")
def test_list_by_service_lambda(self, mock_recover_checks, mock_load_metadata):
# Mock the return value of recover_checks_from_provider
mock_recover_checks.return_value = [
("awslambda_function_url_public", "/path/to/awslambda_function_url_public")
]

# Mock the return value of load_check_metadata
mock_load_metadata.return_value = mock_metadata_lambda

bulk_metadata = CheckMetadata.get_bulk(provider="aws")

result = CheckMetadata.list(
bulk_checks_metadata=bulk_metadata, service="lambda"
)

# Assertions
assert result == {"awslambda_function_url_public"}

@mock.patch("prowler.lib.check.models.load_check_metadata")
@mock.patch("prowler.lib.check.models.recover_checks_from_provider")
def test_list_by_service_awslambda(self, mock_recover_checks, mock_load_metadata):
# Mock the return value of recover_checks_from_provider
mock_recover_checks.return_value = [
("awslambda_function_url_public", "/path/to/awslambda_function_url_public")
]

# Mock the return value of load_check_metadata
mock_load_metadata.return_value = mock_metadata_lambda

bulk_metadata = CheckMetadata.get_bulk(provider="aws")

result = CheckMetadata.list(
bulk_checks_metadata=bulk_metadata, service="awslambda"
)

# Assertions
assert result == {"awslambda_function_url_public"}

@mock.patch("prowler.lib.check.models.load_check_metadata")
@mock.patch("prowler.lib.check.models.recover_checks_from_provider")
def test_list_by_service_invalid(self, mock_recover_checks, mock_load_metadata):
Expand Down

0 comments on commit f576b24

Please sign in to comment.