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 grantsintention: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:
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:
To learn about alternative ways to authenticate, refer to the following documentation:
Service identity in Consul CE
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
.
Service identity in Consul Enterprise Enterprise
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
:
Custom policy in Consul CE
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.
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
:
Refer to Consul ACL Policy Create for details about the consul acl token create
command.
Link the policy to a token
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
.
Custom policy in Consul Enterprise Enterprise
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.
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
:
Refer to Consul ACL Policy Create for details about the consul acl token create
command.
Link the policy to a token
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
.