HashiCorp Cloud Platform
Remediate leaked secrets
HCP Vault Radar can scan for secrets leaked in your code.
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.
- Determine if your company already uses HashiCorp Vault.
Store secrets in Vault KVv2 secret engine
This example uses the KVv2 secrets engine and AppRole auth method. If your application is already configured to access secrets in Vault, use existing secrets engines and auth methods.
Set the following environment variables in your local environment:
VAULT_TOKEN VAULT_ADDR VAULT_NAMESPACEAdd the secret to the Vault KV version 2 secrets engine.
$ vault kv put -mount=mount-path my-secret my-secret-value=secret-valueRead the secret.
$ vault kv get -mount=mount-path my-secretCreate a policy to allow access to the KVv2 secret engine.
$ vault policy write my-policy -<<EOF path "my-secret" { capabilities = [ "read" ] } EOFEnable the
AppRoleauthentication method.$ vault auth enable approleCreate a role and attach the policy.
$ vault write auth/approle/role/my-role \ token_policies=my-policy
Remove the secret from code
Now that the 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.
Populate the environment variable
MY_SECRETwith the secret and start your local server.$ export MY_SECRET=$(vault kv get -mount=month-path {SECRET} my-secret-value)Start the application.
$ run-my-appRemove the secret from source and add a reference to the secret stored at the environment variable MY_SECRET.
mySecret = getenv(“MY_SECRET”)
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 service owner to rotate the secret stored in the Vault.
Update the secret value in Vault
$ vault kv put -mount=mount-path my-secret my-secret-value=new-value