Overview
Google Cloud Functions allow you to host serverless code that is executed when an event is triggered, without requiring the management of a host operating system. These functions can also store environment variables to be used by the code that may contain authentication or other information that need to remain confidential.
Rationale
It is recommended to use the Secret Manager, because environment variables are stored unencrypted, and accessible for all users who have access to the code.
Impact
There should be no impact on the Cloud Function. There are minor costs after 10,000 requests a month to the Secret Manager API as well for a high use of other functions. Modifying the Cloud Function to use the Secret Manager may prevent it running to completion.
Remediation guidance
From Google Cloud Console
To enable Secret Manager API for your Project
- Within the project you wish to enable, navigate to
APIs & Services, then selectEnabled APIs & Servicesin the menu that opens up - Click the button
+ Enable APIS and Services - In the Search bar, search for
Secret Manager APIand select it - Click the blue box that says
Enable
Reviewing environment variables that should be migrated to Secret Manager
- Go to Cloud Functions
- Click on a function name from the list
- Click on
EDITand look underRuntime environment variablesfor variables that should be secrets. Leave this list open for the next step
Migrating environment variables to Secrets within the Secret Manager
- Go to the Secret Manager page
- On the Secret Manager page, click
Create Secret - On the
Create secretpage, underName, enter the name of the Environment Variable you are replacing. This will then be the Secret Variable you will reference in your code. - You will also need to add a version. This is the actual value of the variable that will be referenced from the code. To add a secret version when creating the initial secret, in the Secret value field, enter the value from the Environment Variable you are replacing
- Leave the
Regionssection unchanged - Click the
Create secretbutton - Repeat for all Environment Variables
Granting your runtime's Service Account access to Secrets
- Navigate to
Secret Managerwith an account that has theroles/secretmanager.secretAccessorpermission - Click the name of a secret listed in this screen
- If it is not already open, click
Show Info Panelin this screen to open the panel - In the info panel, click
Add principal - In the
New principalsfield, enter the service account your function uses for its identity. (If you need help locating or updating your runtime's service account, please see the 'docs/securing/function-identity#runtime_service_account' reference.) - In the
Select a roledropdown, chooseSecret Managerand thenSecret Manager Secret Accessor
Modify your code to use the Secrets in Secret Manager
Please see the '/docs/creating-and-accessing-secrets#access' reference for language specific instructions
Deleting the insecure Environment Variables
- Navigate to
Secret ManagerunderSecurity - Click the name of a function. Click
EDIT - Click Runtime, build and connections settings to expand the advanced configuration options
- Click
Security. Hover over the secret you want to remove, then clickDelete - Click
Next. ClickDeploy. The latest version of the runtime will now reference the secrets in Secret Manager
Using Google Cloud CLI
To enable Secret Manager API for your Project
Within the project you wish to enable the API in, run the following command:
gcloud services enable secretmanager.googleapis.com
Reviewing environment variables that should be migrated to Secret Manager
- To view a list of your cloud functions run the following command:
gcloud functions list
- For each cloud function run the following command:
gcloud functions describe <functionName>
- Review the settings of the buildEnvironmentVariables and environmentVariables. Keep this information for the next step
Migrating environment variables to Secrets within the Secret Manager
- Run the following command with the Environment Variable name you are replacing in the
<secretID>. It is most secure to point this command to a file with the Environment Variable value located in it, as if you entered it via command line it would show up in your shell's command history
gcloud secrets create <secretID> --data-file="/path/to/file.txt"
Granting your runtime's Service Account access to Secrets
As of the time of writing, using Google CLI to list Runtime variables is only in beta
Modify your code to use the Secrets in Secret Manager
Please see the '/docs/creating-and-accessing-secrets#access' reference for language specific instructions
Deleting the insecure Environment Variables
gcloud functions deploy <functionName> --remove-env-vars <envVars>
If you need to find the environment variables to remove, you can find them in the output of the command gcloud functions describe <functionName>
Default value
By default, Secret Manager is not enabled.
References
- https://cloud.google.com/functions/docs/configuring/env-var#managing_secrets
- https://cloud.google.com/secret-manager/docs/overview
Additional information
There are slight additional costs to using the Secret Manager API. Review the documentation to determine your organizations' needs.
Multiple Remediation Paths
Google Cloud
SERVICE-WIDE (RECOMMENDED when many resources are affected): Enforce Organization Policies at org/folder level so new resources inherit secure defaults.
gcloud org-policies set-policy policy.yaml
ASSET-LEVEL: Use the product-specific remediation steps above for only the impacted project/resources.
PREVENTIVE: Use org policy constraints/custom constraints and enforce checks in deployment pipelines.
References for Service-Wide Patterns
- GCP Organization Policy overview: https://cloud.google.com/resource-manager/docs/organization-policy/overview
- GCP Organization policy constraints catalog: https://cloud.google.com/resource-manager/docs/organization-policy/org-policy-constraints
- gcloud org-policies: https://cloud.google.com/sdk/gcloud/reference/org-policies
Operational Rollout Workflow
Use this sequence to reduce risk and avoid repeated drift.
1. Contain at Service-Wide Scope First (Recommended)
- Google Cloud: apply organization policy constraints at org/folder scope.
gcloud org-policies set-policy policy.yaml
2. Remediate Existing Affected Assets
- Execute the control-specific Console/CLI steps documented above for each flagged resource.
- Prioritize internet-exposed and production assets first.
3. Validate and Prevent Recurrence
- Re-scan after each remediation batch.
- Track exceptions with owner and expiry date.
- Add preventive checks in IaC/CI pipelines.
Query logic
These are the stored checks tied to this control.
Ensure Secrets are not stored in Cloud Functions environment variables by using Secret Manager
Connectors
Covered asset types
Expected check: eq []
{
functions(
where: {
envVars_SOME: {
key_MATCHES: "(?i).*(api|key|secret|token|password|access|id|auth|app|client|credential|security|private|public|authorization|confidential|encryption|hmac|signature|passphrase|session|authentication|verify|oauth|ssl|tls|jwt|service_account|code|secure|sudo).*"
}
}
) {
...AssetFragment
}
}
Google Cloud