How to Use Kong Vault Authentication Plugin
Vault Authentication plugin integrates Hashicorp Vault
to Kong Enterprise for user to manage their credentials. Users can either load existing credentials from vault or they create and store new credentials to vault via Kong Admin API.
Prepare Vault
I have vault install on my mac, If you’ve set up your vault instance already or prefer any other deployment methods, you can jump to the next section.
Dev Mode
Enable Vault dev mode
1 | vault server -dev -dev-listen-address=0.0.0.0:8200 |
See the unseal code and root token
1 | Unseal Key: 2XuiYKb8GUL5E7Jny+ukn9hPOP+r+UDCBMUxRHpxmdk= |
Create vault environment Variable
1 | export VAULT_ADDR='http://0.0.0.0:8200' |
Create version 1 kv engine
1 | vault secrets enable -version=1 -path=key-auth kv |
Standalone Mode (file storage)
Create Config file
1 | disable_mlock = true |
Start Vault
1 | vault server -config=vault-server.hcl |
Init Vault
open a new terminal window and put below environment variable
1 | export VAULT_ADDR='http://0.0.0.0:8200' |
Then we can init
1 | vault operator init -n=1 -t=1 |
We should get 1 unseal key
and 1 root token
1 | Unseal Key 1: 9LF/fflOotsX8biDBK6yqKgQPcB1803J/jTHbWlaq6E= |
Unseal Vault
1 | vault operator unseal 9LF/fflOotsX8biDBK6yqKgQPcB1803J/jTHbWlaq6E= |
Add Token to Environment Varible
1 | export VAULT_TOKEN=s.A2OPJxNH9g9TpxnqJyloCCr9 |
Create version 1 KV engine
1 | vault secrets enable -path=key-auth -version=1 kv |
Usage example
All examples below are deployed locally with Docker or Minikube with Kong EE version 2.1.4.0-alpine image. Please adjust settings to suit your needs.
Prerequisite : A Kong instance running with Admin API
You can find official documentation here.
In the first part, I will use Admin API
to set up Vault authentication plugin step by step.
Let’s begin:
Create a service
1 | curl -X POST http://localhost:8001/services \ |
Create Route
1 | curl -X POST http://localhost:8001/services/vault-service/routes \ |
When we visit our route at http://<KONG_PROXY>/valut
, we should get HTTP/1.1 200 OK
.
Create Vault object
1 | curl --request POST \ |
You should get a response like below.
1 | { |
Enable Vault Auth plugin on this route
You can also enable JWT plugin on a service or globally.
1 | curl --request POST \ |
If we visit the route again, we will get HTTP/1.1 401 Unauthorized
Create consumer
1 | curl -X POST http://localhost:8001/consumers \ |
Create Credential
Below command creates a credential for bob
and set ttl to 3600 (1 hour) which means Kong will fetch this credential from vault every 1 hour.
1 | curl -X POST http://localhost:8001/vaults/<VAULT_OBJECT_NAME>/credentials/vault-user \ |
We should get a response similar as below:
1 | { |
Access API
1 | curl <KONG_PROXY>:8000/vault \ |
You should be able to access your API now.
Other deployments methods
DBless deployment
Because all credentials are stored in vault, we can still Admin API
to create credentials in dbless deployment. If you prefer to use vault to manager all credential without exposing Admin API, you can create vault credentials manually and load it when Kong starts.
For further detail, please check create credential manually
First we need to generate an UUID
for our vault object. Let’s use uuidgen
to generate one E1B0164F-E80E-4373-9880-60B0C2C515DF
. This will be used as your Vault object ID.
Then we can start Kong with below configurations. Please change all the id, token and secrets to your setting.
1 | _format_version: "2.1" |
Access API
Because we have created credentials previously, we can use credential directly once Kong starts.
1 | curl <KONG_PROXY>:8000/vault \ |
Kubernetes Ingress Controller
We need to use custom entities to define our Vault object on Kubernetes.
Create Namespace
1 | kubectl create namespace kong |
Create vault.json
file.
1 | { |
Store the configuration as a Kubernetes Secret.
1 | kubectl create secret generic -n kong kong-custom-entities --from-file=config=vault.json |
Deploy Kong in DBless mode
Please check here for detail.
You can add environment variable to the ingress-ccontroller
container on your yaml file before deploying the file.
1 | env: |
You can also deploy the official file
1 | kubectl apply -f https://bit.ly/k4k8s-enterprise-install |
After it is deployed you can use kubectl
to edit or jsonpatch
to patch deployment.
1 | kubectl patch deployment ingress-kong -n kong --type json -p='[{"op":"add","path":"/spec/template/spec/containers/1/env/0","value":{"name":"CONTROLLER_KONG_CUSTOM_ENTITIES_SECRET","value":"kong/kong-custom-entities"}}]' |
Deploy service, consumer, plugin and ingress as below
Please save below to kong.yaml
file and kubectl apply -f kong.yaml
to apply it. Please make sure to change <VAULT_ID>
to match the vault object we created above.
1 | apiVersion: apps/v1 |
Access API
After deployment completed, we can visit our route again.
1 | curl <KONG_PROXY>:8000/vault \ |
Other resources
Create credential manually
Users can generate credentials manually into vault and load these credentials to Kong memory.
Create consumer id
On Mac, you can use uuidgen
1 | uuidgen |
Generate 2 random password
We will use these two password as access_token
and secret_token
.
1 | pwgen -s 32 2 |
Combine UUID and Password
Combine uuid
, access_token
and secret_token
into json and then you can create your credentail with Vault UI.
1 | { |
Put credential to vault via vault CLI
You can also use Vault CLI. Please save above content to secret.json
and run.
1 | vault kv put key-auth/I15D9KE9crnIASqrCa98x49Usj3SODsg @secret.json |
User Mapping
-
With Admin API
We can create a user with the generateduuid
above.1
http POST :8001/consumers/ username=bob id=672C883C-F4ED-4EFB-AF2E-97CC263B824D
-
DBless mode
1
2
3consumers:
- username: paul
id: 672C883C-F4ED-4EFB-AF2E-97CC263B824D -
Kubernetes
Assign
consumer.id
to a KongConsumer object is not supported at the moment.