Repository navigation
API for scripts and agents: scoped tokens, plan/patch + base_rev, mr get/set, mr schema (#17, #38) - #67
Merged
Conversation
…, mr get/set, mr schema
- Tokens (api.tokens; secrets.yaml holds sha256 only): `Authorization: Bearer mrt_…` only, scopes
read < operate < apply over an explicit action table, optional allow / from / expires; never the
password, logs, backup / restore, firmware, reboot, SSH keys or token management; a token's change
may not touch api, services.ssh, system.sysctl, the guard, new *_file paths, or add / change an item
that references a secret. Bad tokens count in the login throttle. `mr token list|create|revoke`;
web UI card.
- GET config returns rev; POST plan (dry run: errors, changes, files, services); validate / plan /
apply accept a patch ([{op, path, value}]); a stale base_rev gets 409 (the web UI sends it too).
- Config paths (firewall.forwards[nas].enabled, [key=value], [N]): mr get / set / add / del / export,
edits keep router.yaml's layout, never print secrets.
- mr schema [PATH] / GET schema: JSON Schema from the Go types.
- Tests; CI runs the real `mr api` CGI with tokens (scopes, sources, expiry, no secret in any answer,
plan / 403 / 409 / 400, 20 parallel bad tokens -> 5 x 401 + 15 x 429). docs/api.md.
Closes #17
Closes #38
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.
What
Scoped API tokens (
mr/api_token.go, moduleapi).api.tokensin router.yaml; secrets.yaml holds onlyapi_token_<name>: sha256:<hex>. The token (mrt_+ 32 random bytes) is shown once.Authorization: Bearer mrt_…authenticates (constant-time hash compare) — no cookie, noX-MR, so no CSRF.allow(mon.*),from(CIDRs),expires.api,services.ssh,system.sysctl, theguard, point a*_filekey at a new file, or add / change any item that references a secret (proxy node, PPPoE WAN, SSID, DDNS record — otherwise it could point the router at its own host and have the secret sent there; removing is allowed). A token cannot set token hashes, settles only its own pending change, needsbase_revfor a whole config. Applies recorded asvia=api:<name>.mr token list|create|revoke; web UI card under 管理与 SSH (the browser generates the token and sends only its hash).plan / patch / base_rev (
mr/api_plan.go).GET configreturnsrev(hash of router.yaml);POST planis a dry run (errors, config-level changes, files, services);validate/plan/applyaccept apatch([{op: set|add|del, path, value}]); a stalebase_revgets 409 — the web UI now sends it too, so a browser and an agent cannot silently undo each other.Config paths and
mr get / set / add / del / export(mr/cfgpath.go).firewall.forwards[nas].enabled,[key=value],[N], quoted keys. Edits go through the layout-keeping editor (#11); the result must decode and validate before it is written;delreturns a value to its default;export --flat | --json. Never prints secrets.mr schema [PATH]/GET schema(mr/schema.go): JSON Schema from the Go types by reflection, with enums from the existing value sets — for editors and for #37's MCP tool schemas. This completes #38 (guard and risk levels came with #61).Docs: new
docs/api.md(tokens, scopes, endpoints, path syntax, curl examples), MODULES.md, sys.md, the skill (agents should usemr get/set+ plan rather than editing router.yaml by hand).Verification
mr set/del; patch + stalebase_rev; schema checked against the home and lab configs.mr apiCGI with bearer tokens: scopes, sources, expiry, no secret value in any answer, plan on good / bad patches with 403 / 409 / 400, no apply job started, 20 parallel bad tokens → 5 × 401 + 15 × 429.Authorization: Bearerto CGI asHTTP_AUTHORIZATION.tools/ci.shpasses (private and public trees).Closes #17 · Closes #38
https://claude.ai/code/session_01HWeUjo1dmS7gBkCYHrVjCr