|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright 2022 The Sigstore Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | + |
| 18 | +set -ex |
| 19 | + |
| 20 | +if [[ -z "${KO_DOCKER_REPO}" ]]; then |
| 21 | + echo "Must specify env variable KO_DOCKER_REPO" |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +if [[ -z "${FULCIO_URL}" ]]; then |
| 26 | + echo "Must specify env variable FULCIO_URL" |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +if [[ -z "${REKOR_URL}" ]]; then |
| 31 | + echo "Must specify env variable REKOR_URL" |
| 32 | + exit 1 |
| 33 | +fi |
| 34 | + |
| 35 | +if [[ "${NON_REPRODUCIBLE}"=="1" ]]; then |
| 36 | + echo "creating non-reproducible build by adding a timestamp" |
| 37 | + export TIMESTAMP=`date +%s` |
| 38 | +else |
| 39 | + export TIMESTAMP="TIMESTAMP" |
| 40 | +fi |
| 41 | + |
| 42 | +# To simplify testing failures, use this function to execute a kubectl to create |
| 43 | +# our job and verify that the failure is expected. |
| 44 | +assert_error() { |
| 45 | + local KUBECTL_OUT_FILE="/tmp/kubectl.failure.out" |
| 46 | + match="$@" |
| 47 | + echo looking for ${match} |
| 48 | + kubectl delete job demo -n ${NS} --ignore-not-found=true |
| 49 | + if kubectl create -n ${NS} job demo --image=${demoimage} 2> ${KUBECTL_OUT_FILE} ; then |
| 50 | + echo Failed to block expected Job failure! |
| 51 | + exit 1 |
| 52 | + else |
| 53 | + echo Successfully blocked Job creation with expected error: "${match}" |
| 54 | + if ! grep -q "${match}" ${KUBECTL_OUT_FILE} ; then |
| 55 | + echo Did not get expected failure message, wanted "${match}", got |
| 56 | + cat ${KUBECTL_OUT_FILE} |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +# Publish test image |
| 63 | +echo '::group:: publish test image demoimage' |
| 64 | +pushd $(mktemp -d) |
| 65 | +go mod init example.com/demo |
| 66 | +cat <<EOF > main.go |
| 67 | +package main |
| 68 | +import "fmt" |
| 69 | +func main() { |
| 70 | + fmt.Println("hello world TIMESTAMP") |
| 71 | +} |
| 72 | +EOF |
| 73 | + |
| 74 | +sed -i'' -e "s@TIMESTAMP@${TIMESTAMP}@g" main.go |
| 75 | +cat main.go |
| 76 | +export demoimage=`ko publish -B example.com/demo` |
| 77 | +echo Created image $demoimage |
| 78 | +popd |
| 79 | +echo '::endgroup::' |
| 80 | + |
| 81 | +echo '::group:: Create and label new namespace for verification' |
| 82 | +kubectl create namespace demo-no-tuf |
| 83 | +kubectl label namespace demo-no-tuf policy.sigstore.dev/include=true |
| 84 | +export NS=demo-no-tuf |
| 85 | +echo '::endgroup::' |
| 86 | + |
| 87 | +echo '::group:: Generate New Signing Key that we use for key-ful signing' |
| 88 | +COSIGN_PASSWORD="" cosign generate-key-pair |
| 89 | +echo '::endgroup::' |
| 90 | + |
| 91 | +# Create CIP that requires a signature with a key. |
| 92 | +echo '::group:: Create CIP that requires a keyful signature' |
| 93 | +yq '. | .spec.authorities[0].key.data |= load_str("cosign.pub")' ./test/testdata/policy-controller/e2e/cip-key-no-rekor.yaml | kubectl apply -f - |
| 94 | + |
| 95 | +# Give the policy controller a moment to update the configmap |
| 96 | +# and pick up the change in the admission controller. |
| 97 | +sleep 5 |
| 98 | +echo '::endgroup::' |
| 99 | + |
| 100 | +# This image has not been signed with our key |
| 101 | +# so should fail |
| 102 | +echo '::group:: test job rejection' |
| 103 | +expected_error='no matching signatures' |
| 104 | +assert_error ${expected_error} |
| 105 | +echo '::endgroup::' |
| 106 | + |
| 107 | +# Sign it with key |
| 108 | +echo '::group:: Sign demoimage with key, do not add to rekor' |
| 109 | +COSIGN_PASSWORD="" cosign sign --no-tlog-upload --key cosign.key --allow-insecure-registry ${demoimage} |
| 110 | +echo '::endgroup::' |
| 111 | + |
| 112 | +# TODO(vaikas): This fails because it doesn't have a Rekor entry. Which it obvs |
| 113 | +# does not because of --no-tlog-upload above. |
| 114 | +#echo '::group:: Verify demoimage with cosign key' |
| 115 | +#cosign verify --key cosign.pub --allow-insecure-registry ${demoimage} |
| 116 | +#echo '::endgroup::' |
| 117 | + |
| 118 | +# Then let's test attestations work too with key. |
| 119 | +echo '::group:: Create CIP that requires a keyful attestation' |
| 120 | +yq '. | .spec.authorities[0].key.data |= load_str("cosign.pub")' ./test/testdata/policy-controller/e2e/cip-key-with-attestations-no-rekor.yaml | kubectl apply -f - |
| 121 | + |
| 122 | +# Give the policy controller a moment to update the configmap |
| 123 | +# and pick up the change in the admission controller. |
| 124 | +sleep 5 |
| 125 | +echo '::endgroup::' |
| 126 | + |
| 127 | +# This image has been signed with key, but does not have a key attestation |
| 128 | +# so should fail |
| 129 | +echo '::group:: test job rejection' |
| 130 | +expected_error='no matching attestations' |
| 131 | +assert_error ${expected_error} |
| 132 | +echo '::endgroup::' |
| 133 | + |
| 134 | +# Fine, so create an attestation for it. |
| 135 | +echo '::group:: create keyful attestation, do not add to rekor' |
| 136 | +echo -n 'foobar key e2e test' > ./predicate-file-key-custom |
| 137 | +COSIGN_PASSWORD="" cosign attest --predicate ./predicate-file-key-custom --key ./cosign.key --allow-insecure-registry --no-tlog-upload ${demoimage} |
| 138 | + |
| 139 | +# TODO(vaikas): This again fails though it really shouldn't. |
| 140 | +#cosign verify-attestation --key ./cosign.pub --allow-insecure-registry ${demoimage} |
| 141 | +echo '::endgroup::' |
| 142 | + |
| 143 | +export KUBECTL_SUCCESS_FILE="/tmp/kubectl.success.out" |
| 144 | +echo '::group:: test job success with key signature and key attestation' |
| 145 | +# We signed this with key and it has a key attestation, so should pass. |
| 146 | +if ! kubectl create -n ${NS} job demo2 --image=${demoimage} 2> ${KUBECTL_SUCCESS_FILE} ; then |
| 147 | + echo Failed to create job with both key signature and attestation |
| 148 | + cat ${KUBECTL_SUCCESS_FILE} |
| 149 | + exit 1 |
| 150 | +else |
| 151 | + echo Created the job with key signature and an attestation |
| 152 | +fi |
| 153 | +echo '::endgroup::' |
| 154 | + |
| 155 | +echo '::group::' Cleanup |
| 156 | +kubectl delete cip --all |
| 157 | +kubectl delete ns ${NS} |
| 158 | +rm cosign.key cosign.pub |
| 159 | +echo '::endgroup::' |
0 commit comments