Learn how to create and manage secrets using Cloud Code's Secret Manager integration.
To follow step-by-step guidance for this task directly in the Cloud Shell Editor, click Guide me:
Before you begin
-
In the Google Cloud console, go to the project selector page.
-
Select or create a Google Cloud project.
- Install Git so that Cloud Code can perform Git operations, like cloning a sample.
- Install the Cloud Code plugin if you haven't already.
Create your Cloud Run service
Use Cloud Shell Editor as your environment for creating your Cloud Run service and secret. The editor comes preloaded with the tools needed for cloud development.
To create your service:
In the Cloud Code status bar, click the active project name.
In the Quick Pick menu that appears, select click New Application, and then click Cloud Run Application.
From the list of Cloud Run samples, select Python (Flask): Cloud Run.
Select a folder for your sample and then click Create New Application.
After Cloud Shell Editor loads your service in a new workspace, view its files in
the
Create a secret
Secret Manager allows you to securely store, manage, and access secrets as binary blobs or text strings. Additionally, it manages your secrets, meaning you don't have to deal with virtual machines or operating services.
To create a secret with Cloud Code's Secret Manager integration:
- Click
Secret Manager and give it a moment to load. - If prompted to authorize Cloud Shell to make Google Cloud API calls, click Authorize.
- Click add
Create Secret . - If prompted, select your Google Cloud project from the dropdown selector.
- If prompted, enable the Secret Manager API.
In the Secret Manager - Create Secret tab that appears, enter the following in the Name field:
my-secret
In the Secret Value field, enter:
Hello secret!
Click Create Secret and a message that your secret was successfully created appears.
Add a secret to your code
Secrets are great for storing configuration information such as database passwords, API keys, or TLS certificates needed by an application at runtime.
To add a secret to your code:
Open the
Cloud API view and select theSecret Manager API .This opens a Google Cloud API Detail tab with Secret Manager API as the heading.
In the Install Client Library section, click the Python tab and click play_arrow Run in terminal. This installs the
google-cloud-secret-manager
client library.Open
requirements.txt and add the following line to the bottom of the file:google-cloud-secret-manager==2.1.0
Your changes are automatically saved.
To get the latest value of your secret, open
app.py and copy and paste the following function:after the hello function :def access_secret_version(secret_version_id): """Return the value of a secret's version""" from google.cloud import secretmanager # Create the Secret Manager client. client = secretmanager.SecretManagerServiceClient() # Access the secret version. response = client.access_secret_version(name=secret_version_id) # Return the decoded payload. return response.payload.data.decode('UTF-8')
To call the
access_secret_version
function, replace themessage variable with the following:message = access_secret_version("<SECRET_VERSION_ID>")
If you still have the Secret Manager - Create Secret tab open, file_copy Copy the ID.
To get the ID of a secret version at any time, navigate to
Secret Manager > [SECRET_NAME] > Versions, hold the pointer over over your version, and click Copy resource ID.To add the version ID, replace the placeholder
with your copied version ID.<SECRET_VERSION_ID>
Run on Cloud Run emulator
To test your new secret, run your Cloud Run service locally on the Cloud Run emulator.
- Launch the
Cloud Code menu from the status bar. - To build and deploy your service to the emulator, select
Run on Cloud Run Emulator . - In the Run/Debug on Cloud Run Emulator tab that appears, click Run.
When running the configuration for the first time, this process can take up to 5 minutes. The
Output panel displays the progress as your app is built and deployed.After your app is built, launch your app by clicking the localhost link that appears in your
Output panel. Your secret's value is displayed under the success graphic.
View and create a new secret version
Cloud Code's Secret Manager view gives you a quick look at your project's secrets, with actions for managing them.
Viewing the value of a secret version
- Click the
Secret Manager view. - Expand your secret by clicking on it.
- In the Versions folder, right-click the numbered version that you want to view the value of and select Show Version Value.
Note that you can't edit a secret version. To update a secret's value, you must create a new version.
Create a new secret version
The value of a secret is stored in a secret version. A secret can have many versions. This is helpful in situations where a secret changes. Updating a secret with a new version means you don't have to update your code.
- Click the
Secret Manager view. - Right-click your secret's name and select Create Secret Version.
- In the Secret Manager - Create Version tab that appears, enter a new value and click Create Version.
- After the Secret Manager - Create Secret tab opens, click file_copy Copy top copy the ID.
- To add the newer version ID, replace the current version that appears the message
variable in
app.py with the newer version ID that you copied.
latest
.
View and manage secrets
Disable a secret version
Secret versions are enabled by default after creation, meaning they can be accessed. A disabled secret is inaccessible, but you can always restore access to it at any time.
To disable a secret version:
- Click Cloud Code and then expand the Secret Manager section.
- Expand your secret by clicking on it.
- Under the Versions folder, right-click the numbered version you want to disable.
- Select Disable Version.
Destroy a secret version
When you destroy a secret version, it can't be accessed. Destroying a secret version is permanent.
- Click Cloud Code and then expand the Secret Manager section.
- Expand your secret by clicking on it.
- Under the Versions folder, right-click the version you want to destroy.
- Select Destroy Version.
Cleaning up
To delete just the cluster you created for this quickstart:
- Click Cloud Code and then expand the Kubernetes section.
- Hold the pointer over your cluster name and then click open_in_new Open in Google Cloud console.
- Click Delete and then click Delete.
To delete your project (and associated resources, including any clusters):
Go to the Projects page in the Google Cloud console:
Select the project that you created for this quickstart and then click Delete.
Type the project ID to confirm and then click Shut down.
This shuts down the project and schedules it for deletion.