Skip to content

Commit 7916425

Browse files
fix(memorydb): handle clusters with no security groups (#8666)
1 parent d98063e commit 7916425

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

prowler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ All notable changes to the **Prowler SDK** are documented in this file.
2020
### Fixed
2121
- Renamed `AdditionalUrls` to `AdditionalURLs` field in CheckMetadata [(#8639)](https://github.com/prowler-cloud/prowler/pull/8639)
2222
- TypeError from Python 3.9 in Security Hub module by updating type annotations [(#8619)](https://github.com/prowler-cloud/prowler/pull/8619)
23+
- KeyError when SecurityGroups field is missing in MemoryDB check [(#8666)](https://github.com/prowler-cloud/prowler/pull/8666)
2324

2425
---
2526

prowler/providers/aws/services/memorydb/memorydb_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _describe_clusters(self, regional_client):
3636
region=regional_client.region,
3737
security_groups=[
3838
sg["SecurityGroupId"]
39-
for sg in cluster["SecurityGroups"]
39+
for sg in cluster.get("SecurityGroups", [])
4040
if sg["Status"] == "active"
4141
],
4242
tls_enabled=cluster["TLSEnabled"],

tests/providers/aws/services/memorydb/memorydb_service_test.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,61 @@ def test_describe_clusters(self):
108108
snapshot_limit=5,
109109
)
110110
}
111+
112+
113+
def mock_make_api_call_no_security_groups(self, operation_name, kwargs):
114+
"""Mock that simulates a cluster response WITHOUT the SecurityGroups field"""
115+
if operation_name == "DescribeClusters":
116+
return {
117+
"Clusters": [
118+
{
119+
"Name": MEM_DB_CLUSTER_NAME,
120+
"Description": "Test cluster without SecurityGroups",
121+
"Status": "available",
122+
"NumberOfShards": 1,
123+
"AvailabilityMode": "singleaz",
124+
"Engine": "valkey",
125+
"EngineVersion": MEM_DB_ENGINE_VERSION,
126+
"EnginePatchVersion": "5.0.6",
127+
# SecurityGroups field is MISSING
128+
"TLSEnabled": True,
129+
"ARN": MEM_DB_CLUSTER_ARN,
130+
"SnapshotRetentionLimit": 5,
131+
"AutoMinorVersionUpgrade": True,
132+
},
133+
]
134+
}
135+
return make_api_call(self, operation_name, kwargs)
136+
137+
138+
@patch(
139+
"prowler.providers.aws.aws_provider.AwsProvider.generate_regional_clients",
140+
new=mock_generate_regional_clients,
141+
)
142+
@patch(
143+
"botocore.client.BaseClient._make_api_call",
144+
new=mock_make_api_call_no_security_groups,
145+
)
146+
class Test_MemoryDB_Service_No_Security_Groups:
147+
"""Test class for clusters without SecurityGroups field"""
148+
149+
def test_describe_clusters_no_security_groups(self):
150+
"""Test that clusters without SecurityGroups field are handled correctly"""
151+
aws_provider = set_mocked_aws_provider()
152+
memorydb = MemoryDB(aws_provider)
153+
assert memorydb.clusters == {
154+
MEM_DB_CLUSTER_ARN: Cluster(
155+
name=MEM_DB_CLUSTER_NAME,
156+
arn=MEM_DB_CLUSTER_ARN,
157+
number_of_shards=1,
158+
engine="valkey",
159+
engine_version=MEM_DB_ENGINE_VERSION,
160+
engine_patch_version="5.0.6",
161+
multi_az="singleaz",
162+
region=AWS_REGION_US_EAST_1,
163+
security_groups=[],
164+
tls_enabled=True,
165+
auto_minor_version_upgrade=True,
166+
snapshot_limit=5,
167+
)
168+
}

0 commit comments

Comments
 (0)