From 09e19adbf9e0becec59062aa216a64213c0518f8 Mon Sep 17 00:00:00 2001 From: Stephan Terning Date: Tue, 10 Nov 2026 08:40:02 +0100 Subject: [PATCH 1/2] Preserve published commit images and refuse version reassignment --- .github/workflows/ci.yml | 2 + .github/workflows/release.yml | 39 ++++++------ docs/operations.md | 6 +- infrastructure/scripts/image-digest.sh | 18 ++++++ infrastructure/scripts/publish-version.sh | 32 ++++++++++ .../scripts/test-image-publication.py | 59 +++++++++++++++++++ 6 files changed, 137 insertions(+), 19 deletions(-) create mode 100644 infrastructure/scripts/image-digest.sh create mode 100644 infrastructure/scripts/publish-version.sh create mode 100644 infrastructure/scripts/test-image-publication.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bbc01e5..3ad1519 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,6 +22,8 @@ jobs: TEST_DATABASE_URL: postgresql://tendly:integration-only@localhost:5432/tendly_test steps: - uses: actions/checkout@v7.0.1 + - name: Check image publication safeguards + run: python3 infrastructure/scripts/test-image-publication.py - uses: pnpm/action-setup@v4 - uses: actions/setup-node@v7.1.0 with: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2a2625a..27a7f03 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -15,6 +15,10 @@ jobs: images: needs: verify runs-on: ubuntu-latest + # Serialize each package's writes, including version aliases across commits. + concurrency: + group: publish-image-${{ github.repository }}-${{ matrix.target }} + cancel-in-progress: false permissions: contents: read packages: write @@ -30,40 +34,36 @@ jobs: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Image tags - id: tags + - name: Preserve the existing commit image + id: image env: IMAGE: ghcr.io/${{ github.repository }}-${{ matrix.target }} shell: bash run: | image="${IMAGE,,}" - echo "image=$image" >> "$GITHUB_OUTPUT" - { - echo 'tags<> "$GITHUB_OUTPUT" + echo "name=$image" >> "$GITHUB_OUTPUT" + echo "existing=$(sh infrastructure/scripts/image-digest.sh "$image:sha-$GITHUB_SHA")" >> "$GITHUB_OUTPUT" - uses: docker/build-push-action@v6 - id: build + if: steps.image.outputs.existing == '' with: context: . file: infrastructure/docker/Dockerfile target: ${{ matrix.target }} platforms: linux/amd64,linux/arm64 push: true - tags: ${{ steps.tags.outputs.tags }} + tags: ${{ steps.image.outputs.name }}:sha-${{ github.sha }} labels: | org.opencontainers.image.source=https://github.com/${{ github.repository }} org.opencontainers.image.licenses=AGPL-3.0-or-later org.opencontainers.image.revision=${{ github.sha }} - - name: Record published digest + - name: Preserve version tags and record the digest env: - IMAGE: ${{ steps.tags.outputs.image }} - DIGEST: ${{ steps.build.outputs.digest }} - run: printf 'Image `%s@%s` (commit `%s`)\n' "$IMAGE" "$DIGEST" "$GITHUB_SHA" >> "$GITHUB_STEP_SUMMARY" + IMAGE: ${{ steps.image.outputs.name }} + run: | + version="" + if [ "$GITHUB_REF_TYPE" = tag ]; then version="$GITHUB_REF_NAME"; fi + sh infrastructure/scripts/publish-version.sh "$IMAGE" "$GITHUB_SHA" "$version" >> "$GITHUB_STEP_SUMMARY" + release: if: startsWith(github.ref, 'refs/tags/') needs: images @@ -76,4 +76,7 @@ jobs: env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} RELEASE_TAG: ${{ github.ref_name }} - run: gh release create "$RELEASE_TAG" --verify-tag --generate-notes + run: | + if ! gh release view "$RELEASE_TAG" --json tagName >/dev/null 2>&1; then + gh release create "$RELEASE_TAG" --verify-tag --generate-notes + fi diff --git a/docs/operations.md b/docs/operations.md index ea855b8..6cfb893 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -45,7 +45,11 @@ Confirm all three images finished publishing before deploying, and pin their individual digests for reproducible deployments. Package visibility must be public for installations to pull without registry credentials. Never reuse a released tag; record image digests in release notes. -Registry owners should restrict package/tag overwrite permissions. +Already published commit images are reused on workflow reruns. Version tags are +assigned to those existing digests; a version that already points elsewhere is +rejected. Authentication or registry failures stop publication instead of being +treated as an absent image. Registry owners should also restrict package/tag +overwrite permissions outside this workflow. For a published version, set TENDLY_VERSION to its full version or `sha-` tag, pull the images, diff --git a/infrastructure/scripts/image-digest.sh b/infrastructure/scripts/image-digest.sh new file mode 100644 index 0000000..f33a198 --- /dev/null +++ b/infrastructure/scripts/image-digest.sh @@ -0,0 +1,18 @@ +#!/bin/sh +# Print a registry image digest, or nothing when the manifest does not exist. +# Authentication, networking and other errors must fail, not permit overwrites. +set -eu +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " >&2 + exit 1 +fi +error_file=$(mktemp) +trap 'rm -f "$error_file"' EXIT HUP INT TERM +if manifest=$(docker buildx imagetools inspect "$1" --format '{{json .Manifest}}' 2>"$error_file"); then + printf '%s\n' "$manifest" | jq -er '.digest | select(test("^sha256:[0-9a-f]{64}$"))' +elif grep -Eq 'manifest unknown|MANIFEST_UNKNOWN|: not found$' "$error_file"; then + exit 0 +else + cat "$error_file" >&2 + exit 1 +fi diff --git a/infrastructure/scripts/publish-version.sh b/infrastructure/scripts/publish-version.sh new file mode 100644 index 0000000..b7d14bf --- /dev/null +++ b/infrastructure/scripts/publish-version.sh @@ -0,0 +1,32 @@ +#!/bin/sh +# Assign a version to a previously published commit image without rebuilding it. +set -eu +ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd) +image=$1 +revision=$2 +version=${3:-} +digest=$(sh "$ROOT/image-digest.sh" "$image:sha-$revision") +if [ -z "$digest" ]; then + echo 'The commit image has not finished publishing.' >&2 + exit 1 +fi +if [ -n "$version" ]; then + case "$version" in + *[!A-Za-z0-9.-]*) echo 'Invalid version tag.' >&2; exit 1 ;; + esac + if [ "${#version}" -gt 128 ] || ! printf '%s\n' "$version" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+(-[A-Za-z0-9][A-Za-z0-9.-]*)?$'; then + echo 'Use a version such as v1.2.3 or v1.2.3-rc.1.' >&2 + exit 1 + fi + existing=$(sh "$ROOT/image-digest.sh" "$image:$version") + if [ -n "$existing" ] && [ "$existing" != "$digest" ]; then + echo 'Version already points to a different image; publish a new version.' >&2 + exit 1 + fi + if [ -z "$existing" ]; then + docker buildx imagetools create --tag "$image:$version" "$image@$digest" + fi + actual=$(sh "$ROOT/image-digest.sh" "$image:$version") + test "$actual" = "$digest" +fi +printf 'Image `%s@%s` (commit `%s`)\n' "$image" "$digest" "$revision" diff --git a/infrastructure/scripts/test-image-publication.py b/infrastructure/scripts/test-image-publication.py new file mode 100644 index 0000000..0b08e5f --- /dev/null +++ b/infrastructure/scripts/test-image-publication.py @@ -0,0 +1,59 @@ +"""Exercise registry failure and version reuse with a fake Docker executable.""" +import json +import os +from pathlib import Path +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parent +DIGEST = 'sha256:' + 'a' * 64 +OTHER = 'sha256:' + 'b' * 64 +SHA = 'c' * 40 +FAKE = '''#!/usr/bin/env python3 +import json,os,sys +from pathlib import Path +args=sys.argv[1:] +state=Path(os.environ['FAKE_STATE']) +data=json.loads(state.read_text()) +if args[:3]==['buildx','imagetools','inspect']: + ref=args[3] + value=data.get(ref) + if value=='denied': print('access denied',file=sys.stderr);sys.exit(1) + if not value: print('manifest unknown',file=sys.stderr);sys.exit(1) + print(json.dumps({'digest':value})) +elif args[:3]==['buildx','imagetools','create']: + with open(os.environ['FAKE_CALLS'],'a') as log: log.write('create\\n') + data[args[4]]=args[5].split('@')[1] + state.write_text(json.dumps(data)) +else: sys.exit(2) +''' + + +class Publication(unittest.TestCase): + def exercise(self, version_value=None, commit_value=DIGEST, expected=0, creates=False): + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + fake = root / 'docker' + fake.write_text(FAKE) + fake.chmod(0o755) + state = root / 'state.json' + images = {'example/app:sha-' + SHA: commit_value} + if version_value: images['example/app:v1.2.3'] = version_value + state.write_text(json.dumps(images)) + calls = root / 'calls' + env = dict(os.environ, PATH=tmp + os.pathsep + os.environ['PATH'], FAKE_STATE=str(state), FAKE_CALLS=str(calls)) + result = subprocess.run(['sh', str(ROOT / 'publish-version.sh'), 'example/app', SHA, 'v1.2.3'], env=env, capture_output=True, text=True) + self.assertEqual(result.returncode, expected, result.stderr) + self.assertEqual(calls.exists(), creates) + if expected == 0: self.assertEqual(json.loads(state.read_text())['example/app:v1.2.3'], DIGEST) + + def test_assign_missing_version(self): self.exercise(creates=True) + def test_reuse_same_version(self): self.exercise(version_value=DIGEST) + def test_refuse_reassigned_version(self): self.exercise(version_value=OTHER, expected=1) + def test_auth_failure_never_creates_version(self): self.exercise(version_value='denied', expected=1) + def test_missing_commit_never_creates_version(self): self.exercise(commit_value=None, expected=1) + def test_commit_auth_failure_never_creates_version(self): self.exercise(commit_value='denied', expected=1) + + +if __name__ == '__main__': unittest.main() From 63b2661cf8d4615bcad85f561ca2ffddeabca4f9 Mon Sep 17 00:00:00 2001 From: Stephan Terning Date: Tue, 10 Nov 2026 08:46:33 +0100 Subject: [PATCH 2/2] Propagate registry lookup failures before deciding to publish --- .github/workflows/release.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 27a7f03..5a87292 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -42,7 +42,8 @@ jobs: run: | image="${IMAGE,,}" echo "name=$image" >> "$GITHUB_OUTPUT" - echo "existing=$(sh infrastructure/scripts/image-digest.sh "$image:sha-$GITHUB_SHA")" >> "$GITHUB_OUTPUT" + existing=$(sh infrastructure/scripts/image-digest.sh "$image:sha-$GITHUB_SHA") + echo "existing=$existing" >> "$GITHUB_OUTPUT" - uses: docker/build-push-action@v6 if: steps.image.outputs.existing == '' with: