-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathdeploy_vault_config.sh
executable file
·320 lines (266 loc) · 9.21 KB
/
deploy_vault_config.sh
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
#!/bin/bash
set -euo pipefail
export VAULT_ADDR=https://vault.ci.openshift.org
VAULT_TOKEN="${VAULT_TOKEN:-$(vault token lookup --format=json|jq .data.id -r)}"
[[ -z ${VAULT_TOKEN:-} ]] && echo '$VAULT_TOKEN is undefined' && exit 1
RAW_VAULT_OIDC_VALUES="$(kubectl --context=app.ci get secret -n dex vault-secret -o json)"
VAULT_OIDC_CLIENT_ID="$(echo $RAW_VAULT_OIDC_VALUES|jq '.data["vault-id"]' -r|base64 -d)"
VAULT_OIDC_CLIENT_SECRET="$(echo $RAW_VAULT_OIDC_VALUES|jq '.data["vault-secret"]' -r|base64 -d)"
# Enable kv backend
vault secrets list|grep -q kv || vault secrets enable -version=2 kv
# Enable and configure kubernetes and OIDC auth
vault auth list|grep -q kubernetes || vault auth enable kubernetes
VAULT_KUBE_TOKEN="$(oc --context=app.ci serviceaccounts -n vault get-token vault)"
APP_CI_CA_CERT="$(oc --context=app.ci get configmap -n kube-public kube-root-ca.crt -o json|jq '.data["ca.crt"]' -r)"
vault write auth/kubernetes/config \
token_reviewer_jwt="${VAULT_KUBE_TOKEN}" \
kubernetes_host=https://kubernetes.default.svc.cluster.local \
kubernetes_ca_cert="${APP_CI_CA_CERT}" \
issuer="$(oc --context=app.ci get authentication.config cluster -o template="{{ .spec.serviceAccountIssuer }}")"
vault auth list |grep -q oidc || vault auth enable oidc
echo "Configuring OIDC"
vault write auth/oidc/config -<<EOH
{
"oidc_client_id": "$VAULT_OIDC_CLIENT_ID",
"oidc_client_secret": "$VAULT_OIDC_CLIENT_SECRET",
"default_role": "oidc_default_role",
"oidc_discovery_url": "https://idp.ci.openshift.org"
}
EOH
echo "Configuring OIDC role"
vault write auth/oidc/role/oidc_default_role \
allowed_redirect_uris="https://vault.ci.openshift.org/ui/vault/auth/oidc/oidc/callback,http://localhost:8250/oidc/callback" \
token_ttl=3600 \
token_max_ttl=3600 \
oidc_scopes="profile" \
user_claim="preferred_username"
# Set this as default, ref https://support.hashicorp.com/hc/en-us/articles/360001922527-Configuring-a-Default-UI-Auth-Method
vault write sys/auth/oidc/tune listing_visibility="unauth"
# Extend the default policy to allow everyone to manage secrets at
# secrets/personal/{{ldap_name}}
OIDC_ACCESSOR_ID="$(vault auth list -format=json|jq '.["oidc/"].accessor' -r)"
echo "Configuring default policy"
vault policy write default -<<EOH
path "auth/token/lookup-self" {
capabilities = ["read"]
}
# Allow tokens to renew themselves
path "auth/token/renew-self" {
capabilities = ["update"]
}
# Allow tokens to revoke themselves
path "auth/token/revoke-self" {
capabilities = ["update"]
}
# Allow a token to look up its own capabilities on a path
path "sys/capabilities-self" {
capabilities = ["update"]
}
# Allow a token to look up its own entity by id or name
path "identity/entity/id/{{identity.entity.id}}" {
capabilities = ["read"]
}
path "identity/entity/name/{{identity.entity.name}}" {
capabilities = ["read"]
}
# Allow a token to look up its resultant ACL from all policies. This is useful
# for UIs. It is an internal path because the format may change at any time
# based on how the internal ACL features and capabilities change.
path "sys/internal/ui/resultant-acl" {
capabilities = ["read"]
}
# Allow a token to renew a lease via lease_id in the request body; old path for
# old clients, new path for newer
path "sys/renew" {
capabilities = ["update"]
}
path "sys/leases/renew" {
capabilities = ["update"]
}
# Allow looking up lease properties. This requires knowing the lease ID ahead
# of time and does not divulge any sensitive information.
path "sys/leases/lookup" {
capabilities = ["update"]
}
# Allow a token to manage its own cubbyhole
path "cubbyhole/*" {
capabilities = ["create", "read", "update", "delete", "list"]
}
# Allow a token to wrap arbitrary values in a response-wrapping token
path "sys/wrapping/wrap" {
capabilities = ["update"]
}
# Allow a token to look up the creation time and TTL of a given
# response-wrapping token
path "sys/wrapping/lookup" {
capabilities = ["update"]
}
# Allow a token to unwrap a response-wrapping token. This is a convenience to
# avoid client token swapping since this is also part of the response wrapping
# policy.
path "sys/wrapping/unwrap" {
capabilities = ["update"]
}
# Allow general purpose tools
path "sys/tools/hash" {
capabilities = ["update"]
}
path "sys/tools/hash/*" {
capabilities = ["update"]
}
# Allow checking the status of a Control Group request if the user has the
# accessor
path "sys/control-group/request" {
capabilities = ["update"]
}
EOH
# Create the secret generator policy and role
vault policy write secret-generator -<<EOH
path "kv/data/dptp/*" {
capabilities = ["create", "update", "read"]
}
path "kv/metadata/dptp/*" {
capabilities = ["list"]
}
EOH
vault write auth/kubernetes/role/secret-generator \
bound_service_account_names=secret-generator \
bound_service_account_namespaces=ci \
policies=secret-generator \
ttl=1h
# Create the secret bootstrap policy and role
vault policy write secret-bootstrap -<<EOH
path "kv/data/*" {
capabilities = ["read"]
}
path "kv/metadata/*" {
capabilities = ["list"]
}
EOH
vault write auth/kubernetes/role/secret-bootstrap \
bound_service_account_names=secret-bootstrap \
bound_service_account_namespaces=ci \
policies=secret-bootstrap \
ttl=1h
vault policy write release-controller -<<EOH
path "kv/data/dptp/openshift-ci-release-signature-signer" {
capabilities = ["create", "update", "read"]
}
path "kv/data/dptp/openshift-ci-release-signature-publisher" {
capabilities = ["create", "update", "read"]
}
# Doesn't allow access to the actual data and we can not give
# list for individual names
path "kv/metadata/dptp/*" {
capabilities = ["list"]
}
# Allows getting the old revisions
path "kv/metadata/dptp/openshift-ci-release-signature-*" {
capabilities = ["read"]
}
EOH
getUserIDByLDAPName() {
curl -Ss --fail -H "X-vault-token: ${VAULT_TOKEN}" "$VAULT_ADDR/v1/identity/entity/id?list=true" \
|jq --arg user "$1" '.data.key_info|to_entries[]|select(.value.aliases[0].name == $user)|.key' -r
}
# unused but left for documentation purposes
upsertUser() {
user=$1
if [[ -n "$(getUserIDByLDAPName $1)" ]]; then echo "User $user already exists, skipping create"; return; fi
oidc_acessor="$(vault auth list --format=json |jq '."oidc/".accessor' -r)"
create_response="$(vault write -format=json identity/entity name="$user" policies="default")"
id="$(echo $create_response|jq .data.id -r)"
vault write identity/entity-alias name="$user" canonical_id="$id" mount_accessor="$oidc_acessor" >/dev/null
echo "Successfully created user $user"
}
getUserIDByLDAPName brawilli
vault write identity/group name="release-controller" policies="release-controller" member_entity_ids="$(getUserIDByLDAPName brawilli)"
vault policy write vault-secret-collection-manager -<<EOH
path "identity/group" {
capabilities = ["create", "update"]
}
path "identity/group/*" {
capabilities = ["read", "list", "create", "update", "delete"]
}
path "sys/policies/acl/*" {
capabilities = ["read", "list", "create", "update", "delete"]
}
path "sys/auth" {
capabilities = ["read"]
}
path "identity/entity-alias/id" {
capabilities = ["list"]
}
path "identity/entity/id" {
capabilities = ["list"]
}
path "identity/entity" {
capabilities = ["create", "update"]
}
path "identity/entity-alias" {
capabilities = ["create", "update"]
}
path "identity/entity/id/*" {
capabilities = ["read"]
}
path "identity/entity/name/*" {
capabilities = ["read"]
}
path "kv/metadata/selfservice/*" {
capabilities = ["list", "delete"]
}
# We create a marker item and create is
# actually upsert, so needs both read and
# create
path "kv/data/selfservice/*" {
capabilities = ["create", "read"]
}
EOH
vault write auth/kubernetes/role/vault-secret-collection-manager \
bound_service_account_names=vault-secret-collection-manager \
bound_service_account_namespaces=ci \
policies=vault-secret-collection-manager \
ttl=1h
vault policy write vault-subpath-proxy -<<EOH
path "kv/data/*" {
capabilities = ["read"]
}
path "kv/metadata/*" {
capabilities = ["list"]
}
EOH
vault write auth/kubernetes/role/vault-subpath-proxy \
bound_service_account_names=vault \
bound_service_account_namespaces=vault \
policies=vault-subpath-proxy \
ttl=1h
# Make dptp members admins
echo "Setting up admin policy"
vault policy write admin -<<EOF
path "*" { capabilities = ["create", "read", "update", "delete", "list", "sudo"] }
EOF
# Vault auth plugins are an abstration above the real identity and create an alias. Getting from
# that alias back to the identity id which is the thing we need to set up a group is non-trivial.
echo "Finding ids for dptp members"
dptp_member_aliases='[
"hongkliu",
"nmoraiti",
"sgoeddel",
"jupierce",
"bechen",
"jguzik",
"dgemoli",
"psalajov",
"dmistry",
"prucek",
"hvidosil",
"pruan",
"lxia"
]'
dptp_ids="$(curl -Ss --fail -H "X-vault-token: ${VAULT_TOKEN}" "$VAULT_ADDR/v1/identity/entity/id?list=true" \
|jq \
--argjson dptp_members "$dptp_member_aliases" \
'[.data.key_info|to_entries[]|select([.value.aliases[0].name] | inside($dptp_members))|.key]|@csv' -rc \
|tr -d '"')"
echo "Setting up group for dptp"
vault write identity/group name="dptp" policies="admin" member_entity_ids="$dptp_ids"