feat/go-daemon-passive-service #192

Merged
clawdie merged 5 commits from feat/go-daemon-passive-service into main 2026-10-17 16:01:43 +02:00
Owner
No description provided.
Step 2 of docs/wiki/go-migration.md: a small Go service alongside Rust. It is
read-only by construction and owns no canonical state.

Service:
- cmd/colibri-daemon with --socket/COLIBRI_SOCKET, --data-dir/COLIBRI_DATA_DIR,
  --oxigraph/COLIBRI_OXIGRAPH, --verbose, --read-timeout, --shutdown-grace.
  The default socket lives under a uid-scoped colibri-go directory, deliberately
  distinct from the Rust daemon's colibri.sock so both can run on one host.
- internal/daemon: config, protocol, readiness probes, and the server.

Boundaries this relies on:
- Socket directory is created 0700 and refused rather than tightened if it
  already exists with group/other bits. The socket is chmod 0600 and the mode is
  re-read after bind, so a umask cannot quietly widen it.
- Duplicate-bind is decided by dialling the path: a live listener yields
  ErrAlreadyRunning, ECONNREFUSED means a stale socket is reclaimed, and a
  non-socket or symlink at the path is refused instead of removed.
- Peer authentication reads the kernel's own record: SO_PEERCRED on Linux and
  LOCAL_PEERCRED/xucred on FreeBSD. Peer UID must equal the daemon UID; root is
  not special-cased because the service is read-only. A platform without a peer
  credential API fails closed.
- SIGINT/SIGTERM stop accepting, drain within the grace budget, force-close what
  remains, and unlink the socket.

Protocol is line-oriented JSON in the Rust reference envelope: {"cmd":...} in,
{"ok":true,"data":...} or {"ok":false,"error":...} out. "method" is accepted as
an alias and an "id" is echoed when supplied. Commands are version, capabilities,
readiness, ping, and health. Mutation verbs are an explicit deny-list returning
unsupported_mutation, so a refusal is specific rather than a fallthrough. Unknown
verbs are echoed only when they match a safe pattern.

Readiness fails closed on either dependency: the canonical store must exist and
its recorded schema version must match, and Oxigraph must answer one bounded
query. A missing store is reported, never created, because a passive service must
not conjure canonical state. This adds StoredSchemaVersion to the SQLite store as
a read-only accessor.

Tests: 35 daemon tests plus 2 store tests, race-clean, covering permission
enforcement, duplicate bind, stale-socket reclaim, insecure directory refusal,
foreign-UID rejection, unverifiable peer rejection, unsupported mutations,
non-reflection of unsafe verbs, readiness fail-closed behaviour on both probes,
shutdown, and concurrency. Verified end to end: 0600 socket, duplicate instance
exits 1 while the first keeps serving, SIGTERM exits 0 and removes the socket, and
a restart rebinds the same path.

Native FreeBSD runtime proof is not claimed: the package cross-builds clean for
freebsd/amd64 with peer credentials compiled in, and the runtime proof stays a
separate gate.
Two boundary gaps from the Jev review of 643fad6.

Readiness no longer constructs the store. memory.NewSQLiteStore applies
CREATE TABLE IF NOT EXISTS and writes schema_meta, so readiness could have
repaired an incomplete database just by being asked whether it was ready. Replaced
with memory.ReadOnlySchemaVersion, which opens the file with a mode=ro DSN and
runs one SELECT. Verified against modernc.org/sqlite: writes are refused with
"attempt to write a readonly database", a missing path is refused and not created,
and a 0400 database is still inspectable, which a read-write WAL open would not be.
New tests hash the file and the directory listing across a probe of an incomplete
store to prove nothing is created or modified. The read-write StoredSchemaVersion
accessor is removed so the tempting path no longer exists.

The service now refuses to run as root and verifies who owns its socket directory.
Listen returns ErrRunningAsRoot when the effective UID is 0, before touching the
filesystem, and cmd/colibri-daemon refuses earlier still so no flags are parsed
into a doomed start. ensurePrivateDir additionally requires the directory owner to
match the effective UID, and verifySocketMode checks the socket owner as well as
the mode; an owner that cannot be determined fails closed. Ownership reads through
a platform helper (syscall.Stat_t.Uid on unix, unavailable elsewhere), with a seam
so the mismatch path is testable without a second user or root.

