Repository navigation
Description
Part of #754.
Scope
A worker and a gateway keep their tokens in state.db, one row per token, and refuse to start while a tokens.json sits in their folder. Creating, listing, revoking and checking a token work as today.
In short
simlock token create, list and revoke, and every token check (socket hello, HTTP bearer, the gateway's uplink join token), read and write the tokens table in the daemon's state.db instead of tokens.json. A create or a revoke is one transaction, so two at once never lose one another. A daemon that finds tokens.json refuses with the feature's old-file message, naming <data directory>/tokens.json, with its role's upgrade step: a gateway's says to create each worker's join token again and set it as that worker's gateway.token, since a gateway refuses nuke.run (src/gateway/dispatcher.ts:238).
flowchart LR
C[token create / revoke] --> T["tokens table (changed)"]
V[hello, HTTP, uplink check] --> T
S[daemon start] --> O{"tokens.json present? (new)"}
O -->|yes| X[refuse]
If this goes wrong, a user would notice a token that stops working after a restart, or a revoked one that still works.
Technical spec
Facts
Commit 8db8112b.
| # | Fact | Source |
|---|---|---|
| F1 | TokenStore reads and rewrites the whole tokens.json array per create and revoke, behind an in-process queue (#mutation), mode 0600 |
src/auth/token-store.ts:23-111 |
| F2 | verify re-reads the file on every call |
src/auth/token-store.ts:73-77 |
| F3 | TokenRecord: id (tok_<id>), hash, role (one of TOKEN_ROLES), label?, createdAt |
src/auth/token-store.ts:41-48, :121-131; src/auth/tokens.ts |
| F4 | Bad JSON throws Invalid JSON in token store: <path> (<reason>), a bad shape Invalid token store: <path> (TokenStoreError); it answers INTERNAL on HTTP and the socket |
src/auth/token-store.ts:95-104; src/daemon/error-code.ts:80-96; src/http/errors.ts:151; src/daemon/server.ts:1658 |
| F5 | createAuthFrom builds the store at join(dataDirectory, "tokens.json"); Auth.tokens delegates create, list, revoke; resolveHelloRole and authenticateUplink call verify |
src/auth/index.ts:59-65, :97, :103, :109-111 |
| F6 | createAuth is built before the mode branch, so both daemon kinds use it |
src/daemon/main.ts:222 |
| F7 | auth imports only ports and contract | .dependency-cruiser.mjs:186-188; src/auth/AGENTS.md "Uses: ports, contract only" |
| F8 | The CLI never writes tokens.json; token commands go through the daemon |
src/cli/index.test.ts:2846-2847; src/contract/operations.ts:685-687 |
| F9 | Tests that build a TokenStore or createAuth over MemoryFilesystem: src/auth/token-store.test.ts, auth.test.ts, proxy.test.ts, src/cli/index.test.ts:7173, :7286, src/daemon/dispatcher.test.ts:342, src/daemon/server.test.ts:3779, src/http/app.test.ts:2483, src/http/auth.test.ts:122-136 |
rg -l "TokenStore|createAuth" src -g '*.test.ts' |
| F10 | Tests that write tokens.json directly: src/daemon/main.test.ts:1342, :1731, src/agent-device/gateway-front.test.ts:189, src/agent-device/edge.test.ts:156; e2e reads it at e2e/token-route.test.ts:100 |
as cited |
| F11 | e2e creates tokens through token create |
e2e/http-api.test.ts:73, e2e/gateway-fleet.test.ts:145 |
| F12 | Existing store tests: concurrent creates (:97), concurrent revokes (:132), verify sees another instance's token (:177), corrupt file fails loudly (:213, :223), mode 0600 (:70) |
src/auth/token-store.test.ts |
| F13 | Task 1 adds SqliteSchemaOwner, the transaction helper in src/ports/sqlite.ts, the owner and old-file lists, and the messages |
#754 Shared values |
Modules touched
Searches: TokenStore, tokens.json, createAuth(, TokenStoreError over src/, e2e/, docs/.
src/auth/token-store.ts,src/auth/index.ts(createAuthtakesdatabase: SqliteDatabase; exportstokensOwner),src/auth/testing.ts.src/daemon/main.ts:tokensOwnerin both owner lists,tokens.jsonin both old-file lists,state.databaseintocreateAuth.- Tests of F9 and F10;
e2e/token-route.test.tsreads hashes fromstate.db;src/daemon/gateway-fleet.e2e.test.ts(its restart test). - Comments and titles naming
tokens.json(rg -n "tokens\.json" src docs e2e --glob '!docs/internal/adr/**'):src/core/instance-identity.ts:6,src/ports/logger.ts:121,src/gateway/drain-store.ts:16,src/daemon/dispatcher.ts:182,src/contract/operations.ts:685-687,src/daemon/gateway-fleet.e2e.test.ts:164,:406,e2e/http-api.test.ts:71,e2e/token-route.test.ts:10, and the test titlesrc/cli/index.test.ts:2846, which becomes "CLI: token operations go through the daemon" with the same assertions. - Docs:
docs/CONFIGURATION.md:200,docs/HTTP-API.md:55-58,docs/CLI.md:2732and the upgrade section (tokens are created again; on a gateway every worker'sgateway.tokenis replaced with a new join token, or the worker is refused with 401 and keeps redialling),src/auth/AGENTS.md,src/http/AGENTS.md:95,src/gateway/AGENTS.md:122,src/daemon/AGENTS.md.
flowchart LR
main["daemon/main.ts *"] --> state[state]
main --> auth["auth createAuth *"]
auth --> ts["TokenStore *"]
ts --> ports[ports/sqlite.ts]
Surface and extension points
createAuth(options) takes database: SqliteDatabase in place of filesystem for tokens. AdminSecretManager keeps its filesystem, since admin.token stays a file. src/auth/index.ts exports tokensOwner. Owner tokens, version 1: table tokens with id (key), hash (unique), role (CHECK one of TOKEN_ROLES), label (NULL when absent), created_at. list answers in creation order (ORDER BY rowid), as the array order does today.
Contract and event changes
None (searched src/contract for TokenRecord, tokens.json: the doc comment at src/contract/operations.ts:685-687 changes words only).
Other code on the same state
Searched tokens.json, TokenStore, verify( over src/: auth only writes it (F8). token.revoke closes the revoked token's uplinks (src/gateway/AGENTS.md "token.revoke closes its uplinks"), unchanged. Task 4 adds the gateway's own owner to the same file.
Failure modes
| Call | Outcome | What happens | Limit, counted from |
|---|---|---|---|
create, revoke |
ok | one transaction | — |
| SQLite throws | rollback; the call fails INTERNAL, as a failed write does today |
busy wait 5 s, from the statement | |
verify, list |
ok | one indexed read per call | — |
| SQLite throws | the request, hello or uplink join fails INTERNAL, as a failed file read does today |
busy wait 5 s, from the statement | |
a bad role |
cannot be stored: the CHECK refuses it at insert, so TokenStoreError and its parse checks go |
— | |
| start | tokens.json present |
refuse with the old-file message | — |
Two calls at once: each runs as one synchronous transaction, so neither can see the other half done.
Rules in play
- ADR 0036 §1, tokens in
state.dbon both kinds; §5, refuse on an old file, no import. - ADR 0003, "no CLI code reads
tokens.jsonitself" (F8): the daemon stays the only owner. src/auth/AGENTS.md, auth "Uses: ports, contract only": it takes aSqliteDatabase, never the state module.- Architecture rule 8, the daemon owns all state.
Tests
Seams: TokenStore over openMemoryStateDatabase([tokensOwner]) (model: src/auth/token-store.test.ts), and startDaemon (model: src/daemon/main.test.ts:1342).
- Every existing test in
src/auth/token-store.test.tskeeps its claim on the table, including the creation order of:87(create three, list them in that order), except the file-only cases at:45,:206,:213,:223and the mode test at:70, which go: task 1 proves the file's mode and its damage rule. - A token created, then the daemon restarted on the same
state.db(temp file), verifies and lists with every field, including nolabelwhen none was given. startDaemonas a worker and as a gateway withtokens.jsonin its folder throws the old-file message naming it, each with its role's step word for word.- A gateway restarted on the same
state.dbstill accepts a worker join token created before the stop: extendsrc/daemon/gateway-fleet.e2e.test.ts:410, whose workers reconnect with tokens made before the restart, to run onstate.db. GET /v1/tokens never answers a secret or a hashreads the stored hashes fromstate.db(extendse2e/token-route.test.ts:95).
Done when
pnpm test:changedpasses.pnpm test:e2e e2e/token-route.test.ts e2e/http-api.test.tspasses.rg -n "tokens\.json" src docs --glob '!docs/internal/adr/**'finds only the old-file lists, their tests and thedocs/CLI.mdupgrade section.
Out of scope
admin.token(stays a file).- Importing
tokens.json.
Depends on
- task 1 (state.db opens and checks at start, and the worker's registry lives in it #761): it adds the state module, the owner and old-file lists and the per-role upgrade step this task adds to.
Written by an agent.
Activity
[-]iOS and Android device data in their own state.db tables[/-][+]Tokens live in state.db on both daemon kinds[/+]test: a daemon refuses a tokens.json and keeps its tokens across a re…
docs: tokens live in state.db in the manuals, comments and e2e (#758)
docs: drop a comment about the token file's lost update (#758)
test: the token store lists in creation order not id order, drops no …
test: the restart test verifies both secrets and the daemon manual sa…
feat(auth): tokens live in state.db on both daemon kinds (#784)