Files
Legatus/docs/01_legatus.md

124 lines
6.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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.