Tests: 42 daemon tests plus 5 memory inspection tests and 2 CLI tests, race-clean.
New coverage: root refusal leaves no socket, foreign-owned directory rejected,
unknown owner fails closed, foreign-owned socket rejected, probe reads a read-only
database, and probe leaves an incomplete store byte-identical. Re-verified end to
end: socket 0600 in a 0700 directory owned by the caller, readiness still fails
closed on a missing store, SIGTERM exits 0 and removes the socket.
docs(wiki): record the native FreeBSD passive-service evidence
Some checks failed
CI / markdown (pull_request) Successful in 12s
CI / packaging (pull_request) Successful in 2s
CI / python-tests (pull_request) Successful in 19s
CI / go (pull_request) Failing after 22s
leak-guard / guard (pull_request) Successful in 28s
0fa22a4b05
Step 2 now has runtime proof on FreeBSD, not just the cross-build. The helm
harness ran on valhala and this records its output: FreeBSD 15.1-RELEASE-p4,
go1.26.8, effective UID 1001, 36 assertions PASS with 0 FAIL, staticcheck
2026.2.2 clean, and race tests clean.

The note names what each assertion pins rather than only the count: socket 0600
inside a 0700 directory owned by the caller, both peer-credential paths
(SO_PEERCRED and LOCAL_PEERCRED/xucred) with foreign-UID rejection, root refusal
before any filesystem touch, duplicate bind exiting non-zero while the first
instance keeps serving, read-only readiness that neither creates a missing store
nor modifies an incomplete one nor opens a 0400 database read-write, schema drift
reported instead of migrated, and SIGTERM draining, unlinking the socket, and
exiting 0 with a restart rebinding the same path.

Provenance is stated in the section: the harness output is recorded, not
re-executed by the recorder. okf.verified_by moves to the harness host and
platform_notes now separate what is proven from what is still pending.

wiki-lint --strict requires new jargon in the glossary, so SIGTERM is added to
abbreviations.md as a table row (prose entries are not scanned) and that page's
freshness is bumped.

Review status only: the slice moves from conditionally ready to ready for review.
No runtime ownership is transferred: the service still owns no canonical state and
performs no hydration or dispatch.
test(daemon): eliminate broken pipe race in TestServeRejectsUnverifiablePeer
All checks were successful
CI / markdown (pull_request) Successful in 12s
CI / packaging (pull_request) Successful in 2s
CI / python-tests (pull_request) Successful in 18s
CI / go (pull_request) Successful in 25s
leak-guard / guard (pull_request) Successful in 26s
5f3fe2ea28
When the daemon rejects an unverifiable peer, it sends an immediate
refusal NDJSON response and closes the connection without reading any
command payload. TestServeRejectsUnverifiablePeer previously used
roundTrip(), which attempted to write {"cmd":"version"} before reading.
Under high concurrency or race detection on CI runners, the server closed
the socket before the client's write dispatched, triggering an EPIPE
broken pipe failure.

Extract an expectRejection() helper that dials and reads the immediate
rejection line followed by EOF, matching TestServeRejectsForeignUID.
fix(daemon): require EOF after refusal; attribute evidence to its source
All checks were successful
CI / markdown (pull_request) Successful in 12s
CI / packaging (pull_request) Successful in 2s
CI / python-tests (pull_request) Successful in 17s
leak-guard / guard (pull_request) Successful in 30s
CI / go (pull_request) Successful in 23s
b33a0a5f44
expectRejection accepted any error on the second read, so a server that never
closed the connection would pass after the 3s read deadline rather than fail.
Reuse the reader and require io.EOF, which is what "the server closed the
connection" means here. A never-closing fake peer now fails with
"read error = ... i/o timeout, want io.EOF" instead of passing silently.

Also correct the passive-service evidence section: the recorded 36-assertion
harness proves socket permissions, duplicate bind, missing-store readiness, and
shutdown/restart. Foreign-UID rejection and root refusal are unit tests with
injected UIDs; incomplete-store non-mutation, 0400 inspection, and schema drift
are unit tests; the Linux SO_PEERCRED path is covered on Linux CI. The harness
itself runs non-root on FreeBSD.

Verified natively: focused rejection tests -race -count=50 (100 PASS), full
go test -race ./..., check-go with staticcheck 2026.2.2, and ci-checks.
clawdie deleted branch feat/go-daemon-passive-service 2026-10-17 16:01:44 +02:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
clawdie/colibri!192
No description provided.