Skip to content

Add token commands, audit log and token details in whoami - #29

Merged
lnsp merged 2 commits into
masterfrom
token-commands
Nov 21, 2026
Merged

lnsp merged 2 commits into
masterfrom
token-commands

Conversation

@lnsp

@lnsp lnsp commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Adds the CLI side of RFC-0005 release 3: users create, list and revoke their tokens, project managers list and revoke the tokens bound to their project and read its audit log, and whoami shows what the token in use grants.

Needs a platform with valar/platform#387 deployed. Against an older platform the new commands fail with "the server has no token API; it needs a newer Valar platform" (or "no audit log API"), exit code 1. Existing commands are unchanged; whoami prints only what the server reports.

Commands

Command Route
valar token create <name> [--project p] [--service s] [--scopes read,deploy] [--ttl 90d] POST /v2/tokens
valar token list [--project p] GET /v2/tokens, or GET /v2/projects/{p}/tokens (adds an OWNER column)
valar token revoke <id> [--project p] DELETE /v2/tokens/{id}, or DELETE /v2/projects/{p}/tokens/{id}
valar audit [--project p] [--limit n] [--before id] GET /v2/projects/{p}/audit
valar whoami GET /v2/users/info, now with kind, binding, scopes, created, expires, last used and parent
  • token create prints the value alone on stdout and the grant plus "Store the token now; Valar shows it only once" on stderr, so TOKEN=$(valar token create ci) captures only the token. With --json, stdout is the whole reply including value. When the expiry comes out earlier than --ttl asked (clamped to the parent's), stderr says so.
  • --ttl takes days (90d) or a Go duration (12h, 1h30m) and is sent as hours (2160h). --service without --project is refused locally.
  • token revoke takes only a 32-hex-character ID and refuses anything else locally, because the router answers a malformed ID with the same plain-text 404 as a missing route. It prints how many tokens it revoked (children included); --json prints {"id","revoked"}.
  • audit is a top-level command, not under project or token: the log covers every kind of change (deploys, env, domains, tokens), and it reads the active project (VALAR_PROJECT or .valar.yml) like domain list, with --project to name another. --limit must be 1 to 500 (default 100). When a page is full, the command for the next one is printed to stderr (human mode only).
  • Refusals print the server's error as usual, e.g. Forbidden: only tokens minted in the console or with authctl can mint tokens, exit code 4.
  • The 404/405-in-plain-text check that envError and settingsError each had is now one unknownRoute helper; their behaviour is unchanged. The formatter also normalized a few var (...) blocks in api/client.go.

Config file and stored tokens

The two remaining items of the RFC's CLI list for this release:

  • ~/.valar/config, and any CLI configuration file the CLI rewrites (VALARCONFIG paths included), is written with mode 0600, and ~/.valar is created with 0700. A file other users could read is tightened to 0600 before the new content is written, with one line on stderr: Note: <path> was open to other users; it is now readable only by you (mode 0600). .valar.yml keeps its mode: it is a project file meant for the repository and holds no token.
  • config init, and config endpoint set --token when the endpoint has a URL, ask /users/info about the token before storing it. They refuse agent tokens (this is an agent token, which is for MCP clients, not the CLI; …, exit code 4, nothing written), warn through the existing expiry notice when the token expires within 14 days (once per run, also when the server's Valar-Token-Expires header fires it), and accept the token from a platform that reports no grant. config init used to check only that the endpoint answered; an unknown token is now refused at setup. Its "Verifying credentials... done." became one line, so a warning doesn't land mid-line.

README sections for Tokens and Audit log are added, and the agent skill mentions whoami --json and the CI token recipe (left to the user to run). The repository has no changelog file and no checked-in completions (cobra's valar completion picks the new commands up).

Tests

go build ./..., go vet ./... and go test ./... pass (also with -race). New tests use httptest servers:

  • api: request paths, bodies and decoding for create, list (own and project), revoke and audit (before/limit query); plain-text 404/405 map to ErrNoTokenAPI/ErrNoAuditAPI while JSON 404/403/503 stay API errors; /users/info with and without a token.
  • cmd: --ttl parsing; flag checks; create output (value alone on stdout, grant on stderr, clamped-expiry note, JSON mode); refusals with their exit kinds; token tables (parent names, live/revoked/expired, owner column); revoke messages and argument checks with no request sent; audit table, next-page hint and argument checks; whoami for new, earlier and pre-token servers; end-to-end flag wiring through the root command; verifyToken for user, soon-expiring, non-expiring, agent, pre-token and unknown tokens; config init and config endpoint set end to end with a temporary HOME (written 0600, agent tokens refused and not written, no request without a URL).
  • config: writing with a temporary HOME creates ~/.valar 0700 and the file 0600; a 0644 default file and a 0640 VALARCONFIG file are tightened with a one-line note and keep their content; a 0600 file gets no note.

Verification on a test cluster (not yet run)

On valar-test-0 or valar-test-1 with valar/platform#387 deployed. ROOT is a token minted with authctl (echo 'token create lennart cli-root ttl=1d' | authctl, as in #387's steps); build the CLI from this branch with make build.

export VALAR_ENDPOINT=http://$ADDR:8080/v2 VALAR_TOKEN=$ROOT VALAR_PROJECT=lennart
  1. bin/valar whoami shows Kind: user, Binding: any project, Scopes: all, Created, Expires about a day ahead, and no Parent row.
  2. CI=$(bin/valar token create cli-ci --project lennart --scopes read,deploy --ttl 12h) prints the grant on stderr (Binding: project lennart, Scopes: deploy, read, Parent: <ROOT's id>) and leaves only a valar_ut_ value in $CI. Running it again fails with Conflict: a live token with that name exists, exit code 5.
  3. bin/valar token create cli-long --ttl 30d prints an expiry equal to ROOT's and the line "It expires with the token that created it, before the --ttl asked for."
  4. VALAR_TOKEN=$CI bin/valar whoami shows Binding: project lennart, Scopes: deploy, read and Parent: <ROOT's id>. VALAR_TOKEN=$CI bin/valar token create x fails with Forbidden: token lacks scope all, exit code 4.
  5. bin/valar token list lists cli-root, cli-ci and cli-long, the latter two with cli-root as PARENT, all live. bin/valar token list --project lennart lists cli-ci with OWNER lennart.
  6. bin/valar audit --limit 5 lists the tokens.create attempts and results of step 2, newest first, with ROOT's ID prefix in TOKEN and valar-cli/… in CLIENT, and prints Older rows: valar audit --project lennart --before <id> --limit 5 on stderr; that command continues. bin/valar audit --limit 0 fails locally with exit code 2.
  7. H=$(mktemp -d); env -u VALAR_TOKEN -u VALAR_ENDPOINT -u VALAR_PROJECT HOME=$H bin/valar config init --url $VALAR_ENDPOINT --token $CI --project lennart prints "Warning: your Valar token expires in 12 hours …" (CI's ttl is 12h) and writes the config; stat -c %a $H/.valar $H/.valar/config prints 700 and 600. After chmod 644 $H/.valar/config, env -u VALAR_TOKEN -u VALAR_ENDPOINT -u VALAR_PROJECT HOME=$H bin/valar config context use default prints the Note: … mode 0600 line once and stat shows 600 again. (Agent tokens arrive with release 7, so their refusal is covered by unit tests only.)
  8. bin/valar token revoke --project lennart <cli-ci's id> prints Revoked token <id>.; VALAR_TOKEN=$CI bin/valar whoami then fails with the revocation reason. bin/valar token revoke <ROOT's id> prints Revoked token <id> and 1 token it created. (cli-long).
  9. Against a cluster still on an older platform, bin/valar token list prints "the server has no token API; it needs a newer Valar platform" and exits with 1.

lnsp added 2 commits November 21, 2026 14:04
valar token create|list|revoke drive the platform's /v2/tokens and
/v2/projects/{p}/tokens routes, and valar audit pages a project's audit
log. whoami shows the token's kind, binding, scopes, creation, expiry,
last use and parent where the server reports them.

token create prints the value alone on stdout and the grant on stderr,
so TOKEN=$(valar token create ci) captures only the token. --ttl takes
days (90d) or a Go duration and is sent in hours. Against a server
without these routes, the commands say it needs a newer Valar platform;
the check is shared with the environment and settings APIs.
The CLI writes ~/.valar/config, and any configuration file it rewrites,
with mode 0600 and creates ~/.valar with 0700. A file other users could
read is tightened to 0600 before the tokens are written, with a one-line
note on stderr.

config init and config endpoint set (when the endpoint has a URL) ask
/users/info about the token before storing it: they refuse agent tokens,
warn through the usual expiry notice when it expires within 14 days, and
accept it from a platform that reports no grant.
@lnsp
lnsp merged commit c694a7e into master Nov 21, 2026
3 checks passed
@lnsp
lnsp deleted the token-commands branch November 21, 2026 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant