Skip to content

Commit e77551c

Browse files
author
Ian Lewis
authored
Add pre-submit to verify base images (slsa-framework#592)
* Add comments to verify new base image digests * Add pre-submit to verify Dockerfile base images. * add step to install cosign Signed-off-by: Ian Lewis <[email protected]> * Use specific golang version for tag Signed-off-by: Ian Lewis <[email protected]> * retab Signed-off-by: Ian Lewis <[email protected]> * Add description comment Signed-off-by: Ian Lewis <[email protected]> Signed-off-by: Ian Lewis <[email protected]>
1 parent 9082f8b commit e77551c

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

.github/actions/detect-workflow/Dockerfile

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
FROM golang@sha256:9349ed889adb906efa5ebc06485fe1b6a12fb265a01c9266a137bb1352565560 as builder
15+
FROM golang:1.18.5@sha256:5540a6a6b3b612c382accc545b3f6702de21e77b15d89ad947116c94b5f42993 as builder
1616

1717
WORKDIR /app
1818
COPY . /app
@@ -22,8 +22,6 @@ RUN go get -d -v
2222
# Statically compile our app for use in a distroless container
2323
RUN CGO_ENABLED=0 go build -ldflags="-w -s" -v -o app .
2424

25-
# A distroless container image with some basics like SSL certificates
26-
# https://github.com/GoogleContainerTools/distroless
2725
FROM gcr.io/distroless/static@sha256:21d3f84a4f37c36199fd07ad5544dcafecc17776e3f3628baf9a57c8c0181b3f
2826

2927
COPY --from=builder /app/app /app
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: pre-submit base images
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
workflow_dispatch:
7+
8+
permissions: read-all
9+
10+
jobs:
11+
verify-base-images:
12+
name: verify base images
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: checkout
16+
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3.0.2
17+
- name: install cosign
18+
uses: sigstore/cosign-installer@b3413d484cc23cf8778c3d2aa361568d4eb54679 # tag=v2.5.1
19+
- name: verify images
20+
run: ./.github/workflows/scripts/verify-base-images.sh
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEWZzVzkb8A+DbgDpaJId/bOmV8n7Q
3+
OqxYbK0Iro6GzSmOzxkn+N2AKawLyXi84WSwJQBK//psATakCgAQKkNTAA==
4+
-----END PUBLIC KEY-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)