Learn to use the Vault CLI
You can use the API, command line interface (CLI), or web user interface (UI) to interact with Vault. The Vault CLI is available for common architectures and operating systems.
Scenario
(Persona: Developer)
Danielle is on the development team, and builds applications and plugins which interface with Vault. They installed the Vault binary on their computer, and set up a dev mode server for development and testing.
You can assume Danielle's role to learn about using the Vault CLI with a dev server by checking the server status, and enabling an auth method and secrets engine.
Prerequisites
To complete this tutorial, you need the following:
- Vault binary installed and configured in your system PATH.
Set up the lab
Open a terminal and start a Vault dev server with the literal string
root
as the root token value, and enable TLS.The dev server listens on the loopback interface at 127.0.0.1 on TCP port 8200 with TLS enabled. At runtime, the dev server also automatically unseals, and prints the unseal key and initial root token values to the standard output.
Root tokens
The dev mode server starts with an initial root token value set.
Root token use should be extremely guarded in production environments because they enable full access to the Vault server.
You use the root token here for convenience, and to keep the tutorial steps focused on what you'll learn.
In a new terminal, export the
VAULT_ADDR
andVAULT_CACERT
environment variables using the commands suggested in your Vault dev server output.Copy each command (without the
$
) from the server output, and paste it into the new terminal session.Example:
Example:
Remember to use your dev server's values, not the examples shown here.
Tip
Exporting the configuration as environment variables is convenient, but you can also configure the same information through the CLI flags
-address
and-ca-cert
. You can learn more about these flags in theHTTP Options
section of the help output.Authenticate with Vault by logging in with the initial root token value.
Example output:
The Vault server is ready.
Check the server status
It's always a good idea to check your server status after starting Vault to ensure that it is available for use, and operating under the desired configuration.
You can get a quick summary of the
status
sub-command by using an abbreviated form of the--help
output.Example output:
Check the your dev server status.
Example output:
You can learn from this output that the server is active, initialized, and unsealed. You can also learn about the server configuration, including the Vault version, storage type, cluster name, and more.
You learned earlier that the Vault CLI can also output JSON. Try getting the server status, but requesting that Vault return the output as a JSON object.
Example output:
Vault returns the server status information as a JSON object.
The Vault dev server is ready for use.
Understand the Vault CLI
Vault features a command-line interface (CLI) that wraps API functionality, and formats output in a tabular style by default. If you've followed along since the beginning of the tutorial, you learned that the CLI can also output JSON.
The Vault CLI functionality is part of the same single static binary that powers the server and agent. It is a thin wrapper around the HTTP API, and the CLI commands internally map directly to the HTTP API.
The CLI command features a range of sub-commands, accepts options in the form of flags or environment variables, and handles arguments provided by the user.
The general command syntax is:
Open a new terminal session if necessary, and try some commands.
Try getting the Vault version.
Example output:
The output includes Vault version, the binary SHA summary, and build timestamp.
Tip
This example uses the version
sub-command, but you can also pass the --version
flag to vault
, and get the same output.
Try getting help.
Example output:
Vault provides a listing of common sub-commands and other sub-commands. The first 4 common sub-commands are the most commonly used for most CLI users.
You can get help for any of these commands by invoking the sub-command with the --help
flag. Tokens are an essential authentication idea in Vault, so try getting help for the token
sub-command.
Example output:
Help output provides usage format, and examples for the token
sub-command's own commands to create, look up, renew, revoke, or learn the capabilities for a given token identifier.
What command-line flag can you pass to the Vault CLI to get more information about a command or sub-command?
You can use --help
to get more information about a command or subcommand.
Enable and configure an auth method
You can use the CLI auth
sub-command to enable an auth method.
Before you enable an auth method, check out the help for
auth
.Example output:
Danielle needs an instance of the userpass auth method. With it they can authenticate to Vault with a username and password, and later get a secret value.
Enable the userpass auth method.
Example output:
List the enabled auth methods.
Example output:
By default, Vault enables all auth methods under the path
/auth/
. In this case, you enabled an instance of the userpass auth method at the pathuserpass/
, so the fully qualified path to this auth method becomes/auth/userpass
.Tip
You can override the default path for enabled auth methods and secrets engines to specify your own path with the
-path
flag.Vault also assigns this auth method instance a unique accessor identifier, in this case
auth_userpass_53c1801f
. You can use this accessor to refer to this auth method instance when you use other commands.
The power of path-help
The Vault CLI features extensive self-documentation. This extends even to the paths created as the result of enabling auth methods and secrets engines. As explained earlier, you enabled a new instance of the userpass auth method with the defaults, so this auth method path is /auth/userpass
.
You can use the path-help
sub-command to learn more about what is possible at this path by passing it in as an argument like this.
The output includes help for all possible operations against this path:
Example output:
Create the ACL policy
When Danielle authenticates to Vault with the userpass auth method, Vault can attach access control list policies (ACL policies) to tokens that it issues to them.
In this case, Danielle needs the ability to create, list, read, and update new key/value secrets at the path dev-secrets
. You write ACL policies in HashiCorp Configuration Language (HCL), as shown in this example:
Policy helper
Vault CLI offers the -output-policy
flag to help identify necessary policy to
execute an operation.
For example, the following command returns the policy required to read secrets
from dev-secrets/creds
.
Example output:
The command returns the minimum policy required, and it will not include wildcards.
Write the policy to Vault with the name
developer-vault-policy
.Example output:
Once you've created an ACL policy and enabled a userpass auth method, you can create users who can authenticate using the
write
sub-command.Create a user with the username
danielle-vault-user
, and set their password toFlyaway Cavalier Primary Depose
.Example output:
Enable and configure a secrets engine
You have learned quite a bit about the CLI by enabling a userpass auth method, creating an ACL policy, and creating a user. In this section, your goal is to enable an instance of the key/value secrets engine, and create a secret.
You can learn about the
secrets
command from help.Example output:
List the enabled secrets engines before you enable a secrets engine instance.
Example output:
A Vault server starts with some secrets engines enabled by default, including the
cubbyhole
,identity
, andsys
secrets engines. Since this is a dev server, Vault also enables a default instance of the key/value secrets engine, version 1.Enable a new instance of the version 2 key/value secrets engine, which features secret versioning at the path
/dev-secrets
.Example output:
You've enabled the auth method, created an ACL policy, and enabled secrets engine all with the power of the initial root token. You're now ready to authenticate with Vault as
danielle-vault-user
, and to try creating a new secret indev-secrets
.
Authenticate and create secret
Up to this point, you have been using the root token. You can use the login
sub-command to authenticate with the userpass auth method as danielle-vault-user
.
Get help for the
login
sub-command.The help for
login
is quite long, so this abbreviated version of the output shows only the usage, output, and command options.Abbreviated example output:
Authenticate with Vault using the userpass auth method as
danielle-vault-user
.Vault prompts you to input your password:
Enter your password (
Flyaway Cavalier Primary Depose
) at the prompt. Vault returns login details.Example output:
The output includes the actual token identifier, the token accessor, and details of the token's lifecycle along with metadata and policies.
Vault is flexible in accepting input and displaying output. The following example shows authentication with Vault using the same userpass auth method and user, but by specifying the password as an argument and instructing Vault to not print any output after the login.
You're authenticated to Vault as
danielle-vault-user
.You can put a new secret in the secrets engine at the path
/dev-secrets
with thekv
command. Check the help forkv
.Example output:
Try to put a secret named
creds
in the key/value secrets engine at the path/dev-secrets
with theput
sub-command.You can use the
get
subcommand to get the secret details.
Clean up
Use CTRL+C
to stop the server process in the terminal window where you started
the server, or use this command to kill the server process from any local
terminal session:
Unset the environment variables.
Summary
The Vault CLI allows you to both manage your Vault cluster, and interact with
Vault as a consumer. The built in help
command provides more context for
specific subcommands and their required parameters.