forked from hashicorp/vault-csi-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.bats
303 lines (250 loc) · 14.4 KB
/
provider.bats
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
#!/usr/bin/env bats
load _helpers
export SETUP_TEARDOWN_OUTFILE=/dev/null
if [[ -n "${DISPLAY_SETUP_TEARDOWN_LOGS:-}" ]]; then
export SETUP_TEARDOWN_OUTFILE=/dev/stdout
fi
#SKIP_TEARDOWN=true
CONFIGS=test/bats/configs
setup(){
{ # Braces used to redirect all setup logs.
# 1. Configure Vault.
kubectl --namespace=csi exec vault-0 -- vault namespace create acceptance
# 1. a) Vault policies
cat $CONFIGS/vault-policy-db.hcl | kubectl --namespace=csi exec -i vault-0 -- vault policy write db-policy -
cat $CONFIGS/vault-policy-kv.hcl | kubectl --namespace=csi exec -i vault-0 -- vault policy write kv-policy -
cat $CONFIGS/vault-policy-pki.hcl | kubectl --namespace=csi exec -i vault-0 -- vault policy write pki-policy -
cat $CONFIGS/vault-policy-kv-namespace.hcl | kubectl --namespace=csi exec -i vault-0 -- vault policy write -namespace=acceptance kv-namespace-policy -
# 1. b) Setup kubernetes auth engine.
kubectl --namespace=csi exec vault-0 -- vault auth enable kubernetes
# `issuer` argument corresponds to value of --service-account-issuer for kube-apiserver,
# and for this test assumes the default value that kind sets.
# For EKS: https://oidc.eks.<region>.amazonaws.com/id/<ID> (same as OpenID Connect provider URL)
# For AKS: "<dns-prefix>.hcp.<region>.azmk8s.io" (same as API server address, but with quotes)
# For GKE: https://container.googleapis.com/v1/projects/<project>/locations/<az|region>/clusters/<cluster-name>
kubectl --namespace=csi exec vault-0 -- sh -c 'vault write auth/kubernetes/config \
token_reviewer_jwt="$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
kubernetes_host="https://$KUBERNETES_PORT_443_TCP_ADDR:443" \
kubernetes_ca_cert=@/var/run/secrets/kubernetes.io/serviceaccount/ca.crt \
issuer="https://kubernetes.default.svc.cluster.local"'
kubectl --namespace=csi exec vault-0 -- vault auth enable -namespace=acceptance kubernetes
kubectl --namespace=csi exec vault-0 -- sh -c 'vault write -namespace=acceptance auth/kubernetes/config \
token_reviewer_jwt="$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
kubernetes_host="https://$KUBERNETES_PORT_443_TCP_ADDR:443" \
kubernetes_ca_cert=@/var/run/secrets/kubernetes.io/serviceaccount/ca.crt \
issuer="https://kubernetes.default.svc.cluster.local"'
kubectl --namespace=csi exec vault-0 -- vault write auth/kubernetes/role/db-role \
bound_service_account_names=nginx-db \
bound_service_account_namespaces=test \
policies=db-policy \
ttl=20m
kubectl --namespace=csi exec vault-0 -- vault write auth/kubernetes/role/kv-role \
bound_service_account_names=nginx-kv \
bound_service_account_namespaces=test \
policies=kv-policy \
ttl=20m
kubectl --namespace=csi exec vault-0 -- vault write -namespace=acceptance auth/kubernetes/role/kv-namespace-role \
bound_service_account_names=nginx-kv-namespace \
bound_service_account_namespaces=test \
policies=kv-namespace-policy \
ttl=20m
kubectl --namespace=csi exec vault-0 -- vault write auth/kubernetes/role/pki-role \
bound_service_account_names=nginx-pki \
bound_service_account_namespaces=test \
policies=pki-policy \
ttl=20m
kubectl --namespace=csi exec vault-0 -- vault write auth/kubernetes/role/all-role \
bound_service_account_names=nginx-all \
bound_service_account_namespaces=test \
policies=db-policy,kv-policy,pki-policy \
ttl=20m
# 1. c) Setup pki secrets engine.
kubectl --namespace=csi exec vault-0 -- vault secrets enable pki
kubectl --namespace=csi exec vault-0 -- vault write -field=certificate pki/root/generate/internal \
common_name="example.com"
kubectl --namespace=csi exec vault-0 -- vault write pki/config/urls \
issuing_certificates="http://127.0.0.1:8200/v1/pki/ca"
kubectl --namespace=csi exec vault-0 -- vault write pki/roles/example-dot-com \
allowed_domains="example.com" \
allow_subdomains=true
# 1. d) Setup kv secrets in Vault.
kubectl --namespace=csi exec vault-0 -- vault secrets enable -path=secret -version=2 kv
kubectl --namespace=csi exec vault-0 -- vault kv put secret/kv1 bar1=hello1
kubectl --namespace=csi exec vault-0 -- vault kv put secret/kv2 bar2=hello2
kubectl --namespace=csi exec vault-0 -- vault kv put secret/kv-sync1 bar1=hello-sync1
kubectl --namespace=csi exec vault-0 -- vault kv put secret/kv-sync2 bar2=hello-sync2
kubectl --namespace=csi exec vault-0 -- vault secrets enable -namespace=acceptance -path=secret -version=2 kv
kubectl --namespace=csi exec vault-0 -- vault kv put -namespace=acceptance secret/kv1-namespace greeting=hello-namespaces
# 2. Create shared k8s resources.
kubectl create namespace test
kubectl --namespace=test apply -f $CONFIGS/vault-all-secretproviderclass.yaml
kubectl --namespace=test apply -f $CONFIGS/vault-db-secretproviderclass.yaml
kubectl --namespace=test apply -f $CONFIGS/vault-kv-namespace-secretproviderclass.yaml
kubectl --namespace=test apply -f $CONFIGS/vault-kv-secretproviderclass.yaml
kubectl --namespace=test apply -f $CONFIGS/vault-kv-sync-secretproviderclass.yaml
kubectl --namespace=test apply -f $CONFIGS/vault-kv-sync-multiple-secretproviderclass.yaml
kubectl --namespace=test apply -f $CONFIGS/vault-pki-secretproviderclass.yaml
} > $SETUP_TEARDOWN_OUTFILE
}
teardown(){
if [[ -n $SKIP_TEARDOWN ]]; then
echo "Skipping teardown"
return
fi
{ # Braces used to redirect all teardown logs.
# If the test failed, print some debug output
if [[ "$BATS_ERROR_STATUS" -ne 0 ]]; then
echo "DESCRIBE NGINX PODS"
kubectl describe pod -l app=nginx --all-namespaces=true
echo "PROVIDER LOGS"
kubectl --namespace=csi logs -l app=vault-csi-provider --tail=50
echo "VAULT LOGS"
kubectl --namespace=csi logs vault-0
fi
# Teardown Vault configuration.
kubectl --namespace=csi exec vault-0 -- vault namespace delete acceptance
kubectl --namespace=csi exec vault-0 -- vault auth disable kubernetes
kubectl --namespace=csi exec vault-0 -- vault secrets disable secret
kubectl --namespace=csi exec vault-0 -- vault secrets disable pki
kubectl --namespace=csi exec vault-0 -- vault secrets disable database
kubectl --namespace=csi exec vault-0 -- vault policy delete example-policy
kubectl --namespace=csi exec vault-0 -- vault kv delete secret/kv1
kubectl --namespace=csi exec vault-0 -- vault kv delete secret/kv2
kubectl --namespace=csi exec vault-0 -- vault kv delete secret/kv-sync1
kubectl --namespace=csi exec vault-0 -- vault kv delete secret/kv-sync2
# Teardown shared k8s resources.
kubectl delete --ignore-not-found namespace test
kubectl delete --ignore-not-found namespace negative-test-ns
} > $SETUP_TEARDOWN_OUTFILE
}
@test "1 Inline secrets-store-csi volume" {
helm --namespace=test install nginx $CONFIGS/nginx \
--set engine=kv --set sa=kv \
--wait --timeout=5m
result=$(kubectl --namespace=test exec nginx-kv -- cat /mnt/secrets-store/secret-1)
[[ "$result" == "hello1" ]]
result=$(kubectl --namespace=test exec nginx-kv -- cat /mnt/secrets-store/secret-2)
[[ "$result" == "hello2" ]]
}
@test "2 Sync with kubernetes secrets" {
# Deploy some pods that should cause k8s secrets to be created.
kubectl --namespace=test apply -f $CONFIGS/nginx-kv-env-var.yaml
kubectl --namespace=test wait --for=condition=Ready --timeout=5m pod -l app=nginx
POD=$(kubectl --namespace=test get pod -l app=nginx -o jsonpath="{.items[0].metadata.name}")
result=$(kubectl --namespace=test exec $POD -- cat /mnt/secrets-store/secret-1)
[[ "$result" == "hello-sync1" ]]
result=$(kubectl --namespace=test exec $POD -- cat /mnt/secrets-store/secret-2)
[[ "$result" == "hello-sync2" ]]
run kubectl get secret --namespace=test kvsecret
[ "$status" -eq 0 ]
result=$(kubectl --namespace=test get secret kvsecret -o jsonpath="{.data.pwd}" | base64 -d)
[[ "$result" == "hello-sync1" ]]
result=$(kubectl --namespace=test exec $POD -- printenv | grep SECRET_USERNAME | awk -F"=" '{ print $2 }' | tr -d '\r\n')
[[ "$result" == "hello-sync2" ]]
result=$(kubectl --namespace=test get secret kvsecret -o jsonpath="{.metadata.labels.environment}")
[[ "${result//$'\r'}" == "test" ]]
result=$(kubectl --namespace=test get secret kvsecret -o jsonpath="{.metadata.labels.secrets-store\.csi\.k8s\.io/managed}")
[[ "${result//$'\r'}" == "true" ]]
# There isn't really an event we can wait for to ensure this has happened.
for i in {0..60}; do
result="$(kubectl --namespace=test get secret kvsecret -o json | jq '.metadata.ownerReferences | length')"
if [[ "$result" -eq 2 ]]; then
break
fi
sleep 1
done
[[ "$result" -eq 2 ]]
# Wait for secret deletion in a background process.
kubectl --namespace=test wait --for=delete --timeout=60s secret kvsecret &
WAIT_PID=$!
# Trigger deletion implicitly by deleting only owners.
kubectl --namespace=test delete -f $CONFIGS/nginx-kv-env-var.yaml
echo "Waiting for kvsecret to get deleted"
wait $WAIT_PID
# Ensure it actually got deleted.
run kubectl --namespace=test get secret kvsecret
[ "$status" -eq 1 ]
}
@test "3 SecretProviderClass in different namespace not usable" {
kubectl create namespace negative-test-ns
helm --namespace=negative-test-ns install nginx $CONFIGS/nginx \
--set engine=kv --set sa=kv
kubectl --namespace=negative-test-ns wait --for=condition=PodScheduled --timeout=60s pod nginx-kv
wait_for_success "kubectl --namespace=negative-test-ns describe pod nginx-kv | grep 'FailedMount.*failed to get secretproviderclass negative-test-ns/vault-kv.*not found'"
}
@test "4 Pod with multiple SecretProviderClasses" {
POD=nginx-multiple-volumes
kubectl --namespace=test apply -f $CONFIGS/nginx-kv-multiple-volumes.yaml
kubectl --namespace=test wait --for=condition=Ready --timeout=5m pod $POD
result=$(kubectl --namespace=test exec $POD -- cat /mnt/secrets-store-1/secret-1)
[[ "$result" == "hello-sync1" ]]
result=$(kubectl --namespace=test exec $POD -- cat /mnt/secrets-store-2/secret-2)
[[ "$result" == "hello-sync2" ]]
result=$(kubectl --namespace=test get secret kvsecret-1 -o jsonpath="{.data.username}" | base64 -d)
[[ "$result" == "hello-sync1" ]]
result=$(kubectl --namespace=test get secret kvsecret-2 -o jsonpath="{.data.pwd}" | base64 -d)
[[ "$result" == "hello-sync2" ]]
result=$(kubectl --namespace=test exec $POD -- printenv | grep SECRET_1_USERNAME | awk -F"=" '{ print $2 }' | tr -d '\r\n')
[[ "$result" == "hello-sync1" ]]
result=$(kubectl --namespace=test exec $POD -- printenv | grep SECRET_2_PWD | awk -F"=" '{ print $2 }' | tr -d '\r\n')
[[ "$result" == "hello-sync2" ]]
}
@test "5 SecretProviderClass with query parameters and PUT method" {
helm --namespace=test install nginx $CONFIGS/nginx \
--set engine=pki --set sa=pki \
--wait --timeout=5m
result=$(kubectl --namespace=test exec nginx-pki -- cat /mnt/secrets-store/certs)
[[ "$result" != "" ]]
# Ensure we have some valid x509 certificates.
echo "$result" | jq -r '.data.certificate' | openssl x509 -noout
echo "$result" | jq -r '.data.issuing_ca' | openssl x509 -noout
echo "$result" | jq -r '.data.certificate' | openssl x509 -noout -text | grep "test.example.com"
}
@test "6 Dynamic secrets engine, endpoint is called only once per SecretProviderClass" {
setup_postgres
# Now deploy a pod that will generate some dynamic credentials.
helm --namespace=test install nginx $CONFIGS/nginx \
--set engine=db --set sa=db \
--wait --timeout=5m
# Read the creds out of the pod and verify they work for a query.
DYNAMIC_USERNAME=$(kubectl --namespace=test exec nginx-db -- cat /mnt/secrets-store/dbUsername)
DYNAMIC_PASSWORD=$(kubectl --namespace=test exec nginx-db -- cat /mnt/secrets-store/dbPassword)
result=$(kubectl --namespace=test exec postgres -- psql postgres://${DYNAMIC_USERNAME}:${DYNAMIC_PASSWORD}@127.0.0.1:5432/db --command="SELECT usename FROM pg_catalog.pg_user" --csv | sed -n '3 p')
[[ "$result" != "" ]]
[[ "$result" == "${DYNAMIC_USERNAME}" ]]
}
@test "7 SecretProviderClass with multiple secret types" {
setup_postgres
helm --namespace=test install nginx $CONFIGS/nginx \
--set engine=all --set sa=all \
--wait --timeout=5m
# Verify dynamic database creds.
DYNAMIC_USERNAME=$(kubectl --namespace=test exec nginx-all -- cat /mnt/secrets-store/dbUsername)
DYNAMIC_PASSWORD=$(kubectl --namespace=test exec nginx-all -- cat /mnt/secrets-store/dbPassword)
result=$(kubectl --namespace=test exec postgres -- psql postgres://${DYNAMIC_USERNAME}:${DYNAMIC_PASSWORD}@127.0.0.1:5432/db --command="SELECT usename FROM pg_catalog.pg_user" --csv | sed -n '3 p')
[[ "$result" != "" ]]
[[ "$result" == "${DYNAMIC_USERNAME}" ]]
# Verify kv secret.
result=$(kubectl --namespace=test exec nginx-all -- cat /mnt/secrets-store/secret-1)
[[ "$result" == "hello1" ]]
# Verify certificates.
result=$(kubectl --namespace=test exec nginx-all -- cat /mnt/secrets-store/certs)
[[ "$result" != "" ]]
echo "$result" | jq -r '.data.certificate' | openssl x509 -noout
echo "$result" | jq -r '.data.issuing_ca' | openssl x509 -noout
echo "$result" | jq -r '.data.certificate' | openssl x509 -noout -text | grep "test.example.com"
}
@test "8 Wrong service account does not have access to Vault" {
helm --namespace=test install nginx $CONFIGS/nginx \
--set engine=kv --set sa=pki
kubectl --namespace=test wait --for=condition=PodScheduled --timeout=60s pod nginx-kv
wait_for_success "kubectl --namespace=test describe pod nginx-kv | grep 'FailedMount.*failed to mount secrets store objects for pod test/nginx-kv'"
wait_for_success "kubectl --namespace=test describe pod nginx-kv | grep 'service account name not authorized'"
}
@test "9 Vault Enterprise namespace" {
helm --namespace=test install nginx $CONFIGS/nginx \
--set engine=kv-namespace --set sa=kv-namespace \
--wait --timeout=5m
result=$(kubectl --namespace=test exec nginx-kv-namespace -- cat /mnt/secrets-store/secret-1)
[[ "$result" == "hello-namespaces" ]]
}