|
| 1 | +# Chains Signed Provenance Tutorial |
| 2 | + |
| 3 | +This tutorial will cover how to set up Chains to sign OCI images built in Tekton, and how to automatically generate and sign in-toto attestations for each image. |
| 4 | +This tutorial will also cover how to store these attestations in a transparency log and query the log for the attestation. |
| 5 | + |
| 6 | +This tutorial will guide you through: |
| 7 | +* Generating your own keypair and storing it as a Kubernetes Secret |
| 8 | +* Setting up authentication for your OCI registry to store images, image signatures and signed image attestations |
| 9 | +* Configuring Tekton Chains to generate and sign provenance |
| 10 | +* Building an image with kaniko in a Tekton TaskRun |
| 11 | +* Verifying the signed image and the signed provenance |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | +A Kubernetes cluster with the following installed: |
| 15 | +* Tekton Chains |
| 16 | +* Tekton Pipelines |
| 17 | + |
| 18 | + |
| 19 | +## Generate a Key Pair |
| 20 | +First, we'll generate an encrypted x509 keypair and save it as a Kubernetes secret. |
| 21 | +Install [cosign](https://github.com/sigstore/cosign) and run the following: |
| 22 | + |
| 23 | +```shell |
| 24 | +cosign generate-key-pair k8s://tekton-chains/signing-secrets |
| 25 | +``` |
| 26 | + |
| 27 | +cosign will prompt you for a password, which will be stored in a Kubernetes secret named `signing-secrets` in the `tekton-chains` namespace. |
| 28 | + |
| 29 | +The public key will be written to a local file called `cosign.pub`. |
| 30 | + |
| 31 | + |
| 32 | +## Set up Authentication |
| 33 | +There are two forms of authentication that need to be set up: |
| 34 | +1. The default service account in the default namespace needs permission to push to your registry, since this is what Chains will be using for pushing signatures. See our [authentication doc](../authentication.md) |
| 35 | +1. The Kaniko Task that will build and push the image needs push permissions for your registry |
| 36 | + |
| 37 | +To set up auth for the Kaniko Task, you'll need a Kubernetes secret of a docker `config.json` file which contains the required auth. |
| 38 | +You can create the secret by running: |
| 39 | + |
| 40 | +``` |
| 41 | +kubectl create secret generic [DOCKERCONFIG_SECRET_NAME] --from-file [PATH TO CONFIG.JSON] |
| 42 | +``` |
| 43 | + |
| 44 | +## Configuring Tekton Chains |
| 45 | +You'll need to make these changes to the Tekton Chains Config: |
| 46 | +* `artifacts.taskrun.format=tekton-provenance` |
| 47 | +* `artifacts.taskrun.storage=oci` |
| 48 | +* `transparency.enabled=true` |
| 49 | + |
| 50 | +You can set these fields by running |
| 51 | + |
| 52 | +``` |
| 53 | +kubectl patch configmap chains-config -n tekton-chains -p='{"data":{"artifacts.taskrun.format": "tekton-provenance"}}' |
| 54 | +kubectl patch configmap chains-config -n tekton-chains -p='{"data":{"artifacts.taskrun.storage": "oci"}}' |
| 55 | +kubectl patch configmap chains-config -n tekton-chains -p='{"data":{"transparency.enabled": "true"}}' |
| 56 | +``` |
| 57 | + |
| 58 | +This tells Chains to generate an in-toto attestation and store it in the specified OCI registry. |
| 59 | +Attestations will also be stored in [rekor](https://github.com/sigstore/rekor) since transparency is enabled. |
| 60 | + |
| 61 | +## Start the Kaniko Task |
| 62 | +Great, now that the setup is done we're finally ready to build an image with kaniko! |
| 63 | + |
| 64 | +First, apply the [Kaniko Task](../../examples/kaniko/kaniko.yaml) to your cluster: |
| 65 | + |
| 66 | +``` |
| 67 | +kubectl apply -f examples/kaniko/kaniko.yaml |
| 68 | +``` |
| 69 | + |
| 70 | +and set the following environment variables: |
| 71 | + |
| 72 | +``` |
| 73 | +REGISTRY=[The registry you'll be pushing to] |
| 74 | +DOCKERCONFIG_SECRET_NAME=[The name of the secret with the docker config.json] |
| 75 | +``` |
| 76 | + |
| 77 | +Then, you can start the Kaniko Task with the Tekton CLI tool, [tkn](https://github.com/tektoncd/cli): |
| 78 | + |
| 79 | +``` |
| 80 | +tkn task start --param IMAGE=$REGISTRY/kaniko-chains --use-param-defaults --workspace name=source,emptyDir="" --workspace name=dockerconfig,secret=$DOCKERCONFIG_SECRET_NAME kaniko-chains |
| 81 | +``` |
| 82 | + |
| 83 | +You can watch the logs of this Task until they complete; if authentication is set up correctly than the final image should be pushed to `$REGISTRY/kaniko-chains`. |
| 84 | + |
| 85 | + |
| 86 | +## Verifying the Image and Attestation |
| 87 | +Once the TaskRun has successfully completed, you'll need to wait a few seconds for Chains to generate provenance and sign it. |
| 88 | + |
| 89 | +Once you see the `chains.tekton.dev/signed=true` annotation on your TaskRun you know that Chains has completed the signing process and you're ready to move on to verification: |
| 90 | + |
| 91 | +``` |
| 92 | +kubectl get tr [TASKRUN_NAME] -o json | jq -r .metadata.annotations |
| 93 | +
|
| 94 | +{ |
| 95 | + "chains.tekton.dev/signed": "true", |
| 96 | + ... |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +To verify the image and the attestation, we'll use `cosign` again: |
| 101 | + |
| 102 | +``` |
| 103 | +cosign verify -key cosign.pub $REGISTRY/kaniko-chains |
| 104 | +cosign verify-attestation -key cosign.pub $REGISTRY/kaniko-chains |
| 105 | +``` |
| 106 | + |
| 107 | +You should see verification output for both! |
| 108 | + |
| 109 | +## Finding Provenance in Rekor |
| 110 | +To find provenance for the image in Rekor, first get the digest of the `$REGISTRY/kaniko-chains` image you just built. |
| 111 | +You can look this up in the TaskRun, or pull the image to get the digest. |
| 112 | + |
| 113 | +You can then search rekor to find all entries that match the sha256 digest of the image you just built with the [rekor-cli](https://github.com/sigstore/rekor/releases/) tool: |
| 114 | + |
| 115 | +``` |
| 116 | +rekor-cli search --sha [IMAGE_DIGEST] |
| 117 | +
|
| 118 | +[UUID1] |
| 119 | +[UUID2] |
| 120 | +``` |
| 121 | + |
| 122 | +The search will print out the UUIDs of matching entries. |
| 123 | +It may take a little guessing, but one of those UUIDs holds the attestation. |
| 124 | +You can see the attestation by using [jq](https://github.com/stedolan/jq): |
| 125 | + |
| 126 | +``` |
| 127 | +rekor-cli get --uuid [UUID] --format json | jq -r .Attestation | base64 --decode | jq |
| 128 | +``` |
| 129 | + |
| 130 | +Congratulations! You have officially built an image, signed it, and generated signed provenance for it with Tekton Chains 🎉 |
0 commit comments