mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-01-25 02:08:11 +00:00
fix(gcp): make provider id mandatory in test_connection (#7296)
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user