[docs] API: curl examples end to end on loopback, §8.4 reload answer with load numbers, UI contract deviations
This commit is contained in:
+245
@@ -0,0 +1,245 @@
|
||||
# Ianitor control API
|
||||
|
||||
`ianitor-control` serves a REST + JSON API on loopback only (`127.0.0.1:7878` by default),
|
||||
under `/api/v1`. The OpenAPI document is generated from the code: `GET /api/v1/openapi.json`,
|
||||
or `ianitor-control openapi` without a running server. The web UI's contract is
|
||||
[docs/ui-api.md](ui-api.md); its last section lists where this API differs.
|
||||
|
||||
Everything below was run end to end on loopback against `--dev` with a real edge
|
||||
(2026-11-07, WSL2 Debian); the outputs are trimmed with `jq`, never edited.
|
||||
|
||||
## Ground rules
|
||||
|
||||
- **Token.** Every call needs `Authorization: Bearer <API token>`. Nothing else identifies a
|
||||
caller (no `Remote-User`). Without a valid token: `401` with `token.missing`,
|
||||
`token.unknown`, `token.expired` or `token.revoked`. 120 calls a minute per token.
|
||||
- **Host header.** Only `127.0.0.1:<port>`, `localhost:<port>`, `[::1]:<port>` and the
|
||||
configured `allowed_hosts` are answered (DNS rebinding). A browser write (`Origin` or
|
||||
`Sec-Fetch-Site` present) also needs `X-Ianitor-UI: 1` (CSRF).
|
||||
- **Bodies** are JSON, at most 64 KiB, with no unknown fields. Error messages never repeat a
|
||||
value you sent.
|
||||
- **Errors** have one shape; `code` is stable, `message` is plain English, `field` points at
|
||||
the form field, `params` holds the values in the message, `detail` is the technical text:
|
||||
|
||||
```json
|
||||
{ "error": { "code": "host.hostname.uppercase", "message": "Use lowercase letters only: wiki.dev.test.",
|
||||
"field": "hostname", "params": { "hostname": "Wiki.dev.test", "suggestion": "wiki.dev.test" },
|
||||
"retry_after_s": null, "detail": "hosts/Wiki.dev.test.toml: `Wiki.dev.test` is not a valid hostname: it must be lowercase" },
|
||||
"errors": [ ... one per field problem ... ], "request_id": "9c1f…", "server_time": "2026-11-07T17:19:40Z" }
|
||||
```
|
||||
- **`server_time`** is in every JSON object answer. Times are RFC 3339 UTC.
|
||||
- **Changes are applies.** Adding, changing, removing a host, Undo and Restore answer
|
||||
`202 {"apply": {...}}` at once; poll `GET /applies/{id}` (every second) until `state` is
|
||||
`kept`, `reverted`, `failed` or `needs_attention`. Applies run one at a time, in order;
|
||||
the others are `queued`.
|
||||
- **`Idempotency-Key`** on a write: a retry with the same key gets the same apply back.
|
||||
- **`base_rev`** (from `GET /hosts/{h}` or the overview's `rev`): the change is refused with
|
||||
`409 change.conflict` only if the same host file changed since.
|
||||
|
||||
## Start
|
||||
|
||||
Development, everything temporary on 127.0.0.1 (removed on Ctrl-C):
|
||||
|
||||
```sh
|
||||
cargo build -p ianitor-edge -p ianitor-control # so --dev finds ianitor-edge next to it
|
||||
cargo run -p ianitor-control -- --dev
|
||||
```
|
||||
|
||||
```text
|
||||
Ianitor control (dev) on http://127.0.0.1:7878/api/v1
|
||||
Routes repository: /tmp/ianitor-dev-1523/routes.git
|
||||
Dev proxy: …/ianitor-edge on https://127.0.0.1:18443 (HTTP 18080). tls = "domi" works for app.dev.test, wiki.dev.test, photos.dev.test; CA: /tmp/ianitor-dev-1523/certs/dev-ca.crt
|
||||
API token (shown once): ian_06484a2c0a6ec73b_vceenNC…
|
||||
Press Ctrl-C to stop and remove /tmp/ianitor-dev-1523
|
||||
```
|
||||
|
||||
Without `ianitor-edge` (or with `--edge-bin` pointing nowhere) applies are only validated,
|
||||
compiled and written. In dev mode the health watch lasts 10 s (`--watch-secs`); in
|
||||
production 60 s.
|
||||
|
||||
Production: the first token comes from the CLI on the server, never over the network:
|
||||
|
||||
```sh
|
||||
ianitor-control token create --name mark-laptop --config /etc/ianitor/control.toml
|
||||
```
|
||||
|
||||
The examples use:
|
||||
|
||||
```sh
|
||||
TOKEN=ian_… # from the line above
|
||||
API=http://127.0.0.1:7878/api/v1
|
||||
H="Authorization: Bearer $TOKEN"
|
||||
CA=/tmp/ianitor-dev-1523/certs/dev-ca.crt
|
||||
visit() { curl -s -o /dev/null -w '%{http_code}\n' --cacert "$CA" --resolve "$1:18443:127.0.0.1" "${@:2}"; }
|
||||
python3 -m http.server 3000 --bind 127.0.0.1 & # a test upstream
|
||||
```
|
||||
|
||||
## Add a host
|
||||
|
||||
```sh
|
||||
curl -s -H "$H" -H 'Content-Type: application/json' -H 'Idempotency-Key: add-app-1' \
|
||||
-X POST "$API/hosts" -d '{"host": {"hostname": "app.dev.test",
|
||||
"upstreams": ["http://127.0.0.1:3000"], "tls": "domi", "auth": "none",
|
||||
"health_check": {"path": "/", "interval_secs": 5}}}' | jq '{state: .apply.state, id: .apply.id}'
|
||||
```
|
||||
```json
|
||||
{ "state": "queued", "id": "ch_b720484831a9" }
|
||||
```
|
||||
```sh
|
||||
curl -s -H "$H" "$API/applies/ch_b720484831a9" | jq '{state, summary, reason: .reason.message, steps: [.steps[] | "\(.key)=\(.state)"]}'
|
||||
```
|
||||
```json
|
||||
{ "state": "kept", "summary": "Added app.dev.test",
|
||||
"reason": "app.dev.test is live. Health checks passed for 10 s.",
|
||||
"steps": ["validate=done", "compile=done", "write=done", "reload=done", "watch=done", "push=skipped"] }
|
||||
```
|
||||
```sh
|
||||
visit app.dev.test https://app.dev.test:18443/ # 200
|
||||
```
|
||||
|
||||
The same request with the same `Idempotency-Key` returns `ch_b720484831a9` again and changes
|
||||
nothing. A host that fails its health check during the watch is reverted: `state:
|
||||
"reverted"`, and `reason` says why, e.g. `"a.test was not changed. 127.0.0.1:41999 answered
|
||||
with an error page on 3 of 3 checks. Ianitor undid the change."` The routes repository then
|
||||
holds the change and its revert commit; edge and the files never left the last good config.
|
||||
|
||||
A form error comes back at once, field by field:
|
||||
|
||||
```sh
|
||||
curl -s -H "$H" -H 'Content-Type: application/json' -X POST "$API/hosts" \
|
||||
-d '{"host": {"hostname": "Wiki.dev.test", "upstreams": ["http://10.0.0.23"]}}' \
|
||||
| jq '{code: .error.code, errors: [.errors[] | {code, field, message}]}'
|
||||
```
|
||||
```json
|
||||
{ "code": "apply.failed.validation",
|
||||
"errors": [{ "code": "host.hostname.uppercase", "field": "hostname", "message": "Use lowercase letters only: wiki.dev.test." }] }
|
||||
```
|
||||
|
||||
`POST /hosts/validate?mode=create` checks a body without applying it; `PUT /hosts/{h}` with
|
||||
`{"host": …, "base_rev": …}` changes a host; `PATCH /hosts/{h}` with `{"share_links": true}`
|
||||
turns share links on; `DELETE /hosts/{h}` removes one.
|
||||
|
||||
## Undo
|
||||
|
||||
Undo always names what it will undo; the POST must name the same change, or it is refused
|
||||
with `409 undo.target_moved`. Undo right after an Undo is a redo, and `effect` says so.
|
||||
|
||||
```sh
|
||||
curl -s -H "$H" "$API/undo" | jq '{available, target: .target.summary, effect}'
|
||||
```
|
||||
```json
|
||||
{ "available": true, "target": "Added app.dev.test", "effect": "Removes app.dev.test" }
|
||||
```
|
||||
```sh
|
||||
TARGET=$(curl -s -H "$H" "$API/undo" | jq -r .target.change_id)
|
||||
curl -s -H "$H" -H 'Content-Type: application/json' -X POST "$API/undo" \
|
||||
-d "{\"target_change_id\": \"$TARGET\"}" | jq -r .apply.id
|
||||
# then poll /applies/{id}:
|
||||
```
|
||||
```json
|
||||
{ "state": "kept", "summary": "Undid: Added app.dev.test", "reason": "app.dev.test is removed.",
|
||||
"steps": ["validate=done", "compile=done", "write=done", "reload=done", "watch=skipped", "push=skipped"] }
|
||||
```
|
||||
```sh
|
||||
visit app.dev.test https://app.dev.test:18443/ # 000: no certificate is served for a removed host
|
||||
```
|
||||
|
||||
## History and Restore
|
||||
|
||||
One entry per action (a reverted attempt is one `reverted` entry):
|
||||
|
||||
```sh
|
||||
curl -s -H "$H" "$API/history?limit=5" | jq '[.entries[] | {change_id, summary, state}]'
|
||||
```
|
||||
```json
|
||||
[ { "change_id": "ch_cf0b434ecb4c", "summary": "Undid: Added app.dev.test", "state": "kept" },
|
||||
{ "change_id": "ch_b720484831a9", "summary": "Added app.dev.test", "state": "kept" } ]
|
||||
```
|
||||
```sh
|
||||
curl -s -H "$H" "$API/history/ch_b720484831a9/restore-preview" | jq '{restorable, changes}'
|
||||
```
|
||||
```json
|
||||
{ "restorable": true, "changes": [{ "effect": "added", "fields": [], "hostname": "app.dev.test" }] }
|
||||
```
|
||||
```sh
|
||||
REV=$(curl -s -H "$H" "$API/overview" | jq -r .rev)
|
||||
curl -s -H "$H" -H 'Content-Type: application/json' -X POST "$API/restore" \
|
||||
-d "{\"change_id\": \"ch_b720484831a9\", \"base_rev\": \"$REV\"}" | jq -r .apply.id
|
||||
# then poll /applies/{id}:
|
||||
```
|
||||
```json
|
||||
{ "state": "kept", "summary": "Restored the version of 7 November 2026",
|
||||
"reason": "app.dev.test is live. Health checks passed for 10 s." }
|
||||
```
|
||||
```sh
|
||||
visit app.dev.test https://app.dev.test:18443/ # 200
|
||||
```
|
||||
|
||||
## Share link
|
||||
|
||||
A host with `auth = "forward-auth"` and `share_links = true` (dev mode points sign-in at an
|
||||
Authelia on 127.0.0.1:9091 that is not running, so without a link the request fails closed):
|
||||
|
||||
```sh
|
||||
curl -s -H "$H" -H 'Content-Type: application/json' -X POST "$API/hosts" \
|
||||
-d '{"host": {"hostname": "wiki.dev.test", "upstreams": ["http://127.0.0.1:3000"],
|
||||
"tls": "domi", "auth": "forward-auth", "share_links": true}}' # poll until kept
|
||||
visit wiki.dev.test https://wiki.dev.test:18443/ # 502: sign-in unreachable, fails closed
|
||||
|
||||
curl -s -H "$H" -H 'Content-Type: application/json' -X POST "$API/hosts/wiki.dev.test/share-links" \
|
||||
-d '{"path_prefix": "/", "expires_in_s": 86400}'
|
||||
```
|
||||
```json
|
||||
{ "id": "ec724bb1420da0d8d81ee897e1e5344b", "expires_at": "2026-11-08T17:20:05Z", "path_prefix": "/",
|
||||
"has_password": false, "url": "https://wiki.dev.test:18443/?share=ish1.…", "warning": null }
|
||||
```
|
||||
|
||||
The URL is shown once (control keeps no token). The first visit trades the token for a
|
||||
path-scoped `HttpOnly; Secure; SameSite=Lax` cookie and redirects to the same URL without it:
|
||||
|
||||
```sh
|
||||
curl -s -o /dev/null -w '%{http_code} -> %{redirect_url}\n' --cacert "$CA" \
|
||||
--resolve wiki.dev.test:18443:127.0.0.1 -c jar "$URL" # 303 -> https://wiki.dev.test:18443/
|
||||
visit wiki.dev.test -b jar https://wiki.dev.test:18443/ # 200, no sign-in
|
||||
```
|
||||
|
||||
Stop it; edge refuses it at once (and within 5 s even if control's nudge is lost):
|
||||
|
||||
```sh
|
||||
curl -s -H "$H" -X DELETE -o /dev/null -w '%{http_code}\n' "$API/share-links/ec724bb1420da0d8d81ee897e1e5344b" # 204
|
||||
visit wiki.dev.test -b jar https://wiki.dev.test:18443/ # 502: back to sign-in
|
||||
visit wiki.dev.test "$URL" # 403: "This link no longer works"
|
||||
```
|
||||
|
||||
Options: `"password": "at least 8 characters"` (the visitor gets a password page; 5 wrong
|
||||
tries a minute per link and network), `"replaces": "<id>"` stops an earlier link,
|
||||
`POST /share-links/stop-all` stops every link made so far, `GET /share-links` lists them
|
||||
(without tokens).
|
||||
|
||||
## Tokens
|
||||
|
||||
```sh
|
||||
curl -s -H "$H" -H 'Content-Type: application/json' -X POST "$API/tokens" -d '{"name": "backup script"}'
|
||||
# 201 {"id": "…", "name": "backup script", "token": "ian_…", "expires_at": "2027-11-07T…"} (shown once)
|
||||
curl -s -H "$H" "$API/tokens" # ids, names, dates; never a value or hash
|
||||
curl -s -H "$H" -X DELETE "$API/tokens/<id>" # 204; anything using it stops now
|
||||
curl -s "$API/overview" | jq .error.code # "token.missing"
|
||||
```
|
||||
|
||||
## All calls
|
||||
|
||||
| Call | Does |
|
||||
|:--|:--|
|
||||
| `GET /overview` | the main screen in one call (hosts with health, engine, last apply, undo, active apply, repo, site); `ETag`/`304` |
|
||||
| `GET /hosts`, `GET /hosts/{h}` | hosts; one host with `rev`, its TOML, health and certificate |
|
||||
| `POST /hosts/validate?mode=create\|edit` | check a host body |
|
||||
| `POST /hosts`, `PUT /hosts/{h}`, `PATCH /hosts/{h}`, `DELETE /hosts/{h}?base_rev=` | applies |
|
||||
| `POST /hosts/{h}/certificate/retry` | try Let's Encrypt again now |
|
||||
| `GET /applies`, `GET /applies/{id}`, `DELETE /applies/{id}` | apply records; cancel a queued one |
|
||||
| `GET /history`, `GET /history/{id}`, `GET /history/{id}/restore-preview` | history, one entry, restore preview |
|
||||
| `GET /undo`, `POST /undo`, `POST /restore` | undo and restore |
|
||||
| `POST /repo/push`, `GET /settings` | push now; settings |
|
||||
| `GET/POST /tokens`, `PATCH/DELETE /tokens/{id}` | API tokens |
|
||||
| `POST /hosts/{h}/share-links`, `GET /share-links`, `DELETE /share-links/{id}`, `POST /share-links/stop-all` | share links |
|
||||
| `GET /health` | edge reachability, config hash, trial, paused state, hosts |
|
||||
| `GET /openapi.json` | the OpenAPI document |
|
||||
Reference in New Issue
Block a user