Create a snapshot agent token

This topic describes how to create a token for the Consul snapshot agent.

EnterpriseCreate a snapshot agent token - 图1Enterprise

This feature requires Consul Enterprise(opens in new tab).

Introduction

The consul snapshot agent command starts a process that takes snapshots of the state of the Consul servers and either saves them locally or pushes them to a remote storage service. Refer to Consul Snapshot Agent for additional information.

Requirements

Core ACL functionality is available in all versions of Consul.

Requirements for the agent command

The agent subcommand requires Consul Enterprise. All other snapshot subcommands are available in the open source version of Consul.

Snapshot agent ACL requirements

The Consul snapshot agent must present a token linked to policies that grant the following set of permissions.

  • acl:write: Enables the agent read and snapshot ACL data
  • key:write: Enables the agent to create a key in the Consul KV store that serves as a leader election lock when multiple snapshot agents are running in an environment
  • session:write: Enables the agent to create sessions for the specified Consul node where it is running
  • service:write: Enables the agent to register into the catalog

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

To create a token for the snapshot agent, 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 a snapshot agent running on a node named server-1234 to register into the catalog as the consul-snapshot service. It uses the key consul-snapshot/lock for a leader election lock.

  1. acl = "write"
  2. key "consul-snapshot/lock" {
  3. policy = "write"
  4. }
  5. session "server-1234" {
  6. policy = "write"
  7. }
  8. service "consul-snapshot" {
  9. policy = "write"
  10. }
  1. {
  2. "acl": "write",
  3. "key": {
  4. "consul-snapshot/lock": [{
  5. "policy": "write"
  6. }]
  7. },
  8. "service": {
  9. "consul-snapshot": [{
  10. "policy": "write"
  11. }]
  12. },
  13. "session": {
  14. "server-1234": [{
  15. "policy": "write"
  16. }]
  17. }
  18. }

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 and namespace when creating policies in Consul Enterprise. Policies are only valid in the specified scopes. You must create the policy for the snapshot agent in the default namespace in the 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 snapshot-agent.hcl:

  1. $ consul acl policy create -partition "default" -namespace "default" \
  2. -name snapshot-agent -rules @snapshot-agent.hcl \
  3. -description "Snapshot agent policy"

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 snapshot-agent.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": "snapshot-agent",
  5. "Description": "Snapshot agent policy",
  6. "Partition": "default",
  7. "Namespace": "default",
  8. "Rules": "acl = \"write\"\nkey \"consul-snapshot/lock\" {\n policy = \"write\"\n}\nsession \"server-1234\" {\n policy = \"write\"\n}\nservice \"consul-snapshot\" {\n policy = \"write\"\n}\n"
  9. }'

After registering the policies 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.

You can specify an admin partition and namespace when creating tokens in Consul Enterprise. Tokens are only valid in the specified scopes. The snapshot agent token must be created in the default namespace in the default partition.

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 snapshot-agent.

  1. $ consul acl token create -partition "default" -namespace "default" \
  2. -description "Snapshot agent token" \
  3. -policy-name "snapshot-agent"

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.

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