Batch tokens
Tokens are the core method for authentication within Vault. Tokens can be used directly or dynamically generated by the auth methods. Regardless, clients need valid tokens to interact with Vault.
There are two types of tokens: service tokens and batch tokens. The service tokens are persisted; therefore, they can be renewed or revoked before reaching their time-to-live (TTL) value. On the other hand, batch tokens are not persisted. They are encrypted binary large objects (blobs) that carry enough information for them to be used for Vault actions. Therefore, batch tokens are extremely lightweight and scalable; however, they lack most of the flexibility and features of service tokens. Batch tokens cannot be listed or manually revoked.
Service tokens vs. batch tokens
As the number of machines and apps using Vault for secret management scales, Vault must manage the growing number of client tokens. The creation of service tokens can affect Vault performance since they must be replicated across the primary and secondary clusters. On the other hand, batch tokens are neither persisted to disk nor live in memory; they are not a part of the data replication process.
Due to the lack of features with batch tokens, it's preferable to use service tokens in most use cases. However, think of a scenario where you have a large number of containers (e.g. 100,000s) start up and all request a token from Vault. The use of batch tokens can reduce the stress on the storage backend and improve the overall performance.
Batch tokens are designed to be lightweight with limited flexibility. The following table highlights the differences between batch tokens and service tokens.
Service tokens | Batch tokens | |
---|---|---|
Can be root tokens | Yes | No |
Can create child tokens | Yes | No |
Renewable | Yes | No |
Manually Revocable | Yes | No |
Can be periodic | Yes | No |
Can have explicit Max TTL | Yes | No (always uses a fixed TTL) |
Has accessors | Yes | No |
Has Cubbyhole | Yes | No |
Revoked with parent (if not orphan) | Yes | Stops Working |
Dynamic secrets lease assignment | Self | Parent (if not orphan) |
Can be used across Performance Replication clusters | No | Yes (if orphan) |
Creation scales with performance standby node count | No | Yes |
Cost | Heavyweight; multiple storage writes per token creation | Lightweight; no storage cost for token creation |
Cross cluster usage
To use a batch token across performance replication clusters, it has to be an orphan token since the secondary cluster will not be able to ensure the validity of its parent token.
Prerequisites
To perform the tasks described in this tutorial, you need a Vault environment. Refer to the Getting Started tutorial to install Vault.
Launch Terminal
This tutorial includes a free interactive command-line lab that lets you follow along on actual cloud infrastructure.
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
initialized and unsealed.
Insecure operation
Do not run a Vault dev server in production. This approach starts a Vault server with an in-memory database and runs in an insecure way.
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.
Note
For these tasks, you can use Vault's root token. However, it is recommended that root tokens are only used for enough initial setup or in emergencies. As a best practice, use an authentication method or token that meets the policy requirements.
The Vault server is ready.
Create batch tokens
Create a
test
policy.If you are not familiar with policies, complete the policies tutorial.
Create a batch token with the
test
policy attached, and set a TTL so the token is valid for 20 minutes.The generated token value starts with
b.
orhvb.
to indicate that it is a batch token. Refer to the Tokens for Vault version specific token prefix.Store the generated token in an environment variable,
BATCH_TOKEN
.Example:
Lookup the token details.
Example output:
Notice that
renewable
is set tofalse
.
Test batch tokens
Create some secrets in the Cubbyhole secrets engine.
Output:
Batch tokens do not have a cubbyhole associated with it.
Create a child token.
Batch tokens cannot create child tokens even if their policies have the capabilities to do so.
Try revoking the batch token.
The TTL values of batch tokens are fixed. In this example, the TTL is set to 20 minutes. After 20 minutes, the token expires and Vault will revoke it. Batch tokens cannot be renewed.
There are some trade-offs for using batch tokens; however, depending on your use case, batch tokens can significantly improve the performance of your Vault environment.
Apply batch tokens
Similar to what you did in the Tokens tutorial, use the AppRole auth method as an example to generate a batch token upon a successful login.
Enable the
approle
auth method if not already enabled.Create a role called "shipping", which generates a batch token with a TTL of 20 minutes.
Output:
Verification
Note
If you are not familiar with the AppRole auth method, read the AppRole Pull Authentication tutorial.
Retrieve the RoleID for the
shipping
role and save it to a file.Generate a SecretID for the
shipping
role and save it to a file as well.Authenticate with Vault using the generated
role_id
andsecret_id
. Store the generated client token in a file named,shipping_token.txt
.View the token properties.
Example output:
The output shows the
type
isbatch
.
Clean up
If you wish to clean up your environment after completing the tutorial, follow the steps in this section.
Unset the
BATCH_TOKEN
environment variable.Unset the
VAULT_TOKEN
environment variable.Unset the
VAULT_ADDR
environment variable.Delete the files used to test tokens.
If you are running Vault locally in
-dev
mode, you can stop the Vault dev server by pressing Ctrl+C where the server is running. Or, execute the following command.
Next steps
You learned the characteristics of batch tokens. Next, go through the Token management tutorial to learn the basic operational tasks for the Token auth method.