mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 06:17:23 +01:00
* ops(fleet-env): usage-log points every app at one AI usage log through the fleet file launch.sh.tmpl sets AI_USAGE_DIR, but a launch script is only rewritten by a site sync, so after #1704 no running app had it and Settings > AI said the server is not writing the log. Every launch script already sources /opt/fleet/env with export on. usage-log makes the directory, writes the one line there once, and restarts the apps one at a time. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq * fix(ai): Loki reads and writes the box's usage log without the launch variable Loki's launch script predates /opt/fleet/env, so after the box was pointed at one usage log Loki still had no AI_USAGE_DIR: the card said the server was not writing a log. lib/ai/usage-dir takes the box's directory when the variable is unset and it exists; instrumentation adopts it at boot (and names the app LOKI). Loki walks its own chain, so the package cannot see its calls: recordUsage reports each answered call to the log. scripts/test/usage-dir.ts. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq * docs(env): AI_APP_ID in .env.example Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq * ops(fleet-env): where and retire look beyond the app layout The platform's Google key was retired from /opt/fleet/env and the app env files on 2026-11-13, but nothing had looked at the builder's env (/opt/loki/runner/.env), the named runners' (/etc/loki/runners/*.env) or the Telegram gateway's (~openclaw/.openclaw/.env), where a copy of the same key could still be read by a process that spends. where and retire now also sweep /opt, /etc/loki, /home and /root for env files (not examples, not node_modules, not releases), reading root-owned ones through sudo. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq --------- Co-authored-by: Claude <noreply@anthropic.com>
196 lines
8.2 KiB
Bash
Executable File
196 lines
8.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# /opt/fleet/env — the fleet's shared keys, ONE copy on the box.
|
|
#
|
|
# WHY
|
|
# ---
|
|
# The AI vendors' keys were copied by hand into each app's .env. Eight apps
|
|
# held the shared Groq key; orangecat kept a revoked copy for days
|
|
# (2026-10-08, see api-key-check.sh) and skif never got one, so its AI
|
|
# features stayed switched off. A shared key held N times is rotated N times
|
|
# and forgotten once. Here it is held once; every app reads it first
|
|
# (launch.sh.tmpl, load-build-env.sh) and its own .env may override it with a
|
|
# non-empty value (an empty line keeps the fleet value; FLEET_KEYS_OFF opts out).
|
|
#
|
|
# Runs ON THE BOX as ubuntu (the user every app runs as).
|
|
#
|
|
# Usage:
|
|
# fleet-env.sh show # each key: set or not, an 8-char fingerprint, never a value
|
|
# fleet-env.sh seed # fill keys MISSING from /opt/fleet/env with the value most
|
|
# # apps already share; never overwrites a key already there
|
|
# fleet-env.sh secret KEY # generate KEY (32 random bytes, hex) if the file lacks it
|
|
# fleet-env.sh usage-log # every app writes its AI calls to one log: sets AI_USAGE_DIR in
|
|
# # /opt/fleet/env (once), makes the directory, restarts the apps
|
|
# fleet-env.sh where KEY # every env file on the box that sets KEY, by fingerprint
|
|
# fleet-env.sh retire KEY # switch KEY off everywhere: its line in /opt/fleet/env and in
|
|
# # every app env becomes a dated comment (the value is kept, so
|
|
# # it can be restored by hand), and the units that held it restart
|
|
#
|
|
# Overridable for tests (test-fleet-env.sh): FLEET_ENV, ENV_FILES, KEYS_FILE.
|
|
set -euo pipefail
|
|
|
|
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
FLEET_ENV="${FLEET_ENV:-/opt/fleet/env}"
|
|
KEYS_FILE="${KEYS_FILE:-$HERE/fleet-keys.conf}"
|
|
# shellcheck disable=SC2206
|
|
files=(${ENV_FILES:-/opt/*/shared/.env /opt/*/app/.env})
|
|
|
|
keys() { grep -vE '^[[:space:]]*(#|$)' "$KEYS_FILE"; }
|
|
|
|
value_of() { # first VAR=… in a file, quotes and `export ` stripped
|
|
local run=""; [ -r "$1" ] || run="sudo -n"
|
|
$run sed -nE "s/^(export[[:space:]]+)?$2=//p" "$1" 2>/dev/null | head -1 \
|
|
| sed -E 's/^["'\'']//; s/["'\'']$//; s/[[:space:]]+$//'
|
|
}
|
|
|
|
fingerprint() { printf '%s' "$1" | sha256sum | cut -c1-8; }
|
|
|
|
show() {
|
|
local k v
|
|
while IFS= read -r k; do
|
|
v="$( [ -f "$FLEET_ENV" ] && value_of "$FLEET_ENV" "$k" || true)"
|
|
if [ -n "$v" ]; then printf '%-20s set %s\n' "$k" "$(fingerprint "$v")"
|
|
else printf '%-20s not set\n' "$k"; fi
|
|
done < <(keys)
|
|
}
|
|
|
|
# The value most app env files already agree on: that is the shared key.
|
|
# Placeholders ("your-key", "changeme", "xxx…") never count.
|
|
shared_value() {
|
|
local k="$1" f v
|
|
for f in "${files[@]}"; do
|
|
[ -f "$f" ] || continue
|
|
[ "$f" = "$FLEET_ENV" ] && continue
|
|
v="$(value_of "$f" "$k")"
|
|
case "$v" in ""|*your*|*YOUR*|*changeme*|*placeholder*|xxx*|XXX*) continue ;; esac
|
|
printf '%s\n' "$v"
|
|
done | sort | uniq -c | sort -rn | awk 'NR==1 { $1=""; sub(/^ /, ""); print }'
|
|
}
|
|
|
|
seed() {
|
|
mkdir -p "$(dirname "$FLEET_ENV")"
|
|
[ -f "$FLEET_ENV" ] || { umask 077; printf '# Fleet-wide shared keys (scripts/hetzner/fleet-env.sh). Values are never printed.\n' > "$FLEET_ENV"; }
|
|
chmod 600 "$FLEET_ENV"
|
|
local k v added=0
|
|
while IFS= read -r k; do
|
|
[ -n "$(value_of "$FLEET_ENV" "$k")" ] && continue
|
|
v="$(shared_value "$k")"
|
|
[ -n "$v" ] || continue
|
|
printf '%s="%s"\n' "$k" "${v//\"/\\\"}" >> "$FLEET_ENV"
|
|
echo "seeded $k ($(fingerprint "$v"))"
|
|
added=$((added + 1))
|
|
done < <(keys)
|
|
echo "seeded $added key(s) into $FLEET_ENV"
|
|
}
|
|
|
|
# A key the fleet MAKES rather than collects (a signing secret): generated once,
|
|
# never replaced, never printed.
|
|
secret() {
|
|
local k="${1:?usage: fleet-env.sh secret KEY}"
|
|
grep -qxF "$k" < <(keys) || { echo "refused: $k is not in $(basename "$KEYS_FILE")" >&2; exit 2; }
|
|
mkdir -p "$(dirname "$FLEET_ENV")"
|
|
[ -f "$FLEET_ENV" ] || { umask 077; : > "$FLEET_ENV"; }
|
|
chmod 600 "$FLEET_ENV"
|
|
if [ -n "$(value_of "$FLEET_ENV" "$k")" ]; then echo "$k already set ($(fingerprint "$(value_of "$FLEET_ENV" "$k")"))"; return; fi
|
|
local v; v="$(od -An -tx1 -N32 /dev/urandom | tr -d ' \n')"
|
|
printf '%s="%s"\n' "$k" "$v" >> "$FLEET_ENV"
|
|
echo "generated $k ($(fingerprint "$v"))"
|
|
}
|
|
|
|
# Vendor keys only: a name that is not a credential is never touched.
|
|
key_name() {
|
|
case "${1:-}" in
|
|
*[!A-Z0-9_]*|"") echo "refused: not an env name: ${1:-}" >&2; exit 2 ;;
|
|
*_KEY|*_TOKEN|*_SECRET) ;;
|
|
*) echo "refused: $1 is not a key, token or secret" >&2; exit 2 ;;
|
|
esac
|
|
}
|
|
|
|
# Env files outside the app layout: the builder's, each named runner's, the
|
|
# Telegram gateway's, and anything else named like an env file. On 2026-11-13
|
|
# the platform's Google key was retired from the app files while nothing had
|
|
# looked at these; a key that bills is found wherever it is. Root-owned files
|
|
# are read through sudo.
|
|
elsewhere() {
|
|
[ -n "${ENV_FILES-}" ] && [ -z "${SWEEP_ROOTS-}" ] && return 0 # tests name their files
|
|
# shellcheck disable=SC2086
|
|
sudo -n find ${SWEEP_ROOTS:-/opt /etc/loki /home /root} -maxdepth 5 \
|
|
\( -name '.env' -o -name '.env.*' -o -name '*.env' -o -name 'env' \) -type f \
|
|
-not -path '*/node_modules/*' -not -path '*/releases/*' -not -path '*/.git/*' \
|
|
-not -name '.env.example' -not -name '*.example' 2>/dev/null || true
|
|
}
|
|
|
|
holders() { # env files that set KEY to a non-empty value, the fleet file first
|
|
local k="$1" f
|
|
for f in "$FLEET_ENV" "${files[@]}" $(elsewhere); do
|
|
[ -f "$f" ] || sudo -n test -f "$f" 2>/dev/null || continue
|
|
# The file itself, not a link to it: /opt/<app>/app/.env is a symlink to
|
|
# shared/.env, and an in-place edit of a symlink replaces it with a copy
|
|
# that later changes to shared/.env never reach.
|
|
[ -n "$(value_of "$f" "$k")" ] && readlink -f "$f"
|
|
done | awk '!seen[$0]++'
|
|
}
|
|
|
|
where() {
|
|
local k="${1:-}" f n=0; key_name "$k"
|
|
while IFS= read -r f; do
|
|
printf '%-44s %s\n' "$f" "$(fingerprint "$(value_of "$f" "$k")")"; n=$((n + 1))
|
|
done < <(holders "$k")
|
|
echo "$k is set in $n file(s)"
|
|
}
|
|
|
|
# 2026-11: GEMINI_API_KEY sat here as a "free tier" on a Google project that
|
|
# had billing, and every app read it: about $567 in two days. Taking a key out
|
|
# of one file left it in the others, and a running app keeps the value it
|
|
# started with, so retiring is one command that does all three.
|
|
retire() {
|
|
local k="${1:-}" f n=0 run; key_name "$k"
|
|
local stamp; stamp="$(date -u +%F)"
|
|
while IFS= read -r f; do
|
|
run=""; [ -w "$f" ] || run="sudo -n"
|
|
$run sed -i -E "s/^([[:space:]]*(export[[:space:]]+)?$k=)/# retired $stamp: \\1/" "$f"
|
|
echo "retired $k in $f"; n=$((n + 1))
|
|
done < <(holders "$k")
|
|
echo "retired $k in $n file(s)"
|
|
# The fleet file feeds every app, so with it changed every app restarts.
|
|
[ "$n" -gt 0 ] && restart_apps
|
|
return 0
|
|
}
|
|
|
|
restart_apps() { # one at a time, so the box never loses them together
|
|
[ "${RESTART:-1}" = 1 ] || return 0
|
|
local unit
|
|
for unit in $(systemctl list-units --type=service --state=running --no-legend --plain '*-app.service' 2>/dev/null | awk '{print $1}'); do
|
|
sudo systemctl restart "$unit" && echo "restarted $unit" || echo "could not restart $unit" >&2
|
|
sleep 5
|
|
done
|
|
}
|
|
|
|
# One usage log for the whole box (@bitbaum/ai-kit usage-log): each model call
|
|
# an app makes is one line under AI_USAGE_DIR. Every launch script sources
|
|
# /opt/fleet/env with export on, so one line here reaches every app, including
|
|
# those whose launch script predates the template that sets it. Not a key: the
|
|
# value is a path and is printed.
|
|
usage_log() {
|
|
local dir="${FLEET_USAGE_DIR:-/opt/fleet/ai-usage}"
|
|
mkdir -p "$dir" && chmod 755 "$dir"
|
|
mkdir -p "$(dirname "$FLEET_ENV")"
|
|
[ -f "$FLEET_ENV" ] || { umask 077; : > "$FLEET_ENV"; }
|
|
chmod 600 "$FLEET_ENV"
|
|
if [ "$(value_of "$FLEET_ENV" AI_USAGE_DIR)" = "$dir" ]; then echo "AI_USAGE_DIR already $dir"; return 0; fi
|
|
sed -i -E '/^[[:space:]]*(export[[:space:]]+)?AI_USAGE_DIR=/d' "$FLEET_ENV"
|
|
printf 'AI_USAGE_DIR="%s"\n' "$dir" >> "$FLEET_ENV"
|
|
echo "AI_USAGE_DIR set to $dir"
|
|
restart_apps
|
|
}
|
|
|
|
case "${1:-show}" in
|
|
show) show ;;
|
|
seed) seed; show ;;
|
|
secret) secret "${2:-}" ;;
|
|
usage-log) usage_log ;;
|
|
where) where "${2:-}" ;;
|
|
retire) retire "${2:-}" ;;
|
|
*) echo "usage: fleet-env.sh show|seed|secret KEY|usage-log|where KEY|retire KEY" >&2; exit 2 ;;
|
|
esac
|