diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1f711a1..16fc0ef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -65,6 +65,7 @@ jobs: run: | docker build \ -f apps/gateway/Dockerfile.enclave \ + --build-arg VERSION=v0.0.0-ci \ --build-arg SOURCE_REVISION="$(git rev-parse HEAD)" \ --build-arg AWS_REGION=eu-central-1 \ --build-arg KMS_KEY_ARN=arn:aws:kms:eu-central-1:111122223333:key/12345678-1234-1234-1234-123456789012 \ diff --git a/apps/gateway/Dockerfile.enclave b/apps/gateway/Dockerfile.enclave index 89710b0..f7325af 100644 --- a/apps/gateway/Dockerfile.enclave +++ b/apps/gateway/Dockerfile.enclave @@ -2,13 +2,14 @@ # Pin upstream build inputs by digest. Together with SOURCE_DATE_EPOCH and # rewrite-timestamp (see deploy/nitro/build-release-assets.sh) this builds -# reproducibly: the same revision yields the same PCR0/PCR1/PCR2 on any x86_64 -# machine, so a published release can be independently rebuilt and checked with -# deploy/nitro/verify-build.sh. Build this image with `docker buildx build` +# reproducibly: the same revision and release version yield the same +# PCR0/PCR1/PCR2 on any x86_64 machine, so a published release can be +# independently rebuilt and checked with deploy/nitro/verify-build.sh. Build this image with `docker buildx build` # only -- a plain `docker build` skips the timestamp normalisation and silently # produces different measurements. FROM golang:1.26.11-alpine3.24@sha256:ae52b5bc6ed045a9ed26ab03dc19327a802a9cc9aa8b27ed57f5c6b60579b0bb AS builder +ARG VERSION ARG SOURCE_REVISION ARG AWS_REGION ARG KMS_KEY_ARN @@ -21,6 +22,7 @@ RUN go mod download COPY . . # Reject values that could break linker-flag tokenization. +RUN printf '%s' "$VERSION" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+([.-][0-9A-Za-z.-]+)?$' RUN printf '%s' "$SOURCE_REVISION" | grep -Eq '^[0-9a-f]{40}$' RUN printf '%s' "$AWS_REGION" | grep -Eq '^[a-z]{2}(-gov)?-[a-z]+-[0-9]+$' RUN printf '%s' "$KMS_KEY_ARN" | grep -Eq '^arn:(aws|aws-us-gov|aws-cn):kms:[a-z0-9-]+:[0-9]{12}:key/[0-9a-fA-F-]{36}$' @@ -30,6 +32,8 @@ RUN CGO_ENABLED=0 GOOS=linux go build \ -trimpath \ -tags nitro_enclave \ -ldflags="-s -w -buildid= \ + -X github.com/dappnode/dappnode-nexus-gateway/pkg/buildinfo.Version=${VERSION} \ + -X github.com/dappnode/dappnode-nexus-gateway/pkg/buildinfo.Commit=${SOURCE_REVISION} \ -X github.com/dappnode/dappnode-nexus-gateway/apps/gateway/internal/enclave/profile.SourceRevision=${SOURCE_REVISION} \ -X github.com/dappnode/dappnode-nexus-gateway/apps/gateway/internal/enclave/profile.AWSRegion=${AWS_REGION} \ -X github.com/dappnode/dappnode-nexus-gateway/apps/gateway/internal/enclave/profile.KMSKeyARN=${KMS_KEY_ARN}" \ diff --git a/deploy/nitro/build-release-assets.sh b/deploy/nitro/build-release-assets.sh index 3f58741..4b233aa 100755 --- a/deploy/nitro/build-release-assets.sh +++ b/deploy/nitro/build-release-assets.sh @@ -107,6 +107,7 @@ build_enclave_image() { --label "org.opencontainers.image.source=https://github.com/${SOURCE_REPOSITORY}" \ --label "org.opencontainers.image.revision=${SOURCE_REVISION}" \ --label "org.opencontainers.image.version=${VERSION}" \ + --build-arg "VERSION=${VERSION}" \ --build-arg "SOURCE_REVISION=${SOURCE_REVISION}" \ --build-arg "AWS_REGION=${AWS_REGION}" \ --build-arg "KMS_KEY_ARN=${KMS_KEY_ARN}" \ diff --git a/deploy/nitro/verify-build.sh b/deploy/nitro/verify-build.sh index c6d906d..ea1f6ab 100755 --- a/deploy/nitro/verify-build.sh +++ b/deploy/nitro/verify-build.sh @@ -60,11 +60,12 @@ mapfile -t manifest_fields < <( jq -r ' .schema_version, .source.revision, .build.aws_region, .build.kms_key_arn, .build.source_date_epoch, .build.nitro_cli_version, .build.architecture, - .enclave.measurements.pcr0, .enclave.measurements.pcr1, .enclave.measurements.pcr2 + .enclave.measurements.pcr0, .enclave.measurements.pcr1, .enclave.measurements.pcr2, + .release.version ' "$manifest_path" ) -if [[ "${#manifest_fields[@]}" -ne 10 ]]; then +if [[ "${#manifest_fields[@]}" -ne 11 ]]; then printf 'not a Gateway release manifest: %s\n' "$manifest_path" >&2 exit 1 fi @@ -79,6 +80,7 @@ architecture="${manifest_fields[6]}" expected_pcr0="${manifest_fields[7]}" expected_pcr1="${manifest_fields[8]}" expected_pcr2="${manifest_fields[9]}" +version="${manifest_fields[10]}" if [[ ! "$schema" =~ ^[0-9]+$ ]]; then printf 'manifest has no usable schema_version: %s\n' "$manifest_path" >&2 @@ -93,7 +95,8 @@ fi for pair in "revision:$revision" "aws_region:$region" "kms_key_arn:$kms_key_arn" \ "source_date_epoch:$source_date_epoch" "architecture:$architecture" \ - "pcr0:$expected_pcr0" "pcr1:$expected_pcr1" "pcr2:$expected_pcr2"; do + "pcr0:$expected_pcr0" "pcr1:$expected_pcr1" "pcr2:$expected_pcr2" \ + "release.version:$version"; do [[ -n "${pair#*:}" && "${pair#*:}" != "null" ]] || { printf 'manifest is missing %s\n' "${pair%%:*}" >&2; exit 1; } done @@ -144,7 +147,11 @@ docker buildx build --pull -q --load \ -t "$nitro_cli_image" \ "$context/deploy/nitro" >>"$build_log" 2>&1 || fail +# VERSION is compiled into the binary, so it is part of the measurements. Older +# revisions' Dockerfiles do not declare it; BuildKit only warns about an unused +# build argument, so passing it unconditionally leaves their rebuild unchanged. SOURCE_DATE_EPOCH="$source_date_epoch" docker buildx build --pull --no-cache --provenance=false \ + --build-arg "VERSION=${version}" \ --build-arg "SOURCE_REVISION=${revision}" \ --build-arg "AWS_REGION=${region}" \ --build-arg "KMS_KEY_ARN=${kms_key_arn}" \ @@ -154,7 +161,8 @@ SOURCE_DATE_EPOCH="$source_date_epoch" docker buildx build --pull --no-cache --p # The image labels carry the version and repository. They live in the image # config rather than the filesystem, so they do not reach the measurements and -# are deliberately not reproduced here. +# are deliberately not reproduced here. (The version also reaches the binary, as +# the VERSION build argument above.) docker run --rm \ -v /var/run/docker.sock:/var/run/docker.sock \ -v "${artifacts}:/artifacts" \ @@ -162,7 +170,7 @@ docker run --rm \ --docker-uri "$enclave_image" \ --output-file /artifacts/verify.eif \ --name nexus-gateway \ - --version "$(jq -r '.release.version' "$manifest_path")" >>"$build_log" 2>&1 || fail + --version "$version" >>"$build_log" 2>&1 || fail describe="$(docker run --rm -v "${artifacts}:/artifacts" "$nitro_cli_image" \ describe-eif --eif-path /artifacts/verify.eif 2>>"$build_log")" || fail