What is Vault?
Vault is a tool for securely accessing secrets. A secret is anything that you want to tightly control access to, such as API keys, passwords, certificates, and more. Vault provides a unified interface to any secret, while providing tight access control and recording a detailed audit log.
A modern system requires access to a multitude of secrets: database credentials, API keys for external services, credentials for service-oriented architecture communication, etc. Understanding who is accessing what secrets is already very difficult and platform-specific. Adding on key rolling, secure storage, and detailed audit logs is almost impossible without a custom solution. This is where Vault steps in.
The key features of Vault are:
1) Secure Secret Storage
2) Dynamic Secrets
3) Data Encryption
4) Leasing and Renewal
5) Revocation
Terms used in Vault
- Storage Backend - A storage backend is responsible for durable storage of encrypted data. Backends are not trusted by Vault and are only expected to provide durability. The storage backend is configured when starting the Vault server.
- Barrier - The barrier is cryptographic steel and concrete around the Vault. All data that flows between Vault and the Storage Backend passes through the barrier.
- Secret Backend - A secret backend is responsible for managing secrets.
- Audit Backend - An audit backend is responsible for managing audit logs. Every request to Vault and response from Vault goes through the configured audit backends.
- Credential Backend - A credential backend is used to authenticate users or applications which are connecting to Vault. Once authenticated, the backend returns the list of applicable policies which should be applied. Vault takes an authenticated user and returns a client token that can be used for future requests.
- Client Token - A client token is a conceptually similar to a session cookie on a web site. Once a user authenticates, Vault returns a client token which is used for future requests. The token is used by Vault to verify the identity of the client and to enforce the applicable ACL policies. This token is passed via HTTP headers.
- Secret - A secret is the term for anything returned by Vault which contains confidential or cryptographic material. Not everything returned by Vault is a secret, for example system configuration, status information, or backend policies are not considered Secrets.
- Server - Vault depends on a long-running instance which operates as a server. The Vault server provides an API which clients interact with and manages the interaction between all the backends, ACL enforcement, and secret lease revocation. Having a server based architecture decouples clients from the security keys and policies, enables centralized audit logging and simplifies administration for operators.
Vault Architecture
A very high level overview of Vault looks like this:
![](https://www.vaultproject.io/assets/images/layers-368ccce4.png)
There is a clear separation of components that are inside or outside of the security barrier. Only the storage backend and the HTTP API are outside, all other components are inside the barrier.
The storage backend is untrusted and is used to durably store encrypted data. When the Vault server is started, it must be provided with a storage backend so that data is available across restarts. The HTTP API similarly must be started by the Vault server on start so that clients can interact with it.
Once started, the Vault is in a sealed state. Before any operation can be performed on the Vault it must be unsealed. This is done by providing the unseal keys. When the Vault is initialized it generates an encryption key which is used to protect all the data. That key is protected by a master key. By default, Vault uses a technique known as Shamir's secret sharing algorithm to split the master key into 5 shares, any 3 of which are required to reconstruct the master key.
![Keys](https://www.vaultproject.io/assets/images/keys-468a5903.png)
The number of shares and the minimum threshold required can both be specified. Shamir's technique can be disabled, and the master key used directly for unsealing. Once Vault retrieves the encryption key, it is able to decrypt the data in the storage backend, and enters the unsealed state. Once unsealed, Vault loads all of the configured audit, credential and secret backends.
The configuration of those backends must be stored in Vault since they are security sensitive. Only users with the correct permissions should be able to modify them, meaning they cannot be specified outside of the barrier. By storing them in Vault, any changes to them are protected by the ACL system and tracked by audit logs.
After the Vault is unsealed, requests can be processed from the HTTP API to the Core. The core is used to manage the flow of requests through the system, enforce ACLs, and ensure audit logging is done.
When a client first connects to Vault, it needs to authenticate. Vault provides configurable credential backends providing flexibility in the authentication mechanism used. Human friendly mechanisms such as username/password or GitHub might be used for operators, while applications may use public/private keys or tokens to authenticate. An authentication request flows through core and into a credential backend, which determines if the request is valid and returns a list of associated policies.
Policies are just a named ACL rule. For example, the "root" policy is built-in and permits access to all resources. You can create any number of named policies with fine-grained control over paths. Vault operates exclusively in a whitelist mode, meaning that unless access is explicitly granted via a policy, the action is not allowed. Since a user may have multiple policies associated, an action is allowed if any policy permits it. Policies are stored and managed by an internal policy store. This internal store is manipulated through the system backend, which is always mounted at
sys/
.Once authentication takes place and a credential backend provides a set of applicable policies, a new client token is generated and managed by the token store. This client token is sent back to the client, and is used to make future requests. This is similar to a cookie sent by a website after a user logs in. The client token may have a lease associated with it depending on the credential backend configuration. This means the client token may need to be periodically renewed to avoid invalidation.
Once authenticated, requests are made providing the client token. The token is used to verify the client is authorized and to load the relevant policies. The policies are used to authorize the client request. The request is then routed to the secret backend, which is processed depending on the type of backend. If the backend returns a secret, the core registers it with the expiration manager and attaches a lease ID. The lease ID is used by clients to renew or revoke their secret. If a client allows the lease to expire, the expiration manager automatically revokes the secret.
The core handles logging of requests and responses to the audit broker, which fans the request out to all the configured audit backends. Outside of the request flow, the core performs certain background activity. Lease management is critical, as it allows expired client tokens or secrets to be revoked automatically. Additionally, Vault handles certain partial failure cases by using write ahead logging with a rollback manager. This is managed transparently within the core and is not user visible.
Steps to Install Vault
1) Installing Vault is simple. There are two approaches to installing Vault: downloading a precompiled binary for your system, or installing from source. We will use the precompiled binary format. To install the precompiled binary, download the appropriate package for your system.
2) You can use the following command as well: wget https://releases.hashicorp.com/vault/0.6.0/vault_0.6.0_linux_amd64.zip
Unzip by the command unzip vault_0.6.0_linux_amd64.zip
You will have a binary called vault in it.
3) Once the zip is downloaded, unzip it into any directory. The
vault
binary inside is all that is necessary to run Vault . Any additional files, if any, aren't required to run Vault.Copy the binary to anywhere on your system. If you intend to access it from the command-line, make sure to place it somewhere on your
PATH
.4) Add the path of your vault binary to your .bash_profile file in your home directory.
Execute the following to do it vi ~/bash_profile
export PATH=$PATH:/home/compose/vault (If your vault binary is in /home/compose/vault/ directory)
Alternatively you can also add the unzipped vault binary file in /usr/bin so that you will be able to access vault as a command.
Verifying the Installation
To verify Vault is properly installed, execute the
vault
binary on your system. You should see help output. If you are executing it from the command line, make sure it is on your PATH or you may get an error about vault
not being found.Starting and configuring vault
1) Vault operates as a client/server application. The Vault server is the only piece of the Vault architecture that interacts with the data storage and backends. All operations done via the Vault CLI interact with the server over a TLS connection.
2) Before starting vault you will need to set the following environment variable VAULT_ADDR. To set it execute the following command export VAULT_ADDR='http://127.0.0.1:8200'. 8200 is the default port for vault. You can set this environment variable permanently across all sessions by adding the following line in /etc/environment- VAULT_ADDR='http://127.0.0.1:8200'
3) The dev server is a built-in flag to start a pre-configured server that is not very secure but useful for playing with Vault locally.
To start the Vault dev server, run
vault server -dev
$ vault server -dev
WARNING: Dev mode is enabled!
In this mode, Vault is completely in-memory and unsealed.
Vault is configured to only have a single unseal key. The root
token has already been authenticated with the CLI, so you can
immediately begin using the Vault CLI.
The only step you need to take is to set the following
environment variable since Vault will be talking without TLS:
export VAULT_ADDR='http://127.0.0.1:8200'
The unseal key and root token are reproduced below in case you
want to seal/unseal the Vault or play with authentication.
Unseal Key: 2252546b1a8551e8411502501719c4b3
Root Token: 79bd8011-af5a-f147-557e-c58be4fedf6c
==> Vault server configuration:
Log Level: info
Backend: inmem
Listener 1: tcp (addr: "127.0.0.1:8200", tls: "disabled")
...
You should see output similar to that above. Vault does not fork, so it will continue to run in the foreground; to connect to it with later commands, open another shell.
As you can see, when you start a dev server, Vault warns you loudly. The dev server stores all its data in-memory (but still encrypted), listens on localhost without TLS, and automatically unseals and shows you the unseal key and root access key. The important thing about the dev server is that it is meant for development only. Do not run the dev server in production. Even if it was run in production, it wouldn't be very useful since it stores data in-memory and every restart would clear all your secrets. You can practise vault read/write commands here. We won't be using vault in dev mode as we want our data to stored permanently.
In the next steps you will see how to start and configue a durable vault server.
3) Now you need to make a hcl file to add the configurations of vault in it.
HCL (HashiCorp Configuration Language) is a configuration language built by HashiCorp. The goal of HCL is to build a structured configuration language that is both human and machine friendly for use with command-line tools, but specifically targeted towards DevOps tools, servers, etc. HCL is also fully JSON compatible. That is, JSON can be used as completely valid input to a system expecting HCL. This helps makes systems interoperable with other systems. HCL is heavily inspired by libucl, nginx configuration, and others similar. you can find more details about HCL on https://github.com/hashicorp/hcl
4) You will need to mention a physical backend for the vault. There are various options in the physical backend.
The only physical backends actively maintained by HashiCorp are
consul
, inmem
, and file.
consul
- Store data within Consul. This backend supports HA. It is the most recommended backend for Vault and has been shown to work at high scale under heavy load.etcd
- Store data within etcd. This backend supports HA. This is a community-supported backend.zookeeper
- Store data within Zookeeper. This backend supports HA. This is a community-supported backend.dynamodb
- Store data in a DynamoDB table. This backend supports HA. This is a community-supported backend.s3
- Store data within an S3 bucket S3. This backend does not support HA. This is a community-supported backend.azure
- Store data in an Azure Storage container Azure. This backend does not support HA. This is a community-supported backend.swift
- Store data within an OpenStack Swift container Swift. This backend does not support HA. This is a community-supported backend.mysql
- Store data within MySQL. This backend does not support HA. This is a community-supported backend.postgresql
- Store data within PostgreSQL. This backend does not support HA. This is a community-supported backend.inmem
- Store data in-memory. This is only really useful for development and experimentation. Data is lost whenever Vault is restarted.file
- Store data on the filesystem using a directory structure. This backend does not support HA.
Each of these backend has a different options for configuration. for simplicity we will be using file backend here. A sample fhcl file can be:
You can save the following file with any name but with .hcl extension for example config.hcl which we will store in /home/compose/data/ folder.
backend "file" {
path = "/home/compose/data"
}
listener "tcp" {
address = "0.0.0.0:8200"
tls_disable = 1
}
backend "file" specifies that the data produced by the vault will be stored in a file format
path specifies that the files will be stored in can be any folder.
listener will be tcp
address specifies that which machines will be able to access vault. 127.0.0.1:8200 will give access for requests only from localhost. 8.8.8.8:8200 or 0.0.0.0:8200 will give access to vault from anywhere.
tls_disable will be 1 if you are not providing any SSL certificates for authentication from client.
This is the basic file which you can use.
5) Start your vault server with the following command
vault server -config=/home/compose/data/config.hcl
point the -config to the config hcl file you just created. You need to run this command either as root or with sudo. All the next commands can be run by either root or compose without sudo.
6) If you have started your vault server for the first time then you will need to initialize it. Run the following command
vault init This will give an output as follows:
Unseal Key 1: a33a2812dskfybjgdbgy85a7d6da375bc9bc6c137e65778676f97b3f1482b26401
Unseal Key 2: fa91a7128dfd30f7c500ce1ffwefgtnghjj2871f3519773ada9d04bbcc3620ad02
Unseal Key 3: bb8d5e6d9372c3331044ffe678a4356912035209d6fca68f542f52cf2f3d5e0203
Unseal Key 4: 8c5977a14f8da814fa2f204ac5c2160927cdcf354fhfghfgjbgdbbb0347e4f8b04
Unseal Key 5: cd458edecf025bd02f6b11b3e43341dgdgewtea77756fagh6dc0ba4d775d312405
Initial Root Token: f15db23h-eae6-974f-45b7-se47u52d96ea
Vault initialized with 5 keys and a key threshold of 3. Please
securely distribute the above keys. When the Vault is re-sealed,
restarted, or stopped, you must provide at least 3 of these keys
to unseal it again.
Vault does not store the master key. Without at least 3 keys,
your Vault will remain permanently sealed.
Save these someplace safe as you will need this everytime you unseal your vault server to write or access data. By default you will need to enter any three of the five unseal key to unseal the vault completely.
You can refer to the architecture above for understanding the working of keys. VaultArchitecture
But if you want to change the default and want to use only one key then you can initialize the vault as: vault init -key-share=1 -key-threshold=1 which will generate only one unseal key.
7) After initializing your vault next step you have to do is unseal your vault, otherwise you won't be able to perform any operations on the vault. Execute the following command:
vault unseal you will be asked for an unseal key enter any one of the unseal keys generated during initialization.By default vault needs three keys out of five to be completely unsealed.
See the screenshot below:
![2016-07-22 10_35_28-172.16.120.138 - Remote Desktop - __Remote](https://apassionatechie.files.wordpress.com/2017/03/2016-07-22-10_35_28-172-16-120-138-remote-desktop-__remote.png)
You can check that the Unseal Progress is 1. That means your first key was correct. The Unseal Progress count will increase everytime you execute unseal and enter a key.
So you will need to repeat the above step in total of three times. Entering a different key each time.
![2016-07-22 10_36_38-172.16.120.138 - Remote Desktop - __Remote](https://apassionatechie.files.wordpress.com/2017/03/2016-07-22-10_36_38-172-16-120-138-remote-desktop-__remote.png)
In the end of third time your vault will be completely unsealed.
8) You will now need to login into the vault server to read/write into the vault. Execute the following command to login.
vault auth
Where is token given to you when you initialised the vault. It is present after the five keys. This gives you the root access to vault to perform any activities.
Vault Commands
1) vault status to get the status of vault whether it is running or not.
2) vault write secret/hello excited=yes to write a key-value pair into the vault. where secret/hello is path to access your key. "excited" is your key-name and "yes" is the value. Key and value can be anything.
3) vault read secret/hello to read the value of the key you just wrote.
4) vault write secret/hello excited=very-much to change/update the value of your key
5) vault write secret/hello excited=yes city=Pune to add multiple keys. you can just separate them with space.
6) vault write secret/hello abc=xyz will remove the existing keys (excited and city and create a new one abc)
7) vault read -format=json secret/hello return keys and values in json
8) vault delete secret/hello to delete your path.
9) If you don't want your path start with secret/ then you can mount other backend like generic.
Execute vault mount generic Then you will be able to add paths like generic/hello instead to secret/hello. You can get more info on secret backends here https://www.vaultproject.io/docs/secrets/index.html
10) vault mounts to see the list of mounts
11) vault write generic/hello world=Today to write to newly mounted secret backend.
12) vault read generic/hello to read it.
13) vault token-create with this vault will create a token which you can give to a user so that he an login to the vault. This will add a new user to your server.
The new user can login with vault auth . You can renew or revoke the user with vault renew or vault revoke
To add a user with username and password and not with token use the following commands.
14) vault auth-enable userpass
vault auth -methods //This will display the authentication methods you should see userpass in this
vault write auth/userpass/users/user1 password=Canopy1! policies=root //To add username user1 with password Canopy1! will have root policy attached to it.
vault auth -method=userpass username=compose password=Canopy1! //user can login with this
To add read-only policy to a user execute the following commands
15) Create a file with extension .hcl. Here I have created read-only.hcl
path "secret/*" {
policy = "read"
}
path "auth/token/lookup-self" {
policy = "read"
}
vault policy-write read-policy read-only.hcl //to add the policy named read-policy from file read-only.hcl
vault policies //to display list of policies
vault policies read-policy //to display newly created policy
vault write auth/userpass/users/read-user password=Canopy1! policies=read-policy //to add user with that policy
Now if the new user logs in he/she won't be able to write anything in vault and just read it.
16) vault audit-enable file file_path=/home/compose/data/vault_audit.log //This will add the logs in vault_audit.log file.
Configure vault and AMP
You can add the following lines in brooklyn.properties to access the vault key-values
brooklyn.external.vault=org.apache.brooklyn.core.config.external.vault.VaultUserPassExternalConfigSupplier
brooklyn.external.vault.username=user1 //Login username you created
brooklyn.external.vault.password=Canopy1! //Login password
brooklyn.external.vault.endpoint=http://172.16.120.159:8200/ //Ip address of your vault server
brooklyn.external.vault.path=secret/CP0000/AWS //Path to your secrets
brooklyn.location.jclouds.aws-ec2.identity=$brooklyn:external("vault", "identity")
brooklyn.location.jclouds.aws-ec2.credential=$brooklyn:external("vault", "credential")
This will make AMP access your creds from vault.
Backup and recovery
All of the required vault data is present in the folder you mentioned in your config.hcl as path variable here /home/compose/data. So just take backup of the folder and paste that folder into the recovered machine. A prerequisite is that vault binary should be present in that machine.
Backup can be taken via cronjob as
0 0 * * * rsync -avz --delete root@vault:/home/compose/data /backup/vault/
[…] First install and configure vault using this tutorial: https://apassionatechie.wordpress.com/2017/03/05/hashicorp-vault/ […]
ReplyDelete