Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
40 changes: 22 additions & 18 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,40 +34,37 @@ 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<<EOF'
echo "$image:sha-$GITHUB_SHA"
if [[ "$GITHUB_REF" == refs/tags/* ]]; then
echo "$image:$GITHUB_REF_NAME"
fi
echo EOF
} >> "$GITHUB_OUTPUT"
echo "name=$image" >> "$GITHUB_OUTPUT"
existing=$(sh infrastructure/scripts/image-digest.sh "$image:sha-$GITHUB_SHA")
echo "existing=$existing" >> "$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
Expand All @@ -76,4 +77,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
6 changes: 5 additions & 1 deletion docs/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-<SHA>` tag,
pull the images,
Expand Down
18 changes: 18 additions & 0 deletions infrastructure/scripts/image-digest.sh
Original file line number Diff line number Diff line change
@@ -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 <image-reference>" >&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
32 changes: 32 additions & 0 deletions infrastructure/scripts/publish-version.sh
Original file line number Diff line number Diff line change
@@ -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"
59 changes: 59 additions & 0 deletions infrastructure/scripts/test-image-publication.py
Original file line number Diff line number Diff line change
@@ -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()
Loading