Securing Databases with Dynamic Credentials and HashiCorp Vault
Presented by Thomas Kula, Sr. Solutions Engineer
6 February 2019
Solutions Engineering Hangout: Securing Databases with Dynamic Credentials and HashiCorp Vault
This repository consists of two parts:
- The scripts ran in the HashiHang which demonstrate some basic functionality of Vault's database secrets engine
- some Terraform configuration to provision the resources used by the demo
The demo is geared towards running on an Amazon Linux system using an AWS RDS MySQL database, but with slight modification should be usable on any Unix-like system talking to any MySQL database engine.
NOTE: Using this will resources in AWS which will have a small but non-existent cost.
If you wish to replicate the demo environment, the Terraform code in the terraform
directory will allow you to do so.
- Set the appropriate environment variables which give Terraform access to your AWS account
- Copy
terraform.auto.tfvars.example
toterraform.auto.tfvars
and set the appropriate values in there terraform init
terraform plan -out=tf.plan
terraform apply tf.plan
This will create an AWS RDS MySQL database and a small Amazon Linux EC2 instance
on which to run the demo. It will create a demo_env.sh
file in the home
directory of the ec2-user
user, which will be used in the 'Running' section.
You will need a Linux system and a MySQL database. Edit the demo_env.sh
file
in this repository to point at your MySQL database, and copy it to the home
directory of the user on the machine you will be running the demo.
- Check out this repository, and switch to the
demo-scripts
directory cp ~/demo_env.sh .
from either the 'Using Terraform' or 'Manual Setup' sections
This script:
- Updates the Amazon Linux system
- Installs the
mysql
client andjq
utility - Downloads
vault
andconsul-template
and puts them in thebin
directory indemo-scripts
If you do a manual setup, you can skip these steps, but the rest of the demo assumes that steps 2 and 3 have been done.
To run:
./001-setup
This script:
- Downloads the Sakila Test Database just so we have some data to work with
- Creates a
.my-admin.cnf
file for the database administrative user
To run:
./002-setup
This script:
- Starts Vault in dev mode, putting the PID into
vault.pid
and the Vault log intovault.log
.
To run:
./003-setup
This script:
- Sets up the Vault database secrets engine
- Configures it to talk to our test database
- Creates two roles with different grants for use with that test database
To run:
./004-setup-database-secrets-engine
- The script will print what it's doing and pause, to continue hit
This script:
- Generates basic credentials for both roles created in '004-setup-database-secrets-engine' and demonstrates their usage
To run:
./005-basic-credentials
- The script will print what it's doing and pause, to continue hit
This script:
- Shows how Vault policy enforces which entities can generate credentials for the two created database roles
To run:
./006-policy-enforcement
- The script will print what it's doing and pause, to continue hit
Previous scripts used either the Vault CLI or directly called Vault's RESTful API. Many existing applications do not have that integration, so we demonstrate how consul-template can be used to pull dynamic secrets out of Vault and render them in a file which existing applications can read just like any other configuration file.
To run:
./007-consul-template
- The script will print what it's doing and pause, to continue hit
This script shows some of the advantages of using dynamic credentials: short lived, per-instance secrets. We show a scenario where a single instance was affected, perhaps it accidentially logged its database credentials, and how you can revoke that single instance's access while still leaving the rest of your infrastructure unaffected. We then show how you can revoke an entire class of secrets as a kind of emergency, 'break glass' process.
To run:
./008-lease-expiration
- The script will print what it's doing and pause, to continue hit
This script stops the Vault dev instance.
To run:
./999-cleanup