Write a policy using audit logs
Every Vault operation performed through the command-line interface (CLI), API, or web UI require that the authenticated client is granted access; access defined through policies. Everything in Vault is stored at different paths, like a filesystem, and every action in Vault has a corresponding path and capability. Policies provide a declarative way to grant or forbid access to paths and the capabilities at each path.
In this tutorial, you will learn Vault's policy language and how to query an audit log to define policies.
Prerequisites
Start Vault
Start a Vault dev server with root
as the root token.
The Vault dev server defaults to running at 127.0.0.1:8200
. The server is
initialized and unsealed.
Insecure operation
Do not run a Vault dev server in production. This approach starts a Vault server with an in-memory database and runs in an insecure way.
In another terminal, export an environment variable for the vault
CLI to
address the Vault server.
Export an environment variable for the vault
CLI to authenticate with the
Vault server.
The Vault server is ready.
Enable audit logs
Audit devices keep a detailed log of all requests and response to Vault. The file audit device appends these events to a file.
Audit device filters
Starting in Vault 1.16.0, you can enable audit devices with a filter
option that Vault uses to evaluate audit entries to determine whether it writes them to the log. You should determine if your own audit devices are filtered and make necessary changes to expose the log fields which you need to monitor for your use case.
You can familiarize yourself with Vault filtering concepts and filtering audit entries and how to enable audit filters in the documentation.
Enable the file audit device at a file named vault-audit.log
in the current
directory.
The file audit device is enabled and immediately appends a test message to the audit log file.
Display the contents of the audit log file.
The output displays two entries similar to these examples.
The audit log appends JSON objects to the log file. The first object describes an audit test message. The second object describes the operation that enabled the audit log.
Enable transit secrets engine
The transit secrets engine handles cryptographic functions on data in-transit through keys that Vault manages.
Enable the transit secrets engine at the default path.
The transit secrets engine is enabled and the operation that performed it was written to the audit log.
View only the last entry in the audit log file.
The output displays an entry similar to this example.
The object describes the time, the authorized token, the request, and the response.
Display the request
of the last logged object.
The output displays an entry similar to this example.
The request describes the operation that was performed on the path.
Display the request's path
and the request's operation
.
The output displays that this operation performed an update
on the path
sys/mounts/transit
.
To learn more about querying the Audit Log, read the Querying Audit Device Logs tutorial.
ACL Policy workflow
You can use the information you gained from the audit device log to define a policy that grants the capability to update
at the path sys/mounts/transit
. That policy is expressed in the following example.
You can define your policy as files and place them under version control as part of a Vault configuration codification strategy.
Write the above policy example to the file transit-updates.hcl
.
Write the policy into your Vault server from the file, and name it transit-updates
.
Note
You can also manage and create ACL policies from files in the Vault web UI. Refer to the Vault Policies tutorial for detailed examples,
As part of the policy workflow, you can update existing ACL policies by changing their file content, and simply writing the policy again with the same name. The existing policy will be overwritten, but existing tokens with the policy attached will use the previous policy until a new token is requested.
Additional ACL policy challenges
Use the techniques you've learned here to complete these optional challenges.
Create an encryption key
Create a encryption key named user-data
.
Display the request path
and the request operation
of the last logged
object.
Challenge
Define a policy that grants the capability to update
at path
transit/keys/user-data
.
Encrypt plaintext with the key
The user-data
key is capable of encrypting base64 encoded plaintext and
returns the ciphertext.
Encrypt user sensitive data with the user-data
key.
Display the request path
and the request operation
of the last logged
object.
Challenge
Define a policy that grants the capability to update
at path
transit/encrypt/user-data
.
Decrypt ciphertext with the key
The same key is used to decrypt the ciphertext and return the base64 encoded plaintext.
Create a variable named USER_SECRET_DATA
that stores the cipher text
generated.
Decrypt the user sensitive data with the user-data
key.
Display the request path
and the request operation
of the last logged
object.
Challenge
Define a policy that grants the capability to update
at path
transit/decrypt/user-data
.
Enable a key/value secrets engine
For the next challenges, enable an instance of the Key/Value Secrets Engine version 2 at the path kv-v2
.
Display the request path
and the request operation
of the last logged
object.
Write an example secret in the newly enabled secrets engine.
Display the request path
and the request operation
of the last logged
object.
Challenge
Write a policy to Vault that grants the capability to list
and read
at the paths kv-v2/data/my-secret
and kv-v2/metadata/my-secret
.
Note
The policy uses the +
character for path matching so that it can match the sub-paths kv-v2/data
and kv-v2/metadata
required by the Key/Value Secrets Engine version 2. You can learn more in the KV Secrets Engine - Version 2 ACL rules documentation.
Create a token with the policy attached and set its value to the environment variable MY_SECRET_TOKEN
.
Read the secret using the new token with attached my-secret-read-list policy.
Clean up
Here are the steps you need to completely clean up the scenario content from your local environment.
Stop the Vault server process.
Remove the example policy file.
Remove the audit device log file.
Unset environment variables.
Next steps
In this tutorial, you performed operations and then queried the audit device log to discover the path and action involved in the operations. Learn more about querying the Audit Log, read the Querying Audit Device Logs tutorial.
To define policies requires understanding the paths and capabilities of each authentication method and secrets engine. Learn additional approaches in the Write a Policy from API documentation tutorial.