Generate mTLS Certificates for Consul with Vault
You can use Vault's PKI Secrets Engine to generate and renew dynamic X.509 certificates with consul-template to rotate your certificates. This method enables each agent in the Consul datacenter to have a unique certificate with a relatively short time-to-live (ttl) that is automatically rotated, which allows you to safely and securely scale your datacenter while using mutual TLS (mTLS).
In this tutorial, your will secure your Consul datacenter with mTLS using Vault's PKI secrets engine to create both a root and intermediate CA. You will also use consul-template to fetch, renew, and periodically rotate your mTLS certificates on your Consul nodes.
Prerequisites
- Consul: to complete this tutorial, you can use a local Consul dev agent or an existing Consul deployment.
- Vault: the tutorial assumes you already have a running Vault cluster in your network. You can use a local Vault dev agent or an existing Vault deployment.
- consul-template: to interact with your Consul agent you will need to install the
consul-template
binary on the node. To rotate TLS certificates you need the binary to be installed on every node in the datacenter. You can check Service Configuration with Consul Template to learn how to install and useconsul-template
.
The diagram below shows the minimal architecture needed to demonstrate the functionality.
Configure Vault's PKI secrets engine
In order to communicate with the Vault server, you will need to set the address and token.
Enable Vault's PKI secrets engine at the pki
path.
Tune the PKI secrets engine to issue certificates with a maximum time-to-live (TTL) of 87600 hours.
Note
This tutorial uses a common and recommended pattern which is to have one mount act as the root CA and to use this CA only to sign intermediate CA CSRs from other PKI secrets engines (which you will create in the next few steps). For tighter security, you can store your CA outside of Vault and use the PKI engine only as an intermediate CA.
Configure Vault as Consul's CA
Consul requires that all servers and clients have key pairs that are generated by a single Certificate Authority (CA).
In this lab you will use the PKI secrets engine to generate the necessary CA and certificates.
1. Generate the root CA
Generate the root certificate and save the certificate in CA_cert.crt
.
This generates a new self-signed CA certificate and private key. Vault will automatically revoke the generated root at the end of its lease period (TTL); the CA certificate will sign its own Certificate Revocation List (CRL).
Tuning configuration
You can adapt the TTL to comply with your internal policies on certificate lifecycle.
You can inspect the certificate created using openssl x509 -text -noout -in CA_cert.crt
Configure the CA and CRL URLs.
Example output:
2. Generate an intermediate CA
First, enable the PKI secrets engine at the pki_int
path.
Tune the pki_int
secrets engine to issue certificates with a maximum
time-to-live (TTL) of 43800 hours.
Tuning configuration
You can adapt the TTL to comply with your internal policies on certificate lifecycle.
Request an intermediate certificate signing request (CSR) and save request as
pki_intermediate.csr
.
The command has no output.
3. Sign the CSR and import the certificate into Vault
The command has no output.
Once the CSR is signed, and the root CA returns a certificate, it can be imported back into Vault.
4. Create a Vault role
A role is a logical name that maps to a policy used to generate credentials.
Example output:
For this tutorial, you are using the following options for the role:
allowed_domains
: Specifies the domains of the role. The command usesdc1.consul
as the domain, which is the default configuration you are going to use for Consul.allow_subdomains
: Specifies if clients can request certificates with CNs that are subdomains of the CNs allowed by the other role options (NOTE: This includes wildcard subdomains.)generate_lease
: Specifies if certificates issued/signed against this role will have Vault leases attached to them. Certificates can be added to the CRL by Vault revoke<lease_id>
when certificates are associated with leases.
This completes the Vault configuration as a CA. Move to next step to generate certificates.
Generate a server certificate
You can test the pki
engine is configured correctly by generating your first
certificate.
Certificate TTL tuning
The TTL for the certificate is being set to 24 hours in this tutorial, meaning that this certificate will be valid only for 24 hours before expiring. You can try using a shorter TTL on a test environment to ensure certificates are revoked properly after TTL is expired.
Example output:
Configure Consul
Configure Consul TLS using the following configuration:
To configure TLS encryption for Consul servers, three files are required:
ca_file
- CA (or intermediate) certificate to verify the identity of the other nodes.cert_file
- Consul agent public certificatekey_file
- Consul agent private key
For the first Consul startup, you will use the certificate generated earlier.
Use the following commands to extract the two certificates and private key from
the certs.txt
and place them into the right file and location.
Configure consul-template
When you created the certificate you used as a parameter ttl="24h"
meaning
that this certificates will be valid only for 24 hours before expiring.
Deciding the right trade-off for certificate lifespan is always a compromise between security and agility. A possible third way that does not require you to lower your security is to use consul-template to automate certificate renewal for Consul when the TTL is expired.
Create template files
You can instruct consul-template to generate and retrieve those files from Vault using the following templates:
The template will use the pki_int/issue/consul-dc1
endpoint that Vault exposes
to generate new certificates. It also mentions the common name and alternate
names for the certificate.
Once you have created the templates for consul-template,
you can copy the files into /opt/consul/templates
.
Create consul-template configuration
For this tutorial, you are going to create a configuration file,
consul_template.hcl
, that will instruct consul-template to retrieve the files
needed for the agent, client and server, to configure TLS encryption.
The configuration file for the server contains the information to retrieve the CA certificate as well as the certificate/key pair for the server agent.
The following parameters are needed in order to allow consul-template
to
communicate with Vault:
address
: the address of your Vault server. In this tutorial, Vault runs on the same node as Consul so you can usehttp://localhost:8200
.token
: a valid Vault ACL token with appropriate permissions. You will use Vault root token for this tutorial.
Production check
Earlier in the lab you used a root token to log in to Vault. You will use that token in the next steps to generate the TLS certs. This is not recommended for production use; the recommended security approach is to create a new token based on a specific policy with limited privileges.
Start consul-template
After configuration is completed, you can start consul-template
.
You must provide the file with the -config
parameter.
Verify certificate rotation
The certificate you created manually for the Consul server had a TTL of 24 hours.
This means that after the certificate expires Vault will renew it and consul-template will update the files on your agent it reload Consul configuration automatically to make it pick up the new files.
You can verify the rotation by checking that consul-template keeps listing, every 24 hours, a timestamp and the log line:
You can also use openssl
to verify the certificate content:
and verify that the Not Before
and Not After
values are being updated to
reflect the new certificate.
Next steps
In this tutorial you learned how to configure Consul TLS encryption using Vault's PKI secrets engine to generate short lived certificates and using consul-template to automatically rotate them at the end of their lease time.
To learn how to configure gossip encryption for Consul using Vault as a secret storage management you can follow Secure Consul Gossip Communication with Vault tutorial.
To learn how to create Consul tokens using Vault you can follow Generate Consul Tokens with HashiCorp Vault tutorial.