Info icon
End of Life Notice: For Trend Cloud One™ - Conformity Customers, Conformity will reach its End of Sale on “July 31st, 2025” and End of Life “July 31st, 2026”. The same capabilities and much more is available in TrendAI Vision One™ Cloud Risk Management. For details, please refer to Upgrade to TrendAI Vision One™

Use Secrets Manager for Managing Secrets in Google Cloud Run functions (formerly Cloud Functions)

TrendAI Vision One™ provides continuous assurance that gives peace of mind for your cloud infrastructure, delivering over 1400 automated best practice checks.

Risk Level: Medium (should be achieved)

To prevent unauthorized access or accidental exposure of sensitive information, ensure that Secrets Manager service is used to store and manage secrets instead of storing them in cleartext within Cloud Functions environment variables.

Security

Storing secrets in Google Cloud Functions environment variables is insecure due to lack of encryption, limited access control, and inadequate audit trails. For better security use dedicated secret management services such as Google Cloud Secret Manager. Secret Manager provides additional security measures like encryption at rest and in transit, access controls, auditing, and versioning.

Note: This check can only determine whether a plaintext environment variable is defined and whether Secret Manager (secret environment variables or secret volumes) is used by the function. It cannot determine whether the value of a given environment variable is actually secret or sensitive, since environment variable values are not inspected or retained for evaluation.


Audit

To determine if Cloud Functions environment variables are configured to store secret information, perform the following operations:

Using GCP Console

  1. Sign in to the Google Cloud Management Console.

  2. Select the GCP project that you want to examine from the console top navigation bar.

  3. Navigate to the Cloud Run console available at https://console.cloud.google.com/functions.

  4. In the navigation panel, choose Services. Click on the name (link) of the service that you want to examine.

  5. Select the Containers tab and examine the Variables & Secrets section. Plain-text environment variables are listed with their raw keys and values, while secrets referenced from Secret Manager are listed as environment variables or mounted volumes. Determine if the plain-text environment variables are storing secret information such as access keys, API keys, user passwords, database credentials, access tokens, and sensitive configuration settings. If the plain-text environment variables defined for the selected Google Cloud function contain secret, sensitive information, the function configuration is not secure and compliant.

  6. Repeat steps no. 4 and 5 for each Google Cloud function deployed within the selected GCP project.

  7. Repeat steps no. 2 – 6 for each project available in your Google Cloud Platform (GCP) account.

