Create a DNS token
This topic describes how to create a token that enables the Consul DNS to query services in the network when ACLs are enabled.
Introduction
The Consul binary ships with a DNS server that you can use for service discovery in your network. The agent that fulfills DNS lookups requires appropriate ACL permissions to discover services, nodes, and prepared queries registered in Consul.
A Consul agent must be configured with a token linked to policies that grant the appropriate set of permissions.
Specify the default
token to the Consul agent to authorize the agent to respond to DNS queries. Refer to DNS usage overview for details on configuring and using Consul DNS.
Requirements
Core ACL functionality is available in all versions of Consul.
The DNS token must be linked to policies that grant the following permissions:
service:read
: Enables the agent to perform service lookups for DNSnode:read
: Enables node lookups over DNSquery:read
: Enables the agent to perform prepared query lookups for DNS
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:
DNS token in Consul CE
To create a token for DNS, 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 to enable a Consul agent to respond to DNS queries.
node_prefix "" { policy = "read"}service_prefix "" { policy = "read"}query_prefix "" { policy = "read"}
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 dns-access.hcl
.
$ consul acl policy create \ -name "dns-access" -rules @dns-access.hcl \ -description "DNS Policy"
Link the policy to a token
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 command creates the ACL token linked to the policy dns-access
.
$ consul acl token create \ -description "DNS token" \ -policy-name "dns-access"
DNS token in Consul Enterprise
To create a token for DNS, 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 to enable a Consul agent to respond to DNS queries for resources in any namespace in any partition.
partition "default" { namespace "default" { query_prefix "" { policy = "read" } }}partition_prefix "" { namespace_prefix "" { node_prefix "" { policy = "read" } service_prefix "" { policy = "read" } }}
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 when creating policies in Consul Enterprise. The policy is only valid in the specified admin partition. 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.
consul acl policy create -partition "default" -namespace "default" \ -name dns-access -rules @dns-access.hcl \ -description "DNS Policy"
Link the policy to a token
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 command creates the ACL token linked to the policy dns-access
.
$ consul acl token create -partition "default" -namespace "default" \ -description "DNS token" \ -policy-name "dns-access"
Apply the token
Configure the Consul agent with the token by either specifying the token in the agent configuration file or by using the consul set-agent-token
command.
Apply the token in a file
Specify the token in the default
field of the agent configuration file so that the agent can present it and register into the catalog on startup.
acl = { enabled = true tokens = { default = "<token>" ... } ...}
Apply the token with a command
Set the default
token using the acl.token.default
command. The following command configures a running Consul agent token with the specified token.
$ consul set-agent-token default <acl-token-secret-id>