Generate mTLS certificates for Nomad using Vault
You can use Vault's PKI Secrets Engine to generate and renew dynamic X.509 certificates for your Nomad cluster nodes and Vault Agent to automatically create the appropriate certificate and key files on your nodes. With this method, each node has a unique certificate with a relatively short time-to-live (TTL). This feature, along with automatic certificate rotation, allows you to safely and securely scale your cluster while using mutual TLS (mTLS).
In this tutorial, you will secure your existing Nomad cluster with mTLS using Vault's PKI secrets engine to create both a root and intermediate CA. You will also use Vault Agent to fetch, renew, and periodically rotate your mTLS certificates on your Nomad nodes.
Prerequisites
Running this tutorial requires the following:
- A Nomad environment with Vault installed. You can use this repository to provision a sandbox environment. This tutorial will assume a cluster with one server node and three client nodes.
Note
This tutorial is for demonstration purposes and is using a single Nomad server with a Vault server configured alongside it. In a production cluster, we recommend 3 or 5 Nomad server nodes along with a separate Vault cluster. Refer to the Vault Reference Architecture page to learn how to securely deploy a Vault cluster.
Prepare Vault
If you already have a Vault environment, skip ahead to Log in to Vault.
Initialize Vault server
Run the following command to initialize the Vault server and receive an unseal key and initial root token. If you are running the environment provided in this guide, the Vault server is co-located with the Nomad server. Be sure to note the unseal key and initial root token as you will need these two pieces of information.
The vault operator init
command creates a single Vault unseal key for
convenience. For a production environment, we recommended that you create at
least five unseal key shares and securely distribute them to independent
operators. The command defaults to five key shares and a
key threshold of three. If you have provisioned more than one server, the others will
become standby nodes and remain unsealed.
Unseal Vault
Run the command and provide your unseal key.
The output looks similar to the following.
Log in to Vault
Use the login command to authenticate with Vault using the initial root token from earlier.
Successful output looks similar to the following.
Prepare the PKI environment
This tutorial uses a common and recommended pattern which is to have one mount act as the root CA and the other as the intermediate CA. The root CA signs only the intermediate CA CSRs from other PKI secrets engines. For a higher level of security, you can store your CA outside of Vault and use the PKI engine only as an intermediate CA.
Generate the root CA
Enable the root 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.
Generate the root certificate and save the certificate as CA_cert.crt
.
Note
If you already have an existing root CA certificate in your organization, you can use that in place of generating a new one.
Generate the intermediate CA and CSR
Enable the intermediate PKI secrets engine at the pki_int
path.
Tune the PKI secrets engine at the pki_int
path to issue certificates with a
maximum time-to-live (TTL) of 43800 hours.
Generate a CSR from your intermediate CA and save it as pki_intermediate.csr
.
Sign and deploy the intermediate CA certificate
Sign the intermediate CA CSR with the root certificate and save it as intermediate.cert.pem
.
Import the signed intermediate certificate into Vault.
Set up the AppRole auth method
Vault offers many authentication methods including AWS, GCP, Azure, GitHub, LDAP, and Okta. In a production environment running in a cloud provider, we recommend using the machine's identity and the cloud provider's auth method.
The AppRole authentication method allows applications or services to authenticate with Vault-defined roles. For on-premises environments, we recommend AppRoles. In this tutorial, Nomad is using the AppRole auth method to retrieve certificates from Vault with the PKI engine.
Enable the AppRole auth method.
Create a policy to access the role endpoint
Create a policy file named tls-policy.hcl
, add the following contents to it, and save the file. This adds only the update
capability for the pki_int/issue/nomad-cluster
path. Refer to the Vault policies page for additional information.
Write the policy into Vault.
Create an approle
Create an approle named nomad-cluster
. Consider increasing or decreasing the TTL and SecretID values to match the your environment.
The output looks similar to the following.
Next, get the role ID and save it to a file.
Then, get the SecretID and save it to a file. The SecretID is similar to a password in that you should keep it secret and confidential. Refer to the best practices page for additional information about working with the SecretID.
Create and populate the templates directory
The templates directory contains the files used by Vault Agent to render the certificates and keys on the nodes in your cluster.
Create a directory named templates
in /opt/nomad
.
Create a template file for each configuration below, add the contents to it, and save it with the filename given.
We recommend creating only the server and CLI files for server nodes and client and CLI files for client nodes.
Note
The common_name
value in the files below must match the Nomad node names in your cluster which include the node name and region. If you have a different configuration for the nodes in your cluster, be sure to update the values.
Create the certificates output directory
Create the rendered-cert-files
directory to contain the generated certificate files.
Start Vault Agent
Create a directory for the configuration named vault-agent
in /opt/vault
.
Create the Vault Agent configuration file, name it vault_agent.conf
, add the following contents to it, and save the file in the /opt/vault/vault-agent
directory.
Modify this configuration file to match the templates created for each specific node from the previous step. Include or remove the server and client template blocks as necessary.
Then, start Vault Agent on each node.
The output shows the agent rendering the local files with the certificate data.
Configure Nomad to use TLS
Add the following tls stanza to the agent configuration file of each Nomad server and client node in the cluster.
The rpc_upgrade_mode
attribute ensures that the Nomad servers accept both TLS and non-TLS connections during the upgrade.
Reload the Nomad configuration on each node.
After the reload, remove the rpc_upgrade_mode = true
line from each server agent configuration file and save the file. This instructs the servers to only accept TLS connections.
Then, reload the Nomad configuration on each server node again. Refer to the RPC Upgrade Mode page for additional information.
Configure the Nomad CLI
The Nomad CLI defaults to HTTP instead of HTTPS. Configure the Nomad CLI to connect using TLS by providing paths to the certificate files through environment variables.
Run the status command to confirm connectivity from the CLI.
Encrypt server gossip
All communications over RPC and HTTP in your Nomad cluster are now secure with mTLS.
Nomad servers also communicate with the Serf gossip protocol, which does not use TLS. To learn how to configure gossip encryption, follow the Enable Gossip Encryption for Nomad tutorial.
Next steps
In this tutorial, you configured the Vault PKI engine, installed and used Vault Agent to automatically create certificate files on cluster nodes, and enabled mTLS with those certificates to encrypt traffic between the nodes.
Continue your learning with these resources: