forked from ratify-project/ratify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase-test.bats
626 lines (520 loc) · 31.4 KB
/
base-test.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# Copyright The Ratify Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#!/usr/bin/env bats
load helpers
BATS_TESTS_DIR=${BATS_TESTS_DIR:-test/bats/tests}
WAIT_TIME=60
SLEEP_TIME=1
RATIFY_NAMESPACE=gatekeeper-system
@test "base test without cert rotator" {
teardown() {
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo1 --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod initcontainer-pod --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod initcontainer-pod1 --namespace default --force --ignore-not-found=true'
}
run kubectl apply -f ./library/multi-tenancy-validation/template.yaml
assert_success
sleep 5
run kubectl apply -f ./library/multi-tenancy-validation/samples/constraint.yaml
assert_success
sleep 5
# validate key management provider status property shows success
run bash -c "kubectl get keymanagementproviders.config.ratify.deislabs.io/ratify-notation-inline-cert-0 -o yaml | grep 'issuccess: true'"
assert_success
run kubectl run demo --namespace default --image=registry:5000/notation:signed
assert_success
run kubectl run demo1 --namespace default --image=registry:5000/notation:unsigned
assert_failure
# validate initContainers image
run kubectl apply -f ./test/testdata/pod_initContainers_signed.yaml --namespace default
assert_success
run kubectl apply -f ./test/testdata/pod_initContainers_unsigned.yaml --namespace default
assert_failure
# validate ephemeralContainers image
run kubectl debug demo --image=registry:5000/notation:signed --target=demo
assert_success
run kubectl debug demo --image=registry:5000/notation:unsigned --target=demo
assert_failure
}
@test "crd version test" {
run kubectl delete verifiers.config.ratify.deislabs.io/verifier-notation
assert_success
run kubectl apply -f ./config/samples/clustered/verifier/config_v1alpha1_verifier_notation.yaml
assert_success
run bash -c "kubectl get verifiers.config.ratify.deislabs.io/verifier-notation -o yaml | grep 'apiVersion: config.ratify.deislabs.io/v1beta1'"
assert_success
run kubectl delete stores.config.ratify.deislabs.io/store-oras
assert_success
run kubectl apply -f ./config/samples/clustered/verifier/config_v1alpha1_store_oras_http.yaml
assert_success
run bash -c "kubectl get stores.config.ratify.deislabs.io/store-oras -o yaml | grep 'apiVersion: config.ratify.deislabs.io/v1beta1'"
assert_success
}
@test "notation test" {
teardown() {
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo1 --namespace default --force --ignore-not-found=true'
}
run kubectl apply -f ./library/multi-tenancy-validation/template.yaml
assert_success
sleep 5
run kubectl apply -f ./library/multi-tenancy-validation/samples/constraint.yaml
assert_success
sleep 5
# validate key management provider status property shows success
run bash -c "kubectl get keymanagementproviders.config.ratify.deislabs.io/ratify-notation-inline-cert-0 -o yaml | grep 'issuccess: true'"
assert_success
run kubectl run demo --namespace default --image=registry:5000/notation:signed
assert_success
run kubectl run demo1 --namespace default --image=registry:5000/notation:unsigned
assert_failure
}
@test "notation test timestamping" {
teardown() {
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo-tsa --namespace default --force --ignore-not-found=true'
# restore the original notation verifier for other tests
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl replace -f ./config/samples/clustered/verifier/config_v1beta1_verifier_notation.yaml'
}
# validate key management provider status property shows success
run bash -c "kubectl get keymanagementproviders.config.ratify.deislabs.io/ratify-notation-inline-cert-0 -o yaml | grep 'issuccess: true'"
assert_success
# add the tsaroot certificate as an inline key management provider
cat ./test/bats/tests/config/config_v1beta1_keymanagementprovider_inline.yaml >> tsakmprovider.yaml
cat ./test/bats/tests/certificates/tsarootca.cer | sed 's/^/ /g' >> tsakmprovider.yaml
run kubectl apply -f tsakmprovider.yaml --namespace ${RATIFY_NAMESPACE}
assert_success
# configure the notation verifier to use the inline key management provider
run kubectl replace -f ./test/bats/tests/config/config_v1beta1_verifier_notation_tsa.yaml
assert_success
sleep 10
# verify that the image can now be run
run kubectl run demo-tsa --namespace default --image=registry:5000/notation:tsa
assert_success
}
@test "notation test with certs across namespace" {
teardown() {
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo1 --namespace default --force --ignore-not-found=true'
# restore cert store in ratify namespace
run kubectl apply -f clusterkmprovider.yaml
assert_success
# restore the original notation verifier for other tests
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl apply -f ./config/samples/clustered/verifier/config_v1beta1_verifier_notation.yaml'
# delete new namespace
run kubectl delete namespace new-namespace
assert_success
}
run kubectl apply -f ./library/multi-tenancy-validation/template.yaml
assert_success
sleep 5
run kubectl apply -f ./library/multi-tenancy-validation/samples/constraint.yaml
assert_success
sleep 5
# create a new namespace.
run kubectl create namespace new-namespace
assert_success
sleep 3
# apply the key management provider to new namespace
run bash -c "kubectl get keymanagementproviders.config.ratify.deislabs.io/ratify-notation-inline-cert-0 -o yaml > clusterkmprovider.yaml"
assert_success
sed 's/KeyManagementProvider/NamespacedKeyManagementProvider/' clusterkmprovider.yaml >namespacedkmprovider.yaml
run kubectl apply -f namespacedkmprovider.yaml -n new-namespace
assert_success
# delete the cluster-wide key management provider
run kubectl delete keymanagementproviders.config.ratify.deislabs.io/ratify-notation-inline-cert-0
assert_success
# configure the notation verifier to use inline certificate store in new namespace.
sed 's/default\//new-namespace\//' ./config/samples/clustered/verifier/config_v1beta1_verifier_notation_specificnskmprovider.yaml >verifier-new-namespace.yaml
run kubectl apply -f verifier-new-namespace.yaml
assert_success
sleep 3
run kubectl run demo --namespace new-namespace --image=registry:5000/notation:signed
assert_success
run kubectl run demo1 --namespace new-namespace --image=registry:5000/notation:unsigned
assert_failure
}
@test "cosign test" {
teardown() {
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod cosign-demo-key --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod cosign-demo-unsigned --namespace default --force --ignore-not-found=true'
}
run kubectl apply -f ./library/multi-tenancy-validation/template.yaml
assert_success
sleep 5
run kubectl apply -f ./library/multi-tenancy-validation/samples/constraint.yaml
assert_success
sleep 5
run kubectl run cosign-demo-key --namespace default --image=registry:5000/cosign:signed-key
assert_success
run kubectl run cosign-demo-unsigned --namespace default --image=registry:5000/cosign:unsigned
assert_failure
}
@test "cosign legacy keyed test" {
teardown() {
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod cosign-demo-key --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod cosign-demo-unsigned --namespace default --force --ignore-not-found=true'
}
# use imperative command to guarantee verifier config is updated
run kubectl replace -f ./config/samples/clustered/verifier/config_v1beta1_verifier_cosign_legacy.yaml
sleep 5
run kubectl apply -f ./library/multi-tenancy-validation/template.yaml
assert_success
sleep 5
run kubectl apply -f ./library/multi-tenancy-validation/samples/constraint.yaml
assert_success
sleep 5
run kubectl run cosign-demo-key --namespace default --image=registry:5000/cosign:signed-key
assert_success
run kubectl run cosign-demo-unsigned --namespace default --image=registry:5000/cosign:unsigned
assert_failure
}
@test "cosign keyless test" {
teardown() {
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod cosign-demo-keyless --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl replace -f ./config/samples/clustered/verifier/config_v1beta1_verifier_cosign.yaml'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl replace -f ./config/samples/clustered/store/config_v1beta1_store_oras_http.yaml'
}
run kubectl replace -f ./test/bats/tests/config/config_v1beta1_verifier_cosign_keyless.yaml
sleep 5
run kubectl replace -f ./config/samples/clustered/store/config_v1beta1_store_oras.yaml
sleep 5
wait_for_process 20 10 'kubectl run cosign-demo-keyless --namespace default --image=wabbitnetworks.azurecr.io/test/cosign-image:signed-keyless'
}
@test "cosign legacy keyless test" {
teardown() {
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod cosign-demo-keyless --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl replace -f ./config/samples/clustered/verifier/config_v1beta1_verifier_cosign.yaml'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl replace -f ./config/samples/clustered/store/config_v1beta1_store_oras_http.yaml'
}
# use imperative command to guarantee useHttp is updated
run kubectl replace -f ./config/samples/clustered/verifier/config_v1beta1_verifier_cosign_keyless_legacy.yaml
sleep 5
run kubectl replace -f ./config/samples/clustered/store/config_v1beta1_store_oras.yaml
sleep 5
wait_for_process 20 10 'kubectl run cosign-demo-keyless --namespace default --image=wabbitnetworks.azurecr.io/test/cosign-image:signed-keyless'
}
@test "validate crd add, replace and delete" {
teardown() {
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod crdtest --namespace default --force --ignore-not-found=true'
}
echo "adding license checker, delete notation verifier and validate deployment fails due to missing notation verifier"
run kubectl apply -f ./config/samples/clustered/verifier/config_v1beta1_verifier_complete_licensechecker.yaml
assert_success
run kubectl delete verifiers.config.ratify.deislabs.io/verifier-notation
assert_success
# wait for the httpserver cache to be invalidated
sleep 15
run kubectl run crdtest --namespace default --image=registry:5000/notation:signed
assert_failure
echo "Add notation verifier and validate deployment succeeds"
run kubectl apply -f ./config/samples/clustered/verifier/config_v1beta1_verifier_notation.yaml
assert_success
# wait for the httpserver cache to be invalidated
sleep 15
run kubectl run crdtest --namespace default --image=registry:5000/notation:signed
assert_success
}
@test "store crd status check" {
teardown() {
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete stores.config.ratify.deislabs.io/store-dynamic'
}
# apply a invalid verifier CR, validate status with error
sed 's/:v1/:invalid/' ./config/samples/clustered/store/config_v1beta1_store_dynamic.yaml >invalidstore.yaml
run kubectl apply -f invalidstore.yaml
assert_success
# wait for download of image
sleep 5
run bash -c "kubectl describe stores.config.ratify.deislabs.io/store-dynamic -n ${RATIFY_NAMESPACE} | grep 'plugin not found'"
assert_success
}
@test "configmap update test" {
skip "Skipping test for now as we are no longer watching for configfile update in a K8s environment. This test ensures we are watching config file updates in a non-kub scenario"
run kubectl apply -f ./library/multi-tenancy-validation/template.yaml
assert_success
sleep 5
run kubectl apply -f ./library/multi-tenancy-validation/samples/constraint.yaml
assert_success
sleep 5
run kubectl run demo2 --image=registry:5000/notation:signed
assert_success
run kubectl get configmaps ratify-configuration --namespace=${RATIFY_NAMESPACE} -o yaml >currentConfig.yaml
run kubectl delete -f ./library/multi-tenancy-validation/samples/constraint.yaml
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} "kubectl replace --namespace=${RATIFY_NAMESPACE} -f ${BATS_TESTS_DIR}/configmap/invalidconfigmap.yaml"
echo "Waiting for 150 second for configuration update"
sleep 150
run kubectl apply -f ./library/multi-tenancy-validation/samples/constraint.yaml
assert_success
run kubectl run demo3 --image=registry:5000/notation:signed
echo "Current time after validate : $(date +"%T")"
assert_failure
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} "kubectl replace --namespace=${RATIFY_NAMESPACE} -f currentConfig.yaml"
}
@test "validate mutation tag to digest" {
teardown() {
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod mutate-demo --namespace default --ignore-not-found=true'
}
run kubectl apply -f ./library/multi-tenancy-validation/template.yaml
assert_success
sleep 5
run kubectl apply -f ./library/multi-tenancy-validation/samples/constraint.yaml
assert_success
sleep 5
run kubectl run mutate-demo --namespace default --image=registry:5000/notation:signed
assert_success
result=$(kubectl get pod mutate-demo --namespace default -o json | jq -r ".spec.containers[0].image" | grep @sha)
assert_mutate_success
}
@test "validate inline certificate store provider" {
teardown() {
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete certificatestores.config.ratify.deislabs.io/certstore-inline --namespace ${RATIFY_NAMESPACE} --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo-alternate --namespace default --force --ignore-not-found=true'
# restore the original key management provider
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl apply -f kmprovider_staging.yaml'
# restore the original notation verifier for other tests
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl apply -f ./config/samples/clustered/verifier/config_v1beta1_verifier_notation.yaml'
}
# save the existing key management provider inline resource to restore later
run bash -c "kubectl get keymanagementproviders.config.ratify.deislabs.io/ratify-notation-inline-cert-0 -o yaml > kmprovider_staging.yaml"
assert_success
# configure the default template/constraint
run kubectl apply -f ./library/default/template.yaml
assert_success
run kubectl apply -f ./library/default/samples/constraint.yaml
assert_success
# verify that the image cannot be run due to an invalid cert
run kubectl run demo-alternate --namespace default --image=registry:5000/notation:signed-alternate
assert_failure
# delete the existing key management provider inline resource since certificate store and key management provider cannot be used together
run kubectl delete keymanagementproviders.config.ratify.deislabs.io/ratify-notation-inline-cert-0
assert_success
# add the alternate certificate as an inline certificate store
cat ~/.config/notation/truststore/x509/ca/alternate-cert/alternate-cert.crt | sed 's/^/ /g' >>./test/bats/tests/config/config_v1beta1_certstore_inline.yaml
run kubectl apply -f ./test/bats/tests/config/config_v1beta1_certstore_inline.yaml --namespace ${RATIFY_NAMESPACE}
assert_success
sed -i '9,$d' ./test/bats/tests/config/config_v1beta1_certstore_inline.yaml
# configure the notation verifier to use the inline certificate store
run kubectl apply -f ./test/bats/tests/config/config_v1beta1_verifier_notation.yaml
assert_success
sleep 10
# verify that the image can now be run
run kubectl run demo-alternate --namespace default --image=registry:5000/notation:signed-alternate
assert_success
}
@test "validate inline key management provider" {
teardown() {
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete keymanagementproviders.config.ratify.deislabs.io/keymanagementprovider-inline --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo-alternate --namespace default --force --ignore-not-found=true'
# restore the original notation verifier for other tests
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl apply -f ./config/samples/clustered/verifier/config_v1beta1_verifier_notation.yaml'
}
# configure the default template/constraint
run kubectl apply -f ./library/multi-tenancy-validation/template.yaml
assert_success
run kubectl apply -f ./library/multi-tenancy-validation/samples/constraint.yaml
assert_success
# verify that the image cannot be run due to an invalid cert
sleep 10
run kubectl run demo-alternate --namespace default --image=registry:5000/notation:signed-alternate
assert_failure
sleep 10
# add the alternate certificate as an inline key management provider
cat ~/.config/notation/truststore/x509/ca/alternate-cert/alternate-cert.crt | sed 's/^/ /g' >>./test/bats/tests/config/config_v1beta1_keymanagementprovider_inline.yaml
run kubectl apply -f ./test/bats/tests/config/config_v1beta1_keymanagementprovider_inline.yaml --namespace ${RATIFY_NAMESPACE}
assert_success
sed -i '10,$d' ./test/bats/tests/config/config_v1beta1_keymanagementprovider_inline.yaml
# configure the notation verifier to use the inline key management provider
run kubectl apply -f ./test/bats/tests/config/config_v1beta1_verifier_notation_kmprovider.yaml
assert_success
sleep 10
# verify that the image can now be run
run kubectl run demo-alternate --namespace default --image=registry:5000/notation:signed-alternate
assert_success
}
@test "validate inline key management provider with inline certificate store" {
# this test validates that if a key management provider and certificate store are both configured with the same name,
# the key management provider will take precedence and continue to work as expected
teardown() {
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo1 --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete certificatestores.config.ratify.deislabs.io/ratify-notation-inline-cert-0 --namespace ${RATIFY_NAMESPACE} --ignore-not-found=true'
}
# configure the default template/constraint
run kubectl apply -f ./library/multi-tenancy-validation/template.yaml
assert_success
run kubectl apply -f ./library/multi-tenancy-validation/samples/constraint.yaml
assert_success
# validate key management provider status property shows success
run bash -c "kubectl get keymanagementproviders.config.ratify.deislabs.io/ratify-notation-inline-cert-0 -o yaml | grep 'issuccess: true'"
assert_success
run kubectl run demo --namespace default --image=registry:5000/notation:signed
assert_success
sleep 10
# apply an invalid cert in an inline certificate store
run kubectl apply -f ./test/bats/tests/config/config_v1beta1_certstore_inline_invalid.yaml -n ${RATIFY_NAMESPACE}
assert_success
# validate key management provider status property shows success
run bash -c "kubectl get certificatestores.config.ratify.deislabs.io/ratify-notation-inline-cert-0 -n ${RATIFY_NAMESPACE} -o yaml | grep 'issuccess: true'"
assert_success
# verification should succeed as the existing KMP will take precedence over the new certificate store
run kubectl run demo1 --namespace default --image=registry:5000/notation:signed
assert_success
}
@test "validate K8s secrets ORAS auth provider" {
teardown() {
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo --namespace default --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo1 --namespace default --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl replace -f ./config/samples/clustered/store/config_v1beta1_store_oras_http.yaml'
}
run kubectl apply -f ./library/multi-tenancy-validation/template.yaml
assert_success
sleep 5
run kubectl apply -f ./library/multi-tenancy-validation/samples/constraint.yaml
assert_success
sleep 5
# apply store CRD with K8s secret auth provier enabled
run kubectl apply -f ./config/samples/clustered/store/config_v1beta1_store_oras_k8secretAuth.yaml
assert_success
sleep 5
run kubectl run demo --namespace default --image=registry:5000/notation:signed
assert_success
run kubectl run demo1 --namespace default --image=registry:5000/notation:unsigned
assert_failure
}
@test "validate image signed by leaf cert" {
teardown() {
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete keymanagementproviders.config.ratify.deislabs.io/keymanagementprovider-inline --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo-leaf --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo-leaf2 --namespace default --force --ignore-not-found=true'
# restore the original notation verifier for other tests
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl apply -f ./config/samples/clustered/verifier/config_v1beta1_verifier_notation.yaml'
}
# configure the default template/constraint
run kubectl apply -f ./library/multi-tenancy-validation/template.yaml
assert_success
run kubectl apply -f ./library/multi-tenancy-validation/samples/constraint.yaml
assert_success
# add the root certificate as an inline key management provider
cat ~/.config/notation/truststore/x509/ca/leaf-test/root.crt | sed 's/^/ /g' >>./test/bats/tests/config/config_v1beta1_keymanagementprovider_inline.yaml
run kubectl apply -f ./test/bats/tests/config/config_v1beta1_keymanagementprovider_inline.yaml --namespace ${RATIFY_NAMESPACE}
assert_success
sed -i '10,$d' ./test/bats/tests/config/config_v1beta1_keymanagementprovider_inline.yaml
# configure the notation verifier to use the inline key management provider
run kubectl apply -f ./test/bats/tests/config/config_v1beta1_verifier_notation_kmprovider.yaml
assert_success
# verify that the image can be run with a root cert
run kubectl run demo-leaf --namespace default --image=registry:5000/notation:leafSigned
assert_success
# add the root certificate as an inline key management provider
cat ~/.config/notation/truststore/x509/ca/leaf-test/leaf.crt | sed 's/^/ /g' >>./test/bats/tests/config/config_v1beta1_keymanagementprovider_inline.yaml
run kubectl apply -f ./test/bats/tests/config/config_v1beta1_keymanagementprovider_inline.yaml --namespace ${RATIFY_NAMESPACE}
assert_success
sed -i '10,$d' ./test/bats/tests/config/config_v1beta1_keymanagementprovider_inline.yaml
# wait for the httpserver cache to be invalidated
sleep 15
# verify that the image cannot be run with a leaf cert
run kubectl run demo-leaf2 --namespace default --image=registry:5000/notation:leafSigned
assert_failure
}
@test "validate ratify/gatekeeper tls cert rotation" {
teardown() {
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod demo --namespace default --force --ignore-not-found=true'
}
# update Providers to use the new CA
run kubectl get Provider ratify-mutation-provider -o json | jq --arg ca "$(cat .staging/rotation/ca.crt | base64)" '.spec.caBundle=$ca' | kubectl replace -f -
run kubectl get Provider ratify-provider -o json | jq --arg ca "$(cat .staging/rotation/ca.crt | base64)" '.spec.caBundle=$ca' | kubectl replace -f -
# update the ratify tls secret to use the new tls cert and key
run kubectl get secret ratify-tls -n ${RATIFY_NAMESPACE} -o json | jq --arg cert "$(cat .staging/rotation/server.crt | base64)" --arg key "$(cat .staging/rotation/server.key | base64)" '.data["tls.key"]=$key | .data["tls.crt"]=$cert' | kubectl replace -f -
# update the gatekeeper webhook server tls secret to use the new cert bundle
run kubectl get Secret gatekeeper-webhook-server-cert -n ${RATIFY_NAMESPACE} -o json | jq --arg caCert "$(cat .staging/rotation/gatekeeper/ca.crt | base64)" --arg caKey "$(cat .staging/rotation/gatekeeper/ca.key | base64)" --arg tlsCert "$(cat .staging/rotation/gatekeeper/server.crt | base64)" --arg tlsKey "$(cat .staging/rotation/gatekeeper/server.key | base64)" '.data["ca.crt"]=$caCert | .data["ca.key"]=$caKey | .data["tls.crt"]=$tlsCert | .data["tls.key"]=$tlsKey' | kubectl replace -f -
# volume projection can take up to 90 seconds
sleep 100
# verify that the verification succeeds
run kubectl apply -f ./library/multi-tenancy-validation/template.yaml
assert_success
sleep 5
run kubectl apply -f ./library/multi-tenancy-validation/samples/constraint.yaml
assert_success
sleep 5
run kubectl run demo --namespace default --image=registry:5000/notation:signed
assert_success
}
@test "namespaced notation/cosign verifiers test" {
teardown() {
echo "cleaning up"
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete namespacedverifiers.config.ratify.deislabs.io/verifier-cosign --namespace default --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete namespacedverifiers.config.ratify.deislabs.io/verifier-notation --namespace default --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl apply -f ./config/samples/clustered/verifier/config_v1beta1_verifier_notation.yaml'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl apply -f ./config/samples/clustered/verifier/config_v1beta1_verifier_cosign.yaml'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete namespacedkeymanagementproviders.config.ratify.deislabs.io/ratify-notation-inline-cert-0 -n default --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl apply -f clusternotationkmprovider.yaml'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete namespacedkeymanagementproviders.config.ratify.deislabs.io/ratify-cosign-inline-key-0 -n default --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl apply -f clustercosignkmprovider.yaml'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete namespacedpolicies.config.ratify.deislabs.io/ratify-policy --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl apply -f clusterpolicy.yaml'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod notation-demo --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod notation-demo1 --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod cosign-demo-key --namespace default --force --ignore-not-found=true'
wait_for_process ${WAIT_TIME} ${SLEEP_TIME} 'kubectl delete pod cosign-demo-unsigned --namespace default --force --ignore-not-found=true'
}
run kubectl apply -f ./library/multi-tenancy-validation/template.yaml
run kubectl apply -f ./library/multi-tenancy-validation/samples/constraint.yaml
sleep 3
# apply namespaced policy and delete cluster-wide policy.
run bash -c "kubectl get policies.config.ratify.deislabs.io/ratify-policy -o yaml > clusterpolicy.yaml"
assert_success
sed 's/kind: Policy/kind: NamespacedPolicy/;/^\s*resourceVersion:/d' clusterpolicy.yaml >namespacedpolicy.yaml
run kubectl apply -f namespacedpolicy.yaml
assert_success
# apply namespaced kmp and delete cluster-wide kmp.
run bash -c "kubectl get keymanagementproviders.config.ratify.deislabs.io/ratify-notation-inline-cert-0 -o yaml > clusternotationkmprovider.yaml"
assert_success
sed 's/KeyManagementProvider/NamespacedKeyManagementProvider/' clusternotationkmprovider.yaml >namespacednotationkmprovider.yaml
run kubectl apply -f namespacednotationkmprovider.yaml
assert_success
run bash -c "kubectl get keymanagementproviders.config.ratify.deislabs.io/ratify-cosign-inline-key-0 -o yaml > clustercosignkmprovider.yaml"
assert_success
sed 's/KeyManagementProvider/NamespacedKeyManagementProvider/;/^\s*resourceVersion:/d' clustercosignkmprovider.yaml >namespacedcosignkmprovider.yaml
run kubectl delete namespacedkeymanagementproviders.config.ratify.deislabs.io/ratify-cosign-inline-key-0 -n default --ignore-not-found=true
sleep 5
run kubectl apply -f namespacedcosignkmprovider.yaml
assert_success
sleep 5
# apply namespaced notation verifiers and delete cluster-wide notation verifiers.
run kubectl apply -f ./config/samples/namespaced/verifier/config_v1beta1_verifier_notation.yaml
run kubectl delete verifiers.config.ratify.deislabs.io/verifier-notation --ignore-not-found=true
# validate notation images.
run kubectl run notation-demo --namespace default --image=registry:5000/notation:signed
assert_success
run kubectl run notation-demo1 --namespace default --image=registry:5000/notation:unsigned
assert_failure
# apply namespaced cosign verifiers and delete cluster-wide cosign verifiers.
run kubectl apply -f ./config/samples/namespaced/verifier/config_v1beta1_verifier_cosign.yaml
run kubectl delete verifiers.config.ratify.deislabs.io/verifier-cosign --ignore-not-found=true
# validate cosign images.
run kubectl run cosign-demo-key --namespace default --image=registry:5000/cosign:signed-key
assert_success
run kubectl run cosign-demo-unsigned --namespace default --image=registry:5000/cosign:unsigned
assert_failure
}