diff --git a/website/content/docs/configuration/programmatic-management.mdx b/website/content/docs/configuration/programmatic-management.mdx new file mode 100644 index 000000000000..a04eb8b14c3e --- /dev/null +++ b/website/content/docs/configuration/programmatic-management.mdx @@ -0,0 +1,463 @@ +--- +layout: docs +page_title: Manage Vault resources programmatically +description: >- + Step-by-step instructions for managing Vault resources programmatically with + Terraform +--- + +# Manage Vault resources programmatically with Terraform + +Use Terraform to manage policies, namespaces, and plugins in Vault. + +## Before you start + +- **You must have [Terraform installed](/terraform/install)**. +- **You must have the [Terraform Vault provider](https://registry.terraform.io/providers/hashicorp/vault/latest) configured**. +- **You must have admin access to your Terraform installation**. If you do not + have admin access, you can still generate the relevant configuration files, + but you will need to have someone else apply the changes. +- **You must have a [Vault server running](/vault/tutorials/getting-started/getting-started-dev-server)**. + +## Step 1: Create a resource file for namespaces + +Terraform Vault provider supports a `vault_namespace` resource type for +managing Vault namespaces: + +```hcl +resource "vault_namespace" "" { + path = "" +} +``` + +To manage your Vault namespaces in Terraform: + +1. Use the `vault namespace list` command to identify any unmanaged namespaces + that you need to migrate. For example: + + ```shell-session + $ vault namespace list + + Keys + ---- + admin/ + ``` + +1. Create a new Terraform Vault Provider resource file called + `vault_namespaces.tf` that defines `vault_namespace` resources for each of + the new or existing namespaces resources you want to manage. + + For example, to migrate the `admin` namespace in the example and create a new + `dev` namespace: + + ```hcl + resource "vault_namespace" "admin_ns" { + path = "admin" + } + + resource "vault_namespace" "dev_ns" { + path = "dev" + } + ``` + +## Step 2: Create a resource file for secret engines + +Terraform Vault provider supports discrete types for the different +[auth](https://registry.terraform.io/providers/hashicorp/vault/latest/docs#vault-authentication-configuration-options), +[secret](https://registry.terraform.io/providers/hashicorp/vault/latest/docs/resources/mount), +and [database](https://registry.terraform.io/providers/hashicorp/vault/latest/docs/resources/database_secrets_mount) +plugin types in Vault. + +To migrate a secret engine, use the `vault_mount` resource type: + +```hcl +resource "vault_mount" "" { + path = "" + type = "" +} +``` + +To manage your Vault secret engines in Terraform: + +1. Use the `vault secret list` command to identify any unmanaged secret engines + that you need to migrate. For example: + + ```shell-session + $ vault secrets list | grep -vEw '(cubbyhole|identity|sys)' + + Path Type Accessor Description + ---- ---- -------- ----------- + transit/ transit transit_8291b949 n/a + ``` + +1. Use the `-namespace` flag to check for unmanaged secret engines under any + namespaces you identified in the previous step. For example, to check for + secret engines under the `admin` namespace: + + ```shell-session + $ vault secrets list -namespace=admin | grep -vEw '(cubbyhole|identity|sys)' + + Path Type Accessor Description + ---- ---- -------- ----------- + admin_keys/ kv kv_87edfc65 n/a + ``` + +1. Create a new Terraform Vault Provider resource file called `vault_secrets.tf` + that defines `vault_mount` resources for each of the new or existing secret + engines you want to manage. + + For example, to migrate the `transit` and `admin_keys` secret engines in the + example and enable a new `kv` engine under the new `dev` namespace called + `dev_keys`: + + ```hcl + resource "vault_mount" "transit_plugin" { + path = "transit" + type = "transit" + } + + resource "vault_mount" "admin_keys_plugin" { + namespace = vault_namespace.admin_ns.path + path = "admin_keys" + type = "kv" + options = { + version = "2" + } + } + + resource "vault_mount" "dev_keys_plugin" { + namespace = vault_namespace.dev_ns.path + path = "dev_keys" + type = "kv" + options = { + version = "2" + } + } + ``` + +## Step 3: Create a resource file for policies + +Terraform Vault provider supports a `vault_policy` resource type for +managing Vault policies: + +```hcl +resource "vault_policy" "" { + name = "" + policy = < + EOT +} +``` + +To manage your Vault policies in Terraform: + +1. Use the `vault policy list` command to identify any unmanaged policies that + you need to migrate. For example: + + ```shell-session + $ vault policy list | grep -vEw 'root' + + default + ``` + +1. Create a Terraform Vault Provider resource file called `vault_policies.tf` + that defines `vault_mount` resources for each policy resource you want to + manage in Terraform. You can use the following `bash` code to write all + your existing, non-root policies to the file: + + ```shell-session + for vpolicy in $(vault policy list | grep -vw root) ; do + echo "resource \"vault_policy\" \"vault_$vpolicy\" {" + echo " name = \"$vpolicy\"" + echo " policy = < vault_policies.tf + ``` + +1. Update the `vault_policies.tf` file with any new policies you want to add. + For example, to create a policy for the example `dev_keys` secret engine: + + ```hcl + resource "vault_policy" "dev_team_policy" { + name = "dev_team" + + policy = <listener", "routes": [