Skip to main content

Triage and Remediation

Remediation

Using Console

Below are concise, console-based steps to remediate the finding “Service Account Keys Should Be Managed By Google” for GCP IAM.

1. Identify service accounts with user‑managed keys

  1. In the Google Cloud Console, go to:
    IAM & Admin → Service Accounts
    (URL: https://console.cloud.google.com/iam-admin/serviceaccounts)
  2. In each project, look at the KEYS column:
    • If it shows User-managed keys, that account is in violation.
Repeat for all relevant projects (or use the project selector at the top).

2. Plan migration to Google‑managed credentials (no JSON keys)

Before deleting keys, make sure each workload can use keyless / Google‑managed authentication instead:

a) Google Cloud resources (GCE, GKE, Cloud Run, Cloud Functions, etc.)

  1. For each workload, decide which service account it should run as.
  2. Assign that service account to the resource:
    • Compute Engine VM:
      • Go to Compute Engine → VM instances.
      • Click the VM → Edit.
      • Under Service account, select the target service account.
      • Save.
    • GKE (Workloads):
      • Use Workload Identity (recommended):
        • Enable Workload Identity on the cluster (if not already).
        • Create a Kubernetes service account and bind it to the GCP service account.
        • (Console part is mostly under Kubernetes Engine → Clusters → Security; detailed configuration usually uses gcloud/kubectl.)
    • Cloud Run / Cloud Functions / Cloud Scheduler, etc.:
      • In each service’s Edit page, set the Runtime service account (or equivalent) to the correct service account.
When a Google Cloud resource runs as a service account, Google automatically provides and rotates a Google‑managed key (not visible or downloadable).

b) External workloads (on‑prem, other clouds, laptops/CI)

For anything that used a JSON key file:
  1. Go to IAM & Admin → Workload Identity Federation.
    • URL: https://console.cloud.google.com/iam-admin/workload-identity-pools
  2. Create or use an existing Workload Identity Pool and Provider for your external environment (OIDC, AWS, Azure AD, etc.).
  3. On the Service account page:
    • Go to IAM & Admin → Service Accounts.
    • Click the target service account → Permissions (or Show Info Panel).
    • Grant access to the workload identity pool (role roles/iam.workloadIdentityUser to the pool or provider).
  4. Update the external workload to use Workload Identity Federation instead of a JSON key (you’ll point it to the OIDC / AWS / other identity and the pool/provider; the app then exchanges external identity tokens for short‑lived Google credentials).

3. Remove user‑managed keys

Once workloads are confirmed working with Google‑managed or federated credentials:
  1. Go to IAM & Admin → Service Accounts.
  2. Click the affected service account.
  3. Go to the KEYS tab.
  4. Under User-managed keys:
    • For each key ID:
      • Click Delete (trash icon).
      • Confirm deletion.
Do this for all service accounts reported with user‑managed keys.
You can use an Org Policy to block new user‑managed keys:
  1. Go to: IAM & Admin → Organization Policies.
    (URL: https://console.cloud.google.com/iam-admin/orgpolicies)
  2. Find policy:
    Constraints on service account key creation: constraints/iam.disableServiceAccountKeyCreation
  3. Click it → Edit:
    • Set to Enforced to disable creation of new user‑managed keys.
  4. Save.

5. Verify remediation

  1. Re‑run your security scanner / SCC / policy check.
  2. In IAM & Admin → Service Accounts, confirm:
    • KEYS column shows only Google-managed keys (or none).
    • No service accounts have remaining user‑managed keys.
This satisfies the requirement that “Service Account Keys Should Be Managed By Google.”
Below are concise, CLI‑based steps to (1) prevent new user‑managed keys and (2) remove existing ones.

1. Prevent creation of new user‑managed keys

You do this via Organization/Folder/Project policies:

1.1. Disable creation of new keys

To do it at the project level instead (replace PROJECT_ID):
Or at project level:
These enforce using Google‑managed keys (no user‑managed key creation or upload).

2. Identify existing user‑managed service account keys

List all service accounts for a project:
For each service account, list its keys:
Look for keys with keyType = USER_MANAGED.

3. Remove existing user‑managed keys

Delete each user‑managed key by its full key ID (the name field):
You can script this for all user‑managed keys in a project:

4. Ensure workloads use Google‑managed credentials

For each workload:
  • GCE: Use service account attached to VM (no key file).
  • GKE: Use Workload Identity (no key file).
  • Cloud Run / Cloud Functions / App Engine: Use default or custom service account directly (no key file).
Once workloads no longer rely on user‑managed keys, and the org policies above are enforced, service account keys will be fully Google‑managed.
To have only Google‑managed keys for your service accounts, you must:
  1. Find all user‑managed keys.
  2. Delete them (after migrating workloads off JSON keys).
  3. Optionally enforce org policies to prevent new ones.
Below is how to do 1–2 with Python for GCP IAM.

1. Prerequisites

Make sure you authenticate with an identity that has:
  • roles/iam.serviceAccountAdmin or
  • roles/iam.serviceAccountKeyAdmin
For local dev:

2. Python: List and delete user‑managed keys for a service account

keyType meanings in IAM:
  • USER_MANAGED → JSON keys you create (should be removed).
  • SYSTEM_MANAGED → Google‑managed keys (do not delete these; they’re for things like Workload Identity, GCE, GKE, Cloud Run, etc.).

3. Python: Iterate over all service accounts in a project

If you want to clean up all service accounts in a project:

At org/folder/project level, set these org policies to True to block new user‑managed keys:
  • constraints/iam.disableServiceAccountKeyCreation
  • constraints/iam.disableServiceAccountKeyUpload
Example (project scope):
policy.yaml example:

Use the Python scripts to remove existing USER_MANAGED keys, and use the org policies to ensure only Google‑managed keys are used going forward.
To have keys managed by Google (and avoid user‑managed keys), do not define any google_service_account_key resources for this account (or remove existing google_service_account_key resources from Terraform so they are destroyed; this will immediately invalidate those keys and break anything still using them).terraform plan should show only google_service_account resources (no google_service_account_key creates), and for any removed keys it should show -/+ or - (destroy) actions for the google_service_account_key resources.