Concepts¶
agent ──ssh──▶ authorized_keys: command="ops-gate serve"
│ plain words only; verb, arguments and approval checked against verbs.toml
│ per-class rate limit; signed approval verified and spent
▼
sudo --non-interactive -- <the verb's fixed argv> (sudoers generated from verbs.toml)
The agent never gets a shell. Its SSH key may only run ops-gate serve, the gate runs only the fixed commands of one table, and sudo allows only the commands generated from that same table.
The verb table¶
A verb is a name, a class, a rate limit, typed arguments and one or more fixed steps. A step is an argv, never a shell line; {name} in a step is replaced by the value of that argument.
[verbs.app-logs]
class = "read"
limit = "reads"
about = "The last lines of one Dokku app's logs, at most 500."
arguments = ["app", "lines"]
steps = [["/usr/bin/dokku", "logs", "{app}", "--num", "{lines}"]]
Classes¶
| Class | Runs | RetroSpeedLab's verbs |
|---|---|---|
read |
At once, within its rate limit | disk, docker-usage, containers, gate-table, app-status, app-logs, journal |
repair |
At once, within its rate limit | prune-builder, prune-images, prune-containers, restart, kafka-topics |
approval |
Only with a signed, single-use approval for these exact words on this host | retire, vacuum-journal, restart-guarded |
read and repair behave the same in the gate; the class tells the agent and the reviewer how much harm a verb can do, and each verb names its own limit, so repairs can get a tighter one. A verb of any other class than approval refuses an approval token, so the agent cannot spend one where none is needed.
Argument kinds¶
| Kind | Accepts | Matched in sudoers by |
|---|---|---|
integer |
Up to six digits between minimum and maximum |
[0-9]+ |
choice |
One of values |
A group of the values as alternatives |
app |
An app the server lists right now | apps.pattern |
restartable-app |
A listed app that is neither in apps.never nor in apps.guarded |
apps.pattern |
guarded-app |
A listed app that is in apps.guarded |
apps.pattern |
To check an app argument, the gate runs the table's apps.list command through sudo and accepts only a name it prints, skipping lines that start with apps.header. At RetroSpeedLab, restart takes a restartable-app, so the maintainer can restart the backend but never itself (never), and Bugsink and the database only through restart-guarded, which needs approval.
How the gate checks a call¶
ops-gate serve reads the request from SSH_ORIGINAL_COMMAND and refuses it, with a reason on stderr and exit status 2, at the first rule it breaks:
- At most 512 characters of plain words,
[A-Za-z0-9._:=@-], separated by single spaces. Quotes, pipes, semicolons and redirections never get this far. - The first word is a verb of the table.
- A last word starting with
approval=is the approval token; a verb of classapprovalneeds one, every other verb refuses one. - The verb gets exactly as many arguments as it declares, and each one is valid for its kind.
- Under a lock on the state file: the approval is verified and spent, then the call is counted against the verb's limit, a sliding window of
callspersecondson this host. - Each step runs as
sudo --non-interactive -- <argv>, at moststep_secondslong (a step that times out reports exit status 124). The output loses its ANSI colours and keeps only its lastoutput_characters, cut at a line start.
The gate prints each step as $ <argv> followed by its output and exits with the highest exit status of its steps. Every call is logged to syslog's auth facility under ops-gate: ran '<words>': exit <status> without the approval token, or refused '<command>': <reason>.
Signed approvals¶
An approver holds an Ed25519 private key; each server has the public half in /etc/ops-gate/approver.pub. ops-gate keygen creates the pair, and ops-gate approve or ops_gate.approval.sign_run in your backend signs one run.
What is signed¶
A run approval is the last word of the command:
approval=<expiry>.<nonce>.<signature>
expiry is a Unix time in seconds, nonce 16 random bytes as 32 hex characters, and signature the 128 hex characters of the Ed25519 signature over these lines, joined by newlines:
<purpose>-run
<host>
<word 1>
…
<word n>
<expiry>
<nonce>
The words are the command without the token. The table's purpose keeps a signature for one deployment, or for a table update (<purpose>-enable), from ever counting as another.
When the gate refuses it¶
| Refusal | Cause |
|---|---|
| the approval is malformed | The token does not have the shape above |
| the approval has expired or lasts too long | The expiry has passed, or lies more than approval_seconds ahead |
| the approver did not sign this command on this host | Forged, or signed for another host, other words or another purpose |
| the approval was used already | Its nonce is in the state file |
A spent nonce stays in the state file until its expiry has passed, by which time the approval is refused as expired anyway. Because the gate refuses approvals that last longer than approval_seconds, RetroSpeedLab's backend signs them for 840 of the allowed 900 seconds, leaving a minute for clock skew.
The updater¶
The agent may propose a new verb table in a pull request, but a server only runs a table an approver has signed. ops-gate update, run as root by a timer, installs it:
- It reads the URL in
/etc/ops-gate/approvals-url, which must behttps://and contain{host}, replaces{host}with the content of/etc/ops-gate/hostand fetches a JSON list of table approvals. - Each approval is checked with the
purposeandapproval_secondsof the running table, so a new table cannot loosen the rules it is checked by. It must have exactly the fieldshost,commit,digest,expiry,nonce,signatureandtable, be for this host, be fresh, carry a valid signature and hash to itsdigest. - Its nonce is spent in
/var/lib/ops-updater/used.json. - The new table must pass the same checks as any table. The updater stages it next to
/etc/ops-gate/verbs.toml(mode0640, group: the table'suser), writes its sudoers rules to a hidden file in/etc/sudoers.d(mode0440; sudo skips names with a dot) and runsvisudo --checkon it. - Only if visudo accepts them, the sudoers rules replace
/etc/sudoers.d/<user>and then the table replaces the running one, each with an atomic rename. Otherwise both staged files are removed.
The table approval is a JSON object that ops_gate.approval.sign_table(key, purpose, host, commit, table, expiry) returns; it signs these lines:
<purpose>-enable
<host>
<commit>
<sha256 of the table text>
<expiry>
<nonce>
The updater logs to syslog under ops-updater: installed the table of <commit> (<digest>), or refused an approval: <reason> before it moves on to the next one. A failed fetch fails the run, so the timer's unit shows it. A new table that changes user writes a new drop-in under the new name and leaves the old one in place; remove that by hand.
The generated sudoers rules¶
ops-gate sudoers verbs.toml prints one rule for the apps.list command and for every distinct step of every verb. The gate is the first fence; sudo is the second, so even a bug in the gate cannot run a program outside programs or an argument outside its shape:
- Every step's program must be listed in
programs, or the table is refused. - A step without arguments gets
"", which allows that program with no arguments at all. - A step without placeholders is allowed with exactly its arguments.
- A step with placeholders becomes an anchored regular expression,
^…$, over its arguments: app kinds matchapps.pattern, integers[0-9]+, choices their alternatives. This needs sudo 1.9.10 or later. .and+in literals are escaped as[.]and[+], and,,:,=and\with a backslash. A literal with any other character than[A-Za-z0-9/._=:@+-]is refused, because sudoers could not match it exactly.
The sudoers rules are deliberately coarser than the gate: an app pattern instead of the server's app list, any number instead of the bounds. RetroSpeedLab's table generates these rules:
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/dokku apps\:list
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/df -h --local --exclude-type\=tmpfs --exclude-type\=devtmpfs --exclude-type\=overlay --exclude-type\=squashfs --exclude-type\=efivarfs
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/du -xh --max-depth\=2 --threshold\=1G /var/lib /var/log
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/docker system df
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/docker ps --all
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/sha256sum /etc/ops-gate/verbs[.]toml
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/dokku ^ps\:report [a-z0-9-]+$
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/dokku ^logs [a-z0-9-]+ --num [0-9]+$
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/journalctl ^--unit\=(docker|containerd|nginx|ssh|cron|systemd-journald|dokku-event-listener|postgresql@16-main) --since\=-[0-9]+min --lines\=500 --no-pager --output\=short-iso$
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/docker ^builder prune --all --force --filter until\=[0-9]+h$
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/docker ^image prune --all --force --filter until\=[0-9]+h$
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/docker ^container prune --force --filter until\=[0-9]+h$
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/dokku ^ps\:restart [a-z0-9-]+$
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/docker exec retro-speedlab-lakehouse[.]airflow[.]1 python -m retrospeedlab[.]lake topics src/retrospeedlab/lake/config[.]yaml
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/dokku ps\:retire
maintainer-ops ALL=(root) NOPASSWD: /usr/bin/journalctl ^--vacuum-size\=(200M|500M|1G)$
restart and restart-guarded share one rule, dokku ps:restart <app>; which apps each may restart, and that the second needs approval, only the gate decides.