Remediation
Enable Secret Manager API for your Projectβ
From Google Cloud Consoleβ
- Within the project you wish to enable, select the Navigation hamburger menu in the top left. Hover over
APIs & Services
to under the headingServerless
, then selectEnabled APIs & Services
in the menu that opens up. - Click the button
+ Enable APIS and Services
- In the
Search
bar, search forSecret Manager API
and select it. - Click the blue box that says
Enable
.
From Google Cloud CLIβ
-
Within the project you wish to enable the API in, run the following command.
gcloud services enable Secret Manager API
Reviewing Environment Variables That Should Be Migrated to Secret Managerβ
From Google Cloud Consoleβ
- Log in to the Google Cloud Web Portal
- Go to
Cloud Functions
- Click on a function name from the list
- Click on
Edit
and review theRuntime environment
for variables that should be secrets. Leave this list open for the next step.
From Google Cloud CLIβ
-
To view a list of your cloud functions run
gcloud functions list
-
For each cloud function run the following command.
gcloud functions describe <function_name>
-
Review the settings of the
buildEnvironmentVariables
andenvironmentVariables
. Keep this information for the next step.
Migrating Environment Variables to Secrets within the Secret Managerβ
From Google Cloud Consoleβ
- Go to the
Secret Manager
page in theCloud Console
. - On the
Secret Manager
page, clickCreate Secret
. - On the
Create secret
page, 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
Regions
section unchanged. - Click the
Create secret
button. - Repeat for all Environment Variables
From Google Cloud CLIβ
-
Run the following command with the Environment Variable name you are replacing in the
<secret-id>
. 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 <secret-id> --data-file="/path/to/file.txt"
Granting your Runtime's Service Account Access to Secretsβ
From Google Cloud Consoleβ
- Within the project containing your runtime login with account that has the
roles/secretmanager.secretAccessor
permission. - Select the Navigation hamburger menu in the top left. Hover over
Security
to under the then selectSecret Manager
in the menu that opens up. - Click the name of a secret listed in this screen.
- If it is not already open, click
Show Info Panel
in this screen to open the panel. - In the info panel, click
Add principal
. - In the
New principals
field, 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 role
dropdown, chooseSecret Manager
and thenSecret Manager Secret Accessor
.
From Google Cloud CLIβ
As of the time of writing, using Google CLI to list Runtime variables is only in beta. Because this is likely to change we are not including it here.
Modifying the Code to use the Secrets in Secret Managerβ
From Google Cloud Consoleβ
This depends heavily on which language your runtime is in. For the sake of the brevity of this recommendation, please see the '/docs/creating-and-accessing-secrets#access' reference for language specific instructions.
From Google Cloud CLIβ
This depends heavily on which language your runtime is in. For the sake of the brevity of this recommendation, please see the' /docs/creating-and-accessing-secrets#access' reference for language specific instructions.
Deleting the Insecure Environment Variablesβ
Be certain to do this step last. Removing variables from code actively referencing them will prevent it from completing successfully.
From Google Cloud Consoleβ
- Select the Navigation hamburger menu in the top left. Hover over
Security
then selectSecret Manager
in the menu that opens up. - 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.
From Google Cloud CLIβ
gcloud functions deploy <Function name>--remove-env-vars <env vars>
If you need to find the env vars to remove, they are from the step where gcloud functions describe <function_name>
was run.