Skip to main content
Five Steps for Cloud Compliance Readiness·A practical playbook for security teamsDownload
CIS Google Cloud Platform Foundation v5.0.0 · 1.18

Cloud Functions should read secrets from Secret Manager, not environment variables

  • Level 1
  • Identity and Access Management

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_PASSWORD

These 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

BenchmarkCIS IDProfile
CIS Google Cloud Platform Foundation v5.0.01.18Level 1

Check this on your Google Cloud

ConfigCobra runs this check with every other control of the CIS Google Cloud Platform Foundation Benchmark, read-only.

Get in touch

Let's talk.

Whether you're evaluating ConfigCobra, running an audit, or managing a client fleet — we respond within one business day.

Free trial