Encryption as a Service: Transit Secrets Engine
Vault's transit
secrets engine handles cryptographic functions on
data-in-transit. Vault doesn't store the data sent to the secrets engine, so it
can also be viewed as encryption as a service.
Although the transit
secrets engine provides additional features (sign and
verify data, generate hashes and HMACs of data, and act as a source of random
bytes), its primary use case is to encrypt data. This relieves the burden of
proper encryption/decryption from application developers and pushes the burden
onto the admins of Vault.
Challenge
Think of the following scenario:
Example Inc. recently made headlines for a massive data breach which exposed millions of their users' payment card accounts online. When they tracked down the problem they found that a new HVAC system with management software had been put into their data centers and had created vulnerabilities in their networks and exposed ports and IPs to the databases publicly.
Solutions
The transit
secrets engine enables security teams to fortify data during
transit and at rest. So even if an intrusion occurs, your data is encrypted with
AES-GCM with a 256-bit AES key or other supported key
types.
Even if an attacker were able to access the raw data, they would only have
encrypted bits. This means attackers would need to compromise multiple systems
before exfiltrating data.
This tutorial demonstrates the basics of the transit
secrets engine.

Launch Terminal
This tutorial includes a free interactive command-line lab that lets you follow along on actual cloud infrastructure.
Personas
The end-to-end scenario described in this tutorial involves two personas:
- admin with privileged permissions to manage the encryption keys
- apps with un-privileged permissions encrypt/decrypt secrets via APIs
Prerequisites
To perform the tasks described in this tutorial, you need to have a Vault environment. Refer to the Getting Started tutorial to install Vault.
Policy requirements
Note
For the purpose of this tutorial, you can use root
token to work
with Vault. However, it is recommended that root tokens are only used for just
enough 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 tutorial, your policy must include the following permissions.
If you are not familiar with policies, complete the policies tutorial.
Lab setup
- Open a terminal and start a Vault dev server with
root
as the root token.
The Vault dev server defaults to running at 127.0.0.1:8200
. The server is
also initialized and unsealed.
Insecure operation
Do not run a Vault dev server in production. This approach is only used here to simplify the unsealing process for this demonstration.
- Export an environment variable for the
vault
CLI to address the Vault server.
- Export an environment variable for the
vault
CLI to authenticate with the Vault server.
The Vault server is ready.
Configure Transit secrets engine
(Persona: admin)
The transit
secrets engine must be configured before it can perform its
operations. This step is usually done by an admin or configuration
management tool.
Enable the
transit
secrets engine by executing the following command.NOTE: By default, the secrets engine will mount at the name of the engine. If you wish to enable it at a different path, use the
-path
argument.Example: The following command enables the transit secrets engine at
encryption/
instead oftransit/
.Create an encryption key ring named
orders
by executing the following command.
Create a token for Vault clients
(Persona: admin)
Vault clients must authenticate with Vault and acquire a valid token with appropriate policies allowing to request data encryption and decryption using the specific key.

