Triage and Remediation
- Remediation
Remediation
Using Console
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
- In the Google Cloud Console, go to:
IAM & Admin → Service Accounts
(URL:https://console.cloud.google.com/iam-admin/serviceaccounts) - In each project, look at the KEYS column:
- If it shows User-managed keys, that account is in violation.
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.)
- For each workload, decide which service account it should run as.
- 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.)
- Use Workload Identity (recommended):
- 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.
- Compute Engine VM:
b) External workloads (on‑prem, other clouds, laptops/CI)
For anything that used a JSON key file:- Go to IAM & Admin → Workload Identity Federation.
- URL:
https://console.cloud.google.com/iam-admin/workload-identity-pools
- URL:
- Create or use an existing Workload Identity Pool and Provider for your external environment (OIDC, AWS, Azure AD, etc.).
- 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.workloadIdentityUserto the pool or provider).
- 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:- Go to IAM & Admin → Service Accounts.
- Click the affected service account.
- Go to the KEYS tab.
- Under User-managed keys:
- For each key ID:
- Click Delete (trash icon).
- Confirm deletion.
- For each key ID:
4. Prevent new user‑managed keys (optional but recommended)
You can use an Org Policy to block new user‑managed keys:- Go to: IAM & Admin → Organization Policies.
(URL:https://console.cloud.google.com/iam-admin/orgpolicies) - Find policy:
Constraints on service account key creation:constraints/iam.disableServiceAccountKeyCreation - Click it → Edit:
- Set to Enforced to disable creation of new user‑managed keys.
- Save.
5. Verify remediation
- Re‑run your security scanner / SCC / policy check.
- In IAM & Admin → Service Accounts, confirm:
- KEYS column shows only Google-managed keys (or none).
- No service accounts have remaining user‑managed keys.
Using CLI
Using CLI
Below are concise, CLI‑based steps to (1) prevent new user‑managed keys and (2) remove existing ones.
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).
For each service account, list its keys:Look for keys with
You can script this for all user‑managed keys in a project:
1. Prevent creation of new user‑managed keys
You do this via Organization/Folder/Project policies:1.1. Disable creation of new keys
1.2. Disable upload of external keys (optional but recommended)
2. Identify existing user‑managed service account keys
List all service accounts for a project:keyType = USER_MANAGED.3. Remove existing user‑managed keys
Delete each user‑managed key by its full key ID (thename field):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).
Using Python
Using Python
To have only Google‑managed keys for your service accounts, you must:
Make sure you authenticate with an identity that has:
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.
- Find all user‑managed keys.
- Delete them (after migrating workloads off JSON keys).
- Optionally enforce org policies to prevent new ones.
1. Prerequisites
roles/iam.serviceAccountAdminorroles/iam.serviceAccountKeyAdmin
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:4. Prevent new user‑managed keys (org policy – not Python, but recommended)
At org/folder/project level, set these org policies to True to block new user‑managed keys:constraints/iam.disableServiceAccountKeyCreationconstraints/iam.disableServiceAccountKeyUpload
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.
Using Terraform
Using Terraform
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.
