Che-Chia Chang
SRE @ Maicoin
Microsoft MVP
Kubernetes, Public Cloud
@Golang Taipei
@CNCF Taiwan
@iThome Summit
@DevOps Taipei
API server > Password/Username > Database
Virtual Machine > API Key > Third Party Service
K8s Pod > Private Key > Block Chain Service
API server > Password/Username > Database
Virtual Machine > API Key > Third Party Service
K8s Pod > Private Key > Block Chain Service
# everyone use k8s, right?
apiVersion: v1
kind: Pod
metadata:
name: my-pod-need-secret
spec:
containers:
- name: my-container-need-secret
command:
- /bin/do-some-thing
env:
- name: MYSQL_HOST
value: mysql.stag.chechia.com
- name: MYSQL_PORT
value: "3306"
- name: MYSQL_USERNAME
value: admin
- name: MYSQL_PASSWORD
value: my_password_in_plain_text
apiVersion: v1
kind: Pod
metadata:
name: my-pod-need-secret
spec:
containers:
- name: my-container-need-secret
command:
- /bin/do-some-thing
env:
- name: MYSQL_USERNAME
valueFrom:
secretKeyRef:
name: mysql-secret
key: MYSQL_USERNAME
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: MYSQL_PASSWORD
apiVersion: v1
kind: Secret
metadata:
name: mysql-secret
type: Opaque
data:
username: YWRtaW4=
password: bXlfcGFzc3dvcmRfaW5fcGxhaW5fdGV4dA==
# encoded. looks better?
echo YWRtaW4= | base64 -d
admin%
echo bXlfcGFzc3dvcmRfaW5fcGxhaW5fdGV4dA== | base64 -d
my_password_in_plain_text%
# encoded is not encrypted !!!
# base64 encoded is basically plain text
apiVersion: bitnami.com/v1alpha1
kind: SealedSecret
metadata:
name: mysql-secret
spec:
encryptedData:
MYSQL_USERNAME: AgBy3i4OJSWK+PiTySYZZA9rO43cGDEq.....
MYSQL_PASSWORD: AgBy3i4OJSWK+PiTySYZZA9rO43cGDEq.....
API server > Password/Username > Database
Virtual Machine > API Key > Third Party Service
K8s Pod > Private Key > Block Chain Service
K8s Pod > Some SAFE Magic > get secret
https://www.youtube.com/watch?v=VYfl-DpZ5wM
export VAULT_ADDR=https://vault.chechia.net
# Auth method: Token
VAULT_TOKEN=s.abcdefgh12345678
vault login ${VAULT_TOKEN}
Key Value
--- -----
token s.1234567890abcdefghijk
token_duration 5m
policies ["default" "user"]
# Read key from path
vault read /user/mysql
Key Value
--- -----
username my-username
password my-secret-password
# Read key without permission
vault read /admin/mysql
Permission Denied.
API Server > Vault: 我是這個 IAM 請問我可以進去嗎
Vault > AWS: 請問他真的是這個 IAM 嗎
AWS > API Server: 你這台 VM 的 IAM 是...
AWS > Vault: 這台 VM 的 IAM 是...
Vault > API Server: 請進,你的權限 policy 是...
API Server > Vault: 我要這些 key...
# policy for user
path "/service/postgresql" {
capabilities = ["read", "list"]
}
# policy for admin
path "/service/postgresql" {
capabilities = ["read", "list", "update"]
}
# auth with different policy
vault token create -policy=k8s-pod
vault token create -policy=aws-ec2-iam
vault token create -policy=sre-admin
Key Value
--- -----
token s.1234567890abcdefghijk
# Admin
$ vault token create -policy=user
Key Value
--- -----
token s.1234567890abcdefghijk
token_duration 5m
# Another user
$ vault login s.1234567890abcdefghijk
Key Value
policies ["default" "user"]
$ vault read /team/user/my-secret
User > Vault Token > Vault > policy
API Server > Vault Token in K8s secret > Vault
?
API Server Pod > Some Magic > Vault
叫 vault 去信任 k8s cluster
K8s namespace
K8s service account
各自配權限 policy
# Policy
$ vault policy read user
path "team/user/*" {
capabilities = ["read", "sudo"]
}
...
# Auth methods
$ vault auth list
Path Type Accessor Description
---- ---- -------- -----------
approle/ approle auth_approle_12345678 n/a
aws-ec2/ aws auth_aws_12345678 n/a
aws-iam/ aws auth_aws_12345678 n/a
dev-chechia-k8s/ kubernetes auth_kubernetes_12324567 n/a
stag-chechia-k8s/ kubernetes auth_kubernetes_12324567 n/a
2021 鐵人賽 30 天帶你玩 Terraform
https://ithelp.ithome.com.tw/users/20120327/ironman/4057
git clone https://github.com/chechiachang/terraform-30-days.git
cd dev/southeastasia/chechia_net/vault/singleton
terragrunt init && terragrunt apply
# vault operator init
cd dev/southeastasia/chechia_net/vault/config
terragrunt init && terragrunt apply
# and there is a vault
export VAULT_ADDR=http://vault.chechia.net:8200
export VAULT_ADDR=http://52.139.214.189:8200
export VAULT_TOKEN=
vault secrets list
Path Type Accessor Description
---- ---- -------- -----------
cubbyhole/ cubbyhole cubbyhole_81a553a8 per-token private secret storage
identity/ identity identity_60038fa2 identity store
namespace/ kv kv_6b29e9e3 This is an example KV Version 2 secret engine mount
sys/ system system_b332aa7e system endpoints used for control, policy and debugging
vault auth list
Path Type Accessor Description
---- ---- -------- -----------
dev-kubernetes/ kubernetes auth_kubernetes_9dfe88fb n/a
token/ token auth_token_a0708f09 token based credentials
vault policy list
default
namespace
root
歡迎透過 fb 私敲
有經驗的求討論!
投影片
講稿
SOP
範例 Github
CNTUG
https://t.me/cntug
https://fb.cloudnative.tw
DevOpsTW
https://t.me/devopstw