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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Type=simple
ExecStart=/usr/libexec/projectbluefin-countme
StateDirectory=projectbluefin-countme

# Run as root (no DynamicUser) so bootc status can query status.
# Run as root (no DynamicUser) so the script can read the booted deployment's
# .origin file under /sysroot. It does not run bootc (projectbluefin/utah#1198).
# Hardened sandbox:
PrivateTmp=yes
ProtectSystem=strict
Expand Down
44 changes: 42 additions & 2 deletions system_files/shared/usr/libexec/projectbluefin-countme
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,50 @@ esac

[[ -e $last ]] && (( $(date +%s) - $(stat -c %Y "$last") < 86400 )) && exit 0

# Stream comes from the booted bootc ref when one is available. Digest suffix
# Stream comes from the booted image ref when one is available. Digest suffix
# is stripped; anything outside the known channels (`stable`, `testing`,
# `latest`) becomes `unknown`.
ref=$(bootc status --json 2>/dev/null | jq -r '.status.booted.image.image.image // empty' 2>/dev/null)
#
# This never runs `bootc`. A bootc started from this service runs in
# unconfined_service_t, and its SELinux probe (a chcon to a test label) is
# denied mac_admin, logging two AVC denials every boot
# (projectbluefin/utah#1198). The ref is read from files instead:
# 1. the booted deployment's .origin keyfile: found through ostree= on the
# kernel command line, or through composefs=<id> /
# composefs.digest=<algo>:<id> for composefs-booted hosts (the same
# lookup as utah's ublue-booted-image-sync);
# 2. the boot-time snapshot some images write (/run/ublue-os/booted-image).
booted_ref() {
local cmdline arg id snap origin="" v=""
read -r cmdline < "${COUNTME_CMDLINE:-/proc/cmdline}" || cmdline=""
for arg in $cmdline; do
case $arg in
ostree=*)
origin="$(readlink -f -- "${arg#ostree=}").origin" ;;
composefs=*|composefs.digest=*)
id=${arg#*=}; id=${id##*:}
origin="${COUNTME_SYSROOT:-/sysroot}/state/deploy/$id/$id.origin" ;;
esac
[[ -n $origin ]] && break
done
if [[ -n $origin && -r $origin ]]; then
v=$(sed -n 's/^[[:space:]]*container-image-reference[[:space:]]*=[[:space:]]*//p' "$origin" | head -n1)
# Drop the transport: ostree-image-signed:docker://, ostree-
# unverified-registry:, ostree-remote-image:<remote>:docker://.
v=${v#ostree-remote-registry:*:}
v=${v#ostree-remote-image:*:}
v=${v#ostree-unverified-registry:}
v=${v#ostree-unverified-image:}
v=${v#ostree-image-signed:}
v=${v#docker://}
fi
if [[ -z $v ]]; then
snap="${UBLUE_BOOTED_IMAGE_FILE:-/run/ublue-os/booted-image}"
[[ -r $snap ]] && read -r v < "$snap"
fi
printf '%s\n' "$v"
}
ref=$(booted_ref)
tag=""
if [[ -n $ref ]]; then
ref=${ref%%@*}
Expand Down
121 changes: 105 additions & 16 deletions tests/test_projectbluefin_countme.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,51 @@ setup() {
echo "$@" >> "${TEST_TMPDIR}/curl.log"
exit "${CURL_EXIT:-0}"
EOF
# bootc prints BOOTED_REF as the booted image, or fails when it is unset.
# The script must never run bootc (#1198): a bootc started from this
# unconfined service is denied mac_admin by its SELinux probe. This stub
# leaves a marker, and teardown checks it is absent.
cat << 'EOF' > "${MOCK_BIN}/bootc"
#!/usr/bin/env bash
[[ -n "${BOOTED_REF:-}" ]] || exit 1
printf '{"status":{"booted":{"image":{"image":{"image":"%s"}}}}}\n' "${BOOTED_REF}"
touch "${TEST_TMPDIR}/bootc.called"
exit 1
EOF
chmod +x "${MOCK_BIN}/curl" "${MOCK_BIN}/bootc"
# No booted deployment and no snapshot unless a test creates one.
export COUNTME_CMDLINE="${TEST_TMPDIR}/cmdline"
export UBLUE_BOOTED_IMAGE_FILE="${TEST_TMPDIR}/booted-image"
echo "BOOT_IMAGE=/vmlinuz root=/dev/vda2" > "${COUNTME_CMDLINE}"
export PATH="${MOCK_BIN}:${PATH}"
}

teardown() {
[ ! -e "${TEST_TMPDIR}/bootc.called" ]
rm -rf "${TEST_TMPDIR}"
}

image_info() {
printf '{"image-name":"%s","image-flavor":"%s","image-tag":"latest"}\n' "$1" "$2" > "${IMAGE_INFO}"
}

# booted REF [ORIGIN_VALUE]: fake an ostree deployment whose .origin keyfile
# names REF, reached from ostree= on the kernel command line through a symlink
# like /ostree/boot.N/<stateroot>/<csum>/<serial>.
booted() {
local d="${TEST_TMPDIR}/deploy/abc123.0"
mkdir -p "$d" "${TEST_TMPDIR}/boot/default/abc123"
ln -sfn "$d" "${TEST_TMPDIR}/boot/default/abc123/0"
printf '[origin]\ncontainer-image-reference=%s\n' "${2:-ostree-unverified-registry:$1}" \
> "${d}.origin"
echo "BOOT_IMAGE=/vmlinuz ostree=${TEST_TMPDIR}/boot/default/abc123/0 rw" > "${COUNTME_CMDLINE}"
}

sent_image() {
grep -o '"image":"[^"]*"' "${TEST_TMPDIR}/curl.log"
}

@test "Dakota reports image, flavor, and the booted stream" {
image_info dakota-nvidia nvidia
BOOTED_REF=ghcr.io/projectbluefin/dakota-nvidia:testing run "${SCRIPT}"
booted ghcr.io/projectbluefin/dakota-nvidia:testing
run "${SCRIPT}"
[ "$status" -eq 0 ]
[ "$(sent_image)" = '"image":"dakota-nvidia/nvidia:testing"' ]
grep -q '/v1/ping' "${TEST_TMPDIR}/curl.log"
Expand All @@ -49,41 +69,44 @@ sent_image() {

@test "Utah is counted; a digest-pinned ref keeps its stream" {
image_info utah gaming
BOOTED_REF='ghcr.io/projectbluefin/utah-gaming:stable@sha256:abc' run "${SCRIPT}"
booted 'ghcr.io/projectbluefin/utah-gaming:stable@sha256:abc'
run "${SCRIPT}"
[ "$(sent_image)" = '"image":"utah/gaming:stable"' ]
}

@test "a tag that is not a stream is reported as unknown" {
image_info dakota main
BOOTED_REF=ghcr.io/projectbluefin/dakota:next run "${SCRIPT}"
booted ghcr.io/projectbluefin/dakota:next
run "${SCRIPT}"
[ "$(sent_image)" = '"image":"dakota/main:unknown"' ]
}

@test "latest from bootc is reported as a stream" {
image_info dakota main
BOOTED_REF=ghcr.io/projectbluefin/dakota:latest run "${SCRIPT}"
booted ghcr.io/projectbluefin/dakota:latest
run "${SCRIPT}"
[ "$(sent_image)" = '"image":"dakota/main:latest"' ]
}

@test "Server (DDI) reports its image-tag stream without bootc" {
image_info server main
BOOTED_REF="" run "${SCRIPT}"
run "${SCRIPT}"
[ "$status" -eq 0 ]
[ "$(sent_image)" = '"image":"server/main:latest"' ]
[ -e "${STATE_DIRECTORY}/last" ]
}

@test "Server (DDI) skips when image-tag is empty" {
printf '{"image-name":"server","image-flavor":"main"}\n' > "${IMAGE_INFO}"
BOOTED_REF="" run "${SCRIPT}"
run "${SCRIPT}"
[ "$status" -eq 0 ]
[ ! -e "${TEST_TMPDIR}/curl.log" ]
[ ! -e "${STATE_DIRECTORY}/last" ]
}

@test "Server (DDI) skips when image-name is not exactly server" {
image_info server-foo main
BOOTED_REF="" run "${SCRIPT}"
run "${SCRIPT}"
[ "$status" -eq 0 ]
[ ! -e "${TEST_TMPDIR}/curl.log" ]
}
Expand All @@ -92,14 +115,14 @@ sent_image() {
# dakota with no bootc image and a baked image-tag must not count — the
# baked tag is the compose tag, not the stream the system actually runs.
image_info dakota main
BOOTED_REF="" run "${SCRIPT}"
run "${SCRIPT}"
[ "$status" -eq 0 ]
[ ! -e "${TEST_TMPDIR}/curl.log" ]
}

@test "Server (DDI) reports a non-stream image-tag as unknown" {
printf '{"image-name":"server","image-flavor":"main","image-tag":"20261007"}\n' > "${IMAGE_INFO}"
BOOTED_REF="" run "${SCRIPT}"
run "${SCRIPT}"
[ "$status" -eq 0 ]
[ "$(sent_image)" = '"image":"server/main:unknown"' ]
}
Expand All @@ -114,7 +137,8 @@ sent_image() {
@test "Bluefin Classic and LTS never contact the service" {
for name in bluefin bluefin-lts; do
image_info "$name" main
BOOTED_REF="ghcr.io/ublue-os/${name}:stable" run "${SCRIPT}"
booted ghcr.io/ublue-os/${name}:stable
run "${SCRIPT}"
[ "$status" -eq 0 ]
done
[ ! -e "${TEST_TMPDIR}/curl.log" ]
Expand All @@ -131,17 +155,82 @@ sent_image() {
@test "at most one report per 24 hours" {
image_info dakota main
touch -d '2 hours ago' "${STATE_DIRECTORY}/last"
BOOTED_REF=ghcr.io/projectbluefin/dakota:stable run "${SCRIPT}"
booted ghcr.io/projectbluefin/dakota:stable
run "${SCRIPT}"
[ ! -e "${TEST_TMPDIR}/curl.log" ]

touch -d '25 hours ago' "${STATE_DIRECTORY}/last"
BOOTED_REF=ghcr.io/projectbluefin/dakota:stable run "${SCRIPT}"
booted ghcr.io/projectbluefin/dakota:stable
run "${SCRIPT}"
[ -e "${TEST_TMPDIR}/curl.log" ]
}

@test "a failed send exits 0 and is retried on the next run" {
image_info dakota main
CURL_EXIT=22 BOOTED_REF=ghcr.io/projectbluefin/dakota:stable run "${SCRIPT}"
booted ghcr.io/projectbluefin/dakota:stable
CURL_EXIT=22 run "${SCRIPT}"
[ "$status" -eq 0 ]
[ ! -e "${STATE_DIRECTORY}/last" ]
}

@test "the ref comes from the .origin file for every transport form" {
image_info dakota main
for form in \
'ostree-image-signed:docker://ghcr.io/projectbluefin/dakota:stable' \
'ostree-unverified-image:docker://ghcr.io/projectbluefin/dakota:stable' \
'ostree-remote-registry:fedora:ghcr.io/projectbluefin/dakota:stable' \
'ostree-remote-image:fedora:docker://ghcr.io/projectbluefin/dakota:stable' \
'ostree-unverified-registry:ghcr.io/projectbluefin/dakota:stable'; do
rm -f "${TEST_TMPDIR}/curl.log" "${STATE_DIRECTORY}/last"
booted x "$form"
run "${SCRIPT}"
[ "$status" -eq 0 ]
[ "$(sent_image)" = '"image":"dakota/main:stable"' ]
done
}

@test "a registry port in the ref does not become the stream" {
image_info dakota main
booted localhost:5000/dakota
run "${SCRIPT}"
[ "$(sent_image)" = '"image":"dakota/main:unknown"' ]
}

@test "without an ostree= deployment, the boot-time snapshot supplies the ref" {
image_info dakota main
echo 'ghcr.io/projectbluefin/dakota:testing' > "${UBLUE_BOOTED_IMAGE_FILE}"
run "${SCRIPT}"
[ "$(sent_image)" = '"image":"dakota/main:testing"' ]
}

@test "the .origin file wins over the snapshot" {
image_info dakota main
echo 'ghcr.io/projectbluefin/dakota:testing' > "${UBLUE_BOOTED_IMAGE_FILE}"
booted ghcr.io/projectbluefin/dakota:stable
run "${SCRIPT}"
[ "$(sent_image)" = '"image":"dakota/main:stable"' ]
}

@test "an ostree= path with no .origin file is treated as not booted" {
image_info dakota main
booted ghcr.io/projectbluefin/dakota:stable
rm "${TEST_TMPDIR}"/deploy/abc123.0.origin
run "${SCRIPT}"
[ "$status" -eq 0 ]
[ ! -e "${TEST_TMPDIR}/curl.log" ]
}

@test "composefs hosts read the .origin file from /sysroot/state/deploy" {
image_info dakota main
export COUNTME_SYSROOT="${TEST_TMPDIR}/sysroot"
mkdir -p "${COUNTME_SYSROOT}/state/deploy/abc123"
printf '[origin]\ncontainer-image-reference=ostree-unverified-registry:ghcr.io/projectbluefin/dakota:testing\n' \
> "${COUNTME_SYSROOT}/state/deploy/abc123/abc123.origin"
for arg in composefs=abc123 composefs.digest=sha512:abc123; do
rm -f "${TEST_TMPDIR}/curl.log" "${STATE_DIRECTORY}/last"
echo "BOOT_IMAGE=/vmlinuz ${arg} rw" > "${COUNTME_CMDLINE}"
run "${SCRIPT}"
[ "$status" -eq 0 ]
[ "$(sent_image)" = '"image":"dakota/main:testing"' ]
done
}
Loading