Codify management of HCP Vault Dedicated
Challenge
Manual system administration can become a challenge as the scale of infrastructure increases. Often, an organization must manage multiple Vault environments (development, testing, staging, production, etc.). Keeping up with the increasing management demand soon becomes a challenge without some sort of automation.
Solution
One of the pillars behind the Tao of Hashicorp is automation through codification.
HashiCorp Terraform is an infrastructure as code software tool that enables operation teams to codify Vault configuration tasks such as the creation of policies. Automation through codification allows operators to increase their productivity, move quicker, promote repeatable processes, and reduce human error.
This tutorial demonstrates techniques for creating Vault policies and configurations using the Terraform Vault Provider.
Prerequisites
- Terraform installed
- HCP Vault Dedicated cluster with public cluster address enabled
HCP Vault Dedicated cluster
For this tutorial, follow the enable the public cluster address enabled in the Deploy HCP Vault Dedicated with Terraform tutorial.
HCP project considerations
By default, Terraform will create or manage the HVN defined in the default or oldest HCP project. If you are using multiple
projects in your HCP account, you can control where Terraform will manage the HVN and cluster using the project_id
parameter.
For more information on how to work with multiple projects, refer to the HCP provider documentation.
1 2 3 4 5 6 7 8 9 1011
Note
If the public address is not available, execute the steps in this tutorial from a virtual machine within the VPC with connectivity to your HVN.
Scenario introduction
Vault administrators must manage multiple Vault environments. To automate the Vault server configuration, you are going to use Terraform to provision the following Vault resources.
For an HCP Vault Dedicated cluster, the root level namespace is admin
. In this tutorial,
you are going to create nested namespaces as shown below.
Terraform will provision the following Vault resources.
Type | Name | Description |
---|---|---|
namespace | education | A namespace with path: admin/education |
namespace | training | A namespace with path: admin/education/training |
namespace | boundary | A namespace with path: admin/education/training/boundary |
namespace | test | A namespace with path: admin/test |
ACL Policy | admins | Sets policies for the admin team |
ACL Policy | eaas-client | Sets policies to encrypt/decrpt secrets using transit payment key |
ACL Policy | tester | Sets policies for clients to encode/decode data through transform secrets engine |
auth method | userpass | Enable userpass auth method in the admin/test namespace and create a student uesr |
secrets engine | kv-v2 | Enable kv-v2 secrets engine at kv-v2 in the admin/education namespace |
secrets engine | kv-v2 | Enable kv-v2 secrets engine at secret in the admin/test namespace |
secrets engine | transit | Enable transit secrets engine in the admin/education namespace |
Examine the Terraform files
Clone or download the demo assets from the hashicorp-education/learn-vault-hcp-codify-mgmt GitHub repository to perform the steps described in this tutorial.
To clone the repository, use the git clone
command.
Alternatively, you can download the repository.
This repository contains supporting content for all of the Vault tutorials. The content specific to this tutorial can be found within a sub-directory.
Change the working directory to learn-vault-hcp-codify-mgmt
.
The directory contains Terraform files to configure Vault.
Review main.tf
Open the main.tf
file in your preferred text editor to examine its content.
It defines three vault_namespace
blocks each pointing to a different namespace.
1 2 3 4 5 6 7 8 9 10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
To create a child namespace education
under admin
, you need to do nothing as
all namespaces are by default created under admin.
To create a sub-namespace under education
, add a namespace
field with reference to the parent namespace.
1234
It is strongly recommended to specify the target server specific
information
using environment variables (e.g. VAULT_ADDR
, VAULT_TOKEN
). And that's
what you are going to do in this tutorial.
Review policies.tf
Open the policies.tf
file and examine the
vault_policy
resources.
1 2 3 4 5 6 7 8 9 101112131415161718192021222324252627282930313233343536373839404142434445464748
This creates an admins
policy in all namespaces, an eaas-client
policy in
the admin/education
namespace (line 33 through 38), and a tester
policy in
the admin/test
namespace (line 41 through 46).
Review auth.tf
Open the auth.tf
file. This enables userpass
auth method in the admin/test
namespace and creates a user, "student" with tester
policy attached. The
password is set to "changeme".
1 2 3 4 5 6 7 8 9 1011121314151617181920212223
Review secrets.tf
Open the secrets.tf
file.
1 2 3 4 5 6 7 8 9 10111213141516171819202122232425262728
This Terraform file performs the following:
- Line 2-7: Enables kv-v2 secrets engine at
kv-v2
in theadmin/education
namespace. - Line 10-15: Enables kv-v2 secrets engine at
secret
in theadmin/test
namespace. - Line 18-23: Enables transit secrets engine in the
admin/education
namespace. - Line 26-32: Creates payment key
Run Terraform to configure Vault
Launch the HCP Portal and login.
Click Vault in the left navigation pane.
In the Vault clusters pane, click vault-cluster.
Under Cluster URLs, click Public Cluster URL.
In a terminal, set the
VAULT_ADDR
environment variable to the copied address.Return to the Overview page and click Generate token.
Within a few moments, a new token will be generated.
Copy the Admin Token.
Return to the terminal and set the
VAULT_TOKEN
environment variable.Initialize Terraform to pull Vault provider plugin.
This downloads the Vault plugin. When it completes, it displays a message,
Terraform has been successfully initialized!
Execute the
apply
command to configure Vault.This displays the actions to be performed by Terraform.
When prompted, enter
yes
to accept the plan and proceed with Vault configuration.
Verify the configuration
To validate the Terraform configured environment, make sure that VAULT_ADDR
and VAULT_TOKEN
environment variables are set.
Verify namespaces
Verify that nested namespaces are created as follow:
List namespaces under
admin/
.This verifies that
admin/education
andadmin/test
namespaces exist.List namespaces under
admin/education
.List namespaces under
admin/education/training
.
Verify the "admin/test" namespace settings
In the admin/test
namespace, verify that the following resources exist:
- userpass auth method is enabled
tester
andadmins
policies existstudent
user withtester
policy exists- kv-v2 secrets engine enabled at
secret
List auth methods in the
admin/test
namespace.Verify that
tester
andadmins
policies exists.Optionally, read the
tester
policy.Verify that
student
user exists.Verify that kv-v2 secrets engine is enabled at the
secret
path.Example output:
Verify the "admin/education" namespace settings
In the admin/education
namespace, verify that the following resources exist:
eaas-client
andadmins
policies exist- kv-v2 secrets engine is enabled at
kv-v2
- transit secrets engine is enabled
payment
key is created
List policies.
Optionally, read the
eaas-client
policy.Verify that kv-v2 secrets engine is enables at
kv-v2
, and transit secrets engine is enabled attransit
.Example output:
Verify that
payment
key exists.
Verify policies
You already verified that the admins
policy exists in the admin/education
and admin/test
namespaces. Now, verify that the policy exists in the admin
,
admin/education/training
and admin/education/training/boundary
namespaces.
Check the
admin
namespace.Check the
admin/education/training
namespace.Check the
admin/education/training/boundary
namespace.
Scale an HCP Vault Dedicated cluster up or down
During the Deploy HCP Vault Dedicated with Terraform
tutorial you created an Vault Dedicated cluster using the Development tier by setting the tier
variable in the variables.tf
file to dev
.
Now that the Vault Dedicated cluster has been created, you can scale the Vault Dedicated cluster up
or down to meet organizational needs by changing the tier
variable and re-running
terraform apply
.
Note
HCP Vault Dedicated clusters can be scaled up from the development tier to starter or standard, but starter and standard tier clusters cannot be scaled down to the development tier.
Open the
variables.tf
file.Locate the
tier
variable code blockChange
default
from"dev"
to"standard-small"
and save the changes.Note
Valid tier sizes include:
dev
,starter_small
,standard_small
,standard_medium
,standard_large
.Execute the
apply
command to configure Vault.This displays the actions to be performed by Terraform.
When prompted, enter
yes
to accept the plan and proceed with Vault configuration.The Vault Dedicated cluster will be scaled up from the
development
tier to thestarter_small
tier.
Clean up
When you are done exploring, you can undo the configuration made by Terraform.
The admin token generated by the HCP
Portal is valid for 6 hours.
If it is expired, you need to generate a new one and overwrite the VAULT_TOKEN
value to proceed.
Destroy the Vault resources created by Terraform.
Remove the terraform state files.
Unset the
VAULT_TOKEN
environment variable.Unset the
VAULT_ADDR
environment variable.
Note
To learn more about Terraform, visit Terraform tutorials.
Help and reference
- Terraform Vault Provider documentation page
- Terraform Provider GitHub repository
- Learn Terraform
Terraform users can leverage the Vault's dynamic secrets engine to generate short-live cloud credentials when provisioning cloud resources. Inject secrets into Terraform using the Vault provider tutorial demonstrates the use of AWS secrets engine to manage AWS IAM credentials used by Terraform.