Create a mesh gateway token

This topic describes how to create a token for a mesh gateway.

Introduction

Mesh gateways enable service-to-service traffic between Consul datacenters or between Consul admin partitions. They also enable datacenters to be federated across wide area networks. Refer to Mesh Gateways for additional information.

Gateways must present a token linked to policies that grant the appropriate set of permissions in order to be discoverable and to route to other services in a mesh.

Requirements

Core ACL functionality is available in all versions of Consul.

The mesh gateway must present a token linked to a policy that grants the following permissions:

  • mesh:write to obtain leaf certificates for terminating TLS connections
  • peering:read for Consul cluster peering through mesh gateways. If you are not using cluster peering or if the mesh gateway is not in the default partition, then you can omit the peering:read permission.
  • service:write to allow the mesh gateway to register into the catalog
  • service:read for all services and node:read for all nodes in order to discover and route to services
  • agent:read to enable the consul connect envoy CLI command to automatically discover gRPC settings from the Consul agent. If this command is not used to start the gateway or if the Consul agent uses the default gRPC settings, then you can omit the agent:read permission.

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 OSS

To create a token for the mesh gateway, 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 register as a service named mesh-gateway and to operate as a mesh gateway.

  1. mesh = "write"
  2. peering = "read"
  3. service "mesh-gateway" {
  4. policy = "write"
  5. }
  6. service_prefix "" {
  7. policy = "read"
  8. }
  9. node_prefix "" {
  10. policy = "read"
  11. }
  12. agent_prefix "" {
  13. policy = "read"
  14. }
  1. {
  2. "mesh": "write",
  3. "peering": "read",
  4. "service": {
  5. "mesh-gateway": [{
  6. "policy": "write"
  7. }]
  8. },
  9. "service_prefix": {
  10. "": [{
  11. "policy": "read"
  12. }]
  13. },
  14. "node_prefix": {
  15. "": [{
  16. "policy": "read"
  17. }]
  18. },
  19. "agent_prefix": {
  20. "": [{
  21. "policy": "read"
  22. }]
  23. }
  24. }

Register the policy with Consul

After defining the policy, you can register the policy with Consul using the command line or API endpoint.

The following commands create the ACL policy and token.

Run the consul acl policy create command and specify the policy rules to create a policy. The following example registers a policy defined in mgw-register.hcl:

  1. $ consul acl policy create \
  2. -name "mgw-register" -rules @mgw-register.hcl \
  3. -description "Mesh gateway policy"

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 mgw-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": "mgw-register",
  5. "Description": "Mesh gateway policy",
  6. "Rules": "mesh = \"write\"\npeering = \"read\"\nservice \"mesh-gateway\" {\n policy = \"write\"\n}\nservice_prefix \"\" {\n policy = \"read\"\n}\nnode_prefix \"\" {\n policy = \"read\"\n}\nagent_prefix \"\" {\n policy = \"read\"\n}\n"
  7. }'

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

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 mgw-register.

  1. $ consul acl token create \
  2. -description "Mesh gateway token" \
  3. -policy-name "mgw-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.

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

Consul Enterprise in default partition

To create a token for the mesh gateway, 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.

You can specify an admin partition and namespace when using Consul Enterprise. Mesh gateways must register into the default namespace.

The following example policy is defined in a file. The policy grants the appropriate permissions to register as a service named mesh-gateway and to operate as a mesh gateway in the default partition.

  1. mesh = "write"
  2. partition_prefix "" {
  3. peering = "read"
  4. }
  5. partition "default" {
  6. namespace "default" {
  7. service "mesh-gateway" {
  8. policy = "write"
  9. }
  10. agent_prefix "" {
  11. policy = "read"
  12. }
  13. }
  14. namespace_prefix "" {
  15. node_prefix "" {
  16. policy = "read"
  17. }
  18. service_prefix "" {
  19. policy = "read"
  20. }
  21. }
  22. }
  1. {
  2. "mesh": "write",
  3. "partition": {
  4. "default": [{
  5. "namespace": {
  6. "default": [{
  7. "service": {
  8. "mesh-gateway": [{
  9. "policy": "write"
  10. }]
  11. },
  12. "agent_prefix": {
  13. "": [{
  14. "policy": "read"
  15. }]
  16. }
  17. }]
  18. },
  19. "namespace_prefix": {
  20. "": [{
  21. "node_prefix": {
  22. "": [{
  23. "policy": "read"
  24. }]
  25. },
  26. "service_prefix": {
  27. "": [{
  28. "policy": "read"
  29. }]
  30. }
  31. }]
  32. }
  33. }]
  34. },
  35. "partition_prefix": {
  36. "": [{
  37. "peering": "read"
  38. }]
  39. }
  40. }

Register the policy with Consul

After defining the policy, you can register the policy with Consul using the command line or API endpoint.

The following commands create the ACL policy and token.

Run the consul acl policy create command and specify the policy rules to create a policy. The following example registers a policy defined in mgw-register.hcl:

