@coreguard/agent-commerce 0.1.1 → 0.1.3
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 +275 -11
- package/aer1.mjs +258 -0
- package/anchor.mjs +278 -0
- package/bin/mcp.mjs +0 -0
- package/composition.mjs +150 -0
- package/consumption-store.mjs +114 -38
- package/control-plane-cli.mjs +246 -0
- package/control-plane-http.mjs +275 -0
- package/control-plane.mjs +923 -0
- package/decision.mjs +152 -0
- package/firewall.mjs +1 -1
- package/identity.mjs +205 -0
- package/index.d.ts +848 -1
- package/index.mjs +189 -3
- package/mcp.mjs +476 -21
- package/outcomes-bridge.mjs +169 -0
- package/outcomes.mjs +220 -0
- package/package.json +42 -14
- package/profile-ref.mjs +130 -0
- package/reference.mjs +350 -0
- package/risk-cli.mjs +10 -2
- package/risk.mjs +49 -2
- package/spend.mjs +291 -0
- package/trust.mjs +322 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* CoreGuard Control Plane CLI (CGCP/1) — operator surface over the same plane
|
|
4
|
+
* the SDK and HTTP API use. Every command prints canonical JSON to stdout and
|
|
5
|
+
* exits 0; a failure prints `{error:{code,detail}}` to stderr and exits 1.
|
|
6
|
+
*
|
|
7
|
+
* node packages/agent-commerce/control-plane-cli.mjs --store ./cp.json init
|
|
8
|
+
* ... policy upsert --id p1 --spend-limit 5000 --daily-limit 20000 --approval-above 1000
|
|
9
|
+
* ... agent upsert --id a1 --policy p1
|
|
10
|
+
* ... evaluate --agent a1 --merchant m1 --amount 900
|
|
11
|
+
* ... approval request --agent a1 --merchant m1 --amount 2000
|
|
12
|
+
* ... approval decide --id ap_x --decision approved --by ops@example
|
|
13
|
+
* ... exec --agent a1 --merchant m1 --amount 2000 --approval ap_x
|
|
14
|
+
* ... report
|
|
15
|
+
* ... serve --port 8787 (API key from --api-key or COREGUARD_CONTROL_PLANE_API_KEY)
|
|
16
|
+
*
|
|
17
|
+
* BOUNDARY: single-host, single-API-key. No RBAC/SSO/tenancy (NOT_BUILT).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { createControlPlane, CONTROL_PLANE_VERSION, CONTROL_PLANE_BOUNDARY } from "./control-plane.mjs";
|
|
21
|
+
import { startControlPlaneServer } from "./control-plane-http.mjs";
|
|
22
|
+
|
|
23
|
+
function parseArgs(argv) {
|
|
24
|
+
const positional = [];
|
|
25
|
+
const flags = {};
|
|
26
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
27
|
+
const token = argv[i];
|
|
28
|
+
if (token.startsWith("--")) {
|
|
29
|
+
const key = token.slice(2);
|
|
30
|
+
const next = argv[i + 1];
|
|
31
|
+
if (next === undefined || next.startsWith("--")) {
|
|
32
|
+
flags[key] = true;
|
|
33
|
+
} else {
|
|
34
|
+
flags[key] = next;
|
|
35
|
+
i += 1;
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
positional.push(token);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return { positional, flags };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const list = (value) =>
|
|
45
|
+
value === undefined || value === true ? [] : String(value).split(",").map((s) => s.trim()).filter(Boolean);
|
|
46
|
+
|
|
47
|
+
function requireFlag(flags, name) {
|
|
48
|
+
if (flags[name] === undefined || flags[name] === true) throw new Error(`--${name} is required`);
|
|
49
|
+
return flags[name];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function out(value) {
|
|
53
|
+
process.stdout.write(JSON.stringify(value, null, 2) + "\n");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function main() {
|
|
57
|
+
const { positional, flags } = parseArgs(process.argv.slice(2));
|
|
58
|
+
const storePath = flags.store ?? process.env.COREGUARD_CONTROL_PLANE_STORE ?? "./.coreguard/control-plane.json";
|
|
59
|
+
const plane = createControlPlane({ storePath, lockTimeoutMs: Number(flags["lock-timeout-ms"] ?? 5000) });
|
|
60
|
+
const [command, sub] = positional;
|
|
61
|
+
|
|
62
|
+
switch (command) {
|
|
63
|
+
case undefined:
|
|
64
|
+
case "help":
|
|
65
|
+
out({
|
|
66
|
+
version: CONTROL_PLANE_VERSION,
|
|
67
|
+
boundary: CONTROL_PLANE_BOUNDARY,
|
|
68
|
+
store: storePath,
|
|
69
|
+
commands: [
|
|
70
|
+
"init",
|
|
71
|
+
"agent upsert|list|revoke",
|
|
72
|
+
"policy upsert|list|revoke",
|
|
73
|
+
"approval request|decide|get",
|
|
74
|
+
"evaluate|authorize|exec|verification",
|
|
75
|
+
"spend|report|verify|serve",
|
|
76
|
+
],
|
|
77
|
+
});
|
|
78
|
+
return;
|
|
79
|
+
|
|
80
|
+
case "init":
|
|
81
|
+
out({ version: CONTROL_PLANE_VERSION, storePath, created: plane.getState().createdAt, boundary: CONTROL_PLANE_BOUNDARY });
|
|
82
|
+
return;
|
|
83
|
+
|
|
84
|
+
case "agent": {
|
|
85
|
+
if (sub === "upsert") {
|
|
86
|
+
out(
|
|
87
|
+
plane.upsertAgent({
|
|
88
|
+
id: requireFlag(flags, "id"),
|
|
89
|
+
name: flags.name ?? null,
|
|
90
|
+
policyId: flags.policy ?? null,
|
|
91
|
+
status: flags.status ?? undefined,
|
|
92
|
+
metadata: flags.metadata ? JSON.parse(flags.metadata) : undefined,
|
|
93
|
+
}),
|
|
94
|
+
);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (sub === "list") return out({ agents: plane.listAgents() });
|
|
98
|
+
if (sub === "revoke") return out(plane.revokeAgent(requireFlag(flags, "id"), flags.reason ?? ""));
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
case "policy": {
|
|
103
|
+
if (sub === "upsert") {
|
|
104
|
+
out(
|
|
105
|
+
plane.upsertPolicy({
|
|
106
|
+
id: requireFlag(flags, "id"),
|
|
107
|
+
name: flags.name ?? null,
|
|
108
|
+
status: flags.status ?? undefined,
|
|
109
|
+
note: flags.note ?? null,
|
|
110
|
+
spendLimitMinor: flags["spend-limit"] ?? null,
|
|
111
|
+
dailyLimitMinor: flags["daily-limit"] ?? null,
|
|
112
|
+
totalLimitMinor: flags["total-limit"] ?? null,
|
|
113
|
+
requireApprovalAboveMinor: flags["approval-above"] ?? null,
|
|
114
|
+
currency: flags.currency ?? null,
|
|
115
|
+
allowMerchants: list(flags["allow-merchants"]),
|
|
116
|
+
denyMerchants: list(flags["deny-merchants"]),
|
|
117
|
+
allowAddresses: list(flags["allow-addresses"]),
|
|
118
|
+
denyAddresses: list(flags["deny-addresses"]),
|
|
119
|
+
}),
|
|
120
|
+
);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (sub === "list") return out({ policies: plane.listPolicies() });
|
|
124
|
+
if (sub === "revoke") return out(plane.revokePolicy(requireFlag(flags, "id"), flags.reason ?? ""));
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
case "approval": {
|
|
129
|
+
if (sub === "request") {
|
|
130
|
+
out(
|
|
131
|
+
plane.requestApproval({
|
|
132
|
+
id: flags.id,
|
|
133
|
+
agentId: requireFlag(flags, "agent"),
|
|
134
|
+
policyId: flags.policy ?? null,
|
|
135
|
+
merchant: flags.merchant ?? null,
|
|
136
|
+
address: flags.address ?? null,
|
|
137
|
+
amountMinor: flags.amount ?? null,
|
|
138
|
+
currency: flags.currency ?? null,
|
|
139
|
+
requestedBy: flags.by ?? null,
|
|
140
|
+
expiresAt: flags.expires ?? null,
|
|
141
|
+
}),
|
|
142
|
+
);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (sub === "decide") {
|
|
146
|
+
out(
|
|
147
|
+
plane.decideApproval(requireFlag(flags, "id"), {
|
|
148
|
+
decision: requireFlag(flags, "decision"),
|
|
149
|
+
decidedBy: flags.by ?? null,
|
|
150
|
+
reason: flags.reason ?? null,
|
|
151
|
+
}),
|
|
152
|
+
);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (sub === "get") {
|
|
156
|
+
const record = plane.getApproval(requireFlag(flags, "id"));
|
|
157
|
+
if (!record) throw new Error("unknown approval");
|
|
158
|
+
out(record);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
case "evaluate":
|
|
165
|
+
return out(
|
|
166
|
+
plane.evaluate({
|
|
167
|
+
agentId: requireFlag(flags, "agent"),
|
|
168
|
+
policyId: flags.policy ?? null,
|
|
169
|
+
merchant: flags.merchant ?? null,
|
|
170
|
+
address: flags.address ?? null,
|
|
171
|
+
amountMinor: flags.amount ?? null,
|
|
172
|
+
currency: flags.currency ?? null,
|
|
173
|
+
approvalId: flags.approval ?? null,
|
|
174
|
+
}),
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
case "authorize":
|
|
178
|
+
return out(
|
|
179
|
+
plane.authorize({
|
|
180
|
+
agentId: requireFlag(flags, "agent"),
|
|
181
|
+
policyId: flags.policy ?? null,
|
|
182
|
+
merchant: flags.merchant ?? null,
|
|
183
|
+
address: flags.address ?? null,
|
|
184
|
+
amountMinor: flags.amount ?? null,
|
|
185
|
+
currency: flags.currency ?? null,
|
|
186
|
+
approvalId: flags.approval ?? null,
|
|
187
|
+
}),
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
case "exec":
|
|
191
|
+
return out(
|
|
192
|
+
plane.commitExecution({
|
|
193
|
+
id: flags.id,
|
|
194
|
+
agentId: requireFlag(flags, "agent"),
|
|
195
|
+
policyId: flags.policy ?? null,
|
|
196
|
+
merchant: flags.merchant ?? null,
|
|
197
|
+
address: flags.address ?? null,
|
|
198
|
+
amountMinor: requireFlag(flags, "amount"),
|
|
199
|
+
currency: flags.currency ?? null,
|
|
200
|
+
approvalId: flags.approval ?? null,
|
|
201
|
+
receiptId: flags.receipt ?? null,
|
|
202
|
+
verdict: flags.verdict ?? null,
|
|
203
|
+
orderRef: flags.order ?? null,
|
|
204
|
+
}),
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
case "verification":
|
|
208
|
+
return out(
|
|
209
|
+
plane.recordVerification({
|
|
210
|
+
executionId: flags.execution ?? null,
|
|
211
|
+
receiptId: flags.receipt ?? null,
|
|
212
|
+
verdict: flags.verdict ?? null,
|
|
213
|
+
source: flags.source ?? null,
|
|
214
|
+
}),
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
case "spend":
|
|
218
|
+
return out(plane.spend({ agentId: flags.agent ?? null, policyId: flags.policy ?? null }));
|
|
219
|
+
|
|
220
|
+
case "report":
|
|
221
|
+
return out(plane.auditReport({ limit: Number(flags.limit ?? 200) }));
|
|
222
|
+
|
|
223
|
+
case "verify":
|
|
224
|
+
return out(plane.verifyLedger());
|
|
225
|
+
|
|
226
|
+
case "serve": {
|
|
227
|
+
const apiKey = flags["api-key"] ?? process.env.COREGUARD_CONTROL_PLANE_API_KEY;
|
|
228
|
+
if (!apiKey || apiKey === true) throw new Error("--api-key or COREGUARD_CONTROL_PLANE_API_KEY is required");
|
|
229
|
+
const started = await startControlPlaneServer({
|
|
230
|
+
controlPlane: plane,
|
|
231
|
+
apiKey,
|
|
232
|
+
port: Number(flags.port ?? 8787),
|
|
233
|
+
host: flags.host ?? "127.0.0.1",
|
|
234
|
+
});
|
|
235
|
+
out({ serving: started.url, version: CONTROL_PLANE_VERSION, boundary: CONTROL_PLANE_BOUNDARY });
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
throw new Error(`unknown command: ${positional.join(" ") || "(none)"}`);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
main().catch((err) => {
|
|
244
|
+
process.stderr.write(JSON.stringify({ error: { code: "CLI_ERROR", detail: err?.message ?? String(err) } }) + "\n");
|
|
245
|
+
process.exit(1);
|
|
246
|
+
});
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CoreGuard Control Plane HTTP API (CGCP/1) — zero-dependency node:http surface.
|
|
3
|
+
*
|
|
4
|
+
* The same plane the SDK and CLI use, reachable by an agent runtime, a CI job or
|
|
5
|
+
* an operator console. Every /v1 route requires a bearer API key compared in
|
|
6
|
+
* constant time; /health is deliberately public (liveness only, no data).
|
|
7
|
+
*
|
|
8
|
+
* BOUNDARY (standing): a single API key is host-level authorisation, NOT
|
|
9
|
+
* multi-user identity. There is no RBAC/SSO/tenant isolation here — that is
|
|
10
|
+
* NOT_BUILT and is stated by `CONTROL_PLANE_BOUNDARY` on every report. Bind it
|
|
11
|
+
* to loopback or a private network; TLS and an authenticating reverse proxy are
|
|
12
|
+
* deployment concerns, not claims of this module.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { createServer } from "node:http";
|
|
16
|
+
import { timingSafeEqual } from "node:crypto";
|
|
17
|
+
import { CONTROL_PLANE_BOUNDARY, CONTROL_PLANE_VERSION, createControlPlane } from "./control-plane.mjs";
|
|
18
|
+
|
|
19
|
+
const MAX_BODY_BYTES = 256 * 1024;
|
|
20
|
+
|
|
21
|
+
function json(res, status, body) {
|
|
22
|
+
const payload = JSON.stringify(body);
|
|
23
|
+
res.writeHead(status, {
|
|
24
|
+
"content-type": "application/json; charset=utf-8",
|
|
25
|
+
"cache-control": "no-store",
|
|
26
|
+
"content-length": Buffer.byteLength(payload),
|
|
27
|
+
});
|
|
28
|
+
res.end(payload);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function apiError(res, status, code, detail) {
|
|
32
|
+
json(res, status, { error: { code, detail }, boundary: CONTROL_PLANE_BOUNDARY });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function safeEqual(a, b) {
|
|
36
|
+
const left = Buffer.from(String(a));
|
|
37
|
+
const right = Buffer.from(String(b));
|
|
38
|
+
if (left.length !== right.length) return false;
|
|
39
|
+
return timingSafeEqual(left, right);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function readJson(req, maxBodyBytes) {
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
let size = 0;
|
|
45
|
+
let tooLarge = false;
|
|
46
|
+
const chunks = [];
|
|
47
|
+
req.on("data", (chunk) => {
|
|
48
|
+
size += chunk.length;
|
|
49
|
+
if (size > maxBodyBytes) {
|
|
50
|
+
// Keep draining so the client can read the 413 instead of seeing a
|
|
51
|
+
// reset socket; stop buffering and reject at end of stream.
|
|
52
|
+
tooLarge = true;
|
|
53
|
+
chunks.length = 0;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (tooLarge) return;
|
|
57
|
+
chunks.push(chunk);
|
|
58
|
+
});
|
|
59
|
+
req.on("end", () => {
|
|
60
|
+
if (tooLarge) {
|
|
61
|
+
const err = new Error("request body too large");
|
|
62
|
+
err.statusCode = 413;
|
|
63
|
+
err.code = "BODY_TOO_LARGE";
|
|
64
|
+
return reject(err);
|
|
65
|
+
}
|
|
66
|
+
if (chunks.length === 0) return resolve({});
|
|
67
|
+
try {
|
|
68
|
+
const parsed = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
69
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
70
|
+
const err = new Error("body must be a JSON object");
|
|
71
|
+
err.statusCode = 400;
|
|
72
|
+
err.code = "INVALID_JSON";
|
|
73
|
+
return reject(err);
|
|
74
|
+
}
|
|
75
|
+
return resolve(parsed);
|
|
76
|
+
} catch {
|
|
77
|
+
const err = new Error("body is not valid JSON");
|
|
78
|
+
err.statusCode = 400;
|
|
79
|
+
err.code = "INVALID_JSON";
|
|
80
|
+
return reject(err);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
req.on("error", reject);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Build the request handler. Exported separately from the server so a host can
|
|
89
|
+
* mount it in its own HTTP stack or test it without a socket.
|
|
90
|
+
*/
|
|
91
|
+
export function createControlPlaneHandler({ controlPlane, apiKey, maxBodyBytes = MAX_BODY_BYTES } = {}) {
|
|
92
|
+
if (!controlPlane) throw new TypeError("CGCP/1: controlPlane is required");
|
|
93
|
+
if (typeof apiKey !== "string" || apiKey.length === 0) throw new TypeError("CGCP/1: apiKey is required");
|
|
94
|
+
|
|
95
|
+
return async function handler(req, res) {
|
|
96
|
+
const url = new URL(req.url ?? "/", "http://control-plane.local");
|
|
97
|
+
const path = url.pathname;
|
|
98
|
+
const method = req.method ?? "GET";
|
|
99
|
+
|
|
100
|
+
if (path === "/health" && method === "GET") {
|
|
101
|
+
const ledger = controlPlane.verifyLedger();
|
|
102
|
+
return json(res, 200, {
|
|
103
|
+
ok: ledger.ok,
|
|
104
|
+
version: CONTROL_PLANE_VERSION,
|
|
105
|
+
boundary: CONTROL_PLANE_BOUNDARY,
|
|
106
|
+
chain: { ok: ledger.chain.ok, length: ledger.chain.length },
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const auth = req.headers.authorization ?? "";
|
|
111
|
+
const token = auth.startsWith("Bearer ") ? auth.slice(7) : "";
|
|
112
|
+
if (!safeEqual(token, apiKey)) return apiError(res, 401, "UNAUTHORIZED", "a valid bearer API key is required");
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
let body = {};
|
|
116
|
+
if (method === "POST" || method === "PUT" || method === "PATCH") body = await readJson(req, maxBodyBytes);
|
|
117
|
+
|
|
118
|
+
if (path === "/v1/version" && method === "GET") {
|
|
119
|
+
return json(res, 200, { version: CONTROL_PLANE_VERSION, boundary: CONTROL_PLANE_BOUNDARY });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (path === "/v1/agents" && method === "GET") {
|
|
123
|
+
return json(res, 200, { agents: controlPlane.listAgents() });
|
|
124
|
+
}
|
|
125
|
+
if (path === "/v1/agents" && method === "POST") {
|
|
126
|
+
return json(res, 201, controlPlane.upsertAgent(body));
|
|
127
|
+
}
|
|
128
|
+
const revokeAgent = path.match(/^\/v1\/agents\/([^/]+)\/revoke$/);
|
|
129
|
+
if (revokeAgent && method === "POST") {
|
|
130
|
+
return json(res, 200, controlPlane.revokeAgent(decodeURIComponent(revokeAgent[1]), body.reason ?? ""));
|
|
131
|
+
}
|
|
132
|
+
const bind = path.match(/^\/v1\/agents\/([^/]+)\/policy$/);
|
|
133
|
+
if (bind && method === "POST") {
|
|
134
|
+
return json(res, 200, controlPlane.bindPolicy(decodeURIComponent(bind[1]), body.policyId));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (path === "/v1/policies" && method === "GET") {
|
|
138
|
+
return json(res, 200, { policies: controlPlane.listPolicies() });
|
|
139
|
+
}
|
|
140
|
+
if (path === "/v1/policies" && method === "POST") {
|
|
141
|
+
return json(res, 201, controlPlane.upsertPolicy(body));
|
|
142
|
+
}
|
|
143
|
+
const policy = path.match(/^\/v1\/policies\/([^/]+)$/);
|
|
144
|
+
if (policy && method === "GET") {
|
|
145
|
+
const record = controlPlane.getPolicy(decodeURIComponent(policy[1]));
|
|
146
|
+
if (!record) return apiError(res, 404, "NOT_FOUND", "unknown policy");
|
|
147
|
+
return json(res, 200, record);
|
|
148
|
+
}
|
|
149
|
+
const revokePolicy = path.match(/^\/v1\/policies\/([^/]+)\/revoke$/);
|
|
150
|
+
if (revokePolicy && method === "POST") {
|
|
151
|
+
return json(res, 200, controlPlane.revokePolicy(decodeURIComponent(revokePolicy[1]), body.reason ?? ""));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (path === "/v1/evaluate" && method === "POST") {
|
|
155
|
+
return json(res, 200, controlPlane.evaluate(body));
|
|
156
|
+
}
|
|
157
|
+
if (path === "/v1/authorize" && method === "POST") {
|
|
158
|
+
return json(res, 200, controlPlane.authorize(body));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (path === "/v1/approvals" && method === "POST") {
|
|
162
|
+
return json(res, 201, controlPlane.requestApproval(body));
|
|
163
|
+
}
|
|
164
|
+
const approval = path.match(/^\/v1\/approvals\/([^/]+)$/);
|
|
165
|
+
if (approval && method === "GET") {
|
|
166
|
+
const record = controlPlane.getApproval(decodeURIComponent(approval[1]));
|
|
167
|
+
if (!record) return apiError(res, 404, "NOT_FOUND", "unknown approval");
|
|
168
|
+
return json(res, 200, record);
|
|
169
|
+
}
|
|
170
|
+
const decide = path.match(/^\/v1\/approvals\/([^/]+)\/decide$/);
|
|
171
|
+
if (decide && method === "POST") {
|
|
172
|
+
return json(
|
|
173
|
+
res,
|
|
174
|
+
200,
|
|
175
|
+
controlPlane.decideApproval(decodeURIComponent(decide[1]), {
|
|
176
|
+
decision: body.decision,
|
|
177
|
+
decidedBy: body.decidedBy ?? null,
|
|
178
|
+
reason: body.reason ?? null,
|
|
179
|
+
}),
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (path === "/v1/executions" && method === "GET") {
|
|
184
|
+
return json(res, 200, { executions: controlPlane.listExecutions({ limit: Number(url.searchParams.get("limit") ?? 100) }) });
|
|
185
|
+
}
|
|
186
|
+
if (path === "/v1/executions" && method === "POST") {
|
|
187
|
+
const result = controlPlane.commitExecution(body);
|
|
188
|
+
return json(res, result.committed ? 201 : 409, result);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (path === "/v1/verifications" && method === "POST") {
|
|
192
|
+
return json(res, 201, controlPlane.recordVerification(body));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (path === "/v1/spend" && method === "GET") {
|
|
196
|
+
return json(
|
|
197
|
+
res,
|
|
198
|
+
200,
|
|
199
|
+
controlPlane.spend({
|
|
200
|
+
agentId: url.searchParams.get("agentId"),
|
|
201
|
+
policyId: url.searchParams.get("policyId"),
|
|
202
|
+
}),
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (path === "/v1/audit" && method === "GET") {
|
|
207
|
+
return json(res, 200, { audit: controlPlane.auditReport({ limit: Number(url.searchParams.get("limit") ?? 200) }).recentAudit });
|
|
208
|
+
}
|
|
209
|
+
if (path === "/v1/audit/verify" && method === "GET") {
|
|
210
|
+
return json(res, 200, controlPlane.verifyLedger());
|
|
211
|
+
}
|
|
212
|
+
if (path === "/v1/report" && method === "GET") {
|
|
213
|
+
return json(res, 200, controlPlane.auditReport({ limit: Number(url.searchParams.get("limit") ?? 200) }));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return apiError(res, 404, "NOT_FOUND", `${method} ${path} is not a control-plane route`);
|
|
217
|
+
} catch (err) {
|
|
218
|
+
const status = err?.statusCode ?? (err instanceof TypeError ? 400 : 400);
|
|
219
|
+
if (status === 413) return apiError(res, 413, "BODY_TOO_LARGE", "request body exceeds the configured limit");
|
|
220
|
+
// Never leak a stack trace to a caller.
|
|
221
|
+
return apiError(res, status, err?.code ?? "INVALID_REQUEST", err?.message ?? "request failed");
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Create (but do not start) an HTTP server bound to one plane.
|
|
228
|
+
*/
|
|
229
|
+
export function createControlPlaneServer({ controlPlane, apiKey, maxBodyBytes = MAX_BODY_BYTES } = {}) {
|
|
230
|
+
const handler = createControlPlaneHandler({ controlPlane, apiKey, maxBodyBytes });
|
|
231
|
+
return createServer((req, res) => {
|
|
232
|
+
handler(req, res).catch(() => {
|
|
233
|
+
if (!res.headersSent) apiError(res, 500, "INTERNAL", "request failed");
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Open a plane and start its HTTP API. Port 0 asks the OS for a free port (the
|
|
240
|
+
* resolved URL is returned), which is what the example and tests use.
|
|
241
|
+
*/
|
|
242
|
+
export async function startControlPlaneServer({
|
|
243
|
+
storePath,
|
|
244
|
+
apiKey,
|
|
245
|
+
port = 0,
|
|
246
|
+
host = "127.0.0.1",
|
|
247
|
+
controlPlane = null,
|
|
248
|
+
maxBodyBytes = MAX_BODY_BYTES,
|
|
249
|
+
} = {}) {
|
|
250
|
+
const plane = controlPlane ?? createControlPlane({ storePath });
|
|
251
|
+
const server = createControlPlaneServer({ controlPlane: plane, apiKey, maxBodyBytes });
|
|
252
|
+
await new Promise((resolve, reject) => {
|
|
253
|
+
server.once("error", reject);
|
|
254
|
+
server.listen(port, host, () => {
|
|
255
|
+
server.off("error", reject);
|
|
256
|
+
resolve();
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
const address = server.address();
|
|
260
|
+
const boundPort = typeof address === "object" && address ? address.port : port;
|
|
261
|
+
const baseUrl = `http://${host === "0.0.0.0" ? "127.0.0.1" : host}:${boundPort}`;
|
|
262
|
+
return {
|
|
263
|
+
server,
|
|
264
|
+
controlPlane: plane,
|
|
265
|
+
url: baseUrl,
|
|
266
|
+
port: boundPort,
|
|
267
|
+
close: () =>
|
|
268
|
+
new Promise((resolve, reject) => {
|
|
269
|
+
// Node's fetch keeps sockets alive; without this the close callback
|
|
270
|
+
// waits for the client pool and a test/CLI shutdown hangs.
|
|
271
|
+
server.closeAllConnections?.();
|
|
272
|
+
server.close((err) => (err ? reject(err) : resolve()));
|
|
273
|
+
}),
|
|
274
|
+
};
|
|
275
|
+
}
|