Files
krang/tests/root-integration.sh
lotherk fb6f97ba8e krang: autonomous agent daemon on libsplinter
One fixed session, born once, kept and compacted forever. The daemon wakes on
its own impulse, works on a persistent goal, detects repeated failures,
reflects, changes approach, and records everything. It starts as root, builds
a debootstrap chroot, then drops to an unprivileged uid and applies Landlock
and seccomp. Dashboard, HTTP API and MCP expose the same snapshot; the event
log is root-owned and append-only. Offline suites cover the engine, the daemon,
both sandbox layers and the dashboard/MCP sockets; a root-only script covers
debootstrap, chroot, uid drop and plugin loading.
2026-11-21 17:00:12 +00:00

140 lines
3.9 KiB
Bash
Executable File

#!/bin/sh
# krang -- root-only integration test.
#
# Copyright (c) 2026 Konrad Lother
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# This is the part the offline suite cannot prove: debootstrap, mount namespace,
# chroot, uid drop, Landlock, seccomp and the event log ownership. It does not
# start the autonomous daemon or a provider; it runs --sandbox-test, one
# scripted turn that asks `id -u` and tries `mount(2)` inside the cage.
#
# sudo tests/root-integration.sh
#
# Environment:
# KRANG_TEST_DIR work directory (default: mktemp -d /var/tmp/...)
# KRANG_TEST_ROOTFS reuse an already prepared rootfs, skip debootstrap
# KRANG_KEEP=1 keep the work directory for inspection
set -eu
if [ "$(id -u)" != "0" ]; then
echo "root-integration: must run as root (sudo tests/root-integration.sh)" >&2
exit 1
fi
[ -x ./krang ] || { echo "root-integration: build ./krang first (make)" >&2; exit 1; }
if [ -n "${KRANG_TEST_ROOTFS:-}" ]; then
ROOTFS=$KRANG_TEST_ROOTFS
else
if ! command -v debootstrap >/dev/null 2>&1 &&
[ ! -x /usr/sbin/debootstrap ]; then
echo "root-integration: debootstrap is not installed" >&2
exit 1
fi
fi
WORK=${KRANG_TEST_DIR:-$(mktemp -d /var/tmp/krang-root.XXXXXX)}
KRANG_ROOT=$(cd "$(dirname "$0")/.." && pwd)
SPLINTER=${SPLINTER:-$KRANG_ROOT/../splinter}
PLUGIN=$SPLINTER/plugins/clock/clock.so
test -r "$PLUGIN" || {
echo "root-integration: missing $PLUGIN (build ../splinter first)" >&2
exit 1
}
STATE=$WORK/state
WORKSPACE=$WORK/workspace
CONF=$WORK/krang.conf
if [ -z "${KRANG_TEST_ROOTFS:-}" ]; then
ROOTFS=$WORK/rootfs
fi
mkdir -p "$STATE" "$WORKSPACE"
cleanup() {
if [ "${KRANG_KEEP:-0}" = "1" ]; then
echo "root-integration: kept $WORK"
return
fi
rm -rf "$WORK"
}
trap cleanup EXIT INT TERM
cat >"$CONF" <<EOF
goal = sandbox integration test
endpoint = http://127.0.0.1:1/v1
model = test
api_key_env = KRANG_TEST_KEY
state_dir = $STATE
workspace = $WORKSPACE
rootfs = $ROOTFS
sandbox_user = nobody
sandbox_uid = 65534
sandbox_gid = 65534
plugin = $PLUGIN
impulse_seconds = 5
max_steps = 5
compact_threshold = 60000
compact_target = 20000
dashboard_host = 127.0.0.1
dashboard_port = 18096
mcp_host = 127.0.0.1
mcp_port = 18098
say_timeout = 10
require_landlock = 1
seccomp = 1
prepare_if_missing = 0
EOF
if [ -z "${KRANG_TEST_ROOTFS:-}" ]; then
echo "== prepare-rootfs (debootstrap; network)"
./krang --prepare-rootfs --config "$CONF"
else
echo "== reusing rootfs $ROOTFS"
test -r "$ROOTFS/etc/krang-rootfs" || {
echo "root-integration: no /etc/krang-rootfs in $ROOTFS" >&2
exit 1
}
fi
echo "== sandbox-test (unshare, chroot, uid drop, Landlock, seccomp)"
./krang --sandbox-test --config "$CONF"
EVENTS=$STATE/events.jsonl
SESSIONS=$STATE/sessions/krang.jsonl
test -s "$EVENTS"
test -s "$SESSIONS"
echo "== event log ownership"
EV_UID=$(stat -c %u "$EVENTS")
EV_MODE=$(stat -c %a "$EVENTS")
echo "events: uid=$EV_UID mode=$EV_MODE"
[ "$EV_UID" = "0" ] || { echo "event log is not root-owned" >&2; exit 1; }
[ "$EV_MODE" = "600" ] || { echo "event log mode is not 600" >&2; exit 1; }
echo "== session ownership"
S_UID=$(stat -c %u "$SESSIONS")
echo "session: uid=$S_UID"
[ "$S_UID" = "65534" ] || { echo "session is not owned by the sandbox uid" >&2; exit 1; }
echo "== tool observations"
grep -q '"name":"clock"' "$EVENTS" || {
echo "the clock plugin was not loaded" >&2
exit 1
}
grep -q '65534' "$EVENTS" || { echo "id -u did not report 65534" >&2; exit 1; }
grep -q 'mount-denied\|Operation not permitted\|Permission denied' "$EVENTS" || {
echo "mount was not denied" >&2
exit 1
}
grep -q 'sandbox-test-ok' "$EVENTS" || { echo "answer missing" >&2; exit 1; }
if command -v setpriv >/dev/null 2>&1; then
if setpriv --reuid=65534 --regid=65534 --clear-groups \
test -r "$EVENTS" 2>/dev/null; then
echo "event log is readable by the sandbox uid" >&2
exit 1
fi
echo "== sandbox uid cannot read the event log: ok"
fi
echo "== root-integration: ok"