Skip to content

Spec: the Console API, /api/v1 behind scoped tokens #1753

Closed
6 of 6 issues completed
Closed

Description

@JacobStephens2

This was generated by AI during triage.

Spec A of four from the #1142 grilling (grilling, ADR 0026, merged in PR #1752). Order: Spec A (this) → release step (ready-for-human) → Spec B, the Fleet in the Console (#1142). Spec C (#1768, run and admin, absorbing #952) follows A.

Problem Statement

Each Console can be reached only through its own web page, by a person signed in to that machine. Nothing else can read what the Console shows or do what it does, whether that's another Console, a script or an agent, unless it has a shell on the machine and runs the CLI. So the Day shift can't see or run its Fleet from one place. On 2026-10-23, Runs started by hand on stephens never appeared on thirdshift's Console. Any machine-to-machine view needs a way in that isn't a browser sign-in and isn't a shell.

The sign-in itself is also all-or-nothing. Every address on a Console's sign-in list can do everything the page offers (#952). Failed passwords have no limit: only an emailed sign-in code is capped, at 5 tries per code.

Solution

Each Console serves a versioned HTTP API, /api/v1. Every endpoint is the twin of a CLI command. It takes the same arguments as that command and returns the same JSON as its --json. Spec A covers every view and action the Console offers today. The page becomes a client of the same handlers, authenticated by its sign-in. Other clients authenticate with a token that the machine issues to a named holder, with scopes: read, security, act, run and admin. Each address on the sign-in list carries scopes of its own too, all five by default. A Console calling on someone's behalf names the person, and the owning machine allows only what both the token and that person may do there. Tokens are issued, listed and revoked only from the CLI on the machine itself. The API publishes an OpenAPI description, and failed tokens and passwords are rate-limited per address.

After Spec A, any authorized client can see and act on a machine over HTTPS. Spec B builds the Fleet on top of it.