When the transit secrets engine is enabled at transit
, the policy must include
the following.
This tutorial uses the vault token create
command to generate a client token
and skips the authentication step.
In practice, you can leverage the Vault Agent Auto-Auth method to authenticate with Vault and manage the lifecycle of the client token. To learn more about Vault Agent, visit the App Integration in Learn.
Create a policy named
app-orders
.The policy is created or updated; if it already exists.
Create a token with
app-orders
policy attached.Example output:
Create a
APP_ORDER_TOKEN
environment variable to store the generated client token value.Example:
Copy the generated token value (e.g. s.U4I87snzLMNOPybPIpyozdHC.QttaO
). You are
going to use it to request data encryption and decryption.
Encrypt secrets
(Persona: apps)
Once the transit
secrets engine has been configured, any Vault client holding
a valid token with the proper permissions can send data to encrypt.
Here, you are going to encrypt a plaintext credit card number (4111 1111 1111 1111
).
Note
Vault can encrypt a binary file such as an image. When you send data to Vault for encryption, it must be in the form of base64-encoded plaintext for a safe transport.
Encrypt plaintext (using the shell to do the base64
encoding) using the
orders
encryption key. Be sure to replace <client_token>
with the token
value you copied in the previous step.
Example output:
Notice that the ciphertext starts with vault:v1:
. This prefix indicates that
this value is wrapped by vault
and the version of the orders
encryption key
used was v1
. Therefore, when you decrypt this ciphertext, Vault knows to use
v1
of the key. Later, you are going to rotate the encryption key and learn how
to re-wrap the ciphertext with the latest version of the encryption key.
Warning
Vault does NOT store any data encrypted via the transit/encrypt
endpoint.
The output you received is the ciphertext. You can store this ciphertext at the
desired location (e.g. MySQL database) or pass it to another application.
Decrypt ciphertext
(Persona: apps)
Any client holding a valid token with proper permissions can decrypt ciphertext
generated by Vault. To decrypt the ciphertext, invoke the transit/decrypt
endpoint.
Execute the following command to decrypt the ciphertext emitted in the encrypt
secrets step. Be sure to set the <ciphertext>
value to the
value returned in the previous step.
Example:
The resulting data is base64-encoded and must be decoded to reveal the plaintext.
Rotate the encryption key
(Persona: admin)
One of the benefits of using the Vault transit
secrets engine is its ability
to easily rotate encryption keys. Keys can be rotated manually by a human or by
an automated process which invokes the key rotation API endpoint through cron
,
a CI pipeline, a periodic
Nomad batch
job, Kubernetes Job, etc.
Vault maintains the versioned keyring and the admin can decide the minimum version allowed for decryption operations. When data is encrypted using Vault, the resulting ciphertext is prepended by the version of the key used to encrypt it.
To rotate the encryption key, invoke the
transit/keys/<key_ring_name>/rotate
endpoint. In this tutorial, the key ring name isorders
.Encrypt another data.
Compare the ciphertexts from the encrypt secrets step as generated on your machine.
Notice that the first ciphertext starts with "
vault:v1:
". After rotating the encryption key, the ciphertext starts with "vault:v2:
". This indicates that the data is encrypted using the latest version of the key after the rotation.Rewrap your ciphertext from the encrypt secrets step with the latest version of the encryption key.
Example output:
Notice that the resulting ciphertext now starts with "
vault:v2:
".This operation does not reveal the plaintext data. But Vault will decrypt the value using the appropriate key in the keyring and then encrypt the resulting plaintext with the newest key in the keyring.
Automatic key rotation
Vault version
This feature requires Vault 1.10 or later.
Instead of rotating the key manually, you can configure Vault to automatically rotate the encryption key at a user-defined time interval.
Read the
orders
key information.Output:
The
auto_rotate_period
parameter configures the amount of time the key should live before being automatically rotated. A value of 0 (default) disables automatic rotation for the key.Configure to automatically rotate the
orders
key every 24 hours.Read the
orders
key information again.Output:
Vault will automatically rotate the orders
key every 24 hours.
Update key configuration
(Persona: admin)
Vault admin can update the encryption key configuration to specify the minimum version of ciphertext allowed to be decrypted, the minimum version of the key that can be used to encrypt the plaintext, the key is allowed to be deleted, etc.
This helps to further tighten the data encryption rule.
Execute the key rotation command a few times to generate multiple versions of the key.
Read the
orders
key information.Example output:
In this example, the current version of the key is
6
. However, there is no restriction about the minimum encryption key version, and any of the key versions can decrypt the data (min_decryption_version
).Enforce the use of the encryption key at version
5
or later to decrypt the data.Verify the
orders
key configuration.
Generate data key
(Persona: admin)
When you encrypt your data, the encryption key used to encrypt the plaintext is
referred to as a data key. This data key needs to be protected so that your
encrypted data cannot be decrypted easily by an unauthorized party. In encrypt
secrets step, you encrypted your data by specifying the key
ring name (orders
) and the actual data key used to encrypt the data was never
presented to you.
In this step, you are going to use the transit/datakey
endpoint which returns
the plaintext of a named data key.
Why would I need the data key?
Think of a scenario where you have a 2GB base64 binary large object (blob) that needs to be encrypted. You probably don't want to send the 2GB over the network to Vault and receive the 2GB back. Instead, you can generate a data key and encrypt it locally and use the same data key to decrypt it locally when needed.
The idea with data keys is to allow applications to encrypt and decrypt data without round-tripping through Vault.
The data key is its own full key; you can't decrypt it with the transit key that it is wrapped with. However, because the data key is wrapped by a transit key, and thus protected, you can store it with the data. This way, you can control which Vault clients can decrypt the data through policies.
The response contains the plaintext of the data key as well as its ciphertext. Use the plaintext to encrypt your blob. Store the ciphertext of your data key wherever you want. You can even store it in the key/value secrets engine.
When you need to decrypt the blob, request Vault to decrypt the ciphertext of your data key (decrypt ciphertext) so that you can get the plaintext back to decrypt the blob locally. In other words, once your blob is encrypted, you don't have to persist the data key. You only need to keep the ciphertext version of the data key.
Bring your own key (BYOK)
When your use case requires an external key, users of Vault version 1.11.0 or greater can use BYOK functionality to import an existing encryption key that was generated outside Vault.
The target key for import can originate from an HSM or other external source, and must be prepared according to its origin before you can import it.
The example shown here will use a 256-bit AES key, referred to as the target key. To successfully import the target key, you must perform the following operations to prepare it.
Generate an ephemeral 256-bit AES key.
Wrap the target key using the ephemeral AES key with AES-KWP.
Wrap the AES key under the Vault wrapping key using RSAES-OAEP with MGF1 and either SHA-1, SHA-224, SHA-256, SHA-384, or SHA-512.
Delete the ephemeral AES key.
Append the wrapped target key to the wrapped AES key.
Base64 encode the result.
A specific code example for preparing and wrapping the key for import is beyond the scope of this tutorial. For more details about wrapping the key for import including instructions for wrapping key from an HSM, refer to the key wrapping guide.
Before you can wrap the key for import, you must read the wrapping key from Vault so that it can be used to prepare your key.
The output is the (4096-bit RSA) wrapping key.
Use the wrapping key value at step 3 in the previously detailed preparation steps. Once you have prepared and base64 encoded the ciphertext, export the value to the environment variable IMPORT_CIPHERTEXT
.
Example:
Import the key into a key named biometric-reader
. Imported keys do not support rotation by default, so include the allow_rotation
parameter and set its value to true
so that you can also try rotating the imported key.
Output:
Try using the newly imported key to encrypt some data.
The imported key is working, and the plaintext value was encrypted and returned as the value of the ciphertext
field.
Note
To import subsequent versions of the key, you must use the import_version API endpoint.
Let's take a look a the key information.
The key's latest_version
is currently 1.
Rotate the key.
Note
Once an imported key is rotated within Vault, it will no longer
support importing key material with the import_version
endpoint.
Check the key information once more.
The key's latest_version
is currently 2, and you can no longer import external versions of the key as it is now internally maintained by Vault.
Clean up
Unset the
VAULT_TOKEN
environment variable.Unset the
VAULT_ADDR
environment variable.Unset the
VAULT_NAMESPACE
environment variable.Unset the
APP_ORDER_TOKEN
environment variable.Unset the
IMPORT_CIPHERTEXT
environment variable.You can stop the Vault dev server by pressing Ctrl+C where the server is running. Or, execute the following command.