From 6daec6b5d03a80af3e2496b6bf666340b3d718fd Mon Sep 17 00:00:00 2001 From: Pjotr Prins Date: Sat, 14 Nov 2026 15:10:28 +0000 Subject: log: name the person and the credential, and resolve handles from the registry The access log said who (`user`) but not with what: with short tokens a request can be made by a shared credential, so "which person" and "which credential" are now separate fields -- `scheme` ("jwt" | "opaque") and, for an opaque token, `token` (its registry handle, e.g. `agent-ravanan-3`). Never the token or its hash, and the path still carries no query string. The line also gains `ts` (RFC 3339 UTC), so a redirected stderr file is self-describing rather than relying on journald's prefix. Hand-rolled civil-from-days rather than a new crate: one format, and no Cargo.lock drift. scripts/gn4-log.py reads such a log and shows it as a table with the handle joined against tokens.sqlite: the subject as the registry has it now (flagging a handle whose subject has been repointed), and the token's status today -- [revoked], [expired], [unknown] -- which is how "an agent was working at 14:02" is told apart from "an agent was working at 14:02 with a credential revoked since". It reads the registry read-only, passes non-JSON lines through in table mode (the process log interleaves backtraces), and offers --json and --summary for jq and for counting. CallerSlot now carries the whole Caller (scheme, id, subject) rather than just the subject string. --- scripts/gn4-log.py | 242 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 242 insertions(+) create mode 100755 scripts/gn4-log.py (limited to 'scripts') diff --git a/scripts/gn4-log.py b/scripts/gn4-log.py new file mode 100755 index 0000000..a14e653 --- /dev/null +++ b/scripts/gn4-log.py @@ -0,0 +1,242 @@ +#!/usr/bin/env python3 +"""Read the server's access log and show it with usernames. + +Every request writes one JSON line on stderr (src/lib.rs, `request_log_line`): + + {"event":"request","method":"POST","path":"/compute/gemma","status":200, + "took_ms":912,"request_id":"...","user":"zachs", + "scheme":"opaque","token":"agent-ravanan-3"} + +`user` is already the gn-auth user id, so most lines need no translation. The +`token` handle does: this script joins it against the token registry +(`tokens.sqlite`, `GN4_API_TOKENS_DB`) and shows + +- the subject **as the registry has it now** -- a token's row is authoritative, + so a handle whose target changed is visible; +- the token's status *today* -- `active`, `expired` or `revoked` -- which is + how you tell "an agent was working at 14:02" from "an agent was working at + 14:02 with a credential that has since been revoked"; +- the note on the row, e.g. what the token was minted for. + +Use it on a process log or a proxy log that carries `x-gn4-user`: + + journalctl -u genenetwork4 | scripts/gn4-log.py + scripts/gn4-log.py -f /var/log/gn4.log --since 2026-11-14 --grep gemma + scripts/gn4-log.py -f log.jsonl --json # enriched JSON, for jq + scripts/gn4-log.py -f log.jsonl --summary # who ran what, counted + +Lines that are not JSON (a Ruby backtrace, a GEMMA progress bar) pass through +unchanged unless --quiet; that is deliberate, because the process log +interleaves them and dropping them would hide the interesting failures. +""" +from __future__ import annotations + +import argparse +import json +import os +import re +import sqlite3 +import sys +import time +from collections import Counter +from pathlib import Path + +DATE = re.compile(r"^(?P\d{4}-\d{2}-\d{2})") +TIME = re.compile(r"(?P