[docs] Legatus: CLAUDE.md, design (docs/01), protocol v1 (docs/02), README, MPL-2.0 licence, pre-commit hook

This commit is contained in:
2026-11-17 11:28:31 +01:00
commit 1a00a13434
9 changed files with 1148 additions and 0 deletions
+123
View File
@@ -0,0 +1,123 @@
# 01 · Legatus v0.1: the agent's foundations
Phase T3a of Fabrica's coherence roadmap (`docs/32_coherence.md`, 2.2–2.4). Legatus (envoy) acts for Fabrica on a
machine: it builds there (*build here*) and deploys there (*deploy here*). This page is the design; the wire protocol
Fabrica implements is [02_protocol.md](02_protocol.md).
## The promise it keeps
> **Nothing unsigned runs.** The agent refuses any release whose signature does not chain to the team's release key.
And: no inbound ports (it dials out), one copied command to install, and every refusal is reported to Fabrica, never
silent.
## Pieces
| Crate | What | Pure? |
|:--|:--|:--|
| `legatus-protocol` | enrollment codes and their single use, the messages, the job guard, the agent registry, the release manifest and its signature check, safe archive paths | yes |
| `legatus-linux` | the Linux (systemd) target: release dirs, atomic switch, restart, health, rollback | no (files, `systemctl`) |
| `legatus` | the binary: state, HTTPS/mTLS client, enroll, the job loop, the deploy pipeline | no |
The server side (Fabrica, T3b) uses `legatus-protocol` and `domi-pki` and implements 02_protocol.md.
## Install (one copied command)
Fabrica's *Add target* shows one line with a fresh enrollment code:
```sh
curl -fsSL https://fabrica.example/legatus/install.sh | sudo sh -s -- lgt1-<code>
```
`install.sh` downloads the Legatus binary for this OS and CPU from the same origin, checks its SHA-256 against the
`SHA256SUMS` beside it (and the minisign signature when `minisign` is installed), installs it to
`/usr/local/bin/legatus`, runs `legatus enroll <code>` and starts `legatus.service`.
Trust: the very first download trusts the HTTPS origin, as every `curl | sh` does. From `legatus enroll` on, nothing
is trusted on the network's word: the code pins the Domi root, the root pins the server, the server's certificate pins
the agent, and the team's release key pins every release.
## The enrollment code
`lgt1-` and lowercase base32 of: version, the SHA-256 of the Domi root certificate, a 128-bit secret, the server URL,
and a 4-byte checksum (typos are caught before anything is sent). Single use and short-lived (one hour by default).
Fabrica stores only the secret's SHA-256, so a copy of its database does not hold live codes.
`legatus enroll` makes the agent's key (ECDSA P-256, `0600`, never leaves the machine), sends a CSR with the secret
over TLS to the server, accepts the server only if its chain ends at the pinned root, and gets back its agent
certificate (domi-pki, 30 days), its team and agent ids, its roles, and the team's release keys, which it pins.
## The connection
Outbound HTTPS to the Fabrica server with the agent certificate (mTLS). The agent long-polls for jobs and posts
reports; no WebSocket, no inbound port, works through any HTTP-friendly network. The server is checked against the
pinned root and its host name on every connection; the agent's certificate is checked by Fabrica with
`legatus_protocol::Registry` on every request.
Certificates are refused when:
- **expired** or not yet valid (both directions);
- **replayed**: an agent certificate that is no longer the agent's current one (renewed, re-enrolled) or of an agent
that was removed;
- **foreign**: another root, a Domi certificate that is not an agent's (a phone's device certificate), an agent of
another team, or a chain that skips the agents intermediate.
The agent renews its certificate over the existing connection when 10 days are left.
## Roles
`build` and `deploy`, both off after enrollment unless the code grants them; Fabrica toggles them with a `config`
job. A job for a role that is off is refused and reported. Legatus 0.1 reports `build: []` in its capabilities: the
build role's executor (Fabrica's runner) comes later; the toggle and its enforcement exist now.
## Before touching anything: the release check
A deploy job carries the release manifest (the exact signed bytes) and its minisign signature. The agent:
1. checks the signature with the release keys it pinned at enrollment (a missing signature, an unknown key or a bad
signature: refused);
2. reads the manifest strictly: its team must be the agent's team, its app and version the job's, the artifact named
exactly once;
3. downloads the artifact over mTLS, no larger than the manifest says, and checks its SHA-256 (a tampered artifact:
refused, deleted);
4. only then unpacks it (regular files and directories only; no absolute paths, no `..`, no links, size and count
limits) and hands it to the target backend.
Release keys change only by re-enrolling in v0.1, never by a message from the server: a compromised server alone cannot
make the agent run something the team did not sign. (A key rotation signed by a current key is for later.)
Jobs are guarded against replay: each has a 128-bit id, an issue time and an expiry (at most one hour later); an
expired job, a job from the future, or an id seen before is refused. Seen ids survive restarts.
## The Linux target (systemd)
```text
/var/lib/legatus/apps/<app>/releases/<version>/ one dir per release, unpacked from the signed artifact
/var/lib/legatus/apps/<app>/current -> releases/<version>
```
Deploy: unpack into `releases/.incoming-…`, rename to `releases/<version>`, point a new symlink at it and `rename(2)`
it over `current` (atomic: a reader sees the old or the new release, never neither), `systemctl restart <unit>`, then
the health check: the unit is active and, when the job gives one, a local health URL (`http://127.0.0.1…`,
`localhost` or `[::1]` only) answers 2xx, within the timeout. On a failed check: switch `current` back to the
previous release, restart, check again, and report `failed` with `rolled_back`. Old releases are pruned, keeping the
newest three plus whatever `current` and the previous point at.
The unit file itself is the user's (or, from T3b, generated by Fabrica's UI); its `ExecStart` runs from `current/`.
## Tests (first)
- the enrollment code: round trip, typos caught, single use (twice: refused), expired refused, unknown refused;
- certificates: expired, replayed (superseded, removed agent) and foreign (other root, device certificate, other team,
wrong intermediate) refused, by the registry and on a real TLS handshake both ways;
- releases: a tampered artifact, a wrong key, a missing or garbled signature, a manifest for another app, version or
team: refused, and the backend is never called;
- the job guard: expired, future, too long-lived and replayed jobs refused;
- archive paths: absolute, `..`, links, device files refused;
- Linux: the atomic switch, the health check, auto rollback on a failed check, pruning (on Linux; and in a container
with real systemd: `tests/linux/run.sh`).
## Out of scope for v0.1 (T3b, T5)
The other four targets (NixOS, Windows Server, Docker, Kubernetes), the build executor, Fabrica's server side and UI,
the Windows installer, `.deb`/`.rpm`, remote operations (logs, restart, rollback on demand), signed key rotation.
+153
View File
@@ -0,0 +1,153 @@
# 02 · The Legatus protocol, version 1
What the agent and the Fabrica server say to each other. The types, limits and checks are in the pure crate
`legatus-protocol`; Fabrica implements the server side with that crate and `domi-pki` (handover:
Fabrica `docs/handover/t3a-legatus-protocol.md`). Everything a party receives is hostile until checked.
## Transport
- HTTPS (HTTP/1.1, TLS 1.3 or 1.2) to `https://<server>/legatus/v1/…`; the server is the one in the enrollment code.
- **The agent only dials out.** It never listens.
- **The server's chain** is leaf, the intermediate, and the Domi root itself (the agent pins the root by its SHA-256
until it has stored it). The leaf must be valid for the server's host name and for serverAuth. Fabrica issues it with
`domi_pki::issue_server` from the agents intermediate.
- **The agent's certificate** (mTLS) is required on every endpoint but `enroll`. The server's TLS layer asks for a
client certificate without requiring one; the application checks it per request with
`legatus_protocol::Registry::accept` (domi-pki `verify_agent` and the registry) and answers `401` when it is absent,
`403 certificate_refused` when it is refused.
- Bodies are JSON (`application/json`, UTF-8), at most 256 KiB, unknown fields refused. Every message has `"v": 1`.
- Errors: the HTTP status and `{"error": "<code>", "message": "<plain English>"}`.
- Times are unix seconds (UTC). Each side allows 5 minutes of clock skew.
## Enrollment
### The code (made by Fabrica, shown in *Add target*)
`lgt1-` followed by lowercase RFC 4648 base32 (no padding) of:
| Bytes | Field |
|:--|:--|
| 1 | version, `1` |
| 32 | SHA-256 of the Domi root certificate (DER) |
| 16 | secret, random |
| 1 | `n`, length of the server URL, 1–200 |
| n | server URL: `https://host` or `https://host:port`, nothing else |
| 4 | the first 4 bytes of SHA-256 of everything before |
Fabrica keeps `{sha256(secret), team, agent, roles, expires_at, used_at}`; it never stores the secret.
`EnrollmentBook::issue` / `redeem` is the reference logic: unknown, used and expired codes are refused, and a code is
used up by the first request that presents it, in one transaction (`UPDATE … SET used_at = now WHERE digest = ? AND
used_at IS NULL AND expires_at > now`).
### `POST /legatus/v1/enroll` (TLS, no client certificate)
```json
{ "v": 1, "secret": "<32 lowercase hex>", "csr": "-----BEGIN CERTIFICATE REQUEST-----…",
"host": { "hostname": "web-1", "os": "linux", "arch": "x86_64", "version": "0.1.0" } }
```
The server checks the CSR first (domi-pki: signature, ECDSA P-256), then redeems the code, then issues
`domi_pki::issue_agent(agents_intermediate, csr, team, agent, now)` and records the serial as the agent's current one
(re-enrolling an agent replaces it: its old certificate stops working).
`200`:
```json
{ "v": 1, "team": "acme", "agent": "web-1",
"chain": "<agent certificate PEM><agents intermediate PEM>",
"root": "<Domi root PEM>",
"roles": { "build": false, "deploy": true },
"release_keys": ["RWQ…base64 minisign public key…"],
"poll_seconds": 25 }
```
The agent checks that `root` hashes to the pinned SHA-256, that `chain` is an agent certificate for its own key under
that root (`verify_agent`), and stores all of it. `release_keys` must hold 1 to 8 keys; they are pinned.
Refusals: `403 code_unknown`, `403 code_used`, `403 code_expired`, `400 bad_csr`, `400 bad_request`.
## After enrollment (mTLS)
### `POST /legatus/v1/hello`
```json
{ "v": 1, "version": "0.1.0", "os": "linux", "arch": "x86_64", "hostname": "web-1",
"capabilities": { "deploy": ["linux"], "build": [] },
"deployed": [ { "app": "shop", "version": "1.4.0" } ] }
```
`200`: `{ "v": 1, "team": "acme", "agent": "web-1", "roles": {…}, "server_time": 1800000000, "renew": false }`.
`renew: true` asks the agent to renew now (the server may also just let it renew at 10 days left).
### `GET /legatus/v1/jobs?wait=<seconds, 0–55>`
Long poll. `204` when there is nothing within `wait`; `200` with one job:
```json
{ "v": 1, "id": "<32 lowercase hex>", "issued_at": 1800000000, "expires_at": 1800000600,
"kind": "deploy", "spec": { … } }
```
`expires_at` is at most 3600 s after `issued_at`. The agent refuses (and reports) a job that is expired, issued more
than 5 minutes in the future, lives too long, or whose id it has seen before (`JobGuard`).
`kind` is one of:
- `deploy`:
```json
{ "app": "shop", "version": "1.4.0", "artifact": "shop-linux-x86_64.tar.gz",
"release": { "manifest": "<the manifest JSON, exactly the signed bytes>",
"signature": "<the minisign .minisig file>" },
"target": { "kind": "linux",
"linux": { "unit": "shop.service", "health_url": "http://127.0.0.1:8080/health",
"health_timeout_seconds": 30, "keep": 3 } } }
```
- `config`: `{ "roles": { "build": true, "deploy": true } }` (roles toggled in Fabrica).
Any other `kind` (or target `kind`) is refused with `unsupported` and reported, so Fabrica sees what an older agent
cannot do. `build` is reserved for the build role's executor.
### `GET /legatus/v1/artifacts/<job id>/<artifact name>`
The artifact's bytes (`application/octet-stream`, `Content-Length`). Only for a deploy job sent to this agent.
### `POST /legatus/v1/reports`
```json
{ "v": 1, "job": "<id>", "at": 1800000010, "status": "succeeded",
"step": "health", "message": "shop 1.4.0 is running", "rolled_back": false,
"deployed": { "app": "shop", "version": "1.4.0" } }
```
`status`: `accepted`, `refused`, `running`, `succeeded`, `failed`. `step` (optional): `guard`, `role`, `verify`,
`download`, `unpack`, `switch`, `restart`, `health`, `rollback`. `refused` means nothing was touched. `failed` after
`switch` comes with `rolled_back` (true when the previous release is running again). `message` is plain English, at
most 2000 characters, and never holds a secret. `204` in reply.
Refusal reasons the agent uses (in `message`, prefixed by a stable code): `role_off`, `expired`, `replayed`,
`future`, `unsupported`, `unsigned`, `unknown_key`, `bad_signature`, `bad_manifest`, `wrong_release` (team, app,
version or artifact differ from the job), `tampered` (size or SHA-256), `bad_archive`.
### `POST /legatus/v1/renew`
`{ "v": 1, "csr": "<PEM>" }` → `200 { "v": 1, "chain": "<PEM>" }`. The server records the new serial as *pending*:
the old certificate keeps working until the new one is first used, then the old one is refused
(`Registry::renewed`, `accept`).
## The release manifest (signed by the team's release key in Custos)
```json
{ "v": 1, "team": "acme", "app": "shop", "version": "1.4.0", "created_at": 1800000000,
"artifacts": [ { "name": "shop-linux-x86_64.tar.gz", "sha256": "<64 lowercase hex>", "size": 1234567 } ] }
```
Signed with minisign (Ed25519, prehashed: what Custos's `minisign` format makes today) over the exact bytes. The
release key is the team's (T1's team vault). Artifact names: 1–128 of `A-Z a-z 0-9 . _ + -`, not starting with `.`.
App ids are domi-pki ids; versions 1–64 of `A-Z a-z 0-9 . _ + -`, not `.` or `..`. At most 64 artifacts; a size at
most 4 GiB.
Archives for the Linux target are `.tar.gz` with regular files and directories only.
## Versioning
`v` is 1. A server that sees an agent's `version` it does not know still speaks v1; a new field is a new `v`.