Create a terminating gateway token

This topic describes how to create an ACL token that enables a terminating gateway to register with Consul.

Introduction

Terminating gateways enable connectivity within your organizational network from services in the Consul service mesh to services and destinations outside the mesh.

To learn how to configure terminating gateways, refer to the Terminating Gateways documentation and the Understand Terminating Gateways tutorial.

Requirements

Core ACL functionality is available in all versions of Consul.

The terminating gateway token must be linked to policies that grant the appropriate set of permissions in order to be discoverable and to forward traffic out of the mesh. The following permissions are required:

  • service:write to allow the terminating gateway to register into the catalog
  • service:write for each service that it forwards traffic for
  • node:read for the nodes of each service that it forwards traffic for
  • 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 terminating 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 terminating-gateway and to operate as a terminating gateway. For this example, the terminating gateway forwards traffic for two services named external-service-1 and external-service-2. The policy examples include service:write permissions for these services. If you have additional services, your policy must include service:write permissions for the additional services to be included in the policy rules.

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

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 tgw-register.hcl:

  1. $ consul acl policy create \
  2. -name "tgw-register" -rules @tgw-register.hcl \
  3. -description "Terminating 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 tgw-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": "tgw-register",
  5. "Description": "Terminating gateway policy",
  6. "Rules": "service \"terminating-gateway\" {\n policy = \"write\"\n}\nservice \"external-service-1\" {\n policy = \"write\"\n}\nservice \"external-service-2\" {\n policy = \"write\"\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 tgw-register.

  1. $ consul acl token create \
  2. -description "Terminating gateway token" \
  3. -policy-name "tgw-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": "tgw-register"
  7. }
  8. ]
  9. }'

Consul Enterprise

To create a token for the terminating 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 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 grants the appropriate permissions for a terminating gateway to register as a service named terminating-gateway in namespace ns1 in partition ptn1.

For this example, the terminating gateway forwards traffic for the following two services:

The policy examples include service:write permissions for these services. If you have additional services, your policy must include service:write permissions for the additional services to be included in the policy rules.

The policy contains permissions for resources in multiple namespaces. You must create ACL policies that grant permissions for multiple namespaces in the default namespace.

  1. partition "ptn1" {
  2. namespace "ns1" {
  3. service "terminating-gateway" {
  4. policy = "write"
  5. }
  6. node_prefix "" {
  7. policy = "read"
  8. }
  9. service "external-service-2" {
  10. policy = "write"
  11. }
  12. }
  13. namespace "default" {
  14. service "external-service-1" {
  15. policy = "write"
  16. }
  17. node_prefix "" {
  18. policy = "read"
  19. }
  20. agent_prefix "" {
  21. policy = "read"
  22. }
  23. }
  24. }
  1. {
  2. "partition": {
  3. "ptn1": [{
  4. "namespace": {
  5. "default": [{
  6. "agent_prefix": {
  7. "": [{
  8. "policy": "read"
  9. }]
  10. },
  11. "node_prefix": {
  12. "": [{
  13. "policy": "read"
  14. }]
  15. },
  16. "service": {
  17. "external-service-1": [{
  18. "policy": "write"
  19. }]
  20. }
  21. }],
  22. "ns1": [{
  23. "node_prefix": {
  24. "": [{
  25. "policy": "read"
  26. }]
  27. },
  28. "service": {
  29. "external-service-2": [{
  30. "policy": "write"
  31. }],
  32. "terminating-gateway": [{
  33. "policy": "write"
  34. }]
  35. }
  36. }]
  37. }
  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.

You can specify an admin partition and namespace when creating policies in Consul Enterprise. The policy is only valid in the specified admin partition and namespace. You must create the policy in the same partition where the terminating gateway is registered. If the terminating gateway requires permissions for multiple namespaces, then the policy must be created in the default namespace. The following example creates the policy in the partition ptn1 and default namespace because the example policy contains permissions for multiple namespaces.

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

  1. $ consul acl policy create -partition "ptn1" -namespace "default" \
  2. -name "tgw-register" -rules @tgw-register.hcl \
  3. -description "Terminating 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 tgw-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": "tgw-register",
  5. "Description": "Terminating gateway policy",
  6. "Partition": "ptn1",
  7. "Namespace": "default",
  8. "Rules": "partition \"ptn1\" {\n namespace \"ns1\" {\n service \"terminating-gateway\" {\n policy = \"write\"\n }\n node_prefix \"\" {\n policy = \"read\"\n }\n service \"external-service-2\" {\n policy = \"write\"\n }\n }\n namespace \"default\" {\n service \"external-service-1\" {\n policy = \"write\"\n }\n node_prefix \"\" {\n policy = \"read\"\n }\n agent_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 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.

You can specify an admin partition when creating tokens in Consul Enterprise. The token is only valid in the specified admin partition. You must create the token in the partition where the terminating gateway is registered. If the terminating gateway requires permissions for multiple namespaces, then the token must be created in the default namespace. The following example creates the token in the default namespace in the ptn1 partition because the example policy contains permissions for multiple namespaces.

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.

  1. $ consul acl token create -partition "ptn1" -namespace "default" \
  2. -description "Terminating gateway token" \
  3. -policy-name "tgw-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": "tgw-register"
  7. }
  8. ],
  9. "Partition": "ptn1",
  10. "Namespace": "default"
  11. }'