fix(gcp): surface organization-scan failures instead of silently scanning the home project#11280
Open
puchy22 wants to merge 3 commits into
Open
fix(gcp): surface organization-scan failures instead of silently scanning the home project#11280puchy22 wants to merge 3 commits into
puchy22 wants to merge 3 commits into
Conversation
…ning the credentials' home project When `--organization-id` was requested, `get_projects()` enumerated projects via the Cloud Asset Inventory API. Any `HttpError` from that call (permission denied, API not enabled, etc.) was logged at ERROR level and swallowed; the empty result then fell through to a fallback that read `project_id` from the credentials JSON file and used the service account's host project as the entire scan scope. The user saw a clean banner and a "100% Passed" run with no indication the org scope had been silently downgraded. - Raise the new `GCPGetOrganizationProjectsError` (3011) from the org-id Cloud Asset API failure path with a clear remediation message naming `roles/cloudasset.viewer` and the API to enable. - Gate the credentials-file home-project fallback with `and not organization_id` so it can no longer mask an org-wide request. - Re-raise `GCPBaseException` past the outer catch-all so the new exception propagates to the CLI. Fix #11250
…d for org scans - `organization.mdx`: clarify that `roles/cloudasset.viewer` (or `roles/cloudasset.owner`) must be bound at the organization node — not at a project — and add the `gcloud organizations add-iam-policy-binding` and `gcloud services enable cloudasset.googleapis.com` commands so the requirement is actionable. - `authentication.mdx`: in the Service Account setup section, add a note pointing to the org-scan page for the additional role + API needed when running with `--organization-id`, so users who set up a SA without these requirements don't hit the silent fallback originally reported in #11250. - `CHANGELOG.md`: add 5.27.1 fixed entry for #11280.
Contributor
|
✅ All necessary |
Contributor
|
✅ Conflict Markers Resolved All conflict markers have been successfully resolved in this pull request. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #11280 +/- ##
==========================================
- Coverage 93.97% 93.37% -0.60%
==========================================
Files 237 141 -96
Lines 34829 3458 -31371
==========================================
- Hits 32729 3229 -29500
+ Misses 2100 229 -1871
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Contributor
🔒 Container Security ScanImage: 📊 Vulnerability Summary
5 package(s) affected
|
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Fix #11250
When a user runs
prowler gcp --credentials-file <sa-key.json> --organization-id <org-id>and the service account is missing the IAM/API setup that the Cloud Asset Inventory API requires, Prowler silently scans only the service account's host project instead of the whole organization. The CLI prints a normal banner and a clean run with no error.Root cause is in
GcpProvider.get_projects():--organization-idbranch callscloudasset.assets.list(parent="organizations/<id>", ...). AnyHttpErrorfrom that call (403 permission denied, "Cloud Asset API has not been used", quota project issues, etc.) is caught and logged aterrorlevel only — never raised.projectsleft empty, control falls through to a fallback atgcp_provider.py:785that opens the credentials JSON file and substitutesproject_idfrom it. The service account's host project becomes the entire scan scope, with no warning printed to the user.User accounts don't hit this because, without
--organization-id, Prowler takes the Cloud Resource Manager listing path, which user identities with org-wide bindings (roles/browser,roles/viewerat the org node) can satisfy.Description
Provider:
GCPGetOrganizationProjectsError(code 3011) with a remediation message namingroles/cloudasset.viewerand the Cloud Asset API to enable.get_projects()now raisesGCPGetOrganizationProjectsErroron anyHttpErrorfrom the org-scoped Cloud Asset API call, instead of swallowing it. Two variants of the message: one for "API not enabled" (with the enable link), one for any other failure (carrying the original error and the role hint).elif credentials_file:) is now gated withand not organization_id. An explicit org-wide scan can no longer be silently downgraded to one project.except Exceptionblock now re-raisesGCPBaseExceptionso the new exception propagates to the CLI rather than getting consumed by the broad catch-all.Docs:
docs/user-guide/providers/gcp/organization.mdx: clarify thatroles/cloudasset.viewermust be bound at the organization node (not at a project) and add thegcloud organizations add-iam-policy-bindingandgcloud services enable cloudasset.googleapis.comcommands.docs/user-guide/providers/gcp/authentication.mdx: in the Service Account section, add a note pointing to the org-scan page for the additional role + API needed when using--organization-id.Steps to review
prowler/providers/gcp/gcp_provider.py:631-708— the org-id branch now raisesGCPGetOrganizationProjectsError.prowler/providers/gcp/gcp_provider.py:795-798— the credentials-file fallback is gated onnot organization_id.prowler/providers/gcp/gcp_provider.py:814-820— the catch-all re-raisesGCPBaseException.prowler/providers/gcp/exceptions/exceptions.py— new error code 3011.tests/providers/gcp/gcp_provider_test.py— two new regression tests cover the 403 (permission denied) and "API not enabled" paths. Runpytest tests/providers/gcp/gcp_provider_test.py(23 passed).roles/cloudasset.viewerfrom a service account and runningprowler gcp --credentials-file <key.json> --organization-id <org-id>; with this PR the CLI now exits withGCPGetOrganizationProjectsErrorand a clear remediation message instead of silently scanning the host project.Checklist
Community Checklist
SDK/CLI
roles/cloudasset.viewer+ Cloud Asset API enablement) that was previously needed but silently masked.License
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.