Create a service token

This topic describes how to create a token that you can use to register a service and discover services in the Consul catalog. If you are using Consul service mesh, a sidecar proxy can use the token to discover and route traffic to other services.

Introduction

Services must present a token linked to policies that grant the appropriate set of permissions in order to be discoverable or to interact with other services in a mesh.

Service identities versus custom policies

You can create tokens linked to custom policies or to service identities. Service identities are constructs in Consul that enable you to quickly grant permissions for a group of services, rather than creating similar policies for each service.

We recommend using a service identity to grant permissions for service discovery and service mesh use cases rather than creating a custom policy. This is because service identities automatically grant the service and its sidecar proxy service:write, service:read, and node:read.

Your organization may have requirements or processes for deploying services in a way that is inconsistent with service and node identities. In these cases, you can create custom policies and link them to tokens.

Requirements

Core ACL functionality is available in all versions of Consul.

The service token must be linked to policies that grant the following permissions:

  • service:write: Enables the service to update the catalog. If service mesh is enabled, the service’s sidecar proxy can also update the catalog. Note that this permission implicitly grants intention:read permission to sidecar proxies so that they can read and enforce intentions. Refer to Intention Management Permissions for details.
  • service:read: Enables the service to learn about other services in the network. If service mesh is enabled, the service’s sidecar proxy can also learn about other services in the network.
  • node:read: Enables the sidecar proxy to discover and route traffic to other services in the catalog if service mesh is enabled.

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:

Service identity in Consul OSS

Refer to Service identities for information about creating service identities that you can link to tokens.

You can manually create 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 or service identity to link to create a token. Refer to Consul ACL Token Create for details about the consul acl token create command.

The following example creates an ACL token linked to a service identity for a service named svc1.

  1. $ consul acl token create \
  2. -description "Service token for svc1" \
  3. -service-identity "svc1"

Send a PUT request to the /acl/token endpoint and specify a service identity in the request body to create a token linked to the service identity. An ACL token linked to a policy with permissions to use the API endpoint is required. Refer to ACL Token HTTP API for additional information about using the API endpoint.

The following example creates a token linked to a service identity named svc1:

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

Service identity in Consul Enterprise EnterpriseCreate a service token - 图1Enterprise

Refer to Service identities for information about creating service identities that you can link to tokens.

You can manually create 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 or service identity to link to create a token. Refer to Consul ACL Token Create for details about the consul acl token create command.

You can specify an admin partition, namespace, or both when creating tokens in Consul Enterprise. The token can only include permissions in the specified scope, if any. The following example creates an ACL token that the service can use to register in the ns1 namespace of partition ptn1:

  1. $ consul acl token create -partition "ptn1" -namespace "ns1" \
  2. -description "Service token for svc1" \
  3. -service-identity "svc1"

Send a PUT request to the /acl/token endpoint and specify a service identity in the request body to create a token linked to the service identity. An ACL token linked to a policy with permissions to use the API endpoint is required. Refer to ACL Token HTTP API for additional information about using the API endpoint.

You can specify an admin partition and namespace when creating tokens in Consul Enterprise. The token is only valid in the specified scopes. The following example creates an ACL token that the service can use to register in the ns1 namespace of partition ptn1:

  1. $ curl --request PUT http://127.0.0.1:8500/v1/acl/token \
  2. --header "X-Consul-Token: $CONSUL_HTTP_TOKEN" \
  3. --data '{
  4. "ServiceIdentities": [
  5. {
  6. "ServiceName": "svc1"
  7. }
  8. ],
  9. "Namespace": "ns1",
  10. "Partition": "ptn1"
  11. }'

Custom policy in Consul OSS

When you are unable to link tokens to a service identity, you can define policies, register them with Consul, and link the policies to tokens that enable services to register into the Consul catalog.

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 svc1 service write permissions so that it can register into the catalog. For service mesh, the policy grants the svc1-sidecar-proxy service write permissions so that the sidecar proxy can register into the catalog. It grants service and node read permissions to discover and route to other services.

  1. service "svc1" {
  2. policy = "write"
  3. }
  4. service "svc1-sidecar-proxy" {
  5. policy = "write"
  6. }
  7. service_prefix "" {
  8. policy = "read"
  9. }
  10. node_prefix "" {
  11. policy = "read"
  12. }
  1. {
  2. "node_prefix": {
  3. "": [{
  4. "policy": "read"
  5. }]
  6. },
  7. "service": {
  8. "svc1": [{
  9. "policy": "write"
  10. }],
  11. "svc1-sidecar-proxy": [{
  12. "policy": "write"
  13. }]
  14. },
  15. "service_prefix": {
  16. "": [{
  17. "policy": "read"
  18. }]
  19. }
  20. }

