HCP Vault Dedicated with AWS EKS and JWT auth method
HashiCorp Cloud Platform (HCP) is a fully managed platform offering HashiCorp Products as a Service (HPaaS) to automate infrastructure on any cloud.
In this tutorial, you will learn the process required to authenticate an AWS EKS cluster to HCP Vault Dedicated with the JWT auth method.
Prerequisites
The following prerequisites are required:
- An HCP HashiCorp Virtual Network (HVN).
- A public Vault Dedicated deployment.
- AWS CLI installed.
- kubectl installed.
- helm installed.
- jq installed.
- git installed.
- An EKS Cluster deployed in the VPC associated with your HVN.
Tip
Ensure that you have authenticated with the AWS CLI, and that the CLI is targeting the region where you created your EKS cluster. Review the AWS documentation for instructions on how to configure the AWS CLI.
Complete the steps detailed in the manual deployment tutorial to enable communication between your Vault Dedicated cluster servers and the Vault Agent running in your EKS cluster.
Your EKS cluster security group must also allow traffic from the Vault Dedicated CIDR range. If your EKS cluster endpoint uses port 443, create a security group rule to allow ingress traffic from Vault Dedicated to the primary EKS cluster security group.
The HashiCorp Virtual Network (HVN) section of the HCP UI can assist you in composing the correct eksctl
commands to do this when you establish the peering connection between your HVN and VPC.
You will configure Vault from your development host. As a result, the Vault Dedicated cluster needs to be publicly available. You should instead configure Vault Dedicated over a bastion host using the Vault private interface instead when working in production.
Scenario environments
The scenario consists of 3 distinct environments:
Your local host: This is where you will use the terminal along with
eksctl
,vault
, andkubectl
CLI commands to perform the tasks which make up the scenario.Your HCP account: This is where you have deployed your Vault Dedicated cluster. You can use Terraform to deploy the Vault Dedicated cluster, or do so manually with the HCP web UI.
Your AWS account: This is where you have deployed your EKS cluster and its VPC that is peered to your HVN with appropriate subnet, route table, and security group configuration.
Prepare local scenario environment
On your local host, prepare the scenario environment.
For ease of clean up and simplicity, create a temporary directory that contains all required configurations for the scenario, and assign that directory name value to the environment variable HC_LEARN_LAB
.
Now change into this directory; you will execute all scenario commands on the local host from this directory or one of its descendants.
Kubernetes stores cluster connection information in a special file called kubeconfig
. You can retrieve the Kubernetes configuration settings for your EKS cluster, and merge them into your local kubeconfig
file.
Use the AWS CLI to retrieve the
kubeconfig
.You can use the HCP Portal to retrieve the client configuration information you need to connect the Vault Agents in your EKS cluster to Vault Dedicated. Navigate to the Vault resource page in the HCP portal, and then select the Vault cluster.
Click "Generate Token". Copy the administrator token and set it in your terminal to an environment variable named
VAULT_TOKEN
.Click the clipboard next to "Public". This will copy the public Vault address to your clipboard. Set it as the
VAULT_ADDR
environment variable.Click the clipboard next to "Private". This will copy the private Vault address to your clipboard. Set the Vault private address as the
VAULT_PRIVATE_ADDR
environment variable. You will use this later in the tutorial to enable the EKS cluster to access Vault Dedicated over the HVN peering connection.Since Vault Dedicated uses the Enterprise Namespace feature with a default namespace named
admin
, you need to set theVAULT_NAMESPACE
environment variable value toadmin
.
Install Vault Agents on EKS
Use the official vault-helm chart to install Vault Agents in your EKS cluster.
Retrieve the Helm chart from the HashiCorp Helm repository.
Example output:
Create a
values.yaml
file that sets the external servers to Vault Dedicated over the private address. This will deploy a Vault Agent injector into the EKS cluster.Validate that the values file is correctly populated with your Vault Dedicated private address value (which will differ from the example shown).
Install the HashiCorp Vault Helm chart.
Once the
helm install
command completes, verify the Vault Agent injector pod deploys by issuingkubectl get pods
.Example output:
Kubernetes configuration
Create a namespace, learn-vault
.
Create a cluster role binding.
Create a service account named product
in the learn-vault
namespace.
Configure JWT auth method on HCP Vault Dedicated
Your services need a service account token to authenticate to Vault, which they can obtain from the Vault JWT/OIDC Auth Method.
Enable the JWT/OIDC auth method in Vault Dedicated using your terminal.
Output:
Fetch the service account signing public key from your cluster's JWKS URI.
Start a proxy.
Assign its value to the environment variable
ISSUER
.Stop the proxy by killing its process.
Create the JWT auth method configuration, specifying the
ISSUER
environment variable as value of the OIDC Discovery URL parameter.Successful output example:
Deploy an example workload
Now that the clients have been deployed, it is time to deploy an application workload. This tutorial will use the HashiCups demo application.
Issue the following command to clone the repository to the development host.
Change into the project directory.
Deploy a database to Kubernetes
Note
This tutorial deploys a database into Kubernetes and exposes it to HCP Vault
Dedicated using a public LoadBalancer
service type. In a production
configuration, you should deploy this as a private load balancer restricting
access to only Vault Dedicated.
Deploy a PostgreSQL database. This contains data for various coffees related to a demo application, all hosted in the
products
database.Output:
Verify you've deployed the PostgreSQL database in your Kubernetes cluster.
Example output:
Add the database role to Vault
The product
API needs to read the database username and password from
Vault. Create the role for the product
service account to generate
database credentials.
Enable the database secrets engine.
Output:
Set the
POSTGRES_IP
environment variable to the load balancer DNS hostname.Create the database configuration that allows Vault Dedicated to configure Postgres.
Create a database role for
product
that allows Vault to issue database passwords.Output:
Request a new set of PostgreSQL database credentials for the
product
role.Example output:
Configure Vault policy for database credentials
Create a new product
ACL policy that allows the product
service to read the database credentials specific to product
.
Output:
Create the vault-jwt-product
JWT auth method role to associate the product
. This allows the product
service account in Kubernetes to get a Vault token with the JWT auth method.
Note
The bound_audiences
value in this example is for EKS, but could differ for other platforms. If you are using a platform other than EKS, be sure to consult the documentation for that platform for the correct value.
Deploy the product API
Make sure that you include your Vault Dedicated namespace in the deployment that will access database credentials.
Output:
Deploy the product service.
Output:
The product deployment should initialize.
Example output:
Port forward the web service locally to port 9090.
Output:
Open another terminal and make a request to
localhost:9090/coffees
to check if the web service can pull coffee information from the database.Output:
When you are done, return to the terminal with the port-forward
command and
type Ctrl + C
to exit.
Clean up
Delete the
product
API.Delete the
product
role.Delete the
product
policy.Delete the
product
database role.Revoke all leases for database credentials.
Delete the PostgreSQL database.
Delete the database secrets engine configuration.
Delete the Helm installation for Vault Dedicated.
Disable the database secrets engine in Vault Dedicated.
Disable the JWT auth method in Vault Dedicated.
Delete your Vault Dedicated cluster and the associated HVN.
Remove the scenario directory and all of its contents.
Next steps
In this tutorial, you connected Vault clients on Amazon EKS to Vault Dedicated and retrieved PostgreSQL database credential dynamically. To learn more about the Vault features introduced in this tutorial, refer to the following tutorials.