GCP
Dynamic secrets are a core feature in Vault. A class of dynamic secrets is on-demand, revocable, time-limited access credentials for cloud providers.
Challenge
To consume Google Cloud Platform (GCP) services (e.g. GCP Kubernetes service), the client must have valid GCP credentials. GCP uses service accounts to authenticate services running in GCP with other GCP services. For example, a compute engine virtual machine (VM) requires credentials to authenticate with other GCP services.
Solution
Configure Vault to generate service account keys or OAuth tokens with a time-to-live (TTL) enforcing its validity so that the credentials are automatically revoked when they are no longer used.
In this tutorial, you will configure GCP to support Vault and setup Vault to generate either a OAuth token or service account key.
Personas
The end-to-end scenario described in this tutorial involves two personas:
adminwith privileged permissions to configure Vault secrets engines and GCP resourcesappsread the secrets from Vault
Prerequisites
This tutorial assumes the following:
You have a Google Cloud Platform account with permission to:
Create IAM service accounts, policies
Enable GCP APIs
Access the GCP Cloud Shell
Create VMs
Vault installed on your local machine
jq installed
Policy requirements
Each persona requires a different set of capabilities. These are expressed in policies. If you are not familiar with policies, complete the policies tutorial.
Required Vault policies
Lab setup
Workstation setup
Create a temporary directory to store the files used for this tutorial.
Retrieve the directory path and store it in the
TUTORIAL_TEMPenvironment variable.Go to the
TUTORIAL_TEMPdirectory.
Vault setup
Open a new terminal window and start a Vault dev server with
rootas the root token.The Vault dev server defaults to running at
127.0.0.1:8200. The server is initialized and unsealed.
Insecure operation:
Do not run a Vault dev server in production. This approach starts a Vault server with an in-memory database and runs in an insecure way.
Return to the terminal where you created the
TUTORIAL_TEMPdirectory.Export an environment variable for the
vaultCLI to address the Vault server.Export an environment variable for the
vaultCLI to authenticate with the Vault server.
The Vault server is ready.
Configure GCP services for Vault
(Persona: admin)
Warning:
Resources will be provisioned during this tutorial that may result in charges to your Google Cloud Platform account.
Before Vault can manage dynamic credentials using the GCP secrets engine, you need to configure the necessary resources in GCP. This includes enabling the required GCP APIs, creating a IAM service account and IAM policy for Vault, and a creating key for the service account that Vault will use to authenticate with GCP.
Open a browser and sign into the GCP console.
Click the terminal icon to launch the GCP Cloud Shell.
A new pane will open at the bottom of your window and provision a Cloud Shell instance.
In the Cloud Shell terminal, list the GCP project ID.
The project ID is used when configuring both GCP and Vault.
Verify the cloud resource manager and IAM API's are enabled.
If cloudresourcemanager.googleapis.com and iam.googleapis.com are not listed, expand the menu below and enable the API(s).
Enable GCP APIs:
Enable the iam.googleapis.com API.
Enable the cloudresourcemanager.googleapis.com API.
Create a service account for Vault to authenticate with GCP.
Create a GCP IAM role for Vault and assign the required permissions.
Retrieve the role name and store it in the
ROLE_NAMEenvironment variable.
Retrieve the service account email and store it in the
SERVICE_ACCOUNTenvironment variable.
Add the Vault service account to the GCP project and bind the role to the service account.
Create a service account key credential file.
Click the More button (represented by the vertical ellipsis) and select Download.
Click the folder icon, expand the directory, select the VaultServiceAccountKey.json file, and click Download.
Store the
VaultServiceAccountKey.jsonfile inTUTORIAL_TEMPdirectory you created in the lab setup section.
Example OSX command:
Retrieve and copy the GCP project ID from the Cloud Shell terminal.
Return to the terminal where you set the
TUTORIAL_TEMPenvironment variable and set theGCP_PROJECT_IDenvironment variable to the value from the previous step.
You have completed the necessary configuration in GCP to support the Vault GCP secrets engine.
Configure Vault
(Persona: admin)
There are two types of credentials Vault can generate for GCP - a JSON formated service account key credential file, and an OAuth token. Each of these methods have benefits and contrstraints that need to be considered for your use case.
Service account keys have a limit of ten keys per service account. This limit can quickly be reached in large, or busy environments. Depending on the workload, this limitation can be managed by setting a low TTL that permits the workload to complete. Service keys, however, are supported across most clients and the lifetime of the key can be fully managed by Vault.
OAuth access tokens do not have any limits on the number of tokens that can be requested. This can be beneficial for large environments or when workloads have a long runtime because you will not reach the same limit as service account keys. OAuth tokens are not supported by all clients and Vault is not able to fully manage the token. All tokens have a default TTL of one hour and cannot be revoked by Vault.
Impersonated accounts are a way to generate an OAuth2 access token that is granted the permissions and accesses of another given service account. These access tokens do not have the same 10-key limit as service account keys do, yet they retain their short-lived nature.
Instead of Vault creating a unique service account in GCP based on the Vault roleset name, existing GCP service accounts can be impersonated to simplify the configuration. This will help alleviate reaching the 100 service account quota in GCP.
See the OAuth account impersonation tab below for more details.
In the terminal where you set the
TUTORIAL_TEMPenvironment variable, enable the GCP secrets engine.Configure the GCP secrets engine to use the
VaultServiceAccountKey.jsoncredentials.Example output:
Define the bindings to be used by Vault in a file named
gcpbindings.hcl.
Create a Vault roleset named
edu-app-token.
Example output:
The Vault configuration is complete.
Return to the GCP console.
Expand the hamburger menu and navigate to IAM & Admin >> Service Accounts.
Copy the full email address for the VaultServiceAccount ending with iam.gserviceaccount.com.
In the terminal where you set the
TUTORIAL_TEMPenvironment variable, create an environment variable with the service account email address.Enable the GCP secrets engine.
Configure the GCP secrets engine to use the
VaultServiceAccountKey.jsoncredentials.Example output:
Create a Vault roleset named
edu-app-impersonate.
Example output:
The Vault configuration is complete.
In the terminal where you set the
TUTORIAL_TEMPenvironment variable, enable the GCP secrets engine.Configure the GCP secrets engine to use the
VaultServiceAccountKey.jsoncredentials.Example output:
Define the bindings to be used by Vault in a file named
gcpbindings.hcl.
Create a Vault roleset named
edu-app-key.
Example output:
The Vault configuration is complete.
Request GCP credentials
(Persona: app)
An application can read the Vault roleset to generate a unique OAuth token.
Read the
edu-app-tokenroleset to generate a new token.The token is generated with the default TTL of one hour (60 minutes).
OAuth tokens are managed by GCP, and not visible in the GCP console.
Return to the GCP console.
Expand the hamburger menu and navigate to IAM & Admin >> Service Accounts.
A service account has been created in GCP based on the Vault role name
edu-app-token.Click the vaultedu-app-token123456789@... service account and click the KEYS tab.
The key ID associated with the service account key Vault created will be listed.
This key is required by the Vault node when it authenticates with GCP. If the token is deleted, the Vault node will not be able to authenticate with GCP and produce the following error:

