Skip to content

fix(gcp): surface organization-scan failures instead of silently scanning the home project#11280

Open
puchy22 wants to merge 3 commits into
masterfrom
fix/gcp_orgs_audit_permissions
Open

fix(gcp): surface organization-scan failures instead of silently scanning the home project#11280
puchy22 wants to merge 3 commits into
masterfrom
fix/gcp_orgs_audit_permissions

Conversation

@puchy22
Copy link
Copy Markdown
Member

@puchy22 puchy22 commented May 21, 2026

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():

  1. The --organization-id branch calls cloudasset.assets.list(parent="organizations/<id>", ...). Any HttpError from that call (403 permission denied, "Cloud Asset API has not been used", quota project issues, etc.) is caught and logged at error level only — never raised.
  2. With projects left empty, control falls through to a fallback at gcp_provider.py:785 that opens the credentials JSON file and substitutes project_id from 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/viewer at the org node) can satisfy.

Description

Provider:

  • New exception GCPGetOrganizationProjectsError (code 3011) with a remediation message naming roles/cloudasset.viewer and the Cloud Asset API to enable.
  • get_projects() now raises GCPGetOrganizationProjectsError on any HttpError from 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).
  • The credentials-file home-project fallback (elif credentials_file:) is now gated with and not organization_id. An explicit org-wide scan can no longer be silently downgraded to one project.
  • The outer except Exception block now re-raises GCPBaseException so 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 that roles/cloudasset.viewer 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.
  • 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

  1. prowler/providers/gcp/gcp_provider.py:631-708 — the org-id branch now raises GCPGetOrganizationProjectsError.
  2. prowler/providers/gcp/gcp_provider.py:795-798 — the credentials-file fallback is gated on not organization_id.
  3. prowler/providers/gcp/gcp_provider.py:814-820 — the catch-all re-raises GCPBaseException.
  4. prowler/providers/gcp/exceptions/exceptions.py — new error code 3011.
  5. tests/providers/gcp/gcp_provider_test.py — two new regression tests cover the 403 (permission denied) and "API not enabled" paths. Run pytest tests/providers/gcp/gcp_provider_test.py (23 passed).
  6. Reproduce the original symptom by removing roles/cloudasset.viewer from a service account and running prowler gcp --credentials-file <key.json> --organization-id <org-id>; with this PR the CLI now exits with GCPGetOrganizationProjectsError and a clear remediation message instead of silently scanning the host project.

Checklist

Community Checklist
  • This feature/issue is listed in here
  • Is it assigned to me

SDK/CLI

  • Are there new checks included in this PR? No
    • No new permissions required for the provider. The change documents an existing requirement (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.

…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
@puchy22 puchy22 requested a review from a team as a code owner May 21, 2026 09:17
@github-actions github-actions Bot added the provider/gcp Issues/PRs related with the Google Cloud Platform provider label May 21, 2026
…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.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 21, 2026

✅ All necessary CHANGELOG.md files have been updated.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 21, 2026

Conflict Markers Resolved

All conflict markers have been successfully resolved in this pull request.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 21, 2026

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.37%. Comparing base (c660b35) to head (e0424a4).
⚠️ Report is 1 commits behind head on master.

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     
Flag Coverage Δ
api ?
prowler-py3.10-gcp 93.37% <85.71%> (?)
prowler-py3.11-gcp 93.37% <85.71%> (?)
prowler-py3.12-gcp 93.37% <85.71%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
prowler 93.37% <85.71%> (∅)
api ∅ <ø> (∅)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 21, 2026

🔒 Container Security Scan

Image: prowler:5cb6abb
Last scan: 2026-05-21 09:35:56 UTC

📊 Vulnerability Summary

Severity Count
🔴 Critical 6
Total 6

5 package(s) affected

⚠️ Action Required

Critical severity vulnerabilities detected. These should be addressed before merging:

  • Review the detailed scan results
  • Update affected packages to patched versions
  • Consider using a different base image if updates are unavailable

📋 Resources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation provider/gcp Issues/PRs related with the Google Cloud Platform provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GCP Organization audit with service account fails

1 participant