Automate Consul agent security with auto config
Consul auto_config
is a highly scalable method used to distribute secure
properties such as Access Control List (ACL) tokens, TLS certificates, gossip encryption keys,
and other configuration settings to all Consul agents in a datacenter. This can
provide a great boost in usability for securing agents against eavesdropping,
tampering, and spoofing without having to manually distribute or rotate secrets
across the datacenter. Additionally, auto_config
reduces the technical
overhead associated with securing Consul agents.
In this tutorial, you will:
- Create a secure local Consul datacenter using Docker Compose
- Use the created environment to inspect the server and client
auto_config
settings - Generate a JSON web token (JWT) with either Vault or secint
- Securely join a client to the Consul datacenter with the
auto_config
method
JSON web tokens (JWTs) are an open, industry standard method for representing
claims securely between two parties. With the auto_config
method, Consul
clients use JWTs to securely retrieve gossip encryption keys, TLS certificates,
and/or other security setting changes from Consul servers. Since tokens are
credentials, great care must be taken to prevent security issues. In general,
you should not keep tokens longer than required. To learn more about JSON web
tokens, check the JWT documentation page.
While this tutorial uses elements that are not suitable for production
environments including example certificates, example gossip encryption keys,
and global ACL token usage, it will teach you the core concepts for
deploying and interacting with a secure Consul datacenter using auto_config
.
Refer to the Consul Reference Architecture
for Consul best practices and the Docker Documentation
for Docker best practices.
Prerequisites
This tutorial builds on the concepts learned in the Consul getting started collection and the Deploy a secure local datacenter with Docker Compose tutorial.
Docker
You will need a local install of Docker running on your machine for this tutorial. You can find the instructions for installing Docker on your specific operating system here. Docker Engine 20.10.7 was used in this tutorial.
Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Visit the Docker Compose install guide for operating system specific installation instructions. Docker Compose format 3.7 was used in this tutorial.
Choose your learning path
You can utilize the auto_config
workflow with HashiCorp
Vault or a third-party production-grade secrets
management platform. In this tutorial we will demonstrate the Vault workflow,
and a generic workflow with a proof-of-concept tool, secint.
Clone GitHub repository
Clone the GitHub repository containing the configuration files and resources.
Change into the directory with the newly cloned repository. This directory contains the complete configuration files.
Check out the v0.3
tag of the repository.
Inspect configuration files
You will use the following resources to deploy five containers; three Consul servers, a Vault server, and a single Consul client.
Docker Compose YAML
The following Docker Compose configuration file instructs Docker to create one Vault server and four Consul containers using the respective configuration files. These files will configure various container settings and bootstrap the Consul datacenter with three secure Consul servers. Three servers in a datacenter is the recommended minimum for achieving a balance between availability and performance. These servers together run the Raft-driven consistent state store for updating catalog, session, prepared query, ACLs, and KV state.
Inside your working directory, inspect the file named docker-compose.yml
.
For more information on the Consul Docker image, check Consul's Docker Hub page.
Consul server configuration
On a Consul server agent with the authorization
subkey set to enabled
, the
server will begin to process client auto_config
requests.
Inside your working directory, inspect the three server#.json
files.
Each Consul server configuration file uses the following auto_config
options:
authorization
- Setting the sub keyenabled
totrue
enables the authorization service on this agent. This allows the server agent to processauto_config
RPC requests from clients.static
- This object contains all static authorizer configuration settings.oidc_discovery_url
- The URL used to validate JSON web tokens (JWTs). In this example, Vault's OIDC URL endpointhttp://vault-server:8200/v1/identity/oidc
is used.
bound_issuer
- The value for matching theiss
value in a JSON web token (JWT). The issueriss
claim in a JWT is meant to refer to the resource that issued the JWT. In this example, the Vault server assigns its OIDC URL endpointhttp://vault-server:8200/v1/identity/oidc
as the issuer for generated JWTs.
bound_audiences
- The value for matching theaud
field of the JSON web token (JWT). The audienceaud
claim in a JWT is meant to refer to the authorization servers that should accept the token. In this example, Vault assigns the generated JWT tokens with anaud
value ofconsul-cluster-dc1
.
claim_assertions
- List of assertions about the mapped claims required to authorize the incoming RPC request. In this example,"value.node_name == \"${node}\""
sets the value of thenode_name
variable to the hostname of the consul client making a request to theauto_config
authorization servers.
claim_mappings
- Mappings of claims (key) that will be copied to a metadata field (value). In this example,"/consul/hostname": "node_name"
checks the value of thenode_name
variable against the metadata value of/consul/hostname
.
Check the Consul configuration documentation to learn more.
Consul client configuration
On a Consul client agent with auto_config
enabled, the client agent will use
the value of a JSON web token (JWT) within intro_token_file
to communicate
with the configured server_addresses
to request secure configuration settings.
These settings are then merged into any existing configuration on the client
agent.
Inside your working directory, inspect the client.json
file.
Each Consul client configuration file uses the following auto_config
options:
enabled
- Setting this key totrue
enables theauto_config
client service on the agent. Enabling this option also turns on Consul service mesh because it is required forauto_config
to issue certificates to client agents using the Consul service mesh CA.intro_token_file
- This specifies the file that contains the JSON web token (JWT) to use for the initialauto_config
RPC to the Consul servers.server_addresses
- This specifies the addresses of servers in the local datacenter to use for the initial RPC. These addresses support Cloud Auto-Joining and can optionally include a port to use when making the outbound connection. If not port is provided the server RPC port will be used.
Check the Consul configuration documentation to learn more.
Create the Consul datacenter
From within your working directory, run the following Docker Compose command to create your secure local Consul datacenter.
Note
Your first run will take the longest as Docker will first pull the respective images from the Docker Hub repository. Additional runs will not require to download the image again and should only take a few seconds to complete.
View the Consul UI
Navigate to http://localhost:8500
on your browser to access the Consul UI. Login with the pre-configured ACL token e95b599e-166e-7d80-08ad-aee76e7ddf19
.
Click on the "Nodes" option in the top navigation bar to go to the nodes page.
Notice that the three consul-server#
nodes are present in the datacenter,
however, the consul-client
node is not present since it cannot join the secure
datacenter.
Retrieve Consul client logs
Check the container logs for consul-client
to see details about why the client
could not join the Consul datacenter.
This type of behavior occurs when a client's JSON web token (JWT) cannot be validated by the authorization server. In this example the token file is empty.
Note
Feel free to explore the jwt.io documentation for more information on the properties of a JSON web token (JWT).
Configure Vault to generate JWTs
Vault's built-in Identity Secrets Engine
can be used to generate the JSON Web Tokens (JWTs) required to validate Consul
client auto_join
requests.
Open an interactive shell to the Vault server container.
Login to Vault with the pre-configured Vault's root token, vault-plaintext-root-token
.
Named keys are used by a role to sign JSON web tokens (JWTs). The value for
allowed_client_ids
here will become the value of aud
when a JWT is generated.
The audience aud
claim in a JWT is meant to refer to the authorization servers
that should accept the token, in this case, consul-cluster-dc1
.
Create a named key.
JSON web tokens (JWTs) are generated against a role and signed against a named
key. The template='{"consul": {"hostname": "consul-client" } }'
will create
additional JWT metadata that will be used by the Consul authorization servers to
validate the request.
Create a role.
Policies provide a declarative way to grant or forbid access to certain paths
and operations in Vault. In this example, a policy file will be used that only
grants a user permission to read the token at the path identity/oidc/token/oidc-role-1
.
Create a policy using the included policy.json
file.
Note
Feel free to explore Vault's identity token documentation to learn more about identity token attributes, roles, and keys.
Generate a JWT with Vault
An authorized Vault user/application can request a token that encapsulates identity information for their associated entity. These tokens are signed JSON web tokens (JWTs) following the OIDC ID token structure. In this example, a user entity will be created to manually generate a JWT.
Enable Vault's username/password secrets engine.
Create a user example
with the oidc-policy
attached.
Login as the user example
with the password password
.
After login you should receive an output similar to the following:
Generate a signed JSON web token (JWT). Note that each time this operation is performed, a new JWT will be generated.
Copy the value of token
to your clipboard.
Exit the docker terminal to return to your system shell.
Note
Feel free to explore the contents of your JSON web token (JWT) using the decoder at jwt.io.
Configure your client with the JWT
Copy the token
value to the /vault/tokens/jwt
file in your working
directory, then save the changes.
Delete and recreate the consul-client
container so it will perform the
auto_config
request with the newly updated JSON web token (JWT).
Validate success
Navigate to http://localhost:8500
on your browser to
access the Consul UI. If not already logged in, login with the pre-configured
ACL token e95b599e-166e-7d80-08ad-aee76e7ddf19
.
Click on the "Nodes" option in the top navigation bar to go to the nodes page.
Notice that the consul-client
node is now present amongst the three
consul-server#
nodes. This indicates that consul-client
has successfully
joined the Consul datacenter using the auto_config
method.
All future gossip encryption key, TLS certificate, and/or security setting
changes will automatically be distributed to consul-client
since it used
auto_config
to join the Consul datacenter.
Clean up your environment
To clean up your environment, execute the following command.
Next steps
In this tutorial, you learned to deploy and configure a secure local
containerized Consul datacenter using Docker Compose. You learned how to use
auto_config
to send secure properties throughout your datacenter. You learned
the value auto_config
adds by automatically distributing all future gossip
encryption key, TLS certificate, and/or other security setting changes across
the datacenter. Finally, you learned how to clean up your environment.
You can continue learning how to deploy a Consul datacenter in production by completing the Deployment guide. The collection includes securing the datacenter with Access Control Lists, encryption, DNS configuration, and datacenter federation.
You can also extend your Consul skills by exploring the following tutorials:
- ACL bootstrapping guide
- ACL production guide
- Running Consul on Docker
- Running Consul on Kubernetes
- Service Discovery
- Service Mesh
For additional reference documentation on the official Docker images for Consul and Vault, refer to the following websites: