@agent-custody/receipts 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -13
- package/dist/approval.d.ts +74 -0
- package/dist/approval.js +196 -0
- package/dist/capture.js +49 -1
- package/dist/check.d.ts +19 -0
- package/dist/check.js +288 -0
- package/dist/cli.js +238 -11
- package/dist/config.d.ts +85 -0
- package/dist/config.js +72 -2
- package/dist/console-page.d.ts +4 -0
- package/dist/console-page.js +342 -0
- package/dist/console.d.ts +20 -0
- package/dist/console.js +521 -0
- package/dist/gateway.d.ts +9 -0
- package/dist/gateway.js +335 -45
- package/dist/index.d.ts +9 -2
- package/dist/index.js +7 -1
- package/dist/issue.d.ts +8 -3
- package/dist/issue.js +13 -4
- package/dist/log-store.d.ts +3 -2
- package/dist/log-store.js +2 -0
- package/dist/operations.d.ts +101 -0
- package/dist/operations.js +239 -0
- package/dist/otel.js +12 -5
- package/dist/policy.d.ts +2 -0
- package/dist/policy.js +10 -0
- package/dist/portal.d.ts +78 -4
- package/dist/portal.js +516 -26
- package/dist/receipt.d.ts +36 -1
- package/dist/rest.js +14 -1
- package/dist/simulate.d.ts +58 -0
- package/dist/simulate.js +204 -0
- package/dist/splunk.js +11 -4
- package/dist/sso.d.ts +47 -0
- package/dist/sso.js +161 -0
- package/dist/verify.d.ts +2 -0
- package/dist/verify.js +241 -7
- package/docs/approvals.md +107 -0
- package/docs/codex-recovery.md +132 -0
- package/docs/configuration.md +224 -0
- package/docs/console.md +79 -0
- package/docs/financial-operations.md +99 -0
- package/docs/policies.md +23 -0
- package/docs/threat-model.md +13 -0
- package/docs/tutorials.md +9 -0
- package/docs/usage.md +49 -4
- package/docs/verification.md +26 -8
- package/package.json +8 -2
- package/vectors/audit.json +27 -27
- package/vectors/canonical.json +5 -5
- package/vectors/receipts.json +2866 -464
package/dist/check.js
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
// `agent-custody check`: what a gateway config would fail on at startup, found before it starts. Static checks read the
|
|
2
|
+
// config, the keys, the policy, the grant, the environment, and the paths; --connect also starts the upstreams, which
|
|
3
|
+
// is where tool-name collisions and unreachable servers show up, and compares every tool name the config mentions with
|
|
4
|
+
// the tools the upstreams actually offer.
|
|
5
|
+
import * as cedar from "@cedar-policy/cedar-wasm/nodejs";
|
|
6
|
+
import { accessSync, constants, existsSync, mkdtempSync, readFileSync, rmSync, statSync } from "node:fs";
|
|
7
|
+
import { tmpdir } from "node:os";
|
|
8
|
+
import { delimiter, dirname, isAbsolute, join, resolve } from "node:path";
|
|
9
|
+
import { GatewayConfigSchema, loadConfig } from "./config.js";
|
|
10
|
+
import { generateKeyPair, loadPrivateKey, loadPublicKey, writeKeyPair } from "./crypto.js";
|
|
11
|
+
import { delegationValidAt, verifyDelegation } from "./delegation.js";
|
|
12
|
+
import { hasPermitRule } from "./policy.js";
|
|
13
|
+
import { createGatewayHost } from "./gateway.js";
|
|
14
|
+
const distance = (a, b) => {
|
|
15
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
16
|
+
for (let i = 1; i <= a.length; i++) {
|
|
17
|
+
let prev = row[0];
|
|
18
|
+
row[0] = i;
|
|
19
|
+
for (let j = 1; j <= b.length; j++) {
|
|
20
|
+
const keep = row[j];
|
|
21
|
+
row[j] = Math.min(row[j] + 1, row[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
22
|
+
prev = keep;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return row[b.length];
|
|
26
|
+
};
|
|
27
|
+
/** the nearest existing directory above a path, to ask whether something can be created there */
|
|
28
|
+
function writableAncestor(path) {
|
|
29
|
+
let dir = resolve(path);
|
|
30
|
+
while (!existsSync(dir)) {
|
|
31
|
+
const up = dirname(dir);
|
|
32
|
+
if (up === dir)
|
|
33
|
+
return null;
|
|
34
|
+
dir = up;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
accessSync(dir, constants.W_OK);
|
|
38
|
+
return dir;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function onPath(command) {
|
|
45
|
+
if (isAbsolute(command) || command.includes("/"))
|
|
46
|
+
return existsSync(command);
|
|
47
|
+
return (process.env.PATH ?? "").split(delimiter).some((d) => d && existsSync(join(d, command)));
|
|
48
|
+
}
|
|
49
|
+
export async function checkConfig(configPath, options = {}) {
|
|
50
|
+
const out = [];
|
|
51
|
+
const add = (level, check, message) => out.push({ level, check, message });
|
|
52
|
+
let raw;
|
|
53
|
+
try {
|
|
54
|
+
raw = JSON.parse(readFileSync(configPath, "utf8"));
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
add("error", "config", `${configPath} is not readable JSON: ${e instanceof Error ? e.message : String(e)}`);
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
const known = Object.keys(GatewayConfigSchema.shape);
|
|
61
|
+
if (raw && typeof raw === "object" && !Array.isArray(raw)) {
|
|
62
|
+
for (const key of Object.keys(raw)) {
|
|
63
|
+
if (known.includes(key))
|
|
64
|
+
continue;
|
|
65
|
+
const near = known.find((k) => distance(k.toLowerCase(), key.toLowerCase()) <= 2);
|
|
66
|
+
add("error", "config", `unknown key "${key}" is ignored by the gateway${near ? `; did you mean "${near}"?` : ""} A mistyped key leaves the setting it was meant for at its default`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const parsed = GatewayConfigSchema.safeParse(raw);
|
|
70
|
+
if (!parsed.success) {
|
|
71
|
+
for (const issue of parsed.error.issues)
|
|
72
|
+
add("error", "config", `${issue.path.join(".") || "(root)"}: ${issue.message}`);
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
add("ok", "config", "the config parses");
|
|
76
|
+
const cfg = loadConfig(configPath);
|
|
77
|
+
// the gateway's key
|
|
78
|
+
if (!existsSync(cfg.identity.keyFile)) {
|
|
79
|
+
add("warn", "key", `${cfg.identity.keyFile} does not exist yet; \`agent-custody keygen\` creates it, and the container creates it on first start`);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
try {
|
|
83
|
+
loadPrivateKey(cfg.identity.keyFile);
|
|
84
|
+
add("ok", "key", `the gateway key at ${cfg.identity.keyFile} loads`);
|
|
85
|
+
if (process.platform !== "win32" && (statSync(cfg.identity.keyFile).mode & 0o077) !== 0)
|
|
86
|
+
add("warn", "key", `${cfg.identity.keyFile} is readable by other users; make it mode 0600`);
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
add("error", "key", `${cfg.identity.keyFile}: ${e instanceof Error ? e.message : String(e)}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// the principals whose grants are trusted
|
|
93
|
+
const principals = [];
|
|
94
|
+
for (const f of cfg.trustedPrincipalKeys) {
|
|
95
|
+
try {
|
|
96
|
+
principals.push(loadPublicKey(f));
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
add("error", "principal keys", `${f}: ${e instanceof Error ? e.message : String(e)}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (principals.length === cfg.trustedPrincipalKeys.length)
|
|
103
|
+
add("ok", "principal keys", `${principals.length} trusted principal key${principals.length === 1 ? "" : "s"} load`);
|
|
104
|
+
// the policy
|
|
105
|
+
if (!existsSync(cfg.policyFile)) {
|
|
106
|
+
add("error", "policy", `${cfg.policyFile} does not exist`);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
const text = readFileSync(cfg.policyFile, "utf8");
|
|
110
|
+
const r = cedar.checkParsePolicySet({ staticPolicies: text });
|
|
111
|
+
if (r.type !== "success")
|
|
112
|
+
for (const e of r.errors)
|
|
113
|
+
add("error", "policy", `${cfg.policyFile}: ${e.message}`);
|
|
114
|
+
else if (!hasPermitRule(text))
|
|
115
|
+
add("warn", "policy", `${cfg.policyFile} has no permit rule, so every call is denied`);
|
|
116
|
+
else
|
|
117
|
+
add("ok", "policy", "the policy parses");
|
|
118
|
+
}
|
|
119
|
+
if (cfg.mode === "observe")
|
|
120
|
+
add("warn", "mode", "observe mode forwards every call the grant allows and records what the policy would have decided; nothing is denied by policy");
|
|
121
|
+
// credentials: a variable the config names must be set where the gateway runs
|
|
122
|
+
const envNames = [];
|
|
123
|
+
for (const u of cfg.upstreams ?? (cfg.upstream ? [{ name: "upstream", ...cfg.upstream }] : [])) {
|
|
124
|
+
const name = "name" in u ? u.name : "upstream";
|
|
125
|
+
if ("tokenEnv" in u && u.tokenEnv)
|
|
126
|
+
envNames.push([`upstream ${name}`, u.tokenEnv]);
|
|
127
|
+
if ("rest" in u)
|
|
128
|
+
for (const v of Object.values(u.rest.headerEnv ?? {}))
|
|
129
|
+
envNames.push([`upstream ${name}`, v]);
|
|
130
|
+
if ("a2a" in u)
|
|
131
|
+
for (const v of Object.values(u.a2a.headerEnv ?? {}))
|
|
132
|
+
envNames.push([`upstream ${name}`, v]);
|
|
133
|
+
}
|
|
134
|
+
if (cfg.log?.tokenEnv)
|
|
135
|
+
envNames.push(["log", cfg.log.tokenEnv]);
|
|
136
|
+
if (cfg.splunk?.tokenEnv)
|
|
137
|
+
envNames.push(["splunk", cfg.splunk.tokenEnv]);
|
|
138
|
+
for (const v of Object.values(cfg.otel?.headersEnv ?? {}))
|
|
139
|
+
envNames.push(["otel", v]);
|
|
140
|
+
if (cfg.captureKeyEnv)
|
|
141
|
+
envNames.push(["capture", cfg.captureKeyEnv]);
|
|
142
|
+
const skipEnvironment = options.environment === "skip";
|
|
143
|
+
const missing = skipEnvironment ? [] : envNames.filter(([, v]) => !process.env[v]);
|
|
144
|
+
for (const [who, v] of missing)
|
|
145
|
+
add("error", "environment", `${who}: the variable ${v} is not set here, and a gateway does not start without it`);
|
|
146
|
+
if (envNames.length && skipEnvironment)
|
|
147
|
+
add("ok", "environment", `not checked here: this process does not hold the gateway's credentials (${[...new Set(envNames.map(([, v]) => v))].join(", ")}); run agent-custody check where the gateway runs`);
|
|
148
|
+
else if (envNames.length && !missing.length)
|
|
149
|
+
add("ok", "environment", `${envNames.length} credential variable${envNames.length === 1 ? " is" : "s are"} set`);
|
|
150
|
+
// stdio upstreams: the command must exist where the gateway runs
|
|
151
|
+
for (const u of cfg.upstreams ?? (cfg.upstream ? [{ name: "upstream", ...cfg.upstream }] : [])) {
|
|
152
|
+
if ("command" in u && !onPath(u.command))
|
|
153
|
+
add("error", "upstream", `${"name" in u ? u.name : "upstream"}: the command "${u.command}" is not on the PATH or at that path; in a container it must be inside the image`);
|
|
154
|
+
}
|
|
155
|
+
// a script or file a stdio upstream is started with has to be where the gateway runs, which in a container is inside the image
|
|
156
|
+
for (const u of cfg.upstreams ?? (cfg.upstream ? [{ name: "upstream", ...cfg.upstream }] : [])) {
|
|
157
|
+
if (!("command" in u))
|
|
158
|
+
continue;
|
|
159
|
+
for (const a of u.args)
|
|
160
|
+
if ((a.startsWith("/") || a.startsWith("./")) && !existsSync(a))
|
|
161
|
+
add("warn", "upstream", `${"name" in u ? u.name : "upstream"}: the argument ${a} does not exist here; if it is a file the upstream needs, it must be inside the image`);
|
|
162
|
+
}
|
|
163
|
+
// a REST tool with an idempotency header is a financial operation: every call to it fails unless `operations` configures it
|
|
164
|
+
const operationTools = new Set((cfg.operations?.tools ?? []).map((t) => t.tool));
|
|
165
|
+
for (const u of cfg.upstreams ?? (cfg.upstream ? [{ name: "upstream", ...cfg.upstream }] : [])) {
|
|
166
|
+
if (!("rest" in u))
|
|
167
|
+
continue;
|
|
168
|
+
for (const t of u.rest.tools) {
|
|
169
|
+
if (t.idempotencyHeader && !operationTools.has(t.name))
|
|
170
|
+
add("error", "upstream", `${"name" in u ? u.name : "upstream"}: the tool ${t.name} sets idempotencyHeader, which makes it a financial operation, and every call to it fails unless it is also listed under "operations"; remove idempotencyHeader, or configure it under operations (financial-operations.md)`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// where the gateway writes
|
|
174
|
+
const writes = [["receiptsDir", cfg.receiptsDir]];
|
|
175
|
+
if (cfg.operations)
|
|
176
|
+
writes.push(["operations.database", cfg.operations.database]);
|
|
177
|
+
if (cfg.approvals)
|
|
178
|
+
writes.push(["approvals.usedDir", cfg.approvals.usedDir]);
|
|
179
|
+
if (cfg.logFile)
|
|
180
|
+
writes.push(["logFile", cfg.logFile]);
|
|
181
|
+
if (cfg.revokedGrants)
|
|
182
|
+
writes.push(["revokedGrants", cfg.revokedGrants]);
|
|
183
|
+
let writable = true;
|
|
184
|
+
for (const [key, path] of writes) {
|
|
185
|
+
if (!writableAncestor(path)) {
|
|
186
|
+
writable = false;
|
|
187
|
+
add("error", "paths", `${key} ${path} cannot be created or written; in a container, use an absolute path under /data (/config is read-only)`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (writable)
|
|
191
|
+
add("ok", "paths", "every path the gateway writes can be written");
|
|
192
|
+
if (cfg.revokedGrants && existsSync(cfg.revokedGrants)) {
|
|
193
|
+
try {
|
|
194
|
+
if (!Array.isArray(JSON.parse(readFileSync(cfg.revokedGrants, "utf8"))))
|
|
195
|
+
throw new Error("expected a JSON array of grant digests");
|
|
196
|
+
}
|
|
197
|
+
catch (e) {
|
|
198
|
+
add("error", "revokedGrants", `${cfg.revokedGrants}: ${e instanceof Error ? e.message : String(e)}; a revocation file that cannot be read denies every call`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
// the grant a single-grant gateway serves
|
|
202
|
+
let scopes = [];
|
|
203
|
+
if (cfg.grantFile) {
|
|
204
|
+
try {
|
|
205
|
+
const env = JSON.parse(readFileSync(cfg.grantFile, "utf8"));
|
|
206
|
+
const v = verifyDelegation(env, principals);
|
|
207
|
+
if (!v.ok)
|
|
208
|
+
add("error", "grant", `${cfg.grantFile}: ${v.error}`);
|
|
209
|
+
else {
|
|
210
|
+
scopes = v.delegation.scopes;
|
|
211
|
+
const now = new Date().toISOString();
|
|
212
|
+
if (!delegationValidAt(v.delegation, now))
|
|
213
|
+
add("error", "grant", `${cfg.grantFile} for ${v.delegation.agent} is outside its validity window (${v.delegation.issuedAt} to ${v.delegation.expiresAt})`);
|
|
214
|
+
else if (Date.parse(v.delegation.expiresAt) - Date.now() < 24 * 3600_000)
|
|
215
|
+
add("warn", "grant", `${cfg.grantFile} for ${v.delegation.agent} expires at ${v.delegation.expiresAt}`);
|
|
216
|
+
else
|
|
217
|
+
add("ok", "grant", `the grant for ${v.delegation.agent} verifies; scopes: ${scopes.join(", ")}`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
catch (e) {
|
|
221
|
+
add("error", "grant", `${cfg.grantFile}: ${e instanceof Error ? e.message : String(e)}`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
add("ok", "grant", "no grantFile: each connection over HTTP presents its own grant");
|
|
226
|
+
}
|
|
227
|
+
// what a log outage costs
|
|
228
|
+
if (cfg.log && !cfg.precommit.length)
|
|
229
|
+
add("warn", "precommit", "a remote log is set and precommit is empty: every call is forwarded before its receipt is logged, so a log outage after a call leaves its side effect without evidence. Name the consequential tools in precommit, or [\"*\"]");
|
|
230
|
+
// tool names the config mentions, against the tools the upstreams offer
|
|
231
|
+
const mentioned = () => [
|
|
232
|
+
...cfg.precommit.filter((t) => t !== "*").map((t) => ["precommit", t]),
|
|
233
|
+
...cfg.facts.flatMap((f) => [["facts", f.tool], ...f.forTools.map((t) => ["facts.forTools", t])]),
|
|
234
|
+
...(cfg.approvals?.tools ?? []).map((t) => ["approvals", t.tool]),
|
|
235
|
+
...(cfg.operations?.tools ?? []).flatMap((t) => [["operations", t.tool], ["operations.reconcile", t.reconcile.tool]]),
|
|
236
|
+
...scopes.map((t) => ["grant scope", t]),
|
|
237
|
+
];
|
|
238
|
+
if (!options.connect) {
|
|
239
|
+
add("warn", "upstreams", "not started: run with --connect to start the upstreams, catch tool-name collisions and unreachable servers, and compare the tool names above with the tools offered");
|
|
240
|
+
return out;
|
|
241
|
+
}
|
|
242
|
+
// a gateway key that does not exist yet (the container creates it on first start) must not stop the upstreams being tried
|
|
243
|
+
if (skipEnvironment && envNames.length) {
|
|
244
|
+
add("warn", "upstreams", "not started: the upstreams need credentials that this process does not hold, by design. Run `agent-custody check --connect` where the gateway runs, for example `docker compose exec gateway agent-custody check --config /config/gateway.json --connect`");
|
|
245
|
+
return out;
|
|
246
|
+
}
|
|
247
|
+
let connectCfg = cfg;
|
|
248
|
+
let scratch;
|
|
249
|
+
if (!existsSync(cfg.identity.keyFile)) {
|
|
250
|
+
scratch = mkdtempSync(join(tmpdir(), "agent-custody-check-"));
|
|
251
|
+
connectCfg = { ...cfg, identity: { keyFile: writeKeyPair(generateKeyPair(), scratch, "check").keyFile } };
|
|
252
|
+
}
|
|
253
|
+
let tools;
|
|
254
|
+
try {
|
|
255
|
+
const host = await createGatewayHost(connectCfg, options.signal ? { signal: options.signal } : {});
|
|
256
|
+
try {
|
|
257
|
+
tools = host.tools();
|
|
258
|
+
}
|
|
259
|
+
finally {
|
|
260
|
+
await host.close();
|
|
261
|
+
}
|
|
262
|
+
add("ok", "upstreams", `the upstreams start and offer ${tools.length} tool${tools.length === 1 ? "" : "s"}: ${tools.join(", ")}`);
|
|
263
|
+
}
|
|
264
|
+
catch (e) {
|
|
265
|
+
add("error", "upstreams", `the gateway would not start: ${e instanceof Error ? e.message : String(e)}`);
|
|
266
|
+
}
|
|
267
|
+
finally {
|
|
268
|
+
if (scratch)
|
|
269
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
270
|
+
}
|
|
271
|
+
if (tools) {
|
|
272
|
+
const unknown = mentioned().filter(([, t]) => !tools.includes(t));
|
|
273
|
+
// a scope that names nothing is harmless; a precommit name that matches nothing leaves that tool forwarded with no authorization committed first
|
|
274
|
+
for (const [where, t] of unknown)
|
|
275
|
+
add(where === "grant scope" ? "warn" : "error", "tool names", `${where} names "${t}", which no upstream offers${where === "precommit" ? "; the tool you meant is forwarded before it is logged" : ""}`);
|
|
276
|
+
if (!unknown.length)
|
|
277
|
+
add("ok", "tool names", "every tool the config and the grant name is offered by an upstream");
|
|
278
|
+
}
|
|
279
|
+
return out;
|
|
280
|
+
}
|
|
281
|
+
export function formatFindings(findings) {
|
|
282
|
+
const mark = { ok: "ok ", warn: "warn ", error: "ERROR" };
|
|
283
|
+
const lines = findings.map((f) => `${mark[f.level]} ${f.check}: ${f.message}`);
|
|
284
|
+
const errors = findings.filter((f) => f.level === "error").length;
|
|
285
|
+
const warns = findings.filter((f) => f.level === "warn").length;
|
|
286
|
+
lines.push("", errors ? `${errors} error${errors === 1 ? "" : "s"}, ${warns} warning${warns === 1 ? "" : "s"}: the gateway would not start cleanly` : `no errors, ${warns} warning${warns === 1 ? "" : "s"}`);
|
|
287
|
+
return lines.join("\n");
|
|
288
|
+
}
|