An application can read the Vault roleset to impersonate a service account and generate a unique OAuth token.
Read the
edu-app-tokenroleset to generate a new token.The token is generated with the TTL of ten minutes because the max TTL defined in the secrets engine configuration was set to 10m.
OAuth tokens are managed by GCP, and not visible in the GCP console.
An application can read the Vault roleset to generate a unique service account key.
Read the
edu-approleset to generate a new key.The
lease_durationis set to2mbecause the TTL defined in the secrets engine configuration was set to 2m.They
private_key_datavalue is a base64 encoded blob that has a properly formatted JSON file which the service can use to authenticate with GCP.Generate a new key and base64 decode the
private_key_datafield to reveal the JSON formatted credential file.Example output:
Return to the GCP console.
Expand the hamburger menu and navigate to IAM & Admin >> Service Accounts.
A service account has been created in GCP based on the Vault role name
edu-app-key.Click the vaultedu-app-key123456789@... service account and click the KEYS tab.
The key ID associated with the service account key you created will be listed.

Wait two minutes and refresh the page. They key ID is no longer listed because the TTL was set to two minutes and the key was revoked.
Manage leases
Service token eases can be renewed or revoked by either the requesting application or by a Vault admin.
Renew an existing lease
Depending on the secrets engine configuration, leases can be renewed for a service account key up to the max_ttl defined in the configuration. When you configured the GCP secrets engine you defined a default ttl of 2m and a max_ttl of 10m. This means you can renew the lease for up to a total of 10 minutes.
Read the
edu-approleset to generate a new key.Copy the
lease_idand lookup details about the lease.The
ttlwill be some value less than 2 minutes.Renew the lease.
The lease duration is reset to 2 minutes.
The lease can be renewed up to the
max_ttlvalue. Once themax_ttlis reached, you will receive a message that theTTLhas been capped.The lease will expire once the
max_ttlhas been reached and can no longer be renewed.
Revoke a lease
When an application has completed the process that requires a service account key, the lease can be revoked before the TTL has expired. This is espcially useful with GCP service account keys which allow a maximum of 10 keys per service account. In large environments with multiple applications, the maxiumum number keys could be reached quickly.
Read the
edu-app-keyroleset to generate a new key.Copy the
lease_idand lookup details about the lease.The
ttlwill be some value less than 2 minutes.Use the
lease_idto revoke the lease early.Attempt to look up the lease again.
Example output:
The lease ID is now invalid.
Last updated