Create a replication token

This topic describes how to configure an ACL token for ACL replication between WAN-federated datacenters. If your Consul clusters are connected through peer connections, ACL replication is not required. To learn more about cluster peering, refer to the comparison between WAN federation and cluster peering.

Introduction

Consul agents must present a token linked to policies that grant the appropriate set of permissions. Specify the replication token on each server in a non-primary datacenter. For hands-on instructions on how to configure ACL replication across datacenters, refer to the ACL Replication for Multiple Datacenters tutorial.

Requirements

Core ACL functionality is available in all versions of Consul.

For a Consul server agent with ACL replication enabled in a secondary datacenter, the token must be linked to a policy that grants the following permissions:

  • acl:write: Enables replication of ACL resources
  • operator:write: Enables replication of the proxy-defaults configuration entry and enables CA certification signing in the secondary datacenter
  • service:read and intention:read: Enables replication of the service-defaults and intentions configuration entries

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:

Replication token in Consul OSS

To create a token for ACL replication, 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 for ACL replication.

  1. acl = "write"
  2. operator = "write"
  3. service_prefix "" {
  4. policy = "read"
  5. intentions = "read"
  6. }
  1. {
  2. "acl": "write",
  3. "operator": "write",
  4. "service_prefix": {
  5. "": [{
  6. "intentions": "read",
  7. "policy": "read"
  8. }]
  9. }
  10. }

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 acl-replication.hcl.

  1. $ consul acl policy create \
  2. -name "acl-replication" -rules @acl-replication.hcl \
  3. -description "ACL replication token"

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 acl-replication.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": "acl-replication",
  5. "Description": "ACL replication",
  6. "Rules": "acl = \"write\"\noperator = \"write\"\nservice_prefix \"\" {\n policy = \"read\"\n intentions = \"read\"\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 acl-replication.

  1. $ consul acl token create \
  2. -description "ACL replication" \
  3. -policy-name "acl-replication"

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 acl-replication.

  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": "acl-replication"
  7. }
  8. ]
  9. }'

Replication token in Consul Enterprise

To create a token for ACL replication, 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 following example policy grants the appropriate permissions for ACL replication.

  1. operator = "write"
  2. service_prefix "" {
  3. policy = "read"
  4. intentions = "read"
  5. }
  6. namespace_prefix "" {
  7. acl = "write"
  8. service_prefix "" {
  9. policy = "read"
  10. intentions = "read"
  11. }
  12. }
  1. {
  2. "namespace_prefix": {
  3. "": [{
  4. "acl": "write",
  5. "service_prefix": {
  6. "": [{
  7. "intentions": "read",
  8. "policy": "read"
  9. }]
  10. }
  11. }]
  12. },
  13. "operator": "write",
  14. "service_prefix": {
  15. "": [{
  16. "intentions": "read",
  17. "policy": "read"
  18. }]
  19. }
  20. }

Register the policy with Consul

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

You can specify an admin partition, namespace, or both when registering policies in Consul Enterprise. Policies are only valid in the specified scopes. The policy for replication must be created in the default namespace and default partition.

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 acl-replication.hcl.

  1. $ consul acl policy create -partition "default" -namespace "default" \
  2. -name "acl-replication" -rules @acl-replication.hcl \
  3. -description "ACL replication token"

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 acl-replication.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": "acl-replication",
  5. "Description": "ACL replication",
  6. "Partition": "default",
  7. "Namespace": "default",
  8. "Rules": "operator = \"write\"\nservice_prefix \"\" {\n policy = \"read\"\n intentions = \"read\"\n}\nnamespace_prefix \"\" {\n acl = \"write\"\n service_prefix \"\" {\n policy = \"read\"\n intentions = \"read\"\n }\n}\n"
  9. }'

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.

  1. $ consul acl token create -partition "default" -namespace "default" \
  2. -description "ACL replication" \
  3. -policy-name "acl-replication"

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 acl-replication.

  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": "acl-replication"
  7. }
  8. ],
  9. "Partition": "default",
  10. "Namespace": "default"
  11. }'

Apply the token

Configure the Consul agent with the token by either specifying the token in the agent configuration file or by using the consul set-agent-token command.

Apply the token in a file

Specify the token in the replication field of the agent configuration file so that the agent can present it and register into the catalog on startup.

  1. acl = {
  2. enabled = true
  3. tokens = {
  4. replication = "<token>"
  5. ...
  6. }
  7. ...
  8. }

Apply the token with a command

Set the replication token using the consul set-agent-token command. The following command configures a running Consul agent token with the specified token.

  1. $ consul set-agent-token replication <acl-token-secret-id>