Test and build the secrets engine
In this tutorial series, you learned how to create a new secrets engine backend, build a set of Vault roles, and create workflows to renew and revoke an API token using Vault.
Now, you will build the custom secrets engine and run it in Vault.
To do this, you will:
- Set up your development environment.
You will clone the HashiCups secrets engine repository. This contains many of the interfaces and objects you need to create a secrets engine. - Deploy the HashiCups API to Kubernetes.
You will deploy your own version of HashiCups to Kubernetes to use the secrets engine. - Build the plugin.
You will build a plugin binary for your secrets engine. - Register the plugin to Vault.
You will register the plugin for your secrets engine to Vault's plugin catalog. - Use the secrets engine.
You will use the secrets engine in Vault to revoke and renew JSON Web Tokens (JWTs) for the HashiCups API.
Prerequisites
- Golang 1.16+ installed and configured.
- Vault 1.8+ CLI installed locally.
Note
Complete the tutorial to define the credentials for the secrets engine.
If you want to run the tutorial, you need to deploy HashiCups to a live endpoint. Optional deployment requires additional infrastructure.
- A publicly accessible Kubernetes cluster.
- Kubernetes CLI installed locally.
Set up your development environment
Clone the learn-vault-plugin-secrets-hashicups repository.
Change into the repository directory.
Note
If you are stuck in this tutorial, refer to the
vault-plugin-secrets-hashicups/solution
directory.
Deploy the HashiCups API to Kubernetes
The Vault server needs a live API endpoint for HashiCups that does not exist locally. You can optionally deploy a live instance of HashiCups to your own Kubernetes cluster.
In your terminal, set your Kubernetes configuration to a publicly accessible cluster.
Apply the Kubernetes configuration in kubernetes/
.
You will find two applications in Kubernetes, one for product-api
and the other for
postgres
.
Check that the product-api
has an external IP.
Set the environment variable TEST_HASHICUPS_URL
to the external IP of product-api
.
Check that you can access the HashiCups API health endpoint.
Set the username you will use to test the HashiCups API
as the TEST_HASHICUPS_USERNAME
environment variable.
Set the password you will use to test the HashiCups API
as the TEST_HASHICUPS_PASSWORD
environment variable.
Sign up for the HashiCups API with a username and password
by calling the /signup
API endpoint.
Output:
Build the plugin
You can build the secrets engine into a plugin and register it with a local Vault server.
Note
Alternatively, you can run a Vault server in dev-mode
with vault server -dev -dev-plugin-dir=$(pwd)/vault/plugins
.
Running in dev-mode omits the need to manually initialize, unseal,
and register the plugin to Vault. You can instead go directly to
using the secrets engine.
Build the secrets engine into a plugin using Go.
You can find the binary in vault/plugins/
.
Register the plugin to Vault
Write out the vault/server.hcl
for the Vault server configuration.
You need to set a plugin_directory
to point to the folder with your
custom secrets engine and api_addr
for the plugin to communicate
with Vault. The Vault instance stores everything in
memory and runs locally.
Note
To use your custom secrets engine with your Vault cluster, you need to copy the secrets engine plugin binary into a plugin directory for each Vault node.
Start your Vault server.
In a new terminal, set the VAULT_ADDR
to the local Vault server.
Initialize Vault with one key. Save the initial root token and unseal key.
Set the VAULT_TOKEN
to the root token.
Unseal Vault with the unseal key.
Calculate the SHA256 sum of the compiled secrets engine binary. Vault uses the sum to ensure that the plugin in the catalog matches the binary in the plugin directory!
Register the plugin with Vault using the sum.
Plugins and Proxies
Many plugins use tools or libraries that automatically consume HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environment variables to configure HTTP proxy settings. A specific proxy can be set for each plugins with the vault plugin register
command, refer to Plugin-specific HTTP Proxy settings documentation for details.
Check the status of the HashiCups secrets engine by getting information from the Vault plugin catalog.
Use the secrets engine
Set the VAULT_TOKEN
environment variable to the root token.
Set the username you will use to test the HashiCups API
as the TEST_HASHICUPS_USERNAME
environment variable.
Set the password you will use to test the HashiCups API
as the TEST_HASHICUPS_PASSWORD
environment variable.
Set the environment variable TEST_HASHICUPS_URL
to the external IP of product-api
.
You can use the HashiCups secrets engine in Vault by enabling
it for a path such as hashicups/
.
Configure the secrets engine to access the HashiCups API.
Output:
Reading the configuration from Vault outputs the URL and username for HashiCups but not the password!
Create a role entry for the HashiCups secrets engine named test
.
Retrieve a new HashiCups JSON Web Token (JWT) from Vault to access the HashiCups API.
Copy the value from token
and save the token to
the HASHICUPS_TOKEN
environment variable.
Add a new coffee to the HashiCups API using the token. The API successfully allows you to add a new item.
Output example:
Imagine you accidentally compromised the HashiCups API token. Revoke the token from Vault.
Try to add a new coffee to the HashiCups API using the token. The API returns a 401 Unauthorized
because your secrets engine invalidated the HashiCups API token!
Output example:
You successfully used your custom secrets engine to create and revoke an API token for HashiCups.
Clean up
Stop your Vault server.
Delete the Kubernetes configuration in kubernetes/
.
Next steps
Congratulations! You tested and built your own secrets engine.
If you are stuck in this tutorial, refer to the
plugins/vault-plugin-secrets-hashicups/solution
directory.
To learn more about upgrading plugins, refer to the documentation on registration and reload.
To learn more about Vault plugins, refer to the Vault Plugin System Documentation.
Watch Developing a secrets engine for HashiCorp Vault.