You can specify an admin partition when creating policies in Consul Enterprise. The policy is only valid in the specified admin partition. You must create the policy in the partition where the mesh gateway registers. The following example creates the policy in the default partition.

  1. $ consul acl policy create -partition "default" \
  2. -name mgw-register -rules @mgw-register.hcl \
  3. -description "Mesh gateway policy"

Refer to Consul ACL Policy Create for details about the consul acl policy 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 mgw-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": "mgw-register",
  5. "Description": "Mesh gateway policy",
  6. "Partition": "default",
  7. "Rules": "mesh = \"write\"\npeering = \"read\"\npartition_prefix \"\" {\n peering = \"read\"\n}\nnamespace \"default\" {\n service \"mesh-gateway\" {\n policy = \"write\"\n }\n agent_prefix \"\" {\n policy = \"read\"\n }\n}\nnamespace_prefix \"\" {\n node_prefix \"\" {\n \tpolicy = \"read\"\n }\n service_prefix \"\" {\n policy = \"read\"\n }\n}\n"
  8. }'

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

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.

You can specify an admin partition when creating tokens in Consul Enterprise. The token is only valid in the specified admin partition. The token must be created in the partition where the mesh gateway registers. The following example creates the token in the partition ptn1.

  1. $ consul acl token create -partition "default" \
  2. -description "Mesh gateway token" \
  3. -policy-name "mgw-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 when creating tokens in Consul Enterprise. The token is only valid in the specified admin partition. The token must be created in the partition where the mesh gateway registers. The following example creates the token in the 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": "mgw-register"
  7. }
  8. ],
  9. "Partition": "default"
  10. }'

Consul Enterprise in non-default partition

To create a token for the mesh gateway, 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.

You can specify an admin partition and namespace when using Consul Enterprise. Mesh gateways must register into the default namespace. To register a mesh gateway in a non-default partition, create the ACL policy and token in the partition where the mesh gateway registers.

The following example policy is defined in a file. The policy grants the appropriate permissions to register as a service named mesh-gateway and to operate as a mesh gateway in a non-default partition.

  1. mesh = "write"
  2. namespace "default" {
  3. service "mesh-gateway" {
  4. policy = "write"
  5. }
  6. agent_prefix "" {
  7. policy = "read"
  8. }
  9. }
  10. namespace_prefix "" {
  11. node_prefix "" {
  12. policy = "read"
  13. }
  14. service_prefix "" {
  15. policy = "read"
  16. }
  17. }
  1. {
  2. "mesh": "write",
  3. "namespace": {
  4. "default": [{
  5. "service": {
  6. "mesh-gateway": [{
  7. "policy": "write"
  8. }]
  9. },
  10. "agent_prefix": {
  11. "": [{
  12. "policy": "read"
  13. }]
  14. }
  15. }]
  16. },
  17. "namespace_prefix": {
  18. "": [{
  19. "node_prefix": {
  20. "": [{
  21. "policy": "read"
  22. }]
  23. },
  24. "service_prefix": {
  25. "": [{
  26. "policy": "read"
  27. }]
  28. }
  29. }]
  30. }
  31. }

Register the policy with Consul

After defining the policy, you can register the policy with Consul using the command line or API endpoint.

The following commands create the ACL policy and token.

Run the consul acl policy create command and specify the policy rules to create a policy. The following example registers a policy defined in mgw-register.hcl:

You can specify an admin partition when creating policies in Consul Enterprise. The policy is only valid in the specified admin partition. You must create the policy in the partition where the mesh gateway registers. The following example creates the policy in the partition ptn1.

  1. $ consul acl policy create -partition "ptn1" \
  2. -name mgw-register -rules @mgw-register.hcl \
  3. -description "Mesh gateway policy"

Refer to Consul ACL Policy Create for details about the consul acl policy 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 mgw-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": "mgw-register",
  5. "Description": "Mesh gateway policy",
  6. "Partition": "ptn1",
  7. "Rules": "mesh = \"write\"\npeering = \"read\"\nnamespace \"default\" {\n service \"mesh-gateway\" {\n policy = \"write\"\n }\n agent_prefix \"\" {\n policy = \"read\"\n }\n}\nnamespace_prefix \"\" {\n node_prefix \"\" {\n \tpolicy = \"read\"\n }\n service_prefix \"\" {\n policy = \"read\"\n }\n}\n"
  8. }'

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

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.

You can specify an admin partition when creating tokens in Consul Enterprise. The token is only valid in the specified admin partition. The token must be created in the partition where the mesh gateway registers. The following example creates the token in the partition ptn1.

  1. $ consul acl token create -partition "ptn1" \
  2. -description "Mesh gateway token" \
  3. -policy-name "mgw-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 when creating tokens in Consul Enterprise. The token is only valid in the specified admin partition. The token must be created in the partition where the mesh gateway registers. The following example creates the token in the 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": "mgw-register"
  7. }
  8. ],
  9. "Partition": "ptn1"
  10. }'