Consul
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.
Templated policies versus custom policies
You can create tokens linked to custom policies or to templated policies. Templated policies are constructs in Consul that enable you to quickly grant permissions for common use cases, rather than creating similar policies.
We recommend using the builtin/service
templated policy to grant permissions for service discovery and service mesh use cases rather than creating a custom policy. This is because the builtin/service
templated policy 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 the builtin/service
templated policy. 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:
$ 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:
$ curl --header "X-Consul-Token: $CONSUL_HTTP_TOKEN" ...
To learn about alternative ways to authenticate, refer to the following documentation:
Templated policies in Consul CE
Refer to Templated policies for information about creating templated policies 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 a templated policy 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 the builtin/service
templated policy for a service named svc1
.
$ consul acl token create \
-description "Service token for svc1" \
-templated-policy "builtin/service" \
-var "name:api"
Templated policies in Consul Enterprise Enterprise
Refer to Templated policies for information about the use of templated policies in tokens and roles.
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 templated policy 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
:
$ consul acl token create -partition "ptn1" -namespace "ns1" \
-description "Service token for svc1" \
-templated-policy "builtin/service"
-var "name:svc1"
Custom policy in Consul CE
When you are unable to link tokens to a templated policy, 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.
service "svc1" {
policy = "write"
}
service "svc1-sidecar-proxy" {
policy = "write"
}
service_prefix "" {
policy = "read"
}
node_prefix "" {
policy = "read"
}
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
:
$ consul acl policy create \
-name "svc1-register" -rules @svc1-register.hcl \
-description "Allow svc1 to register into the catalog"
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
.
$ consul acl token create \
-description "Service token for svc1" \
-policy-name "svc1-register"
Custom policy in Consul Enterprise Enterprise
When you are unable to link tokens to a templated policy, 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.
partition "ptn1" {
namespace "ns1" {
service "svc1" {
policy = "write"
}
service "svc1-sidecar-proxy" {
policy = "write"
}
service_prefix "" {
policy = "read"
}
node_prefix "" {
policy = "read"
}
}
}
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
:
$ consul acl policy create -partition "ptn1" -namespace "ns1" \
-name "svc1-register" -rules @svc1-register.hcl \
-description "Custom policy for service svc1"
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
.
$ consul acl token create -partition "ptn1" -namespace "ns1" \
-description "Service token for svc1" \
-policy-name "svc1-register"