Using GCP CLI

  1. Run projects list command (Windows/macOS/Linux) with custom query filters to list the ID of each GCP project available within your Google Cloud account:

    gcloud projects list
    --format="table(projectId)"
    
  2. The command output should return the requested GCP project identifier(s):

    PROJECT_ID
    cc-web-app-project-112233
    cc-bigdata-project-123123
    
  3. Run functions list command (Windows/macOS/Linux) using the ID of the GCP project that you want to examine as the identifier parameter and custom filtering to describe the name, region, and generation of each function deployed within the selected project:

    gcloud functions list
    --project cc-web-app-project-112233
    --format="table(name.basename():label=NAME, name.scope('locations').segment(0):label=REGION, environment:label=GENERATION)"
    
  4. The command output should return the requested function identifier(s), along with their region and generation:

    NAME                  REGION       GENERATION
    cc-worker-function    us-central1  GEN_1
    cc-project5-function  us-central1  GEN_2
    
  5. Run run services list command (Windows/macOS/Linux) using the ID of the same GCP project as the identifier parameter and custom filtering to describe the name and region of each Cloud Run service deployed within the selected project. This command does not return 1st-gen functions, since 1st-gen functions are not Cloud Run services:

    gcloud run services list
    --project cc-web-app-project-112233
    --format="table(metadata.name:label=NAME, region:label=REGION)"
    
  6. The command output should return the requested Cloud Run service identifier(s). 2nd-gen functions (shown as GEN_2 in step 4) are deployed as Cloud Run services and appear here as well; 1st-gen functions (GEN_1) do not:

    NAME                  REGION
    cc-project5-function  us-central1
    
  7. Run functions describe command (Windows/macOS/Linux) using the name of the function that you want to examine as the identifier parameter and custom filters to describe the runtime and build environment variables defined for the selected function:

    gcloud functions describe cc-worker-function
    --format="json(serviceConfig.environmentVariables,buildConfig.environmentVariables)"
    
  8. The command output should return the requested key/value pairs (i.e. runtime and build environment variables):

    {
      "serviceConfig": {
        "environmentVariables": {
          "CC_API_KEY": "ABCD1234ABCD1234ABCD1234"
        }
      },
      "buildConfig": {
        "environmentVariables": {
          "CC_BUILD_VER": "2.4"
        }
      }
    }
    

    Check the key/value pairs returned by the functions describe command output to determine if your environment variables are storing secret information such as access keys, API keys, user passwords, database credentials, access tokens, and sensitive configuration settings. If the environment variables configured for the selected Google Cloud function contain secret and/or sensitive information, the function configuration is not secure and compliant.

  9. If the selected function is a 2nd-gen function (i.e. it also appears in the run services list output from step 6), run run services describe command (Windows/macOS/Linux) using the name of the Cloud Run service as the identifier parameter and custom filters to describe the environment variables defined directly on the underlying Cloud Run service:

    gcloud run services describe cc-project5-function
    --format="json(spec.template.spec.containers[0].env)"
    
  10. The command output should return the requested key/value pairs (i.e. container environment variables):

    {
      "spec": {
        "template": {
          "spec": {
            "containers": [
              {
                "env": [
                  {
                    "name": "CC_API_KEY",
                    "value": "ABCD1234ABCD1234ABCD1234"
                  }
                ]
              }
            ]
          }
        }
      }
    }
    

    Check the key/value pairs returned by the run services describe command output to determine if your environment variables are storing secret information such as access keys, API keys, user passwords, database credentials, access tokens, and sensitive configuration settings. This step may surface environment variables configured directly on the Cloud Run service that are not reflected in the functions describe output. If the environment variables configured for the selected Cloud Run service contain secret and/or sensitive information, the function configuration is not secure and compliant.

  11. Repeat steps no. 7 - 10 for each Google Cloud function deployed within the selected GCP project.

  12. Repeat steps no. 3 - 12 for each project created within your Google Cloud Platform (GCP) account.

Remediation / Resolution

To manage secrets using Secrets Manager service instead of Cloud Functions environment variables, perform the following operations:

Note: Secret Manager secret references are only supported for runtime environment variables (serviceConfig.environmentVariables). Google Cloud Functions does not support binding Secret Manager secrets to build-time environment variables (buildConfig.environmentVariables), in either the GCP Console or the GCP CLI. If a build-time environment variable contains secret or sensitive information, remove it entirely rather than attempting to reference it from Secret Manager, and supply the value to the function at runtime instead.

Using GCP Console

  1. Sign in to the Google Cloud Management Console.

  2. Select the GCP project that you want to access from the console top navigation bar.

  3. Navigate to the API Library page available at https://console.cloud.google.com/apis/library.

  4. Type Secret Manager API in the Search for API & Services search box and press Enter.

  5. Click on the name of the Secret Manager API to open the API overview page.

  6. On the API overview page, choose ENABLE to enable the Secret Manager API for your GCP project.

  7. Navigate to Secret Manager page at https://console.cloud.google.com/security/secret-manager.

  8. A Secret Manager secret is an encrypted wrapper around a collection of secret data versions. Choose CREATE SECRET and follow the setup wizard to create the Secret Manager secret that will replace the secret information stored in cleartext within the function environment variable.

  9. For Name, enter the name of the environment variable that you are replacing. This will be the secret variable that will be referenced in your code.

  10. For Secret value, enter the value from the environment variable that you are replacing. This is the actual value of the variable that will be referenced from your code.

  11. Configure the secret encryption, replication policy, secret rotation and expiration based on your application requirements, then choose CREATE SECRET to create your new Secret Manager secret.

  12. To grant your function's service account access to the new secret, open the newly created Secret Manager secret, select the PERMISSIONS tab, and choose GRANT ACCESS.

  13. On the Grant access to "[secret-name]" panel, enter the service account that your function uses for its identity in the New principals box and select Secret Manager Secret Accessor from the Role dropdown list. Choose SAVE to apply the changes.

  14. Modify your function configuration to use the new secret managed by Secret Manager. Navigate to the Cloud Run console available at https://console.cloud.google.com/run.

  15. In the navigation panel, choose Services. Click on the name (link) of the service that you want to configure, then choose EDIT & DEPLOY NEW REVISION.

  16. Select the Containers tab and choose the Variables & Secrets tab.

  17. Under Secrets exposed as environment variables, choose ADD SECRET REFERENCE and follow the setup process to create the secret reference that will replace the secret information stored in cleartext within the function environment variable. Choose DONE to save the changes.

  18. Choose DEPLOY to deploy the new revision.

  19. After the secret information that replaced the environment variable has been successfully referenced, you can safely remove the non-compliant environment variable from your function configuration. Choose EDIT & DEPLOY NEW REVISION, select the Containers tab, and choose the Variables & Secrets tab.

  20. Use the Delete item (trash bin icon) button to remove any plain-text environment variable that holds secret, sensitive information, listed under Variables & Secrets. Per the note above, this applies only to runtime environment variables — a non-compliant build-time environment variable must be removed from your build configuration directly, since it cannot be referenced from Secret Manager.

  21. Choose DEPLOY to deploy the new revision.

  22. Repeat steps no. 7 - 21 for each Google Cloud function deployed within the selected GCP project.

  23. Repeat steps no. 2 – 22 for each GCP project available in your Google Cloud Platform (GCP) account.

