claude-code: write the agent's home as its own account where it has one

On the control node agents are to run as an account that cannot become root
(hq ADR 0266). The runtime keeps running as the operator, so the token, the
identity and the home-scope items go to the agent's home through a helper
started as that account; the licence and the account file follow the move,
what was placed in the operator's home goes once the agent holds the licence,
and the guard refuses every tool of a session running as anyone else,
recording to the journal. An API key is taken only from the agent's home and
only when it is one, here and in the licence manager, so the tool cannot carry
off the runtime's credential. Unchanged where no agent account is named.
This commit is contained in:
jochen
2026-10-09 10:14:18 +02:00
parent 7339ce8556
commit b57e6e8144
17 changed files with 1467 additions and 102 deletions
@@ -10,8 +10,9 @@ package main
// process, which is fixed at exec. A command's `export`, an `env` key in a settings file, a variable the
// session sets for its tools — none of them reaches it. A nested session the agent starts with the
// variable is refused before it starts: the command names the override;
// - **recorded**: every override is a line in the module's guard.log, with why, the command and the
// machine — and an override that cannot be recorded is not honoured. Refusals are recorded the same way.
// - **recorded**: every override is a line in the module's guard.log — or, where the agent runs as an account
// of its own, in the machine's journal (novox/hq ADR 0266) — with why, the command and the machine; an
// override that cannot be recorded is not honoured. Refusals are recorded the same way.
//
// A guard that fails — a fault in this code — exits 1, which the agent shows and does not block on: a guard
// that refused every command would stop the session the operator needs to repair it.
@@ -25,6 +26,7 @@ import (
"net"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
@@ -55,11 +57,26 @@ type GuardEntry struct {
Session string `json:"session,omitempty"`
}
// JournalRecord is the guard's record where the agent runs as an account of its own (novox/hq ADR 0266): the
// session's account cannot enter the module's state, and must not be able to rewrite what it was refused, so
// each line goes to the machine's journal, which the account may add to and not change. The record's
// location is then this prefix and the journal's identifier.
const JournalRecord = "journal:"
// GuardJournalID is the identifier the guard's lines carry in the journal.
const GuardJournalID = "mesh-claude-code-guard"
// journalSocket is where the journal takes a line; a test points it at its own.
var journalSocket = "/run/systemd/journal/socket"
// RecordGuard appends one line to the record.
func RecordGuard(path string, e GuardEntry) error {
if path == "" {
return fmt.Errorf("no record is kept here")
}
if id, ok := strings.CutPrefix(path, JournalRecord); ok {
return recordInJournal(id, e)
}
if info, err := os.Stat(path); err == nil && info.Size() > GuardLogMax {
_ = os.Rename(path, path+".1")
}
@@ -75,9 +92,68 @@ func RecordGuard(path string, e GuardEntry) error {
return f.Close()
}
// recordInJournal says one line to the journal by its native protocol: the entry as JSON in MESSAGE, one line,
// so nothing in a command can add a field of its own.
func recordInJournal(id string, e GuardEntry) error {
raw, err := json.Marshal(e)
if err != nil {
return err
}
conn, err := net.Dial("unixgram", journalSocket)
if err != nil {
return fmt.Errorf("the journal cannot be reached: %w", err)
}
defer conn.Close()
_, err = conn.Write([]byte("SYSLOG_IDENTIFIER=" + id + "\nPRIORITY=5\nMESSAGE=" + string(raw) + "\n"))
return err
}
// readJournal is the journal's last lines of the guard's record, read as root through the runtime account's
// sudo where this process is not root: what other accounts said is not the runtime account's to read. As JSON,
// with the field the journal itself sets for the program that wrote the line (_EXE), which no writer chooses.
var readJournal = func(id string, last int) ([]byte, error) {
args := []string{"journalctl", "-q", "--no-pager", "-o", "json", "--output-fields=MESSAGE,_EXE", "-t", id,
"-n", strconv.Itoa(last)}
if os.Geteuid() != 0 {
args = append([]string{"sudo", "-n"}, args...)
}
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = append(os.Environ(), "LC_ALL=C", "LANG=C")
return cmd.Output()
}
// guardsOwnLines keeps, of the journal's JSON lines, the messages the guard's own binary wrote (novox/hq ADR
// 0266). The agent's account may add to the journal under any identifier — `logger -t` — and the journal
// says which program wrote each line; a line another program wrote under the guard's identifier is not the
// guard's record. The agent can still run the guard's binary itself, so the record is the guard's words, not
// proof that each refusal happened in a session: what it cannot do is remove one.
func guardsOwnLines(raw []byte, exe string) []byte {
var out []byte
for _, line := range bytes.Split(raw, []byte("\n")) {
var f struct {
Message any `json:"MESSAGE"`
Exe string `json:"_EXE"`
}
if json.Unmarshal(line, &f) != nil || f.Exe != exe {
continue
}
if m, ok := f.Message.(string); ok {
out = append(append(out, m...), '\n')
}
}
return out
}
// ReadGuardLog is the record's last lines, newest last.
func ReadGuardLog(path string, last int) []GuardEntry {
raw, err := os.ReadFile(path)
read := os.ReadFile
if id, ok := strings.CutPrefix(path, JournalRecord); ok {
read = func(string) ([]byte, error) {
raw, err := readJournal(id, last)
return guardsOwnLines(raw, HelperPath()), err
}
}
raw, err := read(path)
if err != nil {
return []GuardEntry{}
}
@@ -183,6 +259,14 @@ func isAgent(cmdline []byte) bool {
return false
}
// currentAccount is the account the session runs as; a test sets its own.
var currentAccount = func() string {
if u, err := user.Current(); err == nil {
return u.Username
}
return "uid " + strconv.Itoa(os.Getuid())
}
// runGuard is the hook: the tool call in, the verdict out as the exit code.
func runGuard(dataPath string, stdin io.Reader, stderr io.Writer) (code int) {
defer func() {
@@ -201,7 +285,7 @@ func runGuard(dataPath string, stdin io.Reader, stderr io.Writer) (code int) {
_ = json.Unmarshal(raw, &data)
}
g := Guard{Data: data, SSH: sshDashG, Resolve: quickLookup}
v := g.Judge(in)
v := g.JudgeAs(currentAccount(), in)
if !v.Refuse {
return 0
}