KV secrets engine - version 2
The kv
secrets engine is used to store arbitrary secrets within the
configured physical storage for Vault.
Key names must always be strings. If you write non-string values directly via the CLI, they will be converted into strings. However, you can preserve non-string values by writing the key/value pairs to Vault from a JSON file or using the HTTP API.
This secrets engine honors the distinction between the create
and update
capabilities inside ACL policies. The patch
capability is also supported
which is used to represent partial updates whereas the update
capability
represents full overwrites.
Setup
Most secrets engines must be configured in advance before they can perform their functions. These steps are usually completed by an operator or configuration management tool.
A v2 kv
secrets engine can be enabled by:
Or, you can pass kv-v2
as the secrets engine type:
Additionally, when running a dev-mode server, the v2 kv
secrets engine is enabled by default at the
path secret/
(for non-dev servers, it is currently v1). It can be disabled, moved, or enabled multiple times at
different paths. Each instance of the KV secrets engine is isolated and unique.
Upgrading from version 1
An existing version 1 kv store can be upgraded to a version 2 kv store via the CLI or API, as shown below. This will start an upgrade process to upgrade the existing key/value data to a versioned format. The mount will be inaccessible during this process. This process could take a long time, so plan accordingly.
Once upgraded to version 2, the former paths at which the data was accessible will no longer suffice. You will need to adjust user policies to add access to the version 2 paths as detailed in the ACL Rules section below. Similarly, users/applications will need to update the paths at which they interact with the kv data once it has been upgraded to version 2.
An existing version 1 kv can be upgraded to a version 2 KV store with the CLI command:
or via the API:
ACL rules
The version 2 kv store uses a prefixed API, which is different from the version 1 API. Before upgrading from a version 1 kv the ACL rules should be changed. Also different paths in the version 2 API can be ACL'ed differently.
Writing and reading versions are prefixed with the data/
path. This policy
that worked for the version 1 kv:
Should be changed to:
There are different levels of data deletion for this backend. To grant a policy the permissions to delete the latest version of a key:
To allow the policy to delete any version of a key:
To allow a policy to undelete data:
To allow a policy to destroy versions:
To allow a policy to list keys:
To allow a policy to view metadata for each version:
To allow a policy to permanently remove all versions and metadata for a key:
The allowed_parameters
, denied_parameters
, and required_parameters
fields are
not supported for policies used with the version 2 kv store. See the Policies Concepts
for a description of these parameters.
See the API Specification for more information.
Usage
After the secrets engine is configured and a user/machine has a Vault token with
the proper permission, it can generate credentials. The kv
secrets engine
allows for writing keys with arbitrary values.
The path-like KV-v1 syntax for referencing a secret (secret/foo
) can still
be used in KV-v2, but we recommend using the -mount=secret
flag syntax to
avoid mistaking it for the actual path to the secret (secret/data/foo
is the
real path).
Writing/Reading arbitrary data
Write arbitrary data:
Read arbitrary data:
Write another version, the previous version will still be accessible. The
-cas
flag can optionally be passed to perform a check-and-set operation. If not set the write will be allowed. In order for a write to be successful,cas
must be set to the current version of the secret. If set to 0 a write will only be allowed if the key doesn’t exist as unset keys do not have any version information. Also remember that soft deletes do not remove any underlying version data from storage. In order to write to a soft deleted key, the cas parameter must match the key's current version.Reading now will return the newest version of the data:
Partial updates can be accomplished using the
vault kv patch
command. A command will initially attempt an HTTPPATCH
request which requires thepatch
ACL capability. ThePATCH
request will fail if the token used is associated with a policy that does not contain thepatch
capability. In this case the command will perform a read, local update, and subsequent write which require both theread
andupdate
ACL capabilities.The
-cas
flag can optionally be passed to perform a check-and-set operation. It will only be used in the case of the initialPATCH
request. The read-then-write flow will use theversion
value from the secret returned by the read to perform a check-and-set operation in the subsequent write.The
vault kv patch
command also supports a-method
flag which can be used to specify HTTPPATCH
or read-then-write. The supported values arepatch
andrw
for HTTPPATCH
and read-then-write, respectively.Perform a patch using the
patch
method:Perform a patch using the read-then-write method:
Reading after a patch will return the newest version of the data in which only the specified fields were updated:
Previous versions can be accessed with the
-version
flag:
Deleting and destroying data
When deleting data the standard vault kv delete
command will perform a
soft delete. It will mark the version as deleted and populate a deletion_time
timestamp. Soft deletes do not remove the underlying version data from storage,
which allows the version to be undeleted. The vault kv undelete
command
handles undeleting versions.
A version's data is permanently deleted only when the key has more versions than
are allowed by the max-versions setting, or when using vault kv destroy
. When
the destroy command is used the underlying version data will be removed and the
key metadata will be marked as destroyed. If a version is cleaned up by going
over max-versions the version metadata will also be removed from the key.
See the commands below for more information:
The latest version of a key can be deleted with the delete command, this also takes a
-versions
flag to delete prior versions:Versions can be undeleted:
Destroying a version permanently deletes the underlying data:
Key metadata
All versions and key metadata can be tracked with the metadata command & API. Deleting the metadata key will cause all metadata and versions for that key to be permanently removed.
See the commands below for more information:
All metadata and versions for a key can be viewed:
The metadata settings for a key can be configured:
Delete-version-after settings will apply only to new versions. Max versions changes will be applied on next write:
Once a key has more versions than max versions the oldest versions are cleaned up:
A secret's key metadata can contain custom metadata used to describe the secret. The data will be stored as string-to-string key-value pairs. The
-custom-metadata
flag can be repeated to add multiple key-value pairs.The
vault kv metadata put
command can be used to fully overwrite the value ofcustom_metadata
:The
vault kv metadata patch
command can be used to partially overwrite the value ofcustom_metadata
. The following invocation will updatecustom_metadata
sub-fieldfoo
but leavebar
untouched:Permanently delete all metadata and versions for a key:
Tutorial
Refer to the Versioned Key/Value Secrets Engine tutorial to learn how to use KV secrets engine v2 to version or roll back secrets.
API
The KV secrets engine has a full HTTP API. Please see the KV secrets engine API for more details.