How to Work With Secrets on Google Cloud Platform (GCP)
Learn a better way to manage your sensitive data

An application normally needs some sensitive data like API keys, database passwords, private keys, etc. It’s common to store them as environment variables. However, this is not a good practice because the values of environment variables are set and stored as plain texts and thus can be revealed easily. If your application is hosted on the Google Cloud Platform (GCP), you can use the Secret Manager to manage these sensitive data, which is a very secure and convenient solution. In this post, we will introduce the essentials of using the Secret Manager for our applications.
Preparation
If you create a project with your private Google account, then you are the owner of the project and don’t need to worry about permissions. However, if you work in a team, you would need to be granted the Secret Manager Admin role if you want to follow all the instructions in this post. Normally, as a developer, you may not have this admin role but only some limited roles to create secret versions or just to view the secret.
Create a secret
Normally we don’t need to create secrets programmatically in our code. We only need to do it from time to time manually when we need to create or update sensitive data. Therefore, it’s more convenient to create secrets in the GCP console where you can have a lot of helpful tips for whats, whys, and hows.
In the GCP console, search for “secret manager” and choose the one displayed below:

Then click “CREATE SECRET” to create a new one:

On the page opened, enter a name for the secret, which must be unique within a project. Therefore, give it a descriptive and unique name that is easy to identify.

You can input your secret value directly or import it from a file. It’s safe to enter the sensitive data directly here as it won’t be revealed anywhere unless you work in an insecure environment and have people watching your screen 👀. For private keys which are normally stored in text files already, it’s more convenient to upload the files directly.
You can leave all other options unchanged because the default ones are sufficient in most cases. You can of course have more fine-grained control over how the secret should be managed by fine-tuning these options. Now click “CREATE SECRET” to create the secret.
The gcloud command for creating a secret is
$ gcloud secrets create my-secret --data-file=/tmp/secretLocally in the terminal, it’s better to specify the secret value through a data file rather than plain text because the latter can be revealed in the Linux command history which is a security issue by itself.
Check the secret value
For the secret created by the Secret Manager, a very important concept is “version”. A secret version contains the actual value of a secret. A secret can contain multiple versions, which are named sequentially from 1. By default, the latest version with the largest version number is used if no version id is specified. We can also access the value of a specific version by specifying the version id.
A benefit of using the versioning feature is that the same secret with different values can be used for different environments and thus minimize the number of secrets to maintain. For example, we normally have different API keys for development, staging, and production environments. We can use different versions of the same secret for these environments.
Let’s check the value of the first version of our secret:


The gcloud command for checking the value of a secret is:











