Cloud Functions should read secrets from Secret Manager, not environment variables
This check looks at the Cloud Functions in your projects and whether sensitive values such as passwords or API keys are stored in plain environment variables. The safe setting is to keep them in Secret Manager and give each function access only to the secrets it needs.
Why it matters
An environment variable on a Cloud Function is part of the function's configuration. Anyone who can view the function, which is a much wider group than the people who should know a database password, can read its variables in the console or from the command line. They can also end up in exported configuration, deployment files and logs that are shared more freely than a secret should be.
Secret Manager is built for sensitive values. Access is controlled per secret through IAM, versions are kept, access can be audited, and a secret can be rotated without editing the function's code. Google's documentation on configuring secrets for Cloud Functions describes how to expose a secret to a function as an environment variable or a mounted file while keeping the value itself outside the function's configuration (Configure secrets).
How to check it yourself
In the Google Cloud console, open Cloud Run functions, select a function, and look at the Runtime, build, connections and security settings for the runtime environment variables. Read each name and ask whether the value is a credential, token or key.
These read-only gcloud commands list functions and show their variables:
gcloud functions list --project <PROJECT_ID> --format="table(name,environment,state)"
gcloud functions describe <FUNCTION_NAME> --region <REGION> --project <PROJECT_ID> --format="yaml(environmentVariables,serviceConfig.environmentVariables)"Names such as PASSWORD, TOKEN, API_KEY or SECRET are a strong sign of a problem. Run this for every project in scope, and do not paste the output into tickets or chat, because it may contain the secret values.
How to fix it
First create the secret and its value in Security > Secret Manager. Then give the function's runtime service account the Secret Manager Secret Accessor role on that one secret only. Finally redeploy the function so it reads the secret, and remove the plain variable.
gcloud secrets create db-password --replication-policy=automatic
printf '%s' "$VALUE" | gcloud secrets versions add db-password --data-file=-
gcloud secrets add-iam-policy-binding db-password --member="serviceAccount:<RUNTIME_SA>" --role="roles/secretmanager.secretAccessor"
gcloud functions deploy <FUNCTION_NAME> --region <REGION> --update-secrets="DB_PASSWORD=db-password:latest" --remove-env-vars=DB_PASSWORDThese commands change your project, so an administrator should review them first. ConfigCobra only reads your configuration and never changes it. Treat the old value as exposed and rotate it once the new setup works.
What changes when you fix it
The function is redeployed and gets a new revision, so there is a short period of change and a small risk that the new version fails if something is missing. Test it in a non-production project first and watch the logs after the rollout.
The runtime service account now needs permission to read the secret. If that permission is missing, the function will fail to start or fail when it reads the secret, which is the most common cause of trouble after this change. If you pin the secret to latest, a rotation reaches the function on the next start, while a pinned version number stays the same until you change it. People who could previously read the password from the function's settings will no longer see it there, which is the intended effect. Other functions or tools that copied the old value also need the new one.
Included in
| Benchmark | CIS ID | Profile |
|---|---|---|
| CIS Google Cloud Platform Foundation v5.0.0 | 1.18 | Level 1 |
More checks in Identity and Access Management
- Use corporate-managed login credentials instead of consumer accountsCIS 1.2
- Enforce multi-factor authentication for all non-service accountsCIS 1.3
- Enforce security-key two-factor authentication for all admin accountsCIS 1.4
- Use only GCP-managed keys for each service account (no user-managed keys)CIS 1.5
- Ensure service accounts hold no admin (owner/editor) privilegesCIS 1.6
Go further
- Identity and Access Management21 controls in this section
- CIS Google Cloud Platform Foundation v5.0.093 controls
- CIS Google Cloud Platform Foundation checklistEvery control in one list, as CSV
- RemediationSee what a fix changes in your tenant before you apply it, then use the generated script
- Monitoring & DriftScheduled scans, drift detection and email alerts when a control regresses
Check this on your Google Cloud
ConfigCobra runs this check with every other control of the CIS Google Cloud Platform Foundation Benchmark, read-only.