--- layout: guides page_title: Versioned KV Secret Engine - Guides sidebar_title: Versioned KV Secret Engine description: |- Vault 0.10.0 introduced version 2 of key-value secret engine which supports versioning of your secrets so that you can undo the accidental deletion of secrets, or compare the different versions of the secret. --- # Versioned Key/Value Secret Engine The [Static Secrets](/guides/secret-mgmt/static-secrets) guide introduced the basics of working with key-value secret engine. **Vault 0.10** introduced [_K/V Secrets Engine v2 with Secret Versioning_](https://www.hashicorp.com/blog/vault-0-10). This guide demonstrates the new features introduced by the key-value secret engine v2. ## Reference Material - [Static Secrets guide](/guides/secret-mgmt/static-secrets) - [KV Secrets Engine - Version 2](/docs/secrets/kv/kv-v2) - [KV Secrets Engine - Version 2 (API)](/api/secret/kv/kv-v2) ~> **NOTE:** An [interactive tutorial](https://www.katacoda.com/hashicorp/scenarios/vault-static-secrets) is also available if you do not have a Vault environment to perform the steps described in this guide. ## Estimated Time to Complete 10 minutes ## Challenge The KV secret engine v1 does not provide a way to version or roll back secrets. This made it difficult to recover from unintentional data loss or overwrite when more than one user is writing at the same path. ## Solution Run the **version 2** of KV secret engine which can retain a configurable number of secret versions. This enables older versions' data to be retrievable in case of unwanted deletion or updates of the data. In addition, its _Check-and-Set_ operations can be used to protect the data from being overwritten unintentionally. ![Versioned KV](/img/vault-versioned-kv-1.png) ## Prerequisites To perform the tasks described in this guide, you need to have a Vault environment. Refer to the [Getting Started](/intro/getting-started/install) guide to install Vault. Make sure that your Vault server has been [initialized and unsealed](/intro/getting-started/deploy). ### Policy requirements -> **NOTE:** For the purpose of this guide, you can use **`root`** token to work with Vault. However, it is recommended that root tokens are only used for initial setup or in emergencies. As a best practice, use tokens with appropriate set of policies based on your role in the organization. To perform all tasks demonstrated in this guide, your policy must include the following permissions: ```shell # To view in Web UI path "sys/mounts" { capabilities = [ "read", "update" ] } # Write and manage secrets in key-value secret engine path "secret*" { capabilities = [ "create", "read", "update", "delete", "list" ] } # To enable secret engines path "sys/mounts/*" { capabilities = [ "create", "read", "update", "delete" ] } ``` If you are not familiar with policies, complete the [policies](/guides/identity/policies) guide. ## Steps This guide demonstrates the basic commands for working with KV secret engine v2. You will perform the following: 1. [Check the KV secret engine version](#step1) 2. [Write secrets](#step2) 3. [Retrieve a specific version of secret](#step3) 4. [Specify the number of versions to keep](#step4) 5. [Delete versions of secret](#step5) 6. [Permanently delete data](#step6) ### Step 1: Check the KV secret engine version ((#step1)) (**Persona:** devops) Before beginning, verify that you are using the v2 of the KV secret engine. #### CLI command To check the KV secret engine version: ```shell-session $ vault secrets list -format=json ... "secret/": { "type": "kv", "description": "key/value secret storage", "accessor": "kv_f05b8b9c", "config": { "default_lease_ttl": 0, "max_lease_ttl": 0, "force_no_cache": false }, "options": { "version": "2" }, ... ``` The indicated **`version`** should be **`2`**. If the version is **`1`**, upgrade it to v2. ```shell-session $ vault kv enable-versioning secret/ ``` #### API call using cURL To check the KV secret engine version: ```shell-session $ curl --header "X-Vault-Token: " \ /v1/sys/mounts ``` Where `` is your valid token, and `` is where your vault server is running. **Example:** ```shell-session $ curl --header "X-Vault-Token: ..." \ http://127.0.0.1:8200/v1/sys/mounts | jq ... "secret/": { "accessor": "kv_f05b8b9c", "config": { "default_lease_ttl": 0, "force_no_cache": false, "max_lease_ttl": 0, "plugin_name": "" }, "description": "key/value secret storage", "local": false, "options": { "version": "2" }, "seal_wrap": false, "type": "kv" }, ... ``` The indicated **`version`** should be **`2`**. If the version is **`1`**, upgrade it to v2. ```shell-session $ cat payload.json { "options": { "version": "2" } } $ curl --header "X-Vault-Token: ..." \ --request POST \ --data @payload.json \ http://127.0.0.1:8200/v1/sys/mounts/secret/tune ``` #### Web UI Open a web browser and launch the Vault UI (e.g. `http://127.0.0.1:8200/ui`) and then login. ![Web UI](/img/vault-versioned-kv-2.png) If `secret/` does not indicates **`v2`**, you can upgrade it from `v1` to `v2` by executing the following CLI command: ```shell-session $ vault kv enable-versioning secret/ ``` Alternatively, you can enable KV secret engine v2 at a different path by clicking **Enable new engine**. Select **KV** from the **Secret engine type** drop-down list. Be sure that the **Version** is set to be **Version 2**. ![Enabling kv-v2](/img/vault-versioned-kv-3.png) Click **Enable Engine** to complete. ### Step 2: Write Secrets ((#step2)) To understand how the versioning works, let's write some test data. #### CLI commands To write secrets, run `vault kv put` command instead of `vault write`: ```shell-session $ vault kv put secret/customer/acme name="ACME Inc." contact_email="jsmith@acme.com" Key Value --- ----- created_time 2018-04-14T00:05:47.115378933Z deletion_time n/a destroyed false version 1 ``` To update the existing secret, run the `vault kv put` command again: ```shell-session $ vault kv put secret/customer/acme name="ACME Inc." contact_email="john.smith@acme.com" Key Value --- ----- created_time 2018-04-14T00:13:35.296018431Z deletion_time n/a destroyed false version 2 ``` Now you have two versions of the `secret/customer/acme` data. Run `vault kv get` to read the data. ```shell-session $ vault kv get secret/customer/acme ====== Metadata ====== Key Value --- ----- created_time 2018-04-14T00:13:35.296018431Z deletion_time n/a destroyed false version 2 ======== Data ======== Key Value --- ----- contact_email john.smith@acme.com name ACME Inc. ``` #### API call using cURL Write some data at `secret/customer/acme`: ```shell-session $ tee payload.json <`**; therefore, to write secrets at `secret/customer/acme`, the API endpoint becomes `/secret/data/customer/acme`. Update the secret to create another version: ```shell-session $ tee payload.json < If the **`cas`** value is missing in your write request, the "`check-and-set parameter required for this call`" error will be returned. If the `cas` does not match the current version number, you will receive the "`check-and-set parameter did not match the current version`" message. ## Next steps This guide introduced the CLI commands and API endpoints to read and write static secrets in the key-value secret engine. Read [Secret as a Service: Dynamic Secrets](/guides/secret-mgmt/dynamic-secrets) guide to learn about the usage of database secret engine.