Vault Agent Caching
Nearly all requests to Vault must be accompanied by a valid token which means that Vault clients must first authenticate with Vault and acquire a token. Secure Introduction of Vault Clients tutorial talked about the approaches to solve this secret zero problem.
Vault Agent with AWS and Vault Agent with Kubernetes tutorials walked through the Vault Agent's Auto-Auth.
Note
This tutorial focuses on the Caching feature of Vault Agent.
Challenge and solution
Depending on the location of your Vault clients and its secret access frequency, you may face some scaling or latency challenge. Even with Vault Performance Replication enabled, the pressure on the storage backend increases as the number of token or lease generation requests increase. Vault 1.0 introduced batch tokens as a solution to relieve some pressure on the storage backend. By design, batch tokens do not support the same level of flexibility and features as service tokens. Therefore, if you need a periodic token for example, you would need service tokens.
To increase the availability of tokens and secrets to the clients, Vault Agent introduced the Caching function.
Vault Agent Caching can cache the tokens and leased secrets proxied through the agent which includes the auto-auth token. This allows for easier access to Vault secrets for edge applications, reduces the I/O burden for basic secrets access for Vault clusters, and allows for secure local access to leased secrets for the life of a valid token.
Prerequisites
To complete this section of the tutorial, you will need:
- AWS account and associated credentials that allow for the creation of resources
- Terraform v0.15.2 or later installed
Provision the cloud resources
Clone or download the demo assets from the hashicorp/vault-guides GitHub repository to perform the steps described in this tutorial.
Clone the repository:
Or download the repository:
This repository contains supporting content for all of the Vault learn tutorials. The content specific to this tutorial can be found within a sub-directory.
Set your working directory to where the
/identity/vault-agent-caching/terraform-aws
was downloaded.The directory contains the following files.
Note
The example Terraform in this repository is created for the demo purpose.
Set an
AWS_ACCESS_KEY_ID
environment variable to hold your AWS access key ID.Set an
AWS_SECRET_ACCESS_KEY
environment variable to hold your AWS secret access key.Create a file named
terraform.tfvars
and specify thekey_name
. (You can copy theterraform.tfvars.example
file and rename it asterraform.tfvars
.)Example:
terraform.tfvarsTip
If you don't have an EC2 key pair, follow the AWS documentation to create one.
Perform a
terraform init
to pull down the necessary provider resources.Run
terraform apply
to provision AWS resources.When prompted, enter
yes
to proceed.The Terraform output will display the public IP address to SSH into your Vault server and client instances.
Configure AWS IAM auth method
Note
This step should be performed on the Vault Server instance.
In this step, you will configure Vault to allow AWS IAM authentication from specific IAM roles.
SSH into the Vault Server instance.
When you are prompted, enter "yes" to continue.
NOTE: If you received
Permissions 0664 for '<key_name>.pem' are too open
error, be sure to set the file permission appropriately.Run the
vault operator init
command to initialize the Vault server.Note
The Vault server is configured to auto-unseal with AWS Key Management Service (KMS); therefore, once the server is initialized, it gets automatically unsealed. To learn how to auto-unseal Vault using AWS KMS, refer to the Auto-unseal using AWS KMS tutorial.
Copy the Initial Root Token value.
Log into Vault using the generated initial root token.
Examine and then execute the
/home/ubuntu/aws_auth.sh
script.Execute the script.
This script enables key/value v1 secrets engine at
kv
, createmyapp
policy, enablesaws
auth method and create a role namedapp-role
. Also, userpass is enabled and created a user (student
) withmyapp
policy attached.Check the
myapp
policy.Examine and then execute the
/home/ubuntu/aws_secrets.sh
script.Execute the script.
This script enables
aws
secrets engine, configures it and set the generated secret's lease to 1 hour and lease max to 24 hours. It also configuresreadonly
role which is mapped to is mapped toarn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
IAM policy.
Start Vault Agent
Note
This step should be performed on the Vault Client instance.
Now, SSH into the Vault Client instance.
When you are prompted, enter "yes" to continue.
Explore the Vault Agent configuration file,
/home/ubuntu/vault-agent.hcl
Tip
Also, read the Server Side Request Forgery (SSRF) Protection section.
Note
Notice the
api_proxy
block sets theuse_auto_auth_token
parameter to true. Since the auto-auth sink is set to/home/ubuntu/vault-token-via-agent
, in absence of client token on requests sent to the API proxy, Vault Agent will use the auto-auth token read fromvault-token-via-agent
. Thevault
block defines the Vault server location (e.g.http://10.0.101.209:8200
).Execute the following command to start the Vault Agent with log level set to
debug
.Open a second SSH terminal into the client machine.
Verify the client token stored in
/home/ubuntu/vault-token-via-agent
.Check the details about the token (e.g. attached policies, TTL, etc.).
Set
VAULT_AGENT_ADDR
environment variable.
Request dynamic secrets and tokens via agent
Execute the following command to request an AWS credential.
See the logs in the terminal where Vault Agent is running.
The log indicates that auto-auth token was used to connect Vault. The received request was forwarded to the Vault server, and the returned response was cached (find an entry
storing response into the cache
in the log).Benefit of Vault Agent
Vault Agent will manage the lifecycle of cached tokens and leases automatically so that the clients do not need to implement a logic to renew the tokens and leases.
Re-run the
vault
command to study the agent behavior.The agent log indicates:
Tip
This returns the same AWS credentials. Notice that the
request_id
andlease_id
match to the ones you received the first time.Log in as a user,
student
via agent whose password is "pAssw0rd
".Store the acquired token in VAULT_TOKEN environment variable.
Examine the agent log in the other terminal.
The log should indicate that the acquired token for user
student
is successfully cached ([DEBUG] cache.leasecache: storing response into the cache
). This is because thelogin
request was sent through the agent.Create a new token with TTL set to 12 minutes.
Examine the agent log in the other terminal.
This time, instead of using the auto-auth token, the command is using the
student
token generated upon successful login. The resulting token was successfully cached.NOTE: The agent will automatically renew the token to keep it valid:
Re-send the token create request.
The same token should be returned, and in the agent log, find the following:
Cache evictions
The eviction of cache will occur when the agent fails to renew leases or tokens. This can happen when the cached lease/token hits it's maximum TTL or if the renewal results in an error. Agent also does some best-effort cache evictions by observing specific request types and response codes.
While agent observes requests and evicts cached entries automatically, you can
trigger a cache eviction by invoking the /agent/v1/cache-clear
endpoint.
If you need to manually evict a stale lease, invoke the
/agent/v1/cache-clear
endpoint with lease ID for which you wish to evict from the cache.Example:
The agent log should show the following:
Let's see what happens when you revoke a token.
Or, revoke a token via API.
Examine the agent log:
Note
In the agent log, notice the message, "triggered caching eviction from revocation request". When a token revocation request is made via the agent, the agent evicts the cached entries associated with the revoked token.
Similarly, if you revoked a staled AWS lease, the agent will automatically evict the cache.
If a situation requires you to clear all cached tokens and leases (e.g. reset after a number of testing), set the
type
toall
.
Clean up
On the server SSH terminal, execute the following command to revoke all leases.
Execute the following commands to destroy cloud resources.
Delete state files.
Server side request forgery (SSRF) protection
Note
Vault 1.3 introduced Server Side Request Forgery (SSRF) protection for Vault Agent. To leverage this feature, download Vault 1.3 or later.
Add require_request_header = true
in the Agent's listener configuration
stanza to enable the SSRF protection.
Example:
Notice the listener
block.
When the require_request_header
is set to true
, the Agent listener will
reject all requests that do NOT have the proper X-Vault-Request: true
header.
For example, the command to request AWS credentials you saw in the request dynamic secrets and tokens via agent step must look like:
With absence of the X-Vault-Request
header, the Agent will throw missing 'X-Vault-Request' header
error, and the request will not be propagated to the
Vault server.
Help and reference
- Blog post: Why Use the Vault Agent for Secrets Management?
- Video: Streamline Secrets Management with Vault Agent and Vault 0.11
- Secure Introduction of Vault Clients
- Vault Agent Auto-Auth
- Vault Agent Caching
- AWS Auth Method