170 lines
6.0 KiB
Bash
Executable File
170 lines
6.0 KiB
Bash
Executable File
#!/bin/sh
|
|
# Assemble the minimal Debian runtime rootfs used by the Crank server images.
|
|
#
|
|
# The final admin-api and mcp-server images are built `FROM scratch`; this
|
|
# script runs inside the Debian package stage before the rootfs is copied into
|
|
# the scratch image. It transfers only the files owned by an explicit package
|
|
# allowlist plus three GNU coreutils programs and their copyright. No apt,
|
|
# libacl, GNU gzip, infocmp, util-linux or BusyBox files are copied.
|
|
#
|
|
# Merged-/usr: Debian exposes /bin, /sbin and /lib as symlinks into /usr. The
|
|
# package file lists still name the legacy paths (for example /bin/bash or
|
|
# /lib/x86_64-linux-gnu/libc.so.6). Before every copy the parent directory is
|
|
# resolved through those symlinks (readlink -f on the dirname only), so regular
|
|
# files and library symlinks land in their real /usr locations while the
|
|
# top-level directory symlinks (/bin -> usr/bin, /lib -> usr/lib,
|
|
# /lib64 -> usr/lib64) and file symlinks (for example /usr/bin/sh -> dash) are
|
|
# preserved unchanged.
|
|
#
|
|
# Inventory: /usr/share/crank/runtime-inventory.tsv is a tab-separated manifest
|
|
# with one row per file placed in the rootfs by this script:
|
|
#
|
|
# package<TAB>normalized absolute path<TAB>sha256 for regular files or the
|
|
# symlink target for symlinks
|
|
#
|
|
# Files generated here instead of shipped by a package use the synthetic
|
|
# package label `runtime-generated`. The scan stage cross-checks this manifest
|
|
# against the real image instead of trusting Trivy alone. The manifest path is
|
|
# fixed and cannot be overridden.
|
|
set -eu
|
|
|
|
dest="${1:-/runtime}"
|
|
|
|
case "$dest" in
|
|
/*) ;;
|
|
*)
|
|
echo "runtime rootfs destination must be an absolute path: $dest" >&2
|
|
exit 64
|
|
;;
|
|
esac
|
|
|
|
# Packages kept in the runtime image. Versions are recorded in dpkg/status.
|
|
# gcc-12-base supplies the copyright target of libgcc-s1; retaining its
|
|
# package-owned documentation preserves the license and its source version.
|
|
packages="base-files libc6 libgcc-s1 gcc-12-base bash dash libtinfo6 libselinux1 libpcre2-8-0 coreutils"
|
|
|
|
# shellcheck disable=SC2086
|
|
set -- $packages
|
|
expected_packages="$#"
|
|
set --
|
|
|
|
# GNU coreutils is installed only so apt resolves dependencies honestly; of the
|
|
# whole package just these files are transferred. install/cp/mv are deliberately
|
|
# left out because they pull in libacl.
|
|
coreutils_files="/bin/mkdir /bin/chmod /usr/bin/env /usr/share/doc/coreutils/copyright"
|
|
|
|
mkdir -p \
|
|
"$dest/usr/local/bin" \
|
|
"$dest/var/lib/dpkg" \
|
|
"$dest/etc" \
|
|
"$dest/tmp" \
|
|
"$dest/run/lock" \
|
|
"$dest/usr/share/crank"
|
|
|
|
# A world-writable sticky /tmp and the run-state directories match the FHS and
|
|
# the previous slim image. /run and /run/lock also give the base-files
|
|
# /var/run and /var/lock symlinks valid targets.
|
|
chmod 1777 "$dest/tmp"
|
|
chmod 0755 "$dest/run"
|
|
chmod 1777 "$dest/run/lock"
|
|
|
|
status="$dest/var/lib/dpkg/status"
|
|
inventory="$dest/usr/share/crank/runtime-inventory.tsv"
|
|
: > "$status"
|
|
: > "$inventory"
|
|
|
|
for package in $packages; do
|
|
version="$(dpkg-query -W -f='${Version}' "$package")"
|
|
printf 'runtime-package: %s %s\n' "$package" "$version"
|
|
|
|
# dpkg/status paragraph, separated from the next one by a blank line.
|
|
dpkg-query -s "$package" >> "$status"
|
|
printf '\n' >> "$status"
|
|
|
|
# shellcheck disable=SC2086
|
|
for source in $(dpkg-query -L "$package"); do
|
|
if [ "$package" = "coreutils" ]; then
|
|
allowed=0
|
|
# shellcheck disable=SC2086
|
|
for candidate in $coreutils_files; do
|
|
if [ "$source" = "$candidate" ]; then
|
|
allowed=1
|
|
break
|
|
fi
|
|
done
|
|
[ "$allowed" -eq 1 ] || continue
|
|
fi
|
|
|
|
if [ ! -f "$source" ] && [ ! -L "$source" ]; then
|
|
continue
|
|
fi
|
|
|
|
directory="$(readlink -f "$(dirname -- "$source")")"
|
|
base="$(basename -- "$source")"
|
|
if [ "$directory" = "/" ]; then
|
|
path="/$base"
|
|
parent="$dest"
|
|
else
|
|
path="$directory/$base"
|
|
parent="$dest$directory"
|
|
fi
|
|
|
|
# Several packages list the same merged-/usr symlink; keep the first
|
|
# copy and skip duplicates so the manifest has one row per path.
|
|
if [ -e "$dest$path" ] || [ -L "$dest$path" ]; then
|
|
continue
|
|
fi
|
|
|
|
mkdir -p "$parent"
|
|
cp -a --no-target-directory -- "$source" "$dest$path"
|
|
|
|
if [ -L "$source" ]; then
|
|
digest="$(readlink -- "$source")"
|
|
else
|
|
digest="$(sha256sum -- "$source")"
|
|
digest="${digest%% *}"
|
|
fi
|
|
printf '%s\t%s\t%s\n' "$package" "$path" "$digest" >> "$inventory"
|
|
done
|
|
done
|
|
|
|
# Docker mounts /etc/hosts and /etc/resolv.conf, but the image still needs the
|
|
# root account and a name-service switch policy. `hosts: files dns` is required
|
|
# for getaddrinfo to fall back to DNS for outbound requests.
|
|
printf 'root:x:0:0:root:/root:/bin/sh\n' > "$dest/etc/passwd"
|
|
printf 'root:x:0:\n' > "$dest/etc/group"
|
|
printf 'passwd: files\ngroup: files\nhosts: files dns\n' > "$dest/etc/nsswitch.conf"
|
|
|
|
for generated in /etc/passwd /etc/group /etc/nsswitch.conf; do
|
|
digest="$(sha256sum -- "$dest$generated")"
|
|
digest="${digest%% *}"
|
|
printf 'runtime-generated\t%s\t%s\n' "$generated" "$digest" >> "$inventory"
|
|
done
|
|
|
|
count="$(grep -c '^Package: ' "$status")"
|
|
if [ "$count" -ne "$expected_packages" ]; then
|
|
echo "dpkg status holds $count packages, expected $expected_packages" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Merged-/usr and shell-entrypoint contracts must hold in the assembled rootfs.
|
|
for link in bin lib lib64 sbin; do
|
|
if [ ! -L "$dest/$link" ]; then
|
|
echo "merged-/usr symlink /$link missing from runtime rootfs" >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
for exe in usr/bin/sh usr/bin/mkdir usr/bin/chmod usr/bin/env usr/bin/bash; do
|
|
if [ ! -x "$dest/$exe" ]; then
|
|
echo "runtime shell or coreutils subset missing: /$exe" >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
if [ "$(grep -c . "$inventory")" -eq 0 ]; then
|
|
echo "runtime inventory is empty" >&2
|
|
exit 1
|
|
fi
|
|
|
|
printf 'runtime-inventory: %s files\n' "$(grep -c . "$inventory")"
|