diff --git a/docs/API.md b/docs/API.md index f0639ca6f9..53245717aa 100644 --- a/docs/API.md +++ b/docs/API.md @@ -124,12 +124,11 @@ POST, PUT and PATCH read their JSON body from standard input; GET and DELETE sen body. A method being supported is not permission to change a resource. Choose each administrative change separately and check its current state afterwards. A timeout or incomplete response can occur **after a change was applied**; -do not repeat a POST, PUT or DELETE merely because the response was lost. +do not replay a POST, PUT, PATCH or DELETE after a lost response. Keep `--globoff`: shell quotes alone do not disable curl's URL globbing. Without it, a path containing a brace list such as `{review,ops}` or a range such as -`[1-3]` can send several requests, including several POST, PUT or DELETE -requests. The helper treats these characters literally, not as a batch of +`[1-3]` can send several requests, including repeated writes. The helper treats these characters literally, not as a batch of targets. Still quote the whole path in the shell, and URL-encode dynamic resource IDs and query values; `--globoff` does not encode them for you. @@ -1223,7 +1222,7 @@ request. ID, secret, expiry and organization bindings stay unchanged. `POST /api/security/tokens//rotate` Replaces one token with a new one and returns `{ "token": "raw-token", "record": { ... } }` -(the raw token is shown once). The new token keeps the old one's name, scopes, +(the raw token is [shown once](#when-a-token-change-times-out)). The new token keeps the old one's name, scopes, organization bindings, metadata and expiry, and the old token stops working. API-token callers must hold every scope on the target token. @@ -2160,3 +2159,50 @@ End-to-end encrypted relay protocol for mobile connectivity. --- > **Note**: This is a summary of the most common endpoints. For a complete list, inspect the network traffic of the Pulse dashboard or check the source code in `internal/api/router.go`. + + +## When a token change times out + +A lost response can follow a saved token change. **Do not repeat the change** +or assume the old credential still works. Keep a working, independent +administrator session. Do not use the token being edited or rotated as your +only way to check the result. + +Use the [private-header helper](#-authentication) with a separate, already +authorised administrator credential to read the saved inventory. This sends no +change and saves the response privately: + +```bash +pulse_api GET /api/security/tokens +``` + +The list contains **metadata only**, not raw secrets or complete organisation +and agent bindings. **Names are not unique.** Compare exact record IDs with the +original response or, where available, an existing rotation audit event's old +and new IDs. Do not identify a replacement from its name alone. + +A single-record `GET /api/security/tokens/` also returns metadata only. +**404 can mean absent or hidden from this caller**, not proof that rotation or +revocation succeeded. A 401, 403, failed read or incomplete response leaves the +result unknown. Do not widen a token's scopes or organisation access to read it. +Coordinate with other administrators before another write. A snapshot does not +prove an earlier request cannot still finish. + +- **Edit:** PATCH keeps the record ID and secret. Compare the saved name and + complete scope list with the intended change before considering another edit. + Do not rotate or recreate the token to resolve an unconfirmed edit. +- **Rotation:** the old token stops working when its replacement is saved. + A complete response already saved in a private file may contain the new + secret. Check its record ID against the saved inventory before using it. + A partial response is not a complete result. GET cannot recover the raw secret. + If the secret is lost, first identify the **exact successor** and settle the + original attempt. Then choose a separate, intentional rotation of that record + from the independent administrator session. Rotation preserves its existing + scopes, bindings, metadata and expiry. Save the one-time result privately and + update only its intended consumers. This is not a replay of the old request. + +If the successor or original outcome cannot be established, stop and +[get help](TROUBLESHOOTING.md#-getting-help) rather than guess, grant full access +or re-enrol agents. Keep tokens, private response files and inventory or audit +exports out of reports. Give only the original action, time and relevant +locally redacted error that you already have. diff --git a/frontend-modern/public/docs/API.md b/frontend-modern/public/docs/API.md index f0639ca6f9..53245717aa 100644 --- a/frontend-modern/public/docs/API.md +++ b/frontend-modern/public/docs/API.md @@ -124,12 +124,11 @@ POST, PUT and PATCH read their JSON body from standard input; GET and DELETE sen body. A method being supported is not permission to change a resource. Choose each administrative change separately and check its current state afterwards. A timeout or incomplete response can occur **after a change was applied**; -do not repeat a POST, PUT or DELETE merely because the response was lost. +do not replay a POST, PUT, PATCH or DELETE after a lost response. Keep `--globoff`: shell quotes alone do not disable curl's URL globbing. Without it, a path containing a brace list such as `{review,ops}` or a range such as -`[1-3]` can send several requests, including several POST, PUT or DELETE -requests. The helper treats these characters literally, not as a batch of +`[1-3]` can send several requests, including repeated writes. The helper treats these characters literally, not as a batch of targets. Still quote the whole path in the shell, and URL-encode dynamic resource IDs and query values; `--globoff` does not encode them for you. @@ -1223,7 +1222,7 @@ request. ID, secret, expiry and organization bindings stay unchanged. `POST /api/security/tokens//rotate` Replaces one token with a new one and returns `{ "token": "raw-token", "record": { ... } }` -(the raw token is shown once). The new token keeps the old one's name, scopes, +(the raw token is [shown once](#when-a-token-change-times-out)). The new token keeps the old one's name, scopes, organization bindings, metadata and expiry, and the old token stops working. API-token callers must hold every scope on the target token. @@ -2160,3 +2159,50 @@ End-to-end encrypted relay protocol for mobile connectivity. --- > **Note**: This is a summary of the most common endpoints. For a complete list, inspect the network traffic of the Pulse dashboard or check the source code in `internal/api/router.go`. + + +## When a token change times out + +A lost response can follow a saved token change. **Do not repeat the change** +or assume the old credential still works. Keep a working, independent +administrator session. Do not use the token being edited or rotated as your +only way to check the result. + +Use the [private-header helper](#-authentication) with a separate, already +authorised administrator credential to read the saved inventory. This sends no +change and saves the response privately: + +```bash +pulse_api GET /api/security/tokens +``` + +The list contains **metadata only**, not raw secrets or complete organisation +and agent bindings. **Names are not unique.** Compare exact record IDs with the +original response or, where available, an existing rotation audit event's old +and new IDs. Do not identify a replacement from its name alone. + +A single-record `GET /api/security/tokens/` also returns metadata only. +**404 can mean absent or hidden from this caller**, not proof that rotation or +revocation succeeded. A 401, 403, failed read or incomplete response leaves the +result unknown. Do not widen a token's scopes or organisation access to read it. +Coordinate with other administrators before another write. A snapshot does not +prove an earlier request cannot still finish. + +- **Edit:** PATCH keeps the record ID and secret. Compare the saved name and + complete scope list with the intended change before considering another edit. + Do not rotate or recreate the token to resolve an unconfirmed edit. +- **Rotation:** the old token stops working when its replacement is saved. + A complete response already saved in a private file may contain the new + secret. Check its record ID against the saved inventory before using it. + A partial response is not a complete result. GET cannot recover the raw secret. + If the secret is lost, first identify the **exact successor** and settle the + original attempt. Then choose a separate, intentional rotation of that record + from the independent administrator session. Rotation preserves its existing + scopes, bindings, metadata and expiry. Save the one-time result privately and + update only its intended consumers. This is not a replay of the old request. + +If the successor or original outcome cannot be established, stop and +[get help](TROUBLESHOOTING.md#-getting-help) rather than guess, grant full access +or re-enrol agents. Keep tokens, private response files and inventory or audit +exports out of reports. Give only the original action, time and relevant +locally redacted error that you already have. diff --git a/scripts/tests/test_api_auth_docs.py b/scripts/tests/test_api_auth_docs.py index fef01b412b..64ec304d06 100644 --- a/scripts/tests/test_api_auth_docs.py +++ b/scripts/tests/test_api_auth_docs.py @@ -13,6 +13,7 @@ import json import os from pathlib import Path import re +import shlex import shutil import stat import subprocess @@ -212,7 +213,14 @@ class APIAuthDocsTest(unittest.TestCase): self.assertNotRegex(bash, r"(?:--insecure|--verbose|--trace\S*|--location|\s-k\b)") blocks = re.findall(r"```bash\n(.*?)```", DOC.read_text(), re.DOTALL) requests = [block for block in blocks if block.startswith("pulse_api ")] - self.assertEqual(len(requests), 9) + expected_requests = [ + ("GET", "/api/state/summary"), + *((method, path) for method, path, _ in FLEET_ACTION_REQUESTS), + ("POST", "/api/config/nodes"), + ("GET", "/api/security/tokens"), + ] + self.assertEqual([tuple(shlex.split(block.splitlines()[0])[1:3]) for block in requests], + expected_requests, "every copied operation needs an explicit expected route") self.assertEqual([block for block in blocks if "curl " in block], [request_helper()], "an example must not bypass the private-response helper") self.assertIn("curl --disable --fail-with-body ", request_helper()) @@ -507,6 +515,52 @@ class APIAuthDocsTest(unittest.TestCase): self.assertEqual(requests, []) self.assertFalse((Path(temporary) / ".config").exists()) + def test_uncertain_token_change_keeps_identity_and_secret_limits_explicit(self): + source = DOC.read_text() + heading = "## When a token change times out\n" + self.assertEqual(source.count(heading), 1, "missing token-change recovery guidance") + section = " ".join(source.split(heading, 1)[1].split()) + for boundary in ( + "Do not repeat the change", "independent administrator session", + "metadata only", "Names are not unique", "exact record IDs", + "404 can mean absent or hidden from this caller", + "snapshot does not prove an earlier request cannot still finish", + "Do not rotate or recreate the token to resolve an unconfirmed edit", + "GET cannot recover the raw secret", "exact successor", + "settle the original attempt", "separate, intentional rotation", + "preserves its existing scopes, bindings, metadata and expiry", + "rather than guess, grant full access or re-enrol agents", + ): + with self.subTest(boundary=boundary): + self.assertIn(boundary, section) + self.assertIn("[shown once](#when-a-token-change-times-out)", source) + self.assertIn("do not replay a POST, PUT, PATCH or DELETE after a lost response", source) + self.assertEqual(DOC.read_bytes(), (ROOT / "frontend-modern/public/docs/API.md").read_bytes()) + + def test_uncertain_token_recovery_copies_only_one_private_metadata_read(self): + heading = "## When a token change times out\n" + source = DOC.read_text() + self.assertIn(heading, source, "missing read-only recovery recipe") + blocks = re.findall(r"```bash\n(.*?)```", source.split(heading, 1)[1], re.DOTALL) + self.assertEqual(blocks, ["pulse_api GET /api/security/tokens\n"], + "recovery must not replay a write or print token metadata") + for status, partial, expected_exit in ((200, False, 0), (401, False, 22), + (403, False, 22), (404, False, 22), + (500, False, 22), (302, False, 1), + (200, True, 18)): + for key, value in (("X-API-Token", TEST_TOKEN), ("Authorization", "Bearer " + TEST_TOKEN)): + with self.subTest(status=status, partial=partial, header=key), \ + private_recording_server(status, partial) as (port, requests), \ + tempfile.TemporaryDirectory() as temporary: + home = Path(temporary) + result = exercise_curl(self, home, f"{key}: {value}", port, + request_helper() + "\n" + blocks[0], private_response=True) + self.assertEqual(result.returncode, expected_exit, result.stderr.decode()) + self.assertEqual(len(requests), 1, "never replay a write, redirect or retry") + sent_path, headers, method, body = requests[0] + self.assertEqual((method, sent_path, body), ("GET", "/api/security/tokens", b"")) + self.assertEqual(headers[key], value) + def test_private_response_assertion_rejects_console_output_regression(self): request = commands()[1] self.assertEqual(request.count('--output "$result_file" '), 1)