Register the policy with Consul

After defining the policies, you can register them 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. The following example registers a policy defined in svc1-register.hcl:

  1. $ consul acl policy create \
  2. -name "svc1-register" -rules @svc1-register.hcl \
  3. -description "Allow svc1 to register into the catalog"

Refer to Consul ACL Policy Create for details about the consul acl token create command.

Send a PUT request to the /acl/policy endpoint and specify the policy rules in the request body to create a policy. The following example registers the policy defined in svc1-register.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": "svc1-register",
  5. "Description": "Allow svc1 to register into the catalog",
  6. "Rules": "service \"svc1\" {\n policy = \"write\"\n}\nservice \"svc1-sidecar-proxy\" {\n policy = \"write\"\n}\nservice_prefix \"\" {\n policy = \"read\"\n}\nnode_prefix \"\" {\n policy = \"read\"\n}\n"
  7. }'

Refer to ACL Policy HTTP API for additional information about using the API endpoint.

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.

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 commands create the ACL token linked to the policy svc1-register.

  1. $ consul acl token create \
  2. -description "Service token for svc1" \
  3. -policy-name "svc1-register"

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 that the svc1 service can use to register in the ns1 namespaces of partition ptn1:

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

Custom policy in Consul Enterprise EnterpriseCreate a service token - 图2Enterprise

When you are unable to link tokens to a service identity, you can define policies, register them with Consul, and link the policies to tokens that enable services to register into the Consul catalog.

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.

You can specify an admin partition and namespace when creating policies in Consul Enterprise. The policy is only valid in the specified scopes.

The following example policy is defined in a file. The policy allows the svc1 service to register in the ns1 namespace of partition ptn1. For service mesh, the policy grants the svc1-sidecar-proxy service write permissions so that the sidecar proxy can register into the catalog. It grants service and node read permissions to discover and route to other services.

  1. partition "ptn1" {
  2. namespace "ns1" {
  3. service "svc1" {
  4. policy = "write"
  5. }
  6. service "svc1-sidecar-proxy" {
  7. policy = "write"
  8. }
  9. service_prefix "" {
  10. policy = "read"
  11. }
  12. node_prefix "" {
  13. policy = "read"
  14. }
  15. }
  16. }
  1. {
  2. "partition": {
  3. "ptn1": [{
  4. "namespace": {
  5. "ns1": [{
  6. "node_prefix": {
  7. "": [{
  8. "policy": "read"
  9. }]
  10. },
  11. "service": {
  12. "svc1": [{
  13. "policy": "write"
  14. }],
  15. "svc1-sidecar-proxy": [{
  16. "policy": "write"
  17. }]
  18. },
  19. "service_prefix": {
  20. "": [{
  21. "policy": "read"
  22. }]
  23. }
  24. }]
  25. }
  26. }]
  27. }
  28. }

Register the policy with Consul

After defining the policies, you can register them 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. The following example registers a policy defined in svc1-register.hcl:

  1. $ consul acl policy create -partition "ptn1" -namespace "ns1" \
  2. -name "svc1-register" -rules @svc1-register.hcl \
  3. -description "Custom policy for service svc1"

Refer to Consul ACL Policy Create for details about the consul acl token create command.

Send a PUT request to the /acl/policy endpoint and specify the policy rules in the request body to create a policy. The following example registers the policy defined in svc1-register.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": "svc1-register",
  5. "Description": "Allow svc1 to register into the catalog",
  6. "Namespace": "ns1",
  7. "Partition": "ptn1",
  8. "Rules": "partition \"ptn1\" {\n namespace \"ns1\" {\n service \"svc1\" {\n policy = \"write\"\n }\n service \"svc1-sidecar-proxy\" {\n policy = \"write\"\n }\n service_prefix \"\" {\n policy = \"read\"\n }\n node_prefix \"\" {\n policy = \"read\"\n }\n }\n}\n"
  9. }'

Refer to ACL Policy HTTP API for additional information about using the API endpoint.

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.

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.

You can specify an admin partition and namespace when creating tokens in Consul Enterprise. The token is only valid in the specified scopes. The following example creates an ACL token that the service can use to register in the ns1 namespace of partition ptn1:

The following commands create the ACL token linked to the policy svc1-register.

  1. $ consul acl token create -partition "ptn1" -namespace "ns1" \
  2. -description "Service token for svc1" \
  3. -policy-name "svc1-register"

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.

You can specify an admin partition and namespace when creating tokens in Consul Enterprise. The token is only valid in the specified scopes. The following example creates an ACL token that the service can use to register in the ns1 namespace of partition ptn1:

  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": "svc1-register"
  7. }
  8. ],
  9. "Namespace": "ns1",
  10. "Partition": "ptn1"
  11. }'