Skip to content

eBPF-based security platform

2.5 KB
Raw Blame
#!/bin/sh
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c) 2026 Authors of shirudo
#
# jobs/shields-put -- load the shields published to a registry into
# shirudo.shield_catalog, where they show as available to append to a
# tree.
#
#   jobs/shields-put [--check] <catalog.json>|-
#
# The catalog is what tools/put-shields --catalog writes: one entry per
# [upatches.tree] entry of a pushed package, with the reference and
# manifest digest bpm tree append takes the package by. A shield put
# again under the same reference and id replaces its row; the others
# stay. The file is loaded whole or not at all. --check reads and checks
# it, and loads nothing.
#
#   CLICKHOUSE_CLIENT  the client command and its options, split at
#                      spaces. Default: clickhouse-client, which reads
#                      CLICKHOUSE_HOST, CLICKHOUSE_USER and
#                      CLICKHOUSE_PASSWORD itself.
#
# Exit status: 0 when every shield is loaded, or checked with --check; 1
# when the file is refused or the load fails, with nothing loaded; 2 for
# wrong usage.

set -eu

here=$(cd "$(dirname "$0")" && pwd)
prog=shields-put
# shellcheck source=tools/backend/jobs/job.sh
. "$here/job.sh"
check=0

usage()
{
	printf '%s\n' 'usage: shields-put [--check] <catalog.json>|-' >&2
	exit 2
}

[ "${1:-}" = --check ] && { check=1; shift; }
[ $# -eq 1 ] || usage
case "$1" in
-) ;;
-*) usage ;;
esac

make_work
if [ "$1" = - ]; then
	cat >"$work/catalog.json"
else
	cp "$1" "$work/catalog.json" 2>/dev/null || die "cannot read $1, nothing loaded"
fi

if [ "$check" -eq 1 ]; then
	python3 "$here/shields-rows.py" "$work/catalog.json" "$work/rows.jsonl" 0
	exit
fi

# Every put gets a time of its own, after every put before it, so that
# the newest row of a shield is the one ReplacingMergeTree keeps.
put_at=$(client --query 'SELECT toUnixTimestamp64Milli(greatest(now64(3),
	(SELECT max(put_at) FROM shirudo.shield_catalog) + toIntervalMillisecond(1)))' \
	</dev/null) || die 'cannot read shirudo.shield_catalog, nothing loaded'
python3 "$here/shields-rows.py" "$work/catalog.json" "$work/rows.jsonl" \
	"$put_at" >"$work/summary" || exit 1
# One insert, one block: the shields land together or not at all.
if [ -s "$work/rows.jsonl" ]; then
	client --query 'INSERT INTO shirudo.shield_catalog FORMAT JSONEachRow' \
		<"$work/rows.jsonl" || die 'cannot insert into shirudo.shield_catalog, nothing loaded'
fi
cat "$work/summary"
printf 'shields-put: loaded shields: %d\n' "$(wc -l <"$work/rows.jsonl")"