|
1 | | -// Policies tab — SRS (Security/Runtime/Safety) proxy. SRS isn't deployed |
2 | | -// here, so every endpoint returns empty list / 404 / 503 so the UI's empty |
3 | | -// states render cleanly instead of erroring out. When SRS lands, replace |
4 | | -// these handlers with reverse-proxy calls. |
| 1 | +// Policies tab — SRS (Security/Runtime/Safety) reverse-proxy. |
| 2 | +// |
| 3 | +// RAI policies (/policies) and OPA rego policies (/opa-policies) are proxied |
| 4 | +// verbatim to SRS, injecting the x-api-key. SRS owns storage + evaluation; |
| 5 | +// the SPA was built against SRS's native shapes (_id, cedar_guardrail, |
| 6 | +// opa_guardrail), so we relay status + body unchanged. |
| 7 | +// |
| 8 | +// The per-agent policy *binding* (/agents/:name/policy) is ours, not SRS's — |
| 9 | +// it records which RAI policy_id an agent enforces, stored in Mongo. The |
| 10 | +// harness reads it (via the run body's `policy` field) and calls SRS's |
| 11 | +// /v1/guardrails/evaluate-tool-call per tool call. |
| 12 | +// |
| 13 | +// If SRS_BASE_URL is unset the proxy routes return 503 SRS_NOT_CONFIGURED so |
| 14 | +// the UI's empty states still render. |
5 | 15 |
|
6 | | -import { Router, type Router as IRouter } from "express"; |
| 16 | +import { Router, type Router as IRouter, type Response } from "express"; |
| 17 | +import { getDb } from "../mongo.js"; |
7 | 18 |
|
8 | 19 | export const policiesRouter: IRouter = Router(); |
9 | 20 |
|
10 | | -policiesRouter.get("/policies", (_req, res) => res.json({ policies: [] })); |
11 | | -policiesRouter.get("/policies/:id", (_req, res) => |
12 | | - res.status(404).json({ error: { code: "NOT_FOUND" } }), |
| 21 | +const SRS_BASE = (process.env["SRS_BASE_URL"] ?? "").replace(/\/+$/, ""); |
| 22 | +const SRS_KEY = process.env["SRS_API_KEY"] ?? ""; |
| 23 | + |
| 24 | +function safeParse(text: string): unknown { |
| 25 | + try { |
| 26 | + return JSON.parse(text); |
| 27 | + } catch { |
| 28 | + return { raw: text }; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Forward a request to SRS, inject x-api-key, relay status + JSON body. |
| 34 | + * |
| 35 | + * `opts.fallback` (used for list GETs): when SRS is unset/unreachable/5xx, |
| 36 | + * respond 200 with the fallback instead of an error, so the Policies page |
| 37 | + * renders its empty state cleanly rather than surfacing "/policies → 502". |
| 38 | + * Writes pass no fallback, so create/update/delete still surface the error. |
| 39 | + */ |
| 40 | +async function srs( |
| 41 | + res: Response, |
| 42 | + method: string, |
| 43 | + path: string, |
| 44 | + opts: { body?: unknown; fallback?: unknown } = {}, |
| 45 | +): Promise<void> { |
| 46 | + const { body, fallback } = opts; |
| 47 | + if (!SRS_BASE) { |
| 48 | + if (fallback !== undefined) { |
| 49 | + res.json(fallback); |
| 50 | + return; |
| 51 | + } |
| 52 | + res.status(503).json({ |
| 53 | + error: { code: "SRS_NOT_CONFIGURED", message: "Policy service (SRS) is not configured. Set SRS_BASE_URL." }, |
| 54 | + }); |
| 55 | + return; |
| 56 | + } |
| 57 | + try { |
| 58 | + const r = await fetch(`${SRS_BASE}${path}`, { |
| 59 | + method, |
| 60 | + headers: { |
| 61 | + "x-api-key": SRS_KEY, |
| 62 | + ...(body !== undefined ? { "content-type": "application/json" } : {}), |
| 63 | + }, |
| 64 | + ...(body !== undefined ? { body: JSON.stringify(body) } : {}), |
| 65 | + }); |
| 66 | + if (fallback !== undefined && r.status >= 500) { |
| 67 | + res.json(fallback); |
| 68 | + return; |
| 69 | + } |
| 70 | + const text = await r.text(); |
| 71 | + res.status(r.status).json(text ? safeParse(text) : {}); |
| 72 | + } catch (err) { |
| 73 | + if (fallback !== undefined) { |
| 74 | + res.json(fallback); |
| 75 | + return; |
| 76 | + } |
| 77 | + res.status(502).json({ error: { code: "SRS_UNREACHABLE", message: (err as Error).message } }); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// ── RAI policies → SRS /v1/rai/policies ─────────────────────────────────── |
| 82 | +policiesRouter.get("/policies", (_req, res) => |
| 83 | + srs(res, "GET", "/v1/rai/policies", { fallback: { policies: [] } }), |
13 | 84 | ); |
14 | | -policiesRouter.post("/policies", (_req, res) => |
15 | | - res.status(503).json({ |
16 | | - error: { code: "SRS_NOT_CONFIGURED", message: "Policy service (SRS) is not deployed in this environment." }, |
17 | | - }), |
| 85 | +policiesRouter.get("/policies/:id", (req, res) => |
| 86 | + srs(res, "GET", `/v1/rai/policies/${encodeURIComponent(req.params["id"]!)}`), |
18 | 87 | ); |
19 | | -policiesRouter.put("/policies/:id", (_req, res) => |
20 | | - res.status(503).json({ error: { code: "SRS_NOT_CONFIGURED" } }), |
| 88 | +policiesRouter.post("/policies", (req, res) => srs(res, "POST", "/v1/rai/policies", { body: req.body ?? {} })); |
| 89 | +policiesRouter.put("/policies/:id", (req, res) => |
| 90 | + srs(res, "PUT", `/v1/rai/policies/${encodeURIComponent(req.params["id"]!)}`, { body: req.body ?? {} }), |
21 | 91 | ); |
22 | | -policiesRouter.delete("/policies/:id", (_req, res) => |
23 | | - res.status(503).json({ error: { code: "SRS_NOT_CONFIGURED" } }), |
| 92 | +policiesRouter.delete("/policies/:id", (req, res) => |
| 93 | + srs(res, "DELETE", `/v1/rai/policies/${encodeURIComponent(req.params["id"]!)}`), |
24 | 94 | ); |
25 | 95 |
|
26 | | -policiesRouter.get("/agents/:name/policy", (_req, res) => res.json({ binding: null })); |
27 | | -policiesRouter.put("/agents/:name/policy", (_req, res) => res.json({ binding: null })); |
28 | | - |
29 | | -policiesRouter.get("/opa-policies", (_req, res) => res.json({ policies: [] })); |
30 | | -policiesRouter.get("/opa-policies/:id", (_req, res) => |
31 | | - res.status(404).json({ error: { code: "NOT_FOUND" } }), |
| 96 | +// ── OPA rego policies → SRS /v1/opa-policies ────────────────────────────── |
| 97 | +policiesRouter.get("/opa-policies", (_req, res) => |
| 98 | + srs(res, "GET", "/v1/opa-policies", { fallback: { policies: [] } }), |
32 | 99 | ); |
33 | | -policiesRouter.post("/opa-policies", (_req, res) => |
34 | | - res.status(503).json({ error: { code: "SRS_NOT_CONFIGURED" } }), |
| 100 | +policiesRouter.get("/opa-policies/:id", (req, res) => |
| 101 | + srs(res, "GET", `/v1/opa-policies/${encodeURIComponent(req.params["id"]!)}`), |
35 | 102 | ); |
36 | | -policiesRouter.put("/opa-policies/:id", (_req, res) => |
37 | | - res.status(503).json({ error: { code: "SRS_NOT_CONFIGURED" } }), |
| 103 | +policiesRouter.post("/opa-policies", (req, res) => srs(res, "POST", "/v1/opa-policies", { body: req.body ?? {} })); |
| 104 | +policiesRouter.put("/opa-policies/:id", (req, res) => |
| 105 | + srs(res, "PUT", `/v1/opa-policies/${encodeURIComponent(req.params["id"]!)}`, { body: req.body ?? {} }), |
38 | 106 | ); |
39 | | -policiesRouter.delete("/opa-policies/:id", (_req, res) => |
40 | | - res.status(503).json({ error: { code: "SRS_NOT_CONFIGURED" } }), |
| 107 | +policiesRouter.delete("/opa-policies/:id", (req, res) => |
| 108 | + srs(res, "DELETE", `/v1/opa-policies/${encodeURIComponent(req.params["id"]!)}`), |
41 | 109 | ); |
| 110 | + |
| 111 | +// ── Per-agent policy binding (agentos-local, Mongo) ─────────────────────── |
| 112 | +interface PolicyBindingDoc { |
| 113 | + _id: string; // agent name |
| 114 | + policyId: string; |
| 115 | + updatedAt: Date; |
| 116 | +} |
| 117 | + |
| 118 | +async function bindingsColl() { |
| 119 | + return (await getDb()).collection<PolicyBindingDoc>("agent_policies"); |
| 120 | +} |
| 121 | + |
| 122 | +policiesRouter.get("/agents/:name/policy", async (req, res, next) => { |
| 123 | + try { |
| 124 | + const doc = await (await bindingsColl()).findOne({ _id: req.params["name"]! }); |
| 125 | + res.json({ binding: doc ? { policyId: doc.policyId } : null }); |
| 126 | + } catch (err) { |
| 127 | + next(err); |
| 128 | + } |
| 129 | +}); |
| 130 | + |
| 131 | +policiesRouter.put("/agents/:name/policy", async (req, res, next) => { |
| 132 | + try { |
| 133 | + const name = req.params["name"]!; |
| 134 | + const body = (req.body ?? {}) as Record<string, unknown>; |
| 135 | + const policyId = typeof body["policy_id"] === "string" ? (body["policy_id"] as string) : null; |
| 136 | + const coll = await bindingsColl(); |
| 137 | + if (!policyId) { |
| 138 | + await coll.deleteOne({ _id: name }); |
| 139 | + res.json({ binding: null }); |
| 140 | + return; |
| 141 | + } |
| 142 | + await coll.updateOne({ _id: name }, { $set: { policyId, updatedAt: new Date() } }, { upsert: true }); |
| 143 | + res.json({ binding: { policyId } }); |
| 144 | + } catch (err) { |
| 145 | + next(err); |
| 146 | + } |
| 147 | +}); |
0 commit comments