Create a token for Vault with Consul storage backend

This topic describes how to create a token for Vault’s Consul storage backend.

Introduction

If you are using Vault to manage secrets in your infrastructure, you can configure Vault to use Consul’s key/value (KV) store as backend storage to persist Vault’s data. Refer to the Consul KV documentation and the Vault storage documentation for additional information.

Requirements

Core ACL functionality is available in all versions of Consul.

The Vault Consul storage backend must present a token linked to policies that grant the following permissions:

Authentication

You must provide an ACL token linked to a policy with acl:write permissions to create and modify ACL tokens and policies using the CLI or API.

You can provide the token manually using the -token option on the command line, but we recommend setting the CONSUL_HTTP_TOKEN environment variable to simplify your workflow:

  1. $ export CONSUL_HTTP_TOKEN=<acl-token-secret-id>

The Consul CLI automatically reads the CONSUL_HTTP_TOKEN environment variable so that you do not have to pass the token to every Consul CLI command.

To authenticate calls to the Consul HTTP API, you must provide the token in the X-Consul-Token header for each call:

  1. $ curl --header "X-Consul-Token: $CONSUL_HTTP_TOKEN" ...

To learn about alternative ways to authenticate, refer to the following documentation:

Create a token linked to a policy

To create a token for Vault’s Consul storage backend, you must define a policy, register the policy with Consul, and link the policy to a token.

Define a policy

You can send policy definitions as command line or API arguments or define them in an external HCL or JSON file. Refer to ACL Rules for details about all of the rules you can use in your policies.

The following example policy is defined in a file. The policy grants the appropriate permissions to enable Vault to register as a service named vault and provides access to the vault/ path in Consul’s KV store.

  1. agent_prefix "" {
  2. policy = "read"
  3. }
  4. key_prefix "vault/" {
  5. policy = "write"
  6. }
  7. service "vault" {
  8. policy = "write"
  9. }
  10. session_prefix "" {
  11. policy = "write"
  12. }
  1. {
  2. "agent_prefix": {
  3. "": [{
  4. "policy": "read"
  5. }]
  6. },
  7. "key_prefix": {
  8. "vault/": [{
  9. "policy": "write"
  10. }]
  11. },
  12. "service": {
  13. "vault": [{
  14. "policy": "write"
  15. }]
  16. },
  17. "session_prefix": {
  18. "": [{
  19. "policy": "write"
  20. }]
  21. }
  22. }

Register the policy with Consul

After defining the policy, you can register the policy with Consul using the command line or API endpoint.

Run the consul acl policy create command and specify the policy rules to create a policy. Refer to Consul ACL Policy Create for details about the consul acl policy create command.

The following example registers a policy defined in vault-storage-backend.hcl.

  1. $ consul acl policy create -partition "default" -namespace "default" \
  2. -name vault-storage-backend -rules @vault-storage-backend.hcl \
  3. -description "Policy for the Vault Consul storage backend"

Send a PUT request to the /acl/policy endpoint and specify the policy rules in the request body to create a policy. Refer to ACL Policy HTTP API for additional information about using the API endpoint.

The following example registers the policy defined in vault-storage-backend.hcl. You must embed policy rules in the Rules field of the request body.

  1. $ curl --request PUT http://127.0.0.1:8500/v1/acl/policy \
  2. --header "X-Consul-Token: $CONSUL_HTTP_TOKEN" \
  3. --data '{
  4. "Name": "vault-storage-backend",
  5. "Description": "Policy for the Vault Consul storage backend",
  6. "Rules": "agent_prefix \"\" {\n policy = \"read\"\n}\nkey_prefix \"vault/\" {\n policy = \"write\"\n}\nservice \"vault\" {\n policy = \"write\"\n}\nsession_prefix \"\" {\n policy = \"write\"\n}\n"
  7. }'

After registering the policy into Consul, you can create and link tokens using the Consul command line or API endpoint. You can also enable Consul to dynamically create tokens from trusted external systems using an auth method.

Run the consul acl token create command and specify the policy name or ID to create a token linked to the policy. Refer to Consul ACL Token Create for details about the consul acl token create command.

The following command creates the ACL token linked to the policy vault-storage-backend.

  1. $ consul acl token create \
  2. -description "Token for the Vault Consul storage backend" \
  3. -policy-name "vault-storage-backend"

Send a PUT request to the /acl/token endpoint and specify the policy name or ID in the request to create an ACL token linked to the policy. Refer to ACL Token HTTP API for additional information about using the API endpoint.

The following example creates the ACL token linked to the policy vault-storage-backend.

  1. $ curl --request PUT http://127.0.0.1:8500/v1/acl/token \
  2. --header "X-Consul-Token: $CONSUL_HTTP_TOKEN" \
  3. --data '{
  4. "Policies": [
  5. {
  6. "Name": "vault-storage-backend"
  7. }
  8. ]
  9. }'