Files
Pulse/scripts/tests/test_api_reference_routes.py
pulse-triage[bot] a662f7d360 Explain read-only checks of saved API token expiry
Distinguish saved expiry, an expired credential and an unavailable record without changing a token or widening access. Keep Created and Last used separate from expiry and preserve existing token-route guidance in place. Link the new section from the existing expiry wording.

Contract-Neutral: Document existing metadata reads and expiry semantics only. No routes, scopes, storage, authentication or runtime behaviour change.
Change-source: pulse-maintainer
2026-11-07 10:30:15 +00:00

296 lines
16 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""Keep docs/API.md aligned with the routes and scopes the server registers.
Static checks only: no server, credentials or network. Each check reads the
reference and a router or handler source file, so drift in either direction
fails here instead of surfacing as a support question.
"""
from __future__ import annotations
from pathlib import Path
import json
import re
import unittest
ROOT = Path(__file__).resolve().parents[2]
DOC = ROOT / "docs/API.md"
SCOPES = ROOT / "pkg/auth/scopes.go"
SCOPE_TOKEN = re.compile(r"`([a-z]+(?::[a-z]+)+)`")
ROUTE_LINE = re.compile(r"`(?:GET|POST|PUT|PATCH|DELETE)(?:/(?:GET|POST|PUT|PATCH|DELETE))* /api/")
def read(path: str | Path) -> str:
return (ROOT / path).read_text(encoding="utf-8") if isinstance(path, str) else path.read_text(encoding="utf-8")
def known_scopes(text: str) -> set[str]:
return set(re.findall(r'Scope\w+\s*=\s*"([^"]+)"', text))
def unknown_scopes_in_reference(doc: str, scopes: set[str]) -> list[str]:
"""Scope-looking tokens on route lines that no code constant defines.
Only tokens whose first segment is a real scope family are judged, so a
request value such as `provider:model` is not mistaken for a scope.
"""
families = {scope.split(":")[0] for scope in scopes}
unknown: list[str] = []
for line in doc.splitlines():
if not ROUTE_LINE.search(line):
continue
for token in SCOPE_TOKEN.findall(line):
if token.split(":")[0] in families and token not in scopes:
unknown.append(f"{token} in: {line.strip()[:100]}")
return unknown
def missing_anchors(pairs: list[tuple[str, str, str]], doc: str, sources: dict[str, str]) -> list[str]:
"""Each pair is (text the reference must contain, source file, text the code must contain)."""
failures: list[str] = []
for doc_text, source, code_text in pairs:
if doc_text not in doc:
failures.append(f"reference lacks: {doc_text}")
if code_text not in sources[source]:
failures.append(f"{source} lacks: {code_text}")
return failures
# (reference text, source file, code text). Routes this guide documents on the
# strength of the registration or handler named here.
ROUTE_ANCHORS = [
("`GET /api/ai/patrol/runs/{id}` (`ai:execute`)",
"internal/api/router_routes_ai_relay.go",
'r.mux.HandleFunc("/api/ai/patrol/runs/", RequireAuth(r.config, RequireScope(config.ScopeAIExecute,'),
("`POST /api/availability-history` (`monitoring:read`)",
"internal/api/router_routes_monitoring.go",
'r.mux.HandleFunc("/api/availability-history", RequireAuth(r.config, RequireScope(config.ScopeMonitoringRead,'),
("`POST /api/agents/agent/{agent_id}/allow-reenroll` (admin, `settings:write`)",
"internal/api/router_routes_registration.go",
'strings.HasSuffix(req.URL.Path, "/allow-reenroll")'),
("`PUT /api/truenas/connections/{id}` (admin, `settings:write`)",
"internal/api/router_routes_registration.go",
"update: r.trueNASHandlers.HandleUpdate,"),
("`POST /api/truenas/connections/{id}/test` (admin, `settings:write`)",
"internal/api/router_routes_registration.go",
"test: r.trueNASHandlers.HandleTestSavedConnection,"),
# Every profile route is wrapped once, for all methods, including GET.
("Token callers also need `settings:write` on every profile route, including GET requests",
"internal/api/router_routes_registration.go",
'r.mux.Handle("/api/admin/profiles/", RequireOrgAdmin(r.config, RequireScope(config.ScopeSettingsWrite,'),
# Everything other than GET or HEAD on the /api/resources/ prefix needs the write scope.
("`POST /api/resources/{id}/link` (`monitoring:write`)",
"internal/api/router_routes_monitoring.go",
"if req.Method != http.MethodGet && req.Method != http.MethodHead {\n\t\t\tscope = config.ScopeMonitoringWrite"),
("`POST /api/resources/{id}/unlink` (`monitoring:write`)",
"internal/api/router_routes_monitoring.go",
"if req.Method != http.MethodGet && req.Method != http.MethodHead {\n\t\t\tscope = config.ScopeMonitoringWrite"),
("`POST /api/resources/{id}/report-merge` (`monitoring:write`)",
"internal/api/router_routes_monitoring.go",
"if req.Method != http.MethodGet && req.Method != http.MethodHead {\n\t\t\tscope = config.ScopeMonitoringWrite"),
# Regeneration replaces the whole inventory, so a token caller must be instance-wide.
("An API token may call this only if it has full-access\nscope (`*`) and is not bound to a non-default organization",
"internal/api/security_setup_fix.go",
"!tokenHasInstanceWideAuthority(callerToken)"),
("`POST /api/agents/agent/uninstall` (`agent:report` token, or an admin session)",
"internal/api/router_routes_registration.go",
"RequireOrgAdmin(r.config, RequireScope(config.ScopeAgentReport, r.unifiedAgentHandlers.HandleUninstall))"),
("API-token callers also need the `ai:execute` scope for AI narration.",
"internal/api/metrics_reporting_handlers.go",
"if !reportCallerMayUseAI(ctx) {\n\t\treturn nil, nil, h.resolveExistingFindings(ctx)"),
("AI narration of the fleet summary follows the same rule as single reports",
"internal/api/metrics_reporting_handlers.go",
"_, fleetNarrator, findings := h.resolveNarrator(ctx)"),
("API-token callers manage only the tokens in their own organizations",
"internal/api/security_tokens.go",
"if !callerTokenMayManage(callerToken, &record) {"),
("`POST /api/security/tokens/<id>/rotate`",
"internal/api/router_routes_auth_security.go",
"r.handleRotateAPIToken(w, req)"),
("from an API token needs an instance-wide full-access token",
"internal/api/security_setup_fix.go",
"API token without instance-wide authority attempted quick security setup"),
("`PUT /api/notifications/email` (admin, `settings:write`)",
"internal/api/alerting/notifications.go",
'case path == "/email" && r.Method == http.MethodPut:\n\t\tif !requireAnyScope(config.ScopeSettingsWrite'),
]
class APIReferenceRoutesTest(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.doc = read(DOC)
cls.scopes = known_scopes(read(SCOPES))
cls.sources = {source: read(source) for _, source, _ in ROUTE_ANCHORS}
def test_scope_catalogue_was_parsed(self) -> None:
for scope in ("monitoring:read", "settings:write", "ai:execute", "audit:read"):
self.assertIn(scope, self.scopes)
def test_every_scope_named_on_a_route_line_exists_in_code(self) -> None:
self.assertEqual(unknown_scopes_in_reference(self.doc, self.scopes), [])
def test_documented_routes_match_their_registration(self) -> None:
self.assertEqual(missing_anchors(ROUTE_ANCHORS, self.doc, self.sources), [])
def test_retired_remediation_routes_say_so(self) -> None:
for route in ("approve", "execute", "rollback"):
line = next(l for l in self.doc.splitlines() if l.startswith(f"- `POST /api/ai/remediation/{route}`"))
self.assertIn("410 Gone", line, route)
self.assertIn("command_remediation_retired", line, route)
def test_patrol_routes_documented_without_a_paid_label_have_no_licence_wrapper(self) -> None:
# Patrol and managing its findings are free; only governed fixes are Pro.
router = read("internal/api/router_routes_ai_relay.go")
for name in ("stream", "run", "acknowledge", "snooze", "suppress", "suppressions", "dismissed"):
route = f"/api/ai/patrol/{name}"
bullets = [line for line in self.doc.splitlines()
if re.match(r"- `(?:GET|POST|DELETE) %s(?:/\{id\})?`" % re.escape(route), line)]
self.assertTrue(bullets, route)
for bullet in bullets:
self.assertNotIn("Pro", bullet)
start = router.index(f'HandleFunc("{route}"')
following = router.find("r.mux.Handle", start + 10)
registration = router[start:following if following != -1 else start + 900]
self.assertNotIn("RequireLicenseFeature", registration, route)
def test_reapprove_is_documented_as_retired(self) -> None:
entry = self.doc.split("`POST /api/ai/findings/{id}/reapprove`", 1)[1].split("\n\n", 1)[0]
for text in ("retired", "410 Gone", "command_fix_retired"):
self.assertIn(text, entry)
def test_validate_token_is_not_filed_under_the_licensed_audit_log(self) -> None:
audit = self.doc.split("## 🧾 Audit Log (Pro)", 1)[1].split("\n## ", 1)[0]
self.assertNotIn("/api/security/validate-token", audit)
self.assertIn("/api/security/validate-token", self.doc)
def test_token_create_explains_scope_defaults_without_widening_the_example(self) -> None:
create = self.doc.split("### Create API Token\n", 1)[1].split("### Edit API Token\n", 1)[0]
prose = " ".join(create.split())
self.assertIn("Missing/`null` `scopes` requests full access (`*`)", prose)
self.assertIn("`[]` returns `400`", prose)
example = re.search(r"```json\n(.*?)\n```", create, re.S)
self.assertIsNotNone(example)
self.assertEqual(json.loads(example.group(1))["scopes"], ["monitoring:read"])
# These are Create defaults, not PATCH defaults or an authority bypass.
handler = read("internal/api/security_tokens.go")
self.assertIn("if raw == nil {\n\t\treturn []string{config.ScopeWildcard}, nil", handler)
self.assertIn("if len(requested) == 0 {\n\t\treturn nil, fmt.Errorf", handler)
self.assertEqual(read("frontend-modern/public/docs/API.md"), self.doc)
def test_token_lifetime_explains_existing_opt_in_and_non_expiring_default(self) -> None:
lifetime = self.doc.split("### Token lifetime\n", 1)[1].split("### List API Tokens\n", 1)[0]
prose = " ".join(re.sub(r"\[([^]]+)\]\([^)]+\)", r"\1", lifetime).split())
for rule in (
'"expiresIn": "24h"',
"minimum of `1m`",
"Use hours for days (`168h`), not `7d`",
"Invalid or shorter durations return `400`",
"Omitting `expiresIn`, sending `null` or an empty string",
"**no expiry**",
"Editing a token does not change its expiry",
"rotation keeps the original expiry rather than starting a new lifetime",
):
self.assertIn(rule, prose)
create = self.doc.split("### Create API Token\n", 1)[1].split("### Edit API Token\n", 1)[0]
self.assertIn("[Optional expiry](#token-lifetime)", create)
# Keep this option tied to creation, not a promise to extend a token.
handler = read("internal/api/security_tokens.go")
for code in (
'ExpiresIn *string `json:"expiresIn,omitempty"`',
'if expiresIn != nil && *expiresIn != ""',
'dur, err := time.ParseDuration(*expiresIn)',
'if dur < time.Minute',
'record.ExpiresAt = &exp',
'newRecord.ExpiresAt = &t',
):
self.assertIn(code, handler)
update = handler.split("type updateTokenRequest struct {", 1)[1].split("}", 1)[0]
self.assertNotIn("Expires", update)
def test_saved_token_expiry_check_is_private_read_only_and_source_bound(self) -> None:
section = self.doc.split("## Checking a saved token’s expiry\n", 1)[1]
prose = " ".join(section.split())
self.assertEqual(re.findall(r"```bash\n(.*?)\n```", section, re.S),
["pulse_api GET /api/security/tokens"])
for rule in (
"working, independent administrator session",
"separate, already authorised administrator credential",
"exact record ID, not its name",
"`expiresAt`",
"RFC 3339 timestamp",
"Pulse server’s clock",
"no longer authenticates",
"absent from that record, the token has no configured expiry",
"failed, partial or hidden record leaves expiry unknown",
"Do not widen access",
"**Created** (`createdAt`) and **Last used** (`lastUsedAt`) are not expiry dates",
"A future expiry does not prove",
"no edit, rotation or test request",
"response file and token metadata out of reports",
"`action-runner:<host>` tokens",
"Pulse clears on activation",
):
self.assertIn(rule, prose)
# The list has no expiry filter. It returns metadata snapshots only,
# so absence and a past timestamp are distinct from a failed read.
handler = read("internal/api/security_tokens.go")
self.assertIn('ExpiresAt *time.Time `json:"expiresAt,omitempty"`', handler)
self.assertIn("dto.ExpiresAt = &expires", handler)
listing = handler.split("func (r *Router) handleListAPITokens(", 1)[1].split(
"func (r *Router) handleGetAPIToken(", 1)[0]
self.assertIn('"tokens": tokens', listing)
self.assertIn("callerTokenMayManage", listing)
self.assertIn("toAPITokenDTO(record)", listing)
self.assertNotIn("IsExpired", listing)
token_config = read("internal/config/api_tokens.go")
self.assertIn("return time.Now().UTC().After(*r.ExpiresAt)", token_config)
self.assertIn("if c.APITokens[idx].IsExpired() {\n\t\t\t\treturn nil, false", token_config)
install = read("internal/api/agenttokens/install.go")
self.assertIn('tokenName = "action-runner:" + hostname', install)
self.assertIn("record.ExpiresAt = nil", install)
# Link without inserting new blocks ahead of the existing edit/rotate
# reference, which must remain in its established reading position.
lifetime = self.doc.split("### Token lifetime\n", 1)[1].split("### List API Tokens\n", 1)[0]
self.assertIn("[expiry](#checking-a-saved-tokens-expiry)", lifetime)
self.assertTrue(self.doc.index("## Checking a saved token’s expiry") >
self.doc.index("## When a token change times out"))
self.assertEqual(read("frontend-modern/public/docs/API.md"), self.doc)
def test_token_edit_keeps_scope_changes_distinct_from_rename_and_rotation(self) -> None:
edit = self.doc.split("### Edit API Token\n", 1)[1].split("### Rotate API Token\n", 1)[0]
prose = " ".join(edit.split())
for rule in (
"Sending neither returns `400`",
"A rename keeps scopes when `scopes` is omitted or `null`",
"mixing `*` with other scopes return `400`",
"API-token callers changing scopes must hold every existing and requested scope",
"Changes apply on the token's next request",
"ID, secret, expiry and organization bindings stay unchanged",
):
self.assertIn(rule, prose)
rotate = self.doc.split("### Rotate API Token\n", 1)[1].split("### Revoke Token\n", 1)[0]
self.assertIn("the old token stops working", rotate)
self.assertIn("metadata and expiry", rotate)
def test_negative_controls_fail_when_reference_or_code_drift(self) -> None:
# An invented scope on a route line is reported.
drifted = self.doc.replace("`POST /api/availability-history` (`monitoring:read`)",
"`POST /api/availability-history` (`monitoring:peek`)")
self.assertNotEqual(drifted, self.doc)
self.assertTrue(unknown_scopes_in_reference(drifted, self.scopes))
# A route dropped from the reference is reported.
without_route = self.doc.replace("`GET /api/ai/patrol/runs/{id}` (`ai:execute`)", "")
self.assertTrue(missing_anchors(ROUTE_ANCHORS, without_route, self.sources))
# A route whose wrapper loses its scope in code is reported.
weakened = dict(self.sources)
key = "internal/api/router_routes_monitoring.go"
weakened[key] = weakened[key].replace("RequireScope(config.ScopeMonitoringRead,", "RequireScope(config.ScopeSettingsRead,")
self.assertTrue(missing_anchors(ROUTE_ANCHORS, self.doc, weakened))
if __name__ == "__main__":
unittest.main()