-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo
executable file
·325 lines (267 loc) · 9.51 KB
/
demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/sh
KINDCONFIG=kind.kubeconfig
CLUSTER_NAME=vault-demo
INGRESS_PORT_HTTP=81 # must not be used by somebody else
INGRESS_PORT_HTTPS=444 # must not be used by somebody else
_functions() {
grep '\(^[A-Za-z].*()[ ]*{\|^###*$\)' $0|grep -v '^__'|sed -e 's/^/ /g' -e "s/^###*/\\\n/g" -e 's/()//g'|tr -d '{#'
}
_usage() {
cat<<EOF
Usage: $0 COMMAND
available commands:
$(echo "$(_functions)")
EOF
}
compl() { # print code for bash completion; execute with eval
echo "$0"|grep "^\." > /dev/null && local exe=$0 || local exe=$(basename $0)
local compl_func_name=_$(echo $(basename $0)|tr ' -' '_')
local func_names=$(_functions|grep -v "compl "|sed 's/ *#.*$//g'|cut -f1 -d" "|tr -d '\n')
echo "execute this function with 'eval \$(${exe} compl)'" >&2
echo "$compl_func_name() { COMPREPLY=( \$(compgen -W \"${func_names}\" -- \${COMP_WORDS[COMP_CWORD]}) ); }; complete -F ${compl_func_name} ${exe}"
}
_wait() {
if [ "$?" = "0" ]
then
read -p "💤 "
fi
}
_download_github_release() {
local repo; repo=$1
local artifact_name; artifact_name=$(basename "${repo}")
local latest_version; latest_version=$(basename "$(curl -fs -o/dev/null -w "%{redirect_url}" "https://github.com/${repo}/releases/latest")")
local basename; basename=${artifact_name}-$(uname)-$(uname -m|sed 's/x86_64/amd64/')
(set -x; curl -LO "https://github.com/${repo}/releases/download/${latest_version}/${basename}")
curl -LO "https://github.com/${repo}/releases/download/${latest_version}/${basename}.sha256sum"
sha256sum -c "${basename}".sha256sum
mv "${basename}" "${artifact_name}"
chmod +x ./"${artifact_name}"
}
_install_kind() {
test -e ./kind && return
_download_github_release kubernetes-sigs/kind
}
install_vault() { # install local vault cli (mac os only) #
which vault &>/dev/null || brew install vault
}
#####################
launch_kind_cluster() { # launch kind cluster for experimentation
set -e
_install_kind
KUBECONFIG=${KINDCONFIG} kubectl cluster-info &>/dev/null && return
cat<<-EOF >kind.conf
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
authorization-mode: "AlwaysAllow"
extraPortMappings:
- containerPort: 80
hostPort: ${INGRESS_PORT_HTTP}
- containerPort: 443
hostPort: ${INGRESS_PORT_HTTPS}
EOF
./kind create cluster --name ${CLUSTER_NAME} --kubeconfig ${KINDCONFIG} --config kind.conf
printf "waiting for node to become ready "
(set +x
for _ in $(seq 0 120)
do
sleep 1
printf "."
kubectl get nodes|grep " Ready " && break
done
)
}
delete_kind_cluster() { # deletes the kind cluster
./kind delete cluster --name ${CLUSTER_NAME} --kubeconfig ${KINDCONFIG}
}
load_image() { # load docker image into cluster
local image_name=$1
: ${image_name:?}
(set -x; ./kind load docker-image --name ${CLUSTER_NAME} --nodes ${CLUSTER_NAME}-control-plane "${image_name}")
}
pause_kind_cluster() { # pauses the kind cluster
docker pause ${CLUSTER_NAME}-control-plane
}
unpause_kind_cluster() { # pauses the kind cluster
docker unpause ${CLUSTER_NAME}-control-plane
}
install_ingress_controller() { # installs and sets up ingress controller
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml
kubectl patch deployments -n ingress-nginx nginx-ingress-controller -p '{"spec":{"template":{"spec":{"containers":[{"name":"nginx-ingress-controller","ports":[{"containerPort":80,"hostPort":80},{"containerPort":443,"hostPort":443}]}],"nodeSelector":{"ingress-ready":"true"},"tolerations":[{"key":"node-role.kubernetes.io/master","operator":"Equal","effect":"NoSchedule"}]}}}}'
}
################
install_k8s_pods() { # installs vault, mariadb and demo app to kubernetes
(set -x; ls k8s-*.yaml|xargs -n1 kubectl apply -f)
}
init_vault() { # init vault
(set -x; vault operator init |tee keys)
}
unseal_vault() { # unseal vault
local key_no=${1:?pass in the unsealing number -- info will be read from file 'keys'}
local unsealing_key=$(cat keys |grep "Key ${key_no}:"|cut -f2 -d:)
: ${unsealing_key:?}
(set -x; vault operator unseal ${unsealing_key})
}
set_vault_env() { # get and export vault token and vault addr
local token=$(cat keys|grep "Token:"|cut -f2 -d:)
: ${token}
set -x
echo "export VAULT_TOKEN='${token}'"
echo "export VAULT_ADDR='${VAULT_ADDR}'"
}
enable_audit_log() { # enables audit log for vault
(set -x; vault audit enable file file_path=/tmp/audit.log)
}
enable_kv_secret_backend() { # enables kv secret backend
(set -x; vault secrets enable -version=2 kv)
}
create_vault_user() { # create username/password auth with foo/bar user
(set -x; vault auth enable userpass)
_wait
(set -x; vault write auth/userpass/users/foo \
password=bar \
"policies=testdb-ro,kv-foo"
)
_wait
(set -x
vault login -method=userpass \
username=foo \
password=bar
)
}
enable_mariadb() { # enables mariadb secrets engine and configuration
(set -x; vault secrets enable -path mariadb database)
_wait
(set -x
vault write mariadb/config/testdb \
plugin_name=mysql-database-plugin \
connection_url="{{username}}:{{password}}@tcp(mariadb:3306)/" \
allowed_roles="testdb-ro,testdb-rw" \
username="root" \
password="mypass"
)
_wait
(set -x
vault write mariadb/roles/testdb-ro \
db_name=testdb \
creation_statements="CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';GRANT SELECT ON *.* TO '{{name}}'@'%';" \
default_ttl="10s" \
max_ttl="10s"
)
_wait
(set -x
vault write mariadb/roles/testdb-rw \
db_name=testdb \
creation_statements="CREATE USER '{{name}}'@'%' IDENTIFIED BY '{{password}}';GRANT ALL ON *.* TO '{{name}}'@'%';" \
default_ttl="10s" \
max_ttl="10s"
)
_wait
(set -x
vault read mariadb/creds/testdb-rw
)
kubectl exec deploy/mariadb -- \
/bin/sh -c "echo 'create table testdb (foo INT);insert into testdb (foo) values (23);insert into testdb (foo) values (42);select * from testdb;'|mariadb -uroot -pmypass -Dtestdb"
}
enable_postgres() { # enables mariadb secrets engine and configuration
(set -x; vault secrets enable -path postgres database)
_wait
(set -x
vault write postgres/config/testdb \
plugin_name=postgresql-database-plugin \
connection_url="postgresql://{{username}}:{{password}}@postgres:5432" \
allowed_roles="testdb-ro,testdb-rw" \
username="postgres" \
password="mypass"
)
vault write postgres/roles/my-role \
db_name="my-postgresql-database" \
creation_statements="CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}'; \
GRANT SELECT ON ALL TABLES IN SCHEMA public TO \"{{name}}\";" \
default_ttl="1h" \
max_ttl="24h"
_wait
(set -x
vault write postgres/roles/testdb-ro \
db_name=testdb \
creation_statements="CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}'; \
GRANT SELECT ON ALL TABLES IN SCHEMA public TO \"{{name}}\";" \
default_ttl="10s" \
max_ttl="10s"
)
_wait
(set -x
vault write postgres/roles/testdb-rw \
db_name=testdb \
creation_statements="CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}'; \
GRANT ALL ON ALL TABLES IN SCHEMA public TO \"{{name}}\";" \
default_ttl="10s" \
max_ttl="10s"
)
_wait
(set -x
vault read postgres/creds/testdb-rw
)
kubectl exec deploy/postgres -- \
/bin/sh -c "echo 'create table \"testdb\" (foo int);insert into testdb (foo) values (23);insert into testdb (foo) values (42);select * from testdb;'|psql -Upostgres"
}
enable_mongodb() { # enables mongodb secrets engine and configuration
(set -x; vault secrets enable -path mongodb database)
_wait
(set -x
vault write mongodb/config/mongodb \
plugin_name=mongodb-database-plugin \
connection_url="mongodb://{{username}}:{{password}}@mongodb:27017/admin?tls=false" \
allowed_roles="mongo-rw" \
username="mongouser" \
password="mongopass"
)
vault write mongodb/roles/mongo-rw \
db_name=mongodb \
creation_statements='{ "db": "admin", "roles": [{ "role": "readWrite" }, {"role": "read", "db": "foo"}] }' \
default_ttl="1h" \
max_ttl="24h"
_wait
(set -x
vault read mongodb/creds/mongo-rw
)
}
enable_k8s_auth() { # enables kubernetes authentication
(set -x; kubectl apply -f vault-sa.yaml)
_wait
(set -x; vault auth enable kubernetes)
_wait
local ca=$(kubectl get secret vault-auth-secret -ojsonpath="{.data.ca}"|base64 --decode)
local token=$(kubectl get secret vault-auth-secret -ojsonpath="{.data.token}"|base64 --decode)
(set -x
vault write auth/kubernetes/config \
"token_reviewer_jwt=${token}" \
"kubernetes_host=https://kubernetes" \
"kubernetes_ca_cert=${ca}"
)
_wait
(set -x
vault write auth/kubernetes/role/test \
bound_service_account_names="default,demo-app-sa" \
bound_service_account_namespaces=default \
policies=testdb-ro \
ttl=1h
)
}
###########
if [ -z "$1" ] || ! echo $(_functions)|grep $1 >/dev/null
then
_usage
exit 1
fi
export VAULT_ADDR=${VAULT_ADDR:=http://localhost:8200}
test -e ${KINDCONFIG} && export KUBECONFIG=${KINDCONFIG}
"$@"