User Stories

  1. As the Day shift, I want each Console to serve a versioned API at /api/v1, so that clients written today keep working when the page changes.
  2. As the Day shift, I want every API endpoint to return exactly the JSON its CLI twin prints with --json, so that what I learn from the CLI holds for the API.
  3. As the Day shift, I want every API endpoint to take the same arguments as its CLI twin, so that I can translate a command into a request without a separate reference.
  4. As the Day shift, I want the Inbox through the API, with every group and its active-run count, so that a client can see what asks something of me.
  5. As the Day shift, I want a Command's details through the API (its child Ticket runs, stages, waterfall, Command log and Session traces), so that a client can show what the page shows.
  6. As the Day shift, I want the History, Lanes, Stages, metrics, Scores, Bug origins, Experiments and cron jobs views through the API, so that no view exists only on the page.
  7. As the Day shift, I want Other-account Commands in the API exactly as in status, tagged with their account and never stoppable or dismissable, so that Console: opt-in setting to show other local accounts' Commands (read-only) #1446's boundary holds over HTTP.
  8. As the Day shift, I want to Stop a live Command through the API, so that a client can end work as thirdshift stop does.
  9. As the Day shift, I want to make a Dismissal, and take one back, through the API, so that a client can clear Needs you and Ready for review.
  10. As the Day shift, I want to enter my Scores, settle a Bug origin and approve a proposed Eval task through the API, so that the eval judgments aren't page-only.
  11. As the Day shift, I want to set the Console's Theme and hidden sections through the API, so that the page's own look is an API call like everything else.
  12. As the Day shift, I want the Console page to use the same handlers as the API, so that the page and the API can never disagree.
  13. As the Day shift, I want my existing sign-in to authenticate API calls made from the page, so that nothing changes for me in the browser.
  14. As the machine's owner, I want to issue a token from the CLI with thirdshift console token create <holder> --scopes <list> [--expires <date>], so that I decide who gets in and with what.
  15. As the machine's owner, I want a token's scopes to default to read, so that a token I issue carelessly can only look.
  16. As the machine's owner, I want the token printed once and never shown again, so that it exists only where I paste it.
  17. As the machine's owner, I want the machine to keep only a hash of each token, so that reading its files doesn't hand out working tokens.
  18. As the machine's owner, I want thirdshift console token list to show each token's holder, scopes, issue date, expiry and last use, so that I can see who still uses what.
  19. As the machine's owner, I want thirdshift console token revoke <holder> to take effect on the very next request, so that a leaked token stops working at once.
  20. As the machine's owner, I want an expired token refused, so that a temporary grant ends without me remembering it.
  21. As the machine's owner, I want a token refused when it's sent over plain HTTP, except to 127.0.0.1, so that tokens never cross the network in the clear.
  22. As the machine's owner, I want repeated failed tokens from one address slowed and then refused for a while, so that tokens can't be guessed.
  23. As the machine's owner, I want repeated failed passwords limited the same way, so that sign-in can't be brute-forced either.
  24. As the machine's owner, I want every API call that changes anything recorded in the Console log, naming the token's holder and any person it names, so that I can audit remote actions.
  25. As the machine's owner, I want reads left out of the Console log, as today, so that the log stays readable.
  26. As the machine's owner, I want a token with read to see every view except a Security run's details and findings, so that I can share visibility without sharing vulnerabilities.
  27. As the machine's owner, I want the security scope to open Security run details and findings, so that I choose who sees them.
  28. As the machine's owner, I want a Security run's row and stages visible with read, without its details, so that a reader still knows a Security run is running.
  29. As the machine's owner, I want act needed for Stop, Dismissal and Undo, the eval judgments, and the look settings, so that a reader can't change anything.
  30. As the machine's owner, I want the run and admin scopes to exist from the start, even though their endpoints arrive in Spec C, so that tokens issued now don't need reissuing later.
  31. As the machine's owner, I want each address on the Console sign-in list to carry scopes of its own, with all five as the default, so that a one-person list behaves as today and make user config readable and writeable on thirdshift console web ui #952's read/write split is possible.
  32. As the machine's owner, I want to set an address's scopes only from the CLI on the machine, so that no signed-in browser or token can widen anyone's access.
  33. As the machine's owner, I want a call that names a person refused unless that person is on my own sign-in list, so that someone I never allowed can't reach my machine through another Console.
  34. As the machine's owner, I want such a call allowed only within both the token's scopes and that person's scopes on my machine, so that neither one can widen the other.
  35. As the machine's owner, I want the Activity log to name the person and the machine they acted from for a Stop or Dismissal made through the API, so that the log still says who did it.
  36. As the machine's owner, I want a call that names no person attributed to the token's holder in the Activity log, so that a script's actions are still traceable.
  37. As the machine's owner, I want token issuing and revoking, the sign-in list, address scopes, console install and setup to have no API endpoint at all, so that a stolen token can't widen its own access.
  38. As a client author, I want /api/v1/whoami to say which machine and thirdshift version I reached, the API version, and who I am with which scopes, so that I can check a token before relying on it.
  39. As a client author, I want a published OpenAPI description of /api/v1, so that scripts and agents can discover the API without reading the source.
  40. As a client author, I want the OpenAPI description available from the CLI too, so that it follows the CLI-first rule.
  41. As a client author, I want errors as JSON with distinct statuses: unauthenticated, missing scope, unknown person, rate-limited, and the CLI's own refusals (such as an Other-account Command), so that a client can tell them apart.
  42. As a client author, I want a refusal to name the scope or account that blocked the call, so that I know what to ask for.
  43. As a client author, I want every action endpoint to need a POST, and reads to be GETs, so that nothing changes on a read.
  44. As the Day shift on --no-sign-in over an SSH tunnel, I want the API to work there with full scopes on 127.0.0.1, as the page does, so that local use stays simple.
  45. As an agent working in this repository, I want a test to fail when an API endpoint has no CLI twin, when its JSON differs from that twin's, or when the OpenAPI description misses a route, so that the three ways in can't drift apart.

