Create a Consul ESM token

This topic describes how to create a token for the Consul external service monitor.

Introduction

Consul external service monitor (ESM) can monitor third-party or external services in contexts where you are unable to run a Consul agent. To learn more about Consul ESM, refer to the Register External Services with Consul Service Discovery tutorial.

Requirements

Core ACL functionality is available in all versions of Consul.

Consul ESM must present a token linked to policies that grant the following permissions:

  • agent:read: Enables checking version compatibility and calculating network coordinates
  • key:write: Enables storing state in the Consul KV store
  • node:read: Enables discovering Consul nodes to monitor
  • node:write: Enables updating status for the nodes that Consul ESM monitors
  • service:write: Enables Consul ESM to register as a service in the catalog
  • session:write: Enables Consul ESM is registered to acquire a leader lock
  • acl:read: (Enterprise-only) Enables Consul ESM to scan namespaces for nodes and health checks to monitor

Consul ESM only supports default admin partitions.

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:

Consul ESM token in Consul OSS

To create a token for Consul ESM, 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 Consul ESM running on an agent with the node name agent1 to monitor two nodes named node1 and node2. It allows Consul ESM to register into the catalog as the consul-esm service and write keys with the prefix consul-esm/ in the Consul KV store.

  1. agent "agent1" {
  2. policy = "read"
  3. }
  4. key_prefix "consul-esm/" {
  5. policy = "write"
  6. }
  7. node_prefix "" {
  8. policy = "read"
  9. }
  10. service "consul-esm" {
  11. policy = "write"
  12. }
  13. session "agent1" {
  14. policy = "write"
  15. }
  16. node "node1" {
  17. policy = "write"
  18. }
  19. node "node2" {
  20. policy = "write"
  21. }
  1. {
  2. "agent": {
  3. "agent1": [{
  4. "policy": "read"
  5. }]
  6. },
  7. "key_prefix": {
  8. "consul-esm/": [{
  9. "policy": "write"
  10. }]
  11. },
  12. "node": {
  13. "node1": [{
  14. "policy": "write"
  15. }],
  16. "node2": [{
  17. "policy": "write"
  18. }]
  19. },
  20. "node_prefix": {
  21. "": [{
  22. "policy": "read"
  23. }]
  24. },
  25. "service": {
  26. "consul-esm": [{
  27. "policy": "write"
  28. }]
  29. },
  30. "session": {
  31. "agent1": [{
  32. "policy": "write"
  33. }]
  34. }
  35. }

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 esm-policy.hcl.

  1. $ consul acl policy create \
  2. -name "esm-policy" -rules @esm-policy.hcl \
  3. -description "Policy for Consul ESM"

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 esm-policy.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": "esm-policy",
  5. "Description": "Policy for Consul ESM",
  6. "Rules": "agent \"agent1\" {\n policy = \"read\"\n}\nkey_prefix \"consul-esm/\" {\n policy = \"write\"\n}\nnode_prefix \"\" {\n policy = \"read\"\n}\nservice \"consul-esm\" {\n policy = \"write\"\n}\nsession \"agent1\" {\n policy = \"write\"\n}\nnode \"node1\" {\n policy = \"write\"\n}\nnode \"node2\" {\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 example creates an ACL token linked to the policy esm-policy.

  1. $ consul acl token create \
  2. -description "Token for Consul ESM" \
  3. -policy-name "esm-policy"

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 an ACL token linked to the policy esm-policy.

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

Consul ESM token in Consul Enterprise

To create a token for Consul ESM, 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 Consul ESM running on an agent named agent1 to monitor two nodes named node1 and node2. It allows Consul ESM to register into the catalog as the consul-esm service, to write keys with the prefix consul-esm/ in the Consul KV store, and to scan the default and ns1 namespaces for nodes and health checks to monitor.

  1. partition "default" {
  2. agent "agent1" {
  3. policy = "read"
  4. }
  5. key_prefix "consul-esm/" {
  6. policy = "write"
  7. }
  8. node_prefix "" {
  9. policy = "read"
  10. }
  11. service "consul-esm" {
  12. policy = "write"
  13. }
  14. session "agent1" {
  15. policy = "write"
  16. }
  17. node "node1" {
  18. policy = "write"
  19. }
  20. node "node1" {
  21. policy = "write"
  22. }
  23. namespace "default" {
  24. acl = "read"
  25. }
  26. namespace "ns1" {
  27. acl = "read"
  28. }
  29. }
  1. {
  2. "partition": {
  3. "default": [{
  4. "agent": {
  5. "agent1": [{
  6. "policy": "read"
  7. }]
  8. },
  9. "key_prefix": {
  10. "consul-esm/": [{
  11. "policy": "write"
  12. }]
  13. },
  14. "namespace": {
  15. "default": [{
  16. "acl": "read"
  17. }],
  18. "ns1": [{
  19. "acl": "read"
  20. }]
  21. },
  22. "node": {
  23. "node1": [{
  24. "policy": "write"
  25. },
  26. {
  27. "policy": "write"
  28. }]
  29. },
  30. "node_prefix": {
  31. "": [{
  32. "policy": "read"
  33. }]
  34. },
  35. "service": {
  36. "consul-esm": [{
  37. "policy": "write"
  38. }]
  39. },
  40. "session": {
  41. "agent1": [{
  42. "policy": "write"
  43. }]
  44. }
  45. }]
  46. }
  47. }

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. The policy is only valid in the specified scopes. The example policy contains permissions for multiple namespaces in multiple partitions. You must create ACL policies that grant permissions for multiple namespaces in multiple partitions in the default namespace and 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 command registers a policy defined in esm-policy.hcl.

  1. $ consul acl policy create -partition "default" -namespace "default" \
  2. -name "esm-policy" -rules @esm-policy.hcl \
  3. -description "Policy for Consul ESM"

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 esm-policy.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": "esm-policy",
  5. "Description": "Policy for Consul ESM",
  6. "Partition": "default",
  7. "Namespace": "default",
  8. "Rules": "partition \"default\" {\n agent \"agent1\" {\n policy = \"read\"\n }\n key_prefix \"consul-esm/\" {\n policy = \"write\"\n }\n node_prefix \"\" {\n policy = \"read\"\n }\n service \"consul-esm\" {\n policy = \"write\"\n }\n session \"agent1\" {\n policy = \"write\"\n }\n\n node \"node1\" {\n policy = \"write\"\n }\n node \"node1\" {\n policy = \"write\"\n }\n\n namespace \"default\" {\n acl = \"read\"\n }\n namespace \"ns1\" {\n acl = \"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.

You can specify an admin partition and namespace when creating tokens in Consul Enterprise. The token must be created in the partition and namespace where the policy was created. The following example creates an ACL token 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 esm-policy.

  1. $ consul acl token create -partition "default" -namespace "default" \
  2. -description "Token for Consul ESM" \
  3. -policy-name "esm-policy"

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 an ACL token linked to the policy esm-policy.

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