fix(ci): harden the build and image workflows after review
Pass ref and repository values to run steps as environment instead of interpolating them into the script (a tag name may contain shell metacharacters). In the image workflow: refuse tags that are not vMAJOR.MINOR.PATCH[-prerelease] before anything runs, purge exactly the packages the prepare step added instead of autoremove and fail on any package or /etc file difference from the base image, send the registry write probe with an empty body, and remove the registry credential after the push also when the build fails. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
fe6d19dde5
commit
0bc8d19775
3 files changed
+60
-15
No files matched your search
@@ -45,12 +45,17 @@ jobs:
|
||||
GIT_CONFIG_VALUE_0: "AUTHORIZATION: bearer ${{ secrets.GITHUB_TOKEN }}"
|
||||
SHA: ${{ github.event.pull_request.head.sha || github.sha }}
|
||||
REF: ${{ github.ref }}
|
||||
# Passed as environment, never interpolated into the script: a tag name may
|
||||
# contain shell metacharacters.
|
||||
REPO_URL: ${{ github.server_url }}/${{ github.repository }}
|
||||
REF_TYPE: ${{ github.ref_type }}
|
||||
REF_NAME: ${{ github.ref_name }}
|
||||
run: |
|
||||
git clone "${{ github.server_url }}/${{ github.repository }}" .
|
||||
git clone "$REPO_URL" .
|
||||
git checkout "$SHA" || { git fetch origin "$REF" && git checkout "$SHA"; }
|
||||
# Make sure the tag object itself is present (not only the commit it points at).
|
||||
if [ "${{ github.ref_type }}" = "tag" ]; then
|
||||
git fetch origin "+refs/tags/${{ github.ref_name }}:refs/tags/${{ github.ref_name }}"
|
||||
if [ "$REF_TYPE" = "tag" ]; then
|
||||
git fetch origin "+refs/tags/${REF_NAME}:refs/tags/${REF_NAME}"
|
||||
fi
|
||||
echo "Checked out $(git rev-parse HEAD)"
|
||||
|
||||
|
||||
+50
-11
@@ -61,19 +61,42 @@ jobs:
|
||||
DEBIAN_FRONTEND: noninteractive
|
||||
IMAGE_PATH: mortis/molecule-ubuntu2404
|
||||
steps:
|
||||
# A tag becomes the image tag. OCI tags cannot contain '+' (semver build metadata),
|
||||
# and the trigger glob `v*` also matches names like `vfoo`: refuse both before
|
||||
# anything is installed or built. Only bash builtins are used.
|
||||
- name: Validate the ref
|
||||
env:
|
||||
REF: ${{ github.ref }}
|
||||
REF_NAME: ${{ github.ref_name }}
|
||||
run: |
|
||||
case "$REF" in
|
||||
refs/tags/*)
|
||||
if ! [[ "$REF_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
|
||||
echo "::error::Tag '$REF_NAME' is not vMAJOR.MINOR.PATCH[-prerelease]; refusing to build or push."
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
# One step, in this order, so the filesystem the build starts from is the pristine
|
||||
# base image. Kaniko unpacks the base image into the job container's own root
|
||||
# filesystem and snapshots from there, so anything installed in the job container
|
||||
# first (curl and its libraries) would count as "already present" and be missing
|
||||
# from the layers that install the same packages (ca-certificates, libkrb5 ...).
|
||||
# The tools are therefore installed, used, purged, and the result is compared with
|
||||
# a listing taken before the install; any difference under /usr, /lib, /bin, /sbin
|
||||
# or /opt fails the job.
|
||||
# The tools are therefore installed, used, and exactly the packages the install
|
||||
# added are purged (not `autoremove`, which would also drop base packages that
|
||||
# are marked auto-installed). The result is compared with what was recorded
|
||||
# before the install: the package names and versions must be identical, and any
|
||||
# file difference under /usr, /lib, /bin, /sbin, /opt or /etc fails the job.
|
||||
# Names only, no sizes or modes, and /var/log, /run and dpkg's *-old files are
|
||||
# not compared: logs and dpkg state the install touched may end up in the first
|
||||
# layer as modified files. That is cosmetic; after the first push, list the
|
||||
# layers (crane export | tar t) and look for run/act, root/ and var/log/apt.
|
||||
#
|
||||
# The pinned kaniko v1.28.6 image has index digest
|
||||
# sha256:2fa20b0a4b40c7db72b479d57e55c2cf79f2178533b2d3bcbd129d6bd3c087ed and
|
||||
# linux/amd64 manifest sha256:a533e3ac0623adbb4704bcd1f1250e1a36915431e69535847e5ebe369a408c4e.
|
||||
# Its CA bundle (layer 1) and executor (layer 9) are fetched by content digest and
|
||||
# Its CA bundle (layers[1]) and executor (layers[9], both 0-based) are fetched by content digest and
|
||||
# verified with sha256sum.
|
||||
#
|
||||
# The credential: the job token first; the org secrets only after a write probe was
|
||||
@@ -102,6 +125,9 @@ jobs:
|
||||
-o \( -type f -o -type l \) -print | LC_ALL=C sort
|
||||
}
|
||||
snapshot > /kaniko/fs.before
|
||||
pkgs() { dpkg-query -W -f='${Package}:${Architecture}\n' | LC_ALL=C sort; }
|
||||
pkgs > /kaniko/pkgs.before
|
||||
dpkg-query -W -f='${Package}:${Architecture} ${Version}\n' | LC_ALL=C sort > /kaniko/pkgv.before
|
||||
|
||||
apt-get update
|
||||
apt-get install -y --no-install-recommends ca-certificates curl
|
||||
@@ -125,8 +151,8 @@ jobs:
|
||||
# package, 401/403 means refused. The unused upload session expires on its own.
|
||||
probe() {
|
||||
printf 'user = "%s:%s"\n' "$1" "$2" \
|
||||
| curl -sS -o /dev/null -w '%{http_code}' -K - -X POST \
|
||||
"https://${registry}/v2/${IMAGE_PATH}/blobs/uploads/" || echo 000
|
||||
| curl -sS -o /dev/null -w '%{http_code}' -K - -X POST -d '' \
|
||||
"https://${registry}/v2/${IMAGE_PATH}/blobs/uploads/" || true
|
||||
}
|
||||
write_config() {
|
||||
auth="$(printf '%s:%s' "$1" "$2" | base64 -w0)"
|
||||
@@ -162,18 +188,25 @@ jobs:
|
||||
echo "$method" > /kaniko/credential-method
|
||||
echo "Registry credential in use: $method"
|
||||
|
||||
# Back to the pristine base image (see above).
|
||||
apt-get purge -y ca-certificates curl
|
||||
apt-get autoremove -y --purge
|
||||
# Back to the pristine base image (see above): purge exactly what the install
|
||||
# added, then compare packages (names and versions) and files with the record.
|
||||
pkgs > /kaniko/pkgs.installed
|
||||
comm -13 /kaniko/pkgs.before /kaniko/pkgs.installed | xargs apt-get purge -y
|
||||
apt-get clean
|
||||
rm -rf /var/lib/apt/lists/* /tmp/*
|
||||
dpkg-query -W -f='${Package}:${Architecture} ${Version}\n' | LC_ALL=C sort > /kaniko/pkgv.after
|
||||
if ! diff /kaniko/pkgv.before /kaniko/pkgv.after; then
|
||||
echo "::error::The installed packages differ from the pristine base image (listed above): the install" \
|
||||
"upgraded or left packages behind, so the build would start from a modified filesystem."
|
||||
exit 1
|
||||
fi
|
||||
snapshot > /kaniko/fs.after
|
||||
diff /kaniko/fs.before /kaniko/fs.after > /kaniko/fs.diff || true
|
||||
if grep -E '^[<>] /(usr|lib|bin|sbin|opt)' /kaniko/fs.diff; then
|
||||
if grep -E '^[<>] /(usr|lib|bin|sbin|opt|etc)/' /kaniko/fs.diff; then
|
||||
echo "::error::The job container differs from the pristine base image (listed above); the build would produce wrong layers."
|
||||
exit 1
|
||||
fi
|
||||
echo "Filesystem check passed ($(grep -c '^[<>]' /kaniko/fs.diff || true) unrelated differences outside /usr, /lib, /bin, /sbin, /opt)"
|
||||
echo "Package and filesystem check passed ($(grep -c '^[<>]' /kaniko/fs.diff || true) unrelated file differences outside the checked directories)"
|
||||
|
||||
- name: Build and push with kaniko
|
||||
env:
|
||||
@@ -229,3 +262,9 @@ jobs:
|
||||
echo "Pin it by digest: \`${image}@${digest}\`"
|
||||
} >> "$GITHUB_STEP_SUMMARY" || true
|
||||
fi
|
||||
|
||||
# The credential file stays in the job container for the whole build, where the
|
||||
# Dockerfile's RUN steps run as root; remove it as soon as the push is over.
|
||||
- name: Remove the registry credential
|
||||
if: always()
|
||||
run: rm -f /kaniko/.docker/config.json
|
||||
@@ -51,8 +51,9 @@ jobs:
|
||||
GIT_CONFIG_VALUE_0: "AUTHORIZATION: bearer ${{ secrets.GITHUB_TOKEN }}"
|
||||
SHA: ${{ github.event.pull_request.head.sha || github.sha }}
|
||||
REF: ${{ github.ref }}
|
||||
REPO_URL: ${{ github.server_url }}/${{ github.repository }}
|
||||
run: |
|
||||
git clone "${{ github.server_url }}/${{ github.repository }}" .
|
||||
git clone "$REPO_URL" .
|
||||
# A clone has no refs/pull/*: fetch the run's ref when the sha is not on a branch.
|
||||
git checkout "$SHA" || { git fetch origin "$REF" && git checkout "$SHA"; }
|
||||
echo "Checked out $(git rev-parse HEAD)"
|
||||
|
||||
Reference in new issue
Block a user