curl -X POST "http://localhost:8001/vaults" \
--no-progress-meter --fail-with-body \
-H "Content-Type: application/json" \
--json '{
"name": "hcv",
"prefix": "hashicorp-vault",
"description": "Storing secrets in HashiCorp Vault",
"config": {
"host": "'$HCV_HOST'",
"kv": "v1",
"mount": "kong",
"port": 8200,
"protocol": "http",
"auth_method": "gcp_gce",
"gcp_auth_role": "'$GCP_AUTH_ROLE'",
"gcp_login_path": "/v1/auth/gcp/login"
}
}'Configure HashiCorp Vault as a vault backend with GCP workload identity
Run Kong Gateway on a GCE instance with a service account attached. Enable the GCP auth method in HashiCorp Vault and create a GCE role bound to your Kong Gateway service account.
Then in Kong Gateway, configure a Vault entity with the following:
- Set
config.auth_methodtogcp_gce - Set
config.gcp_auth_roleto the Vault role name. The GCE instance identity token is provided automatically by the instance metadata service, no credential files are required.
Prerequisites
GCE instance with service account
To complete this tutorial, Kong Gateway must be running on a GCE (Google Compute Engine) instance with a service account that Kong Gateway will use to authenticate to HashiCorp Vault.
- Enable the following GCP APIs in your project:
iam.googleapis.comcompute.googleapis.com
- Export the bound project, zone, and labels for your GCE instance:
export GCP_PROJECT='YOUR GCP PROJECT' export GCE_ZONE='YOUR GCE INSTANCE ZONE' export GCE_LABEL='YOUR GCE INSTANCE LABELS' - Create a service account to attach to your GCE instance with the “Compute Viewer” and “View Service Accounts” permissions.
- Export the service account email attached to your GCE instance:
export GCE_SERVICE_ACCOUNT="kong@YOUR-PROJECT.iam.gserviceaccount.com" - Attach the service account to your GCE instance.
If Kong Gateway isn’t running on a GCE instance, use GCP service account authentication instead.
HashiCorp Vault
You need HashiCorp Vault installed on your VM.
The steps in this how to assume that HashiCorp Vault and Kong Gateway are installed on the same VM. Production instances will often install HashiCorp Vault and Kong Gateway on separate VMS. If this is the case, see the HashiCorp Vault GCP authentication documentation for the configuration changes you’ll need to make.
Configure HashiCorp Vault
Before you can configure the Vault entity in Kong Gateway, you must configure HashiCorp Vault to authenticate clients using GCE instance identity tokens and store a secret.
Create configuration files
First, create the primary configuration file config.hcl for HashiCorp Vault in the ./vault directory:
listener "tcp" {
address = "0.0.0.0:8200"
tls_disable = true
}
storage "file" {
path = "./vault/data"
}
ui = trueThen, create the HashiCorp Vault policy file rw-secrets.hcl in the ./vault directory:
path "*" {
capabilities = ["create", "read", "update", "delete", "list", "sudo"]
}Configure the Vault and store a secret
- In a new terminal, start HashiCorp Vault:
vault server -config=./vault/config.hcl - In your previous terminal, set the Vault address:
export VAULT_ADDR="http://localhost:8200" - Initialize the Vault:
vault operator init -key-shares=1 -key-threshold=1This outputs your unseal key and initial root token. Export them as environment variables:
export HCV_UNSEAL_KEY='YOUR-UNSEAL-KEY' export DECK_HCV_TOKEN='YOUR-INITIAL-ROOT-TOKEN' - Unseal your Vault:
vault operator unseal $HCV_UNSEAL_KEY - Log in to your Vault:
vault login $DECK_HCV_TOKEN - Write the policy to access secrets:
vault policy write rw-secrets ./vault/rw-secrets.hcl - Enable GCP authentication:
vault auth enable gcp - Create a GCE role that binds to Kong Gateway’s GCP service account:
vault write auth/gcp/role/kong-role \ type="gce" \ bound_projects="$GCP_PROJECT" \ bound_zones="$GCE_ZONE" \ bound_labels="$GCE_LABEL" \ bound_service_accounts="$GCE_SERVICE_ACCOUNT" \ token_policies="rw-secrets" - Enable the K/V secrets engine:
vault secrets enable -path=kong kv - Create a secret:
vault kv put kong/headers/request header="x-kong:test" - Confirm you can retrieve the secret through Vault:
vault kv get kong/headers/request
Set environment variables
Find the internal IP for your VM:
hostname -IExport the following environment variables before creating the Vault entity:
export HCV_HOST="YOUR VM INTERNAL IP"
export GCP_AUTH_ROLE=kong-roleCreate a Vault entity for HashiCorp Vault
Using decK, create a Vault entity in the kong.yaml file with the required parameters for HashiCorp Vault GCP workload identity authentication:
Validate
To validate that the secret was stored correctly in HashiCorp Vault, call a secret from your vault using the kong vault get command.
sudo -E kong vault get {vault://hashicorp-vault/headers/request/header}If the vault was configured correctly, this command returns the value of the secret. You can use {vault://hashicorp-vault/headers/request/header} to reference the secret in any referenceable field.
For more information about supported secret types, see What can be stored as a secret.
Cleanup
Clean up HashiCorp Vault
Stop the HashiCorp Vault process by running the following:
pkill vaultUnset environment variables:
unset VAULT_ADDR
unset VAULT_GCP_CREDENTIALS_FILEDestroy the Kong Gateway container
curl -Ls https://get.konghq.com/quickstart | bash -s -- -dFAQs
What if Kong Gateway is not running on a GCE instance?
The gcp_gce auth method requires Kong Gateway to run on a GCE instance.
It relies on the GCE instance metadata service to provide the identity token automatically.
If Kong Gateway isn’t on GCE, use GCP service account authentication (gcp_iam) instead, which works from any environment.
How do I rotate my secrets in HashiCorp Vault and how does Kong Gateway pick up the new secret values?
You can rotate your secret in HashiCorp Vault by creating a new secret version with the updated value. You’ll also want to configure the ttl settings in your Kong Gateway Vault entity so that Kong Gateway pulls the rotated secret periodically.
Can I configure Vault in a different way without using the Vault entity directly?
Yes, you can also configure a Vault in one of the following ways:
- Using environment variables, set at Kong Gateway startup
- Using parameters in
kong.conf, set at Kong Gateway startup
See the Vault reference for your provider for the available parameters and their format in each method.