Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access environment variables stored in Google Secret Manager from Google Cloud Build

How can I access the variables I define in Google Secret Manager from my Google Cloud Build Pipeline ?

like image 784
martinkaburu Avatar asked Feb 03 '20 06:02

martinkaburu


People also ask

How do you safely manage environment variables in a cloud environment?

To set, update, or remove environment variables of an existing service, use the gcloud run services update command. You can use any of the following flags, as needed: --set-env-vars. --update-env-vars.

What is secret manager in Google Cloud builds?

Secret Manager is a Google Cloud service that securely stores API keys, passwords, and other sensitive data. To include sensitive information in your builds, you can store the information in Secret Manager and then configure your build to access the information from Secret Manager. Enable the Cloud Build and Secret Manager APIs.

How to access the secret material using PHP on Google Cloud?

// snippet is showing how to access the secret material. To run this code, first learn about using PHP on Google Cloud and install the Secret Manager PHP SDK. On Compute Engine or GKE, you must authenticate with the cloud-platform scope . // Import the Secret Manager client library.

How to secure your application with environment variables?

(+) Moving away from storing secrets on plain text on environment variables is also a correct step in order to secure our applications. (+) Having the possibility to implement this in a way where it does not interfere with the local environment is one of the selling points.

How to consume secrets from Google secret manager in GKE?

Consuming (or retrieving) secrets from Google Secret Manager in GKE can be achieved in various ways, either by directly calling the API, using some Kubernetes native tools or 3rd party Open Source software. This article lists 5 optionsto integrate GKE and GSM, for each option I listed the pros and cons and a link to a code sample.


Video Answer


1 Answers

You can access to secret from Cloud Build by using the standard Cloud Builder gcloud

But, there is 2 issues:

  1. If you want to use the secret value in another Cloud Build step, you have to store your secret in a file, the only way to reuse a previous value from one step to another one
  2. The current Cloud Builder gcloud isn't up to date (today, 03 feb 2020). You have to add a gcloud component update for using the correct version. I opened an issue for this.
steps:
    - name: gcr.io/cloud-builders/gcloud
      entrypoint: "bash"
      args:
          - "-c"
          - |
              gcloud components update
              # Store the secret is a temporary file
              gcloud beta secrets versions access --secret=MySecretName latest > my-secret-file.txt
    - name: AnotherCloudBuildStepImage
      entrypoint: "bash"
      args:
          - "-c"
          - |
              # For getting the secret and pass it to a command/script
              ./my-script.sh $(cat my-secret-file.txt)

Think to grant the role Secret Manager Secret Accessor roles/secretmanager.secretAccessor to the Cloud Build default service account <PROJECT_ID>@cloudbuild.gserviceaccount.com

EDIT

You can access to the secret from anywhere, either with the gcloud CLI installed (and initialized with a service account authorized to access secrets) or via API call

curl -H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://secretmanager.googleapis.com/v1beta1/projects/PROJECT_ID/secrets/MySecretName/versions/latest:access

Note: You recieve the secret in the data field, in base64 encoded format. Don't forget to decode it before using it!

You have to generate an access token on a service account with the correct role granted. Here I use again gcloud, because it's easier. But according with your platform, use the most appropriate method. A python script can also do the job.


EDIT 2

A new way to get secrets exists now in Cloud Build. Less boiler plate, safer. Have a look and use this way now.

like image 169
guillaume blaquiere Avatar answered Sep 27 '22 00:09

guillaume blaquiere