Skip to content

Commit

Permalink
feat(gcp): resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrooot committed Dec 13, 2024
1 parent 1a3e3bf commit 1e8fdaa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 41 deletions.
59 changes: 32 additions & 27 deletions prowler/providers/gcp/gcp_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,16 @@ def setup_session(
Setup the GCP session with the provided credentials file or service account to impersonate
Args:
credentials_file: str
service_account: dict
gcp_credentials: dict
service_account_key: dict
credentials_file: str -> The credentials file path used to authenticate
service_account: dict -> The service account to impersonate
gcp_credentials: dict -> The GCP credentials following the format:
{
"client_id": str,
"client_secret": str,
"refresh_token": str,
"type": str
}
service_account_key: dict -> The service account key, used to authenticate
Returns:
Credentials object and default project ID
Expand Down Expand Up @@ -369,7 +375,6 @@ def setup_session(
)

if service_account_key:
logger.info("Using service account key")
logger.info(

Check warning on line 378 in prowler/providers/gcp/gcp_provider.py

View check run for this annotation

Codecov / codecov/patch

prowler/providers/gcp/gcp_provider.py#L378

Added line #L378 was not covered by tests
"GCP provider: Setting credentials from service account key..."
)
Expand Down Expand Up @@ -757,6 +762,28 @@ def update_projects_with_organizations(self):
f"{error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}"
)

def is_project_matching(self, input_project: str, project_to_match: str) -> bool:
"""
Check if the input project matches the project to match
Args:
input_project: str
project_to_match: str
Returns:
bool
Usage:
>>> GcpProvider.is_project_matching(input_project, project_to_match)
"""
return (
"*" in input_project
and re.search(
"." + input_project if input_project.startswith("*") else input_project,
project_to_match,
)
) or input_project == project_to_match

@staticmethod
def validate_static_arguments(
client_id: str = None, client_secret: str = None, refresh_token: str = None
Expand Down Expand Up @@ -788,28 +815,6 @@ def validate_static_arguments(
"type": "authorized_user",
}

def is_project_matching(self, input_project: str, project_to_match: str) -> bool:
"""
Check if the input project matches the project to match
Args:
input_project: str
project_to_match: str
Returns:
bool
Usage:
>>> GcpProvider.is_project_matching(input_project, project_to_match)
"""
return (
"*" in input_project
and re.search(
"." + input_project if input_project.startswith("*") else input_project,
project_to_match,
)
) or input_project == project_to_match

@staticmethod
def validate_project_id(provider_id: str, credentials: str = None) -> None:
"""
Expand Down
28 changes: 14 additions & 14 deletions tests/providers/gcp/gcp_provider_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,20 @@ def test_init_only_client_id(self):
GcpProvider(client_id="test-client-id")
assert "client_secret and refresh_token are required" in e.value.args[0]

def test_validate_static_arguments(self):
output = GcpProvider.validate_static_arguments(
client_id="test-client-id",
client_secret="test-client-secret",
refresh_token="test-refresh-token",
)

assert output == {
"client_id": "test-client-id",
"client_secret": "test-client-secret",
"refresh_token": "test-refresh-token",
"type": "authorized_user",
}

def test_test_connection_with_exception(self):
with patch(
"prowler.providers.gcp.gcp_provider.GcpProvider.setup_session",
Expand All @@ -815,20 +829,6 @@ def test_test_connection_with_exception_service_account_key(self):
assert e.type == GCPTestConnectionError
assert "Test exception" in e.value.args[0]

def test_validate_static_arguments(self):
output = GcpProvider.validate_static_arguments(
client_id="test-client-id",
client_secret="test-client-secret",
refresh_token="test-refresh-token",
)

assert output == {
"client_id": "test-client-id",
"client_secret": "test-client-secret",
"refresh_token": "test-refresh-token",
"type": "authorized_user",
}

def test_test_connection_valid_project_id(self):
project_id = "test-project-id"
mocked_service = MagicMock()
Expand Down

0 comments on commit 1e8fdaa

Please sign in to comment.