mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-10-29 06:44:21 +00:00
fix(gcp): keyerrors in services cloudsql and monitoring (#8909)
This commit is contained in:
1 parent
8c3e1b96f9
commit
272e4547b2
5 files changed
+399
-5
No files matched your search
@@ -63,3 +63,154 @@ class TestCloudSQLService:
|
||||
]
|
||||
assert cloudsql_client.instances[1].flags == []
|
||||
assert cloudsql_client.instances[1].project_id == GCP_PROJECT_ID
|
||||
|
||||
def test_instances_without_backup_configuration(self):
|
||||
"""Test that CloudSQL service handles instances without backupConfiguration field"""
|
||||
|
||||
def mock_api_client_without_backup_config(*args, **kwargs):
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
client = MagicMock()
|
||||
|
||||
client.instances().list().execute.return_value = {
|
||||
"items": [
|
||||
{
|
||||
"name": "instance_no_backup_config",
|
||||
"databaseVersion": "MYSQL_8_0",
|
||||
"region": "us-east1",
|
||||
"ipAddresses": [{"type": "PRIVATE", "ipAddress": "10.0.0.1"}],
|
||||
"settings": {
|
||||
"ipConfiguration": {
|
||||
"requireSsl": True,
|
||||
"sslMode": "ENCRYPTED_ONLY",
|
||||
"authorizedNetworks": [],
|
||||
},
|
||||
"databaseFlags": [],
|
||||
# backupConfiguration is missing
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
client.instances().list_next.return_value = None
|
||||
|
||||
return client
|
||||
|
||||
with (
|
||||
patch(
|
||||
"prowler.providers.gcp.lib.service.service.GCPService.__is_api_active__",
|
||||
new=mock_is_api_active,
|
||||
),
|
||||
patch(
|
||||
"prowler.providers.gcp.lib.service.service.GCPService.__generate_client__",
|
||||
new=mock_api_client_without_backup_config,
|
||||
),
|
||||
):
|
||||
cloudsql_client = CloudSQL(
|
||||
set_mocked_gcp_provider(project_ids=[GCP_PROJECT_ID])
|
||||
)
|
||||
|
||||
# Should handle gracefully with default value
|
||||
assert len(cloudsql_client.instances) == 1
|
||||
assert cloudsql_client.instances[0].name == "instance_no_backup_config"
|
||||
assert (
|
||||
cloudsql_client.instances[0].automated_backups is False
|
||||
) # Default value
|
||||
|
||||
def test_instances_with_empty_backup_configuration(self):
|
||||
"""Test that CloudSQL service handles instances with empty backupConfiguration"""
|
||||
|
||||
def mock_api_client_with_empty_backup_config(*args, **kwargs):
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
client = MagicMock()
|
||||
|
||||
client.instances().list().execute.return_value = {
|
||||
"items": [
|
||||
{
|
||||
"name": "instance_empty_backup_config",
|
||||
"databaseVersion": "POSTGRES_14",
|
||||
"region": "europe-west1",
|
||||
"ipAddresses": [
|
||||
{"type": "PRIMARY", "ipAddress": "34.34.34.34"}
|
||||
],
|
||||
"settings": {
|
||||
"ipConfiguration": {
|
||||
"requireSsl": False,
|
||||
},
|
||||
"backupConfiguration": {}, # Empty but present
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
client.instances().list_next.return_value = None
|
||||
|
||||
return client
|
||||
|
||||
with (
|
||||
patch(
|
||||
"prowler.providers.gcp.lib.service.service.GCPService.__is_api_active__",
|
||||
new=mock_is_api_active,
|
||||
),
|
||||
patch(
|
||||
"prowler.providers.gcp.lib.service.service.GCPService.__generate_client__",
|
||||
new=mock_api_client_with_empty_backup_config,
|
||||
),
|
||||
):
|
||||
cloudsql_client = CloudSQL(
|
||||
set_mocked_gcp_provider(project_ids=[GCP_PROJECT_ID])
|
||||
)
|
||||
|
||||
# Should handle gracefully with default value when 'enabled' key is missing
|
||||
assert len(cloudsql_client.instances) == 1
|
||||
assert cloudsql_client.instances[0].name == "instance_empty_backup_config"
|
||||
assert (
|
||||
cloudsql_client.instances[0].automated_backups is False
|
||||
) # Default value
|
||||
|
||||
def test_instances_without_settings_fields(self):
|
||||
"""Test that CloudSQL service handles instances with minimal settings"""
|
||||
|
||||
def mock_api_client_with_minimal_settings(*args, **kwargs):
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
client = MagicMock()
|
||||
|
||||
client.instances().list().execute.return_value = {
|
||||
"items": [
|
||||
{
|
||||
"name": "instance_minimal",
|
||||
"databaseVersion": "SQLSERVER_2019_STANDARD",
|
||||
"region": "asia-east1",
|
||||
"settings": {}, # Minimal settings object
|
||||
}
|
||||
]
|
||||
}
|
||||
client.instances().list_next.return_value = None
|
||||
|
||||
return client
|
||||
|
||||
with (
|
||||
patch(
|
||||
"prowler.providers.gcp.lib.service.service.GCPService.__is_api_active__",
|
||||
new=mock_is_api_active,
|
||||
),
|
||||
patch(
|
||||
"prowler.providers.gcp.lib.service.service.GCPService.__generate_client__",
|
||||
new=mock_api_client_with_minimal_settings,
|
||||
),
|
||||
):
|
||||
cloudsql_client = CloudSQL(
|
||||
set_mocked_gcp_provider(project_ids=[GCP_PROJECT_ID])
|
||||
)
|
||||
|
||||
# Should handle gracefully with all default values
|
||||
assert len(cloudsql_client.instances) == 1
|
||||
assert cloudsql_client.instances[0].name == "instance_minimal"
|
||||
assert cloudsql_client.instances[0].automated_backups is False
|
||||
assert cloudsql_client.instances[0].require_ssl is False
|
||||
assert (
|
||||
cloudsql_client.instances[0].ssl_mode
|
||||
== "ALLOW_UNENCRYPTED_AND_ENCRYPTED"
|
||||
)
|
||||
assert cloudsql_client.instances[0].authorized_networks == []
|
||||
assert cloudsql_client.instances[0].flags == []
|
||||
Reference in new issue
Block a user