eBPF-based security platform
#!/bin/sh
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c) 2026 Authors of shirudo
#
# tools/put-shields -- build every shield package and push it to one
# registry namespace, whose signed bpm-index then lists them all: each
# entry names the repository, version and manifest digest that
# `shirudo bpm tree append` takes a package by.
#
# tools/put-shields --version <version> [--keys-dir <dir>] [--new-index]
# [--catalog <file>] oci://<registry>/<namespace>
# [<beefile>|<dir>...]
#
# A shield is a package whose Beefile has an [upatches.tree] entry. The
# Beefiles are the ones given and, for each directory given, every
# Beefile and *.bee under it that has one; without either, those under
# upatches/. Each is built with `shirudo bpm build` at
# <version>, with the BPF headers of this checkout and, for a package
# without a vmlinux.h of its own, its vendor/linux/vmlinux.h, and pushed with `shirudo bpm push` to
# oci://<registry>/<namespace>/<package>, which adds it to
# <namespace>/bpm-index:stable, signed with the same keys. --new-index
# starts that index with the first package, for a namespace that never
# had one. The index lists a name and version once: push the next build
# under a new version.
#
# Each push is reported on standard error, and standard output gets the
# `shirudo bpm tree append` command that appends that shield by its
# reference and manifest digest, less the tree to fill in. --catalog
# writes the shields pushed, with their fixes and selectors, as
# jobs/shields-put of tools/backend loads them, so that the backend lists
# them as available. When a build or a push fails it holds the shields
# pushed before, which the index does not take a second time.
#
# SHIRUDO the shirudo binary. Default: shirudo.
# SHIRUDO_BUILD its helper, as shirudo reads it.
# SHIRUDO_KEYS_DIR the keys directory when --keys-dir is not given.
# BPM_OCI_* registry access, as for shirudo bpm push.
#
# Exit status: 0 when every package is pushed; 1 when a build or a push
# fails, with the packages before it pushed; 2 for wrong usage.
set -eu
repo=$(cd "$(dirname "$0")/.." && pwd)
shirudo=${SHIRUDO:-shirudo}
keys=${SHIRUDO_KEYS_DIR:-}
version=
new_index=
catalog=
work=
die()
{
printf 'put-shields: %s\n' "$1" >&2
exit 1
}
usage()
{
printf '%s\n' \
'usage: put-shields --version <version> [--keys-dir <dir>] [--new-index]' \
' [--catalog <file>] oci://<registry>/<namespace> [<beefile>|<dir>...]' >&2
exit 2
}
cleanup()
{
[ -z "$work" ] || rm -rf "$work"
}
# The shields pushed so far, for --catalog: none pushed writes nothing.
write_catalog()
{
[ -n "$catalog" ] && [ -s "$work/entries" ] || return 0
{
printf '{"shields": [\n'
paste -sd, "$work/entries"
printf ']}\n'
} >"$catalog" || die "cannot write $catalog"
}
# A build or a push failed: what is pushed stays pushed, so it is listed.
fail()
{
write_catalog
die "$1"
}
is_shield()
{
grep -q '^\[upatches\.tree\]' "$1"
}
# Every Beefile and *.bee under directory $1 with an [upatches.tree] entry.
shields_under()
{
found=$(find "$1" \( -name Beefile -o -name '*.bee' \) -type f | sort |
while read -r beefile; do
! is_shield "$beefile" || realpath "$beefile"
done)
[ -n "$found" ] || die "no Beefile under $1 has an [upatches.tree] entry"
printf '%s\n' "$found"
}
while [ $# -gt 0 ]; do
case "$1" in
--version)
[ $# -ge 2 ] || usage
version=$2
shift 2
;;
--keys-dir)
[ $# -ge 2 ] || usage
keys=$2
shift 2
;;
--new-index)
new_index=--new-index
shift
;;
--catalog)
[ $# -ge 2 ] || usage
catalog=$2
shift 2
;;
-*) usage ;;
*) break ;;
esac
done
[ -n "$version" ] && [ $# -ge 1 ] || usage
[ -n "$keys" ] || keys=/var/lib/shirudo/keys
case "$1" in
oci://*/?*) namespace=${1%/} ;;
*) usage ;;
esac
shift
trap cleanup EXIT
trap 'exit 1' HUP INT TERM
work=$(mktemp -d)
[ $# -gt 0 ] || set -- "$repo/upatches"
for shield; do
if [ -d "$shield" ]; then
shields_under "$shield"
elif [ -f "$shield" ]; then
is_shield "$shield" || die "$shield has no [upatches.tree] entry"
realpath "$shield"
else
die "$shield is neither a Beefile nor a directory"
fi
done >"$work/given"
# A Beefile given twice, or in two directories given, is built once.
awk '!seen[$0]++' "$work/given" >"$work/beefiles"
keys=$(realpath "$keys")
# The builds run in a directory of their own, where a relative path to
# the binary or its helper would name nothing.
case "$shirudo" in
*/*) shirudo=$(realpath "$shirudo") || die "no shirudo at $shirudo" ;;
esac
case "${SHIRUDO_BUILD:-}" in
*/*)
SHIRUDO_BUILD=$(realpath "$SHIRUDO_BUILD") || die "no helper at $SHIRUDO_BUILD"
export SHIRUDO_BUILD
;;
esac
mkdir "$work/out"
# bpm build looks for shirudo's BPF headers in the directory it runs in.
[ ! -d "$repo/bpf/lib" ] || ln -s "$repo/bpf" "$work/out/bpf"
while read -r beefile; do
# A Java shield's source is a prebuilt .jar; its build.sh --jar-only
# makes it, where a BPF shield's source compiles in bpm build.
dir=$(dirname "$beefile")
if grep -q '^type = "java"' "$beefile" && [ -x "$dir/build.sh" ]; then
( cd "$dir" && ./build.sh --jar-only ) </dev/null ||
fail "cannot build the jar of $beefile"
fi
# A package that brings no vmlinux.h builds against the checkout's.
vmlinux=
[ -e "$dir/vmlinux.h" ] || [ ! -e "$repo/vendor/linux/vmlinux.h" ] ||
vmlinux=$repo/vendor/linux/vmlinux.h
built=$(cd "$work/out" && "$shirudo" bpm build "$dir" \
--file "$beefile" --version "$version" --keys-dir "$keys" \
${vmlinux:+--vmlinux "$vmlinux"} </dev/null) ||
fail "cannot build $beefile"
bee=$(basename "$(printf '%s\n' "$built" | tail -n 1)")
name=$(basename "$bee" "-$version.bee")
# shellcheck disable=SC2086 # --new-index or nothing
pushed=$("$shirudo" bpm push "$work/out/$bee" "$namespace/$name" \
--keys-dir "$keys" $new_index </dev/null) ||
fail "cannot push $name $version to $namespace/$name"
new_index=
printf 'put-shields: pushed %s\n' "$pushed" >&2
# shellcheck disable=SC2086 # the reference and its digest, to be split
set -- $pushed
printf 'shirudo bpm tree append %s --package-digest %s oci://<registry>/<tree> --tree-id <tree-id>\n' \
"$1" "$2"
python3 "$repo/tools/shield-entries.py" "$beefile" "$1" "$2" "$version" \
>>"$work/entries" || fail "cannot read the tree entries of $beefile"
rm -f "$work/out/$bee"
done <"$work/beefiles"
write_catalog