Vault: Managed Database Credentials
with Hashicorp Vault
About Me
相關資源
大綱
DB 帳號密碼管理的難題
Authentication 認證
you are who you say you are
Authorization 授權
you are allowed to do what you are trying to do
User Story
app(username=app, password) > DB
DB > hi, app!
難題/認證
對於資料庫來說,username=app的人,就是app
難題/認證/密碼保管與暴露
難題/認證/密碼暴露
User Story (v2)
Vault DB secret engine 支援,沒支援可以自己寫 plugin
Demo 1: Vault DB secret engine
Setup
git clone git@github.com:chechiachang/vault-playground.git
cd vault-playground/deploy/04-docker-and-db/
docker-compose up -d
docker ps
docker exec -it 04-docker-and-db-mariadb_1-1 bash
docker exec -it 04-docker-and-db-vault_1-1 sh
apk update && apk add mysql-client
mysql -h mariadb_1 -u root -p
Demo 1: Vault DB secret engine
// Connect to docker vault
export VAULT_ADDR=http://127.0.0.1:8200
vault status
vault login
vault secrets list
// Configure secret engine database (terraform)
vault secrets list
vault policy list
cd usage/03-terraform-lives
terragrunt init
terragrunt apply
Demo 1: Vault DB secret engine
Use database secret engine
// root configure (already done by terraform)
vault secrets list
vault list localhost_mariadb/config
vault read localhost_mariadb/config/localhost_mariadb
vault list localhost_mariadb/roles
vault read localhost_mariadb/roles/database_admin
vault policy list
vault policy read dba
// create token for dba (will automatically done in Demo 2)
vault token create -policy=dba -display-name=dba \
-ttl=1h -use-limit=5
Demo 1: Vault DB secret engine
// auth as dba
vault login
vault policy list
vault read localhost_mariadb/creds/database_admin
mysql --sql -h localhost -u <username> -p
// do dba things
show databases;
select user from mysql.user;
...
exit
解決存放與取用,回到難題/認證
對於資料庫來說,username=app的人,就是app
Vault 的解決方案
難題/認證/Vault Auth method
Vault 仰賴外部可信第三方(trusted authority)
開發機器上沒有密碼,就不會因為機器被駭/側錄,造成密碼洩漏
難題/認證/Vault Auth method/AWS
使用 Vault auth method,信任第三方取得身份 從雙方查核,變成三方查核
User Story (v3)
app > trusted authority
app < (identity) < trusted authority
app > (identity) > vault > trusted authority
app < (DB credential) < vault>
Demo 2: Auth method
https://developer.hashicorp.com/vault/docs/auth/github
// root
vault policy list
vault policy read sre
vault list auth/github-chechia-net/map/users
vault read auth/github-chechia-net/map/users/chechiachang
vault list auth/github-chechia-net/map/teams
vault read auth/github-chechia-net/map/teams/sre
vault auth help github-chechia-net
// user (github login)
vault login -method=github -path=github-chechia-net token=$TOKEN
vault read localhost_mariadb/creds/database_admin
Demo 2: Auth method
https://developer.hashicorp.com/vault/docs/auth/aws
// User login Public Cloud
aws-vault exec dev -- vault login -method=aws -path=aws=sre
gcloud ... vault login -method=gcp -path=gcp=sre
az login ... vault login -method=azure -path=azure=sre
// app login k8s
難題/認證/Vault Auth method/Kubernetes
app > sa -> k8s api server -> vault -> DB
難題/認證/Vault Auth method/小結
User Story (v?)
需求會一直改變
難題/授權
MySQL 為例,使用 sql 管理授權
user based access control
grant write on mydb.* to app@'%'
grant all on mydb.* to admin@'%'
grant read on mydb.* to metabase@'%'
role based access control
create role myrole;
grant all on mydb.* to myrole;
grant myrole to app@'%';
難題/授權/Scale up
難題/授權/Scale up
resource "mysql_role" "myrole" {
for_each = var.roles
name = each.key
}
resource "mysql_grant" "developer" {
for_each = var.roles
role = each.key
database = each.value.database
privileges = each.value.privilleges // ["SELECT", "UPDATE"]
}
難題/授權/PR Review
難題/授權/Automation
Vault 管理授權
vault policy list
database_admin
database_readonly
database_write
vault policy read database_write
path "chechia-net-myapp/database/database" {
capabilities = ["create", "read", "update", "patch", "delete", "list"]
}
Demo 3: Terraform 設定 Vault 與 DB secret engine
// destroy
cd vault-playground/usage/03-terraform-lives
terragrunt destroy
cd vault-playground/deploy/04-docker-and-db/
docker-compose down
// create new
docker-compose up -d
docker ps
cd vault-playground/usage/03-terraform-lives
terragrunt apply
Demo 3: Terraform 設定 Vault 與 DB secret engine
總結