HashiCorp Cloud Platform
Remediate leaked Google Cloud (GCP) secrets
HCP Vault Radar can scan for leaked Google Cloud Platform (GCP) secrets using dynamic secrets, for information on how to use statics secrets, go to the active secrets documentation.
Create an incident
Leaked secrets can lead to unauthorized access to your services. To prevent malicious activity, HashiCorp recommends users to rotate and store the secret in Vault.
- Follow your organization’s guidelines for emergency rotation of a secret.
- Contact your GCP account owner.
- Determine if your company already uses HashiCorp Vault.
Configure Vault's Google Cloud (GCP) secret engine
This example uses the GCP secrets engine and roleset. If your application is already configured to access secrets in Vault, use existing secrets engines and auth methods.
Set up a service account for the Google Cloud secret engine, the service account should have the following minimum scopes:
https://www.googleapis.com/auth/cloud-platformMinimum required permissions
# Service account + key admin iam.serviceAccounts.create iam.serviceAccounts.delete iam.serviceAccounts.get iam.serviceAccounts.list iam.serviceAccounts.update iam.serviceAccountKeys.create iam.serviceAccountKeys.delete iam.serviceAccountKeys.get iam.serviceAccountKeys.listWhen using rolesets or static accounts with bindings, Vault must have the following permissions:
<service>.<resource>.getIamPolicy <service>.<resource>.setIamPolicyWhere
serviceandresourcecorrespond to permissions you will grant, for example:# Projects resourcemanager.projects.getIamPolicy resourcemanager.projects.setIamPolicy # All compute compute.*.getIamPolicy compute.*.setIamPolicy # BigQuery datasets bigquery.datasets.get bigquery.datasets.updateAssign the set of roles required to get resource-specific
getIamPolicy/setIamPolicypermissions. At a minimum you will need to assignroles/iam.serviceAccountAdminandroles/iam.serviceAccountKeyAdminso Vault can manage service accounts and keys.For more information on IAM requirement, refer to this document.
Set the following environment variables in your local environment:
VAULT_TOKEN VAULT_ADDR VAULT_NAMESPACE GOOGLE_APPLICATION_CREDENTIALSEnable the GCP secret engine in Vault.
$ vault secrets enable gcp
Configure the secrets engine with account credentials, or leave blank or unwritten to use Application Default Credentials.
$ vault write gcp/config credentials=@my-credentials.jsonCreate a roleset in Vault.
$ vault write gcp/roleset/my-token-roleset \ project={my-project-id} \ secret_type="access_token" \ token_scopes="https://www.googleapis.com/auth/cloud-platform" \ bindings=-<<EOF resource "//cloudresourcemanager.googleapis.com/projects/{my-project-id}" { roles = ["roles/viewer"] } EOFUse the roleset to read credentials from GCP. For more information, refer to the Vault documentation.
$ vault read gcp/roleset/my-token-roleset/token Key Value --- ----- expires_at_seconds 1537402548 token ya29.c.ElodBmNLMNOPgcBpnXcE4ywG4w1k... token_ttl 3599
Remove the secret from code
Now that the GCP secret is available from Vault, remove the hardcoded secret from the source code. This example uses an environment variable to store the secret from Vault.
Create an environment variable with the secret from Vault.
$ export MY_SECRET=$($ curl \ --header "X-Vault-Token: ..." \ --request GET \ https://127.0.0.1:8200/v1/gcp/config)Remove the secret from source and add a reference to the secret stored at the environment variable
MY_SECRET.mySecret = getenv(“MY_SECRET”)Start your local server and test.
Revoke the secret
Revoke the secret to complete the remediation process.
- Validate the environment variable value is from Vault.
- Deploy and test the application.
- Work with the GCP service owner to revoke the previous secret value.