Implementation Decisions

  • One route table, three ways in. The Console's existing route table, which already pairs each page route with its CLI command, gains an API path and a scope for each entry. Each view's handler produces the CLI's own --json value, so its JSON can't drift from the CLI's. The page renders that value as HTML, and /api/v1 returns it as JSON. The page stays server-rendered with htmx. "The page is a client" means it shares handlers and authentication, not that it becomes a single-page app.
  • Versioning. Everything lives under /api/v1. The JSON is the CLI's versioned --json shape. A breaking change means /api/v2, while /api/v1 stays served.
  • Scopes. read, security, act, run, admin. Every route-table entry declares exactly one. Spec A uses read, security and act. run and admin can be granted but gate no endpoint until Spec C. The look settings (Theme, hidden sections) need act, keeping what the page does today.
  • Authentication. A request is authenticated by the existing sign-in session (cookie) or by a bearer token in the Authorization header. A session's scopes are its address's scopes. A token's effective scopes are its own, narrowed by the named person's scopes when the request names a person.
  • Naming a person. A client acting for a signed-in person (in Spec B, another Console) sends that person's address in a request header. The owning machine refuses the call when the address isn't on its sign-in list, and otherwise allows the intersection of the token's and the person's scopes.
  • Address scopes. The User config's [console] gains a per-address scopes table beside allowed_emails. An address absent from the table has all five scopes. A CLI command sets and shows them, and setup writes the table out. The API never writes it.
  • Tokens. 32 random bytes, encoded with a recognizable prefix so secret scanners can spot one. The machine stores SHA-256 hashes with holder, scopes, issue date, optional expiry and last use, in an owner-only file beside the Credentials. argon2 stays for passwords only. The CLI gets console token create, console token list [--json] and console token revoke. A holder name is unique on a machine, and revoking deletes the record.
  • HTTPS only. A token is accepted only when the request reached the proxy over HTTPS (the X-Forwarded-Proto the installed Caddy and Apache configs already set) or came straight to 127.0.0.1 with no proxy headers. Otherwise it's refused without being checked, so a token sent in the clear can't be confirmed as valid.
  • Rate limiting. An in-memory, per-address limiter written in thirdshift covers failed tokens and failed passwords. The address is the proxy's forwarded client address, trusted only from 127.0.0.1. It backs off, then refuses for a cool-down. A restart clears it, which is acceptable. The existing 5-tries-per-code limit on sign-in codes stays.
  • Auditing. API calls that change anything append to the Console log with the token's holder (or the session's address), any named person, the endpoint and the outcome. Activity log lines for Stop and Dismissal name the person and the machine they acted from, as "by from the Console on " or "by token ", alongside today's CLI and Console wording.
  • whoami. /api/v1/whoami returns the machine's hostname, thirdshift version, API version, and the caller's identity and effective scopes. Its CLI twin reports the same for the local caller.
  • OpenAPI. Generated, for example with utoipa, from the same route table, and served at /api/v1/openapi.json, with a CLI twin that prints it. A test asserts the description and the routes match.
  • Errors. JSON bodies: 401 unauthenticated or expired, 403 missing scope or unknown person (naming which), 429 rate-limited with a retry time, and the CLI command's own refusal as 409 or 422 with its message (for example, a Stop of an Other-account Command names the owning account).
  • Never in the API: token management, the sign-in list, address scopes, console install, setup.
  • Docs. docs/guide.md gets an API section (tokens, scopes, whoami, OpenAPI, plus where tokens are stored and how to revoke one), and the site's hand-written pages say the Console has an API where they describe the Console.

Testing Decisions

  • Good tests exercise external behaviour only: the binary, fixture Command records and HTTP, as tests/console.rs already does. No test reaches into handlers or token storage.
  • Seam 1, the Console over HTTP (tests/console.rs and its tests/console/ modules). Start the binary on fixture records, issue a token through the CLI, then call /api/v1. It covers: each scope allowing and refusing the right endpoints; the JSON for an endpoint equalling the CLI's --json for the same records; Stop and Dismissal through the API showing up in the Activity log and Console log with the right wording; a named person missing from the list, or lacking a scope, being refused; revocation and expiry taking effect on the next request; plain-HTTP tokens being refused; failed tokens and passwords being rate-limited; and Other-account Commands staying refused. Prior art: tests/console/stop.rs, dismiss.rs, other_accounts.rs, accounts.rs, password_codes.rs.
  • Seam 2, the route table (the existing CLI-parity unit test in the Console module). It extends to assert that every route has an API path, a scope and a CLI twin, and that the OpenAPI description lists exactly the routed endpoints. Prior art: every_console_route_has_a_cli_equivalent and its should-panic twin.
  • The token CLI (token create/list/revoke, address scopes) is tested through the binary, as with tests/user_config.rs and tests/setup.rs.
  • The site browser tests (site/tests/) cover the page still working on its own sign-in after it switches to shared handlers.

Out of Scope

  • The Fleet: the machine list, fetching other Consoles, merged rows, the --machine CLI and fleet add/list/remove, and holder-side token storage. That's Spec B (Spec: the Fleet in the Console #1142).
  • The run and admin endpoints: starting Commands, writing the User config, update. Also make user config readable and writeable on thirdshift console web ui #952's User config page. That's Spec C.
  • Releasing, installing on each machine and pairing. That's the release step.
  • Streaming (SSE) through the API. The live page keeps its own stream, and Spec B polls.
  • Any change to who may sign in, or to how passwords and sign-in codes work, beyond the per-address limit.

Further Notes

Sub-issues

Sub-issues

6 of 6 Issues completed
Loading 6 sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions