|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# verify-base-images.sh verifies that base images used in Dockerfiles are |
| 4 | +# referenced by image digest and signed by their developers. It should be run at |
| 5 | +# the git repository's root directory. |
| 6 | +# |
| 7 | +# distroless images are verified with cosign using the distroless project's |
| 8 | +# public key available here: |
| 9 | +# https://github.com/GoogleContainerTools/distroless#how-do-i-verify-distroless-images |
| 10 | +# |
| 11 | +# All other images are assumed to be Docker official images that are signed |
| 12 | +# using Docker Content Trust (https://docs.docker.com/engine/security/trust/). |
| 13 | +# The public key for Docker official images in included in Docker releases by |
| 14 | +# default so no signers or keys need to be added. |
| 15 | + |
| 16 | +set -euo pipefail |
| 17 | + |
| 18 | +# NOTE: Use read to avoid whitespace issues. |
| 19 | +find . -name Dockerfile -print0 | while IFS= read -r -d '' f; do |
| 20 | + echo "Checking $f" |
| 21 | + grep "^FROM " "$f" | while IFS= read -r line; do |
| 22 | + image_full=$(echo "$line" | awk '{ print $2 }') |
| 23 | + image_name=$(echo "$image_full" | cut -d '@' -f 1) |
| 24 | + image_sha=$(echo "$image_full" | cut -d '@' -f 2- | cut -d ':' -f 2-) |
| 25 | + |
| 26 | + echo "Verifying base image $image_full" |
| 27 | + |
| 28 | + # verify that the image contains a sha. |
| 29 | + if [ "$image_sha" == "" ]; then |
| 30 | + echo "\"$image_full\" should be referenced by digest." |
| 31 | + exit 2 |
| 32 | + fi |
| 33 | + |
| 34 | + # verify distroless base images. |
| 35 | + if [[ "$image_name" == gcr.io/distroless/* ]]; then |
| 36 | + # verify the image signature. |
| 37 | + cosign verify --key .github/workflows/scripts/distroless.pub "$image_full" |
| 38 | + else |
| 39 | + # All other base images should be signed using Docker Content Trust. |
| 40 | + if ! (DOCKER_CONTENT_TRUST=1 docker trust inspect --pretty "$image_name" | grep "$image_sha"); then |
| 41 | + echo "$image_full: unable to verify Docker Content Trust." |
| 42 | + exit 2 |
| 43 | + fi |
| 44 | + fi |
| 45 | + done |
| 46 | +done |
0 commit comments