All guides

HashiCorp Vault: what to set up first

A new Vault server starts sealed, and initialization hands out one root token that can do anything. Set up storage, the seal, auditing, authentication and policies before the first application secret goes in.

A laptop on a desk beside stacks of books and a lamp

Storage and the seal

A Vault server starts in a sealed state. It has access to the physical storage and no way to decrypt the data stored there. To unseal Vault is to obtain the plaintext root key, which Vault needs in order to read the decryption key.

There are two ways to unseal:

  • Shamir seal. The default configuration splits the unseal key into shares with Shamir's Secret Sharing. Vault requires a threshold of shares to reconstruct the key, and operators add shares one at a time. With multiple nodes, you must unseal each node.
  • Auto unseal. A trusted device or service takes over the job of protecting the unseal key from Vault. When Vault starts, it contacts that device or service and asks it to decrypt the root key kept in storage.

Integrated storage stores Vault's data on the server's filesystem and does not rely on any third-party systems. All the nodes in a cluster have a replicated copy of the data.

HCL

storage "raft" {
  path    = "/opt/vault/data"
  node_id = "vault-1"
}

seal "awskms" {
  region     = "us-east-1"
  kms_key_id = "<kms-key-id>"
}

cluster_addr = "https://vault-1.example.com:8201"

The path, node ID, address, Region and key ID are yours to choose. cluster_addr is required with integrated storage. HashiCorp strongly recommends setting disable_mlock to true and disabling memory swapping on the system.

For the AWS KMS seal, HashiCorp strongly recommends passing credentials through environment variables. When none are provided, the AWS SDK falls back to shared file credentials or to IAM role credentials. Vault needs kms:Encrypt, kms:Decrypt and kms:DescribeKey on the key.

A majority of the voting nodes must be alive for integrated storage to continue to operate.

Initialization and unseal keys

Shell

vault operator init -key-shares=5 -key-threshold=3
vault operator unseal

You only need to initialize one Vault server to initialize the storage backend. Five shares and a threshold of three are the defaults of the two flags. With the -pgp-keys flag, the generated unseal keys are encrypted with the PGP keys you list.

Run vault operator unseal with no arguments and it prompts for the key. Passing the key as an argument is not recommended, because the key is then available in your shell history.

With auto unseal, initialization yields recovery keys in place of unseal keys. A recovery key cannot decrypt the root key. With the auto unseal mechanism out of order, recovery keys are therefore not enough to unseal Vault. If the KMS key becomes unavailable or is deleted, you cannot recover access to the cluster until the key is available again. Protect that key from deletion.

Audit devices before any secret

Shell

vault audit enable file file_path=/var/log/vault_audit.log

The log path is the one in HashiCorp's example, so choose your own. Audit devices write a detailed record of every API request and response, apart from a small set of exceptions. A device is a file, a syslog server or a socket. By default, Vault writes only a keyed hash (HMAC-SHA256) of most string values to the audit log.

With audit devices enabled, Vault must be able to log to at least one of them. When it cannot, it refuses the API request. HashiCorp's recommendation on the same page is to enable at least two audit devices.

Auth methods and policies

Auth methods perform authentication and assign an identity and a set of policies to a user. All auth methods are mounted underneath the auth/ prefix, at auth/<type> by default.

A policy denies by default, which means an empty one grants nothing. This policy follows HashiCorp's example for the KV version 2 engine, with a path of your own:

HCL

# my-app.hcl
path "secret/data/my-app/*" {
  capabilities = ["read"]
}

path "secret/metadata/my-app/*" {
  capabilities = ["list"]
}

Shell

vault auth enable userpass
vault policy write my-app my-app.hcl

userpass is the method that the documentation enables as its example.

The KV version 2 engine

Shell

vault secrets enable -path=secret -version=2 kv
vault kv put -mount=secret creds passcode=my-long-passcode
vault kv get -mount=secret creds
vault kv get -mount=secret -version=1 creds

The mount, the path creds and the key come from the documentation's examples. The kv secrets engine stores and versions static secrets. Version 2 uses soft deletes, which make data inaccessible while allowing data recovery.

The -mount flag exists because of one detail. The full path of a KV version 2 secret, the one used in policies and raw API calls, contains a nested /data/ element: secret/data/creds.

The root token

Root tokens have the root policy attached and can do anything in Vault. vault operator init generates the initial root token, and that token never expires. The Vault team's recommendation for root tokens: use one only for the initial setup that requires it or in an emergency, and revoke it as soon as that need has passed.

Before you revoke it, confirm that an administrator can sign in through an auth method. Then run vault token revoke with the root token as its argument. If a root token is needed again, vault operator generate-root creates one with the permission of a quorum of unseal key holders.

What to check before production

  1. vault operator raft list-peers shows the current peer set, and vault operator raft snapshot save writes a snapshot that you have restored in a test.
  2. At least two audit devices are enabled.
  3. Unseal or recovery key shares are held by different people.
  4. The initial root token is revoked.
  5. Each application has its own policy.
  6. Access to the KMS key behind auto unseal is limited to Vault.

Vault's LICENSE file names the Business Source License 1.1.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides