diff --git a/prowler/CHANGELOG.md b/prowler/CHANGELOG.md index d141e66e18..f89dfe39b5 100644 --- a/prowler/CHANGELOG.md +++ b/prowler/CHANGELOG.md @@ -20,6 +20,7 @@ All notable changes to the **Prowler SDK** are documented in this file. ### Fixed - Renamed `AdditionalUrls` to `AdditionalURLs` field in CheckMetadata [(#8639)](https://github.com/prowler-cloud/prowler/pull/8639) - TypeError from Python 3.9 in Security Hub module by updating type annotations [(#8619)](https://github.com/prowler-cloud/prowler/pull/8619) +- KeyError when SecurityGroups field is missing in MemoryDB check [(#8666)](https://github.com/prowler-cloud/prowler/pull/8666) --- diff --git a/prowler/providers/aws/services/memorydb/memorydb_service.py b/prowler/providers/aws/services/memorydb/memorydb_service.py index 6bfbb255e3..e1a13fcba9 100644 --- a/prowler/providers/aws/services/memorydb/memorydb_service.py +++ b/prowler/providers/aws/services/memorydb/memorydb_service.py @@ -36,7 +36,7 @@ class MemoryDB(AWSService): region=regional_client.region, security_groups=[ sg["SecurityGroupId"] - for sg in cluster["SecurityGroups"] + for sg in cluster.get("SecurityGroups", []) if sg["Status"] == "active" ], tls_enabled=cluster["TLSEnabled"], diff --git a/tests/providers/aws/services/memorydb/memorydb_service_test.py b/tests/providers/aws/services/memorydb/memorydb_service_test.py index 9b804449e9..8115317920 100644 --- a/tests/providers/aws/services/memorydb/memorydb_service_test.py +++ b/tests/providers/aws/services/memorydb/memorydb_service_test.py @@ -108,3 +108,61 @@ class Test_MemoryDB_Service: snapshot_limit=5, ) } + + +def mock_make_api_call_no_security_groups(self, operation_name, kwargs): + """Mock that simulates a cluster response WITHOUT the SecurityGroups field""" + if operation_name == "DescribeClusters": + return { + "Clusters": [ + { + "Name": MEM_DB_CLUSTER_NAME, + "Description": "Test cluster without SecurityGroups", + "Status": "available", + "NumberOfShards": 1, + "AvailabilityMode": "singleaz", + "Engine": "valkey", + "EngineVersion": MEM_DB_ENGINE_VERSION, + "EnginePatchVersion": "5.0.6", + # SecurityGroups field is MISSING + "TLSEnabled": True, + "ARN": MEM_DB_CLUSTER_ARN, + "SnapshotRetentionLimit": 5, + "AutoMinorVersionUpgrade": True, + }, + ] + } + return make_api_call(self, operation_name, kwargs) + + +@patch( + "prowler.providers.aws.aws_provider.AwsProvider.generate_regional_clients", + new=mock_generate_regional_clients, +) +@patch( + "botocore.client.BaseClient._make_api_call", + new=mock_make_api_call_no_security_groups, +) +class Test_MemoryDB_Service_No_Security_Groups: + """Test class for clusters without SecurityGroups field""" + + def test_describe_clusters_no_security_groups(self): + """Test that clusters without SecurityGroups field are handled correctly""" + aws_provider = set_mocked_aws_provider() + memorydb = MemoryDB(aws_provider) + assert memorydb.clusters == { + MEM_DB_CLUSTER_ARN: Cluster( + name=MEM_DB_CLUSTER_NAME, + arn=MEM_DB_CLUSTER_ARN, + number_of_shards=1, + engine="valkey", + engine_version=MEM_DB_ENGINE_VERSION, + engine_patch_version="5.0.6", + multi_az="singleaz", + region=AWS_REGION_US_EAST_1, + security_groups=[], + tls_enabled=True, + auto_minor_version_upgrade=True, + snapshot_limit=5, + ) + }