Files

154 lines
7.2 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.
# 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`.