Repository navigation
Add token commands, audit log and token details in whoami - #29
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
whoamishows 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;
whoamiprints only what the server reports.Commands
valar token create <name> [--project p] [--service s] [--scopes read,deploy] [--ttl 90d]POST /v2/tokensvalar token list [--project p]GET /v2/tokens, orGET /v2/projects/{p}/tokens(adds an OWNER column)valar token revoke <id> [--project p]DELETE /v2/tokens/{id}, orDELETE /v2/projects/{p}/tokens/{id}valar audit [--project p] [--limit n] [--before id]GET /v2/projects/{p}/auditvalar whoamiGET /v2/users/info, now with kind, binding, scopes, created, expires, last used and parenttoken createprints the value alone on stdout and the grant plus "Store the token now; Valar shows it only once" on stderr, soTOKEN=$(valar token create ci)captures only the token. With--json, stdout is the whole reply includingvalue. When the expiry comes out earlier than--ttlasked (clamped to the parent's), stderr says so.--ttltakes days (90d) or a Go duration (12h,1h30m) and is sent as hours (2160h).--servicewithout--projectis refused locally.token revoketakes 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);--jsonprints{"id","revoked"}.auditis a top-level command, not underprojectortoken: the log covers every kind of change (deploys, env, domains, tokens), and it reads the active project (VALAR_PROJECT or.valar.yml) likedomain list, with--projectto name another.--limitmust be 1 to 500 (default 100). When a page is full, the command for the next one is printed to stderr (human mode only).erroras usual, e.g.Forbidden: only tokens minted in the console or with authctl can mint tokens, exit code 4.envErrorandsettingsErroreach had is now oneunknownRoutehelper; their behaviour is unchanged. The formatter also normalized a fewvar (...)blocks inapi/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 (VALARCONFIGpaths included), is written with mode 0600, and~/.valaris 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.ymlkeeps its mode: it is a project file meant for the repository and holds no token.config init, andconfig endpoint set --tokenwhen the endpoint has a URL, ask/users/infoabout 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'sValar-Token-Expiresheader fires it), and accept the token from a platform that reports no grant.config initused 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 --jsonand the CI token recipe (left to the user to run). The repository has no changelog file and no checked-in completions (cobra'svalar completionpicks the new commands up).Tests
go build ./...,go vet ./...andgo 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/limitquery); plain-text 404/405 map toErrNoTokenAPI/ErrNoAuditAPIwhile JSON 404/403/503 stay API errors;/users/infowith and without a token.cmd:--ttlparsing; 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;whoamifor new, earlier and pre-token servers; end-to-end flag wiring through the root command;verifyTokenfor user, soon-expiring, non-expiring, agent, pre-token and unknown tokens;config initandconfig endpoint setend 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~/.valar0700 and the file 0600; a 0644 default file and a 0640VALARCONFIGfile 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 withmake build.bin/valar whoamishowsKind: user,Binding: any project,Scopes: all,Created,Expiresabout a day ahead, and noParentrow.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 avalar_ut_value in$CI. Running it again fails withConflict: a live token with that name exists, exit code 5.bin/valar token create cli-long --ttl 30dprints an expiry equal to ROOT's and the line "It expires with the token that created it, before the --ttl asked for."VALAR_TOKEN=$CI bin/valar whoamishowsBinding: project lennart,Scopes: deploy, readandParent: <ROOT's id>.VALAR_TOKEN=$CI bin/valar token create xfails withForbidden: token lacks scope all, exit code 4.bin/valar token listlistscli-root,cli-ciandcli-long, the latter two withcli-rootas PARENT, alllive.bin/valar token list --project lennartlistscli-ciwith OWNERlennart.bin/valar audit --limit 5lists thetokens.createattempts and results of step 2, newest first, with ROOT's ID prefix in TOKEN andvalar-cli/…in CLIENT, and printsOlder rows: valar audit --project lennart --before <id> --limit 5on stderr; that command continues.bin/valar audit --limit 0fails locally with exit code 2.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 lennartprints "Warning: your Valar token expires in 12 hours …" (CI's ttl is 12h) and writes the config;stat -c %a $H/.valar $H/.valar/configprints700and600. Afterchmod 644 $H/.valar/config,env -u VALAR_TOKEN -u VALAR_ENDPOINT -u VALAR_PROJECT HOME=$H bin/valar config context use defaultprints theNote: … mode 0600line once andstatshows600again. (Agent tokens arrive with release 7, so their refusal is covered by unit tests only.)bin/valar token revoke --project lennart <cli-ci's id>printsRevoked token <id>.;VALAR_TOKEN=$CI bin/valar whoamithen fails with the revocation reason.bin/valar token revoke <ROOT's id>printsRevoked token <id> and 1 token it created.(cli-long).bin/valar token listprints "the server has no token API; it needs a newer Valar platform" and exits with 1.