From d95fccd163d50748cf3e9803170bae1ced4bfb58 Mon Sep 17 00:00:00 2001 From: Prowler Bot Date: Wed, 19 Mar 2025 15:53:37 +0100 Subject: [PATCH] fix(gcp): make provider id mandatory in test_connection (#7315) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Pedro Martín --- prowler/providers/gcp/gcp_provider.py | 12 ++++++++++-- tests/providers/gcp/gcp_provider_test.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/prowler/providers/gcp/gcp_provider.py b/prowler/providers/gcp/gcp_provider.py index ee3a1ad4d3..3d6700a560 100644 --- a/prowler/providers/gcp/gcp_provider.py +++ b/prowler/providers/gcp/gcp_provider.py @@ -2,7 +2,6 @@ import json import os import re import sys -from typing import Optional from colorama import Fore, Style from google.auth import default, impersonated_credentials, load_credentials_from_dict @@ -426,7 +425,7 @@ class GcpProvider(Provider): credentials_file: str = None, service_account: str = None, raise_on_exception: bool = True, - provider_id: Optional[str] = None, + provider_id: str = None, client_id: str = None, client_secret: str = None, refresh_token: str = None, @@ -483,6 +482,11 @@ class GcpProvider(Provider): ... ) """ try: + if not provider_id: + logger.error("Provider ID is required.") + raise GCPInvalidProviderIdError( + file=__file__, message="Provider ID is required." + ) # Set the GCP credentials using the provided client_id, client_secret and refresh_token from ADC gcp_credentials = None if any([client_id, client_secret, refresh_token]): @@ -495,6 +499,10 @@ class GcpProvider(Provider): gcp_credentials=gcp_credentials, service_account_key=service_account_key, ) + + if not project_id: + project_id = provider_id + if provider_id and project_id != provider_id: # Logic to check if the provider ID matches the project ID GcpProvider.validate_project_id( diff --git a/tests/providers/gcp/gcp_provider_test.py b/tests/providers/gcp/gcp_provider_test.py index c82e5320a8..a0096cc7fb 100644 --- a/tests/providers/gcp/gcp_provider_test.py +++ b/tests/providers/gcp/gcp_provider_test.py @@ -810,6 +810,7 @@ class TestGCPProvider: ): with pytest.raises(Exception) as e: GcpProvider.test_connection( + provider_id="test-provider-id", client_id="test-client-id", client_secret="test-client-secret", refresh_token="test-refresh-token", @@ -817,6 +818,20 @@ class TestGCPProvider: assert e.type == GCPTestConnectionError assert "Test exception" in e.value.args[0] + def test_test_connection_with_exception_no_project_id(self): + with patch( + "prowler.providers.gcp.gcp_provider.GcpProvider.setup_session", + side_effect=GCPInvalidProviderIdError("Test exception"), + ): + with pytest.raises(GCPInvalidProviderIdError) as e: + GcpProvider.test_connection( + client_id="test-client-id", + client_secret="test-client-secret", + refresh_token="test-refresh-token", + ) + assert e.type == GCPInvalidProviderIdError + assert "[3008] Provider ID is required." in e.value.args[0] + def test_test_connection_with_exception_service_account_key(self): with patch( "prowler.providers.gcp.gcp_provider.GcpProvider.setup_session", @@ -824,6 +839,7 @@ class TestGCPProvider: ): with pytest.raises(Exception) as e: GcpProvider.test_connection( + provider_id="test-provider-id", service_account_key={"test": "key"}, ) assert e.type == GCPTestConnectionError