forked from sigstore/policy-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e_test_cluster_with_scalable.sh
executable file
·156 lines (133 loc) · 4.65 KB
/
e2e_test_cluster_with_scalable.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
#!/usr/bin/env bash
#
# Copyright 2022 The Sigstore 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.
set -ex
if [[ -z "${OIDC_TOKEN}" ]]; then
if [[ -z "${TOKEN_ISSUER}" ]]; then
echo "Must specify either env variable OIDC_TOKEN or TOKEN_ISSUER"
exit 1
else
export OIDC_TOKEN=`curl -s ${ISSUER_URL}`
fi
fi
if [[ -z "${KO_DOCKER_REPO}" ]]; then
echo "Must specify env variable KO_DOCKER_REPO"
exit 1
fi
if [[ -z "${FULCIO_URL}" ]]; then
echo "Must specify env variable FULCIO_URL"
exit 1
fi
if [[ -z "${REKOR_URL}" ]]; then
echo "Must specify env variable REKOR_URL"
exit 1
fi
if [[ -z "${TUF_ROOT_FILE}" ]]; then
echo "must specify env variable TUF_ROOT_FILE"
exit 1
fi
if [[ -z "${TUF_MIRROR}" ]]; then
echo "must specify env variable TUF_MIRROR"
exit 1
fi
if [[ "${NON_REPRODUCIBLE}"=="1" ]]; then
echo "creating non-reproducible build by adding a timestamp"
export TIMESTAMP=`date +%s`
else
export TIMESTAMP="TIMESTAMP"
fi
# Initialize cosign with our TUF root
cosign initialize --mirror ${TUF_MIRROR} --root ${TUF_ROOT_FILE}
# To simplify testing failures, use this function to execute a kubectl to scale
# deployment up and verify that the failure is expected.
assert_error() {
local KUBECTL_OUT_FILE="/tmp/kubectl.failure.out"
match="$@"
echo looking for ${match}
if kubectl patch -n ${NS} deployment test-deployment --type "json" -p '[{"op":"replace", "path":"/spec/replicas", "value":5}]' 2> ${KUBECTL_OUT_FILE} ; then
echo Failed to block expected scaling failure!
exit 1
else
echo Successfully blocked scaling with expected error: "${match}"
if ! grep -q "${match}" ${KUBECTL_OUT_FILE} ; then
echo Did not get expected failure message, wanted "${match}", got
cat ${KUBECTL_OUT_FILE}
exit 1
fi
fi
}
# Publish test image
echo '::group:: publish test image demoimage'
pushd $(mktemp -d)
go mod init example.com/demo
cat <<EOF > main.go
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("hello world deployment TIMESTAMP")
time.Sleep(10*time.Minute)
}
EOF
sed -i'' -e "s@TIMESTAMP@${TIMESTAMP}@g" main.go
cat main.go
export demoimage=`ko publish -B example.com/demo`
echo Created image $demoimage
popd
echo '::endgroup::'
echo '::group:: Create test namespace but do not label for verification'
kubectl create namespace demo-scalable
export NS=demo-scalable
echo '::endgroup::'
echo '::group:: Deploy deployment with unsigned image'
sed "s#TEST_IMAGE#${demoimage}#" ./test/testdata/policy-controller/e2e/test-deployment.yaml | kubectl apply -f -
echo '::endgroup::'
echo '::group:: Label test namespace for verification'
kubectl label namespace ${NS} policy.sigstore.dev/include=true
echo '::endgroup::'
echo '::group:: Deploy ClusterImagePolicy with keyless signing'
kubectl apply -f ./test/testdata/policy-controller/e2e/cip-keyless.yaml
# Give the policy controller a moment to update the configmap
# and pick up the change in the admission controller.
sleep 5
echo '::endgroup::'
echo '::group:: Try to scale the Deployment up - should fail'
expected_error="failed policy: image-policy-keyless: spec.template.spec.containers"
assert_error ${expected_error}
echo '::endgroup::'
echo '::group:: Try to scale the Deployment down - should work'
if ! kubectl patch -n ${NS} deployment test-deployment --type "json" -p '[{"op":"replace", "path":"/spec/replicas", "value":1}]' ; then
echo Failed to scale down
exit 1
fi
echo '::endgroup::'
echo '::group:: Sign demo image'
cosign sign --rekor-url ${REKOR_URL} --fulcio-url ${FULCIO_URL} --yes --allow-insecure-registry ${demoimage} --identity-token ${OIDC_TOKEN}
echo '::endgroup::'
echo '::group:: Verify demo image'
cosign verify --rekor-url ${REKOR_URL} --allow-insecure-registry --certificate-identity-regexp='.*' --certificate-oidc-issuer-regexp='.*' ${demoimage}
echo '::endgroup::'
echo '::group:: Try to scale the Deployment up - should work'
if ! kubectl patch -n ${NS} deployment test-deployment --type "json" -p '[{"op":"replace", "path":"/spec/replicas", "value":5}]' ; then
echo Failed to scale up with signed image
exit 1
fi
echo '::endgroup::'
echo '::group::' Cleanup
kubectl delete cip --all
kubectl delete ns ${NS}
echo '::endgroup::'