Using GCP CLI

  1. Run services enable command (Windows/macOS/Linux) using the ID of the GCP project that you want to configure as the identifier parameter, to enable the Secret Manager API for the selected project:

    gcloud services enable secretmanager.googleapis.com
    --project cc-web-app-project-112233
    
  2. If successful, the command output should return the ID and status of the performed operation:

    Operation "operations/acat.p0-123456789012-abcd1234-abcd-1234-abcd-1234abcd1234" finished successfully.
    
  3. Run secrets create command (Windows/macOS/Linux) to create the Secret Manager secret that will replace the secret information stored in cleartext within the specified function environment variable:

    gcloud secrets create CC_API_KEY
    --replication-policy automatic
    --data-file="secret-file.txt"
    
  4. If successful, the command output should return the version of the newly created secret:

    Created version [1] of the secret [CC_API_KEY]
    
  5. Run secrets add-iam-policy-binding command (Windows/macOS/Linux) to grant your function's service account access to the new Secret Manager secret:

    gcloud secrets add-iam-policy-binding CC_API_KEY
    --member="[service-account-name]"
    --role="roles/secretmanager.secretAccessor"
    
  6. The command output should return the new policy available for the configured Secret Manager secret:

    Updated IAM policy for secret [CC_API_KEY].
    bindings:
    - members:
      - serviceAccount:[service-account-name]
      role: roles/secretmanager.secretAccessor
    etag: ABCD1234ABCD
    version: 1
    
  7. Run functions deploy command (Windows/macOS/Linux) using the name of the function that you want to configure as the identifier parameter, to bind the new Secret Manager secret as a secret environment variable and redeploy the function:

    gcloud functions deploy cc-worker-function
    --update-secrets=CC_API_KEY=CC_API_KEY:1
    
  8. If successful, the command output should return the ID and status of the performed operation:

    Operation "operations/acat.p1-123456789012-abcd1234-abcd-1234-abcd-1234abcd1234" finished successfully.
    
  9. After the secret information that replaced the environment variable has been successfully referenced, you can safely remove the non-compliant environment variable from your function configuration. Run functions deploy command (Windows/macOS/Linux) using the name of the environment variable that you want to delete as the identifier parameter, to remove the specified variable from your function configuration and redeploy the function:

    gcloud functions deploy cc-worker-function
    --remove-env-vars CC_API_KEY
    
  10. If successful, the command output should return the ID and status of the performed operation:

    Operation "operations/acat.p1-123456789012-abcd1234-abcd-1234-abcd-1234abcd1234" finished successfully.
    
  11. Repeat steps no. 3 - 10 for each Google Cloud function deployed within the selected GCP project.

  12. Repeat steps no. 1 – 11 for each GCP project available in your Google Cloud Platform (GCP) account.

References

Publication date Jun 29, 2023