hmcp-core 0.0.1 → 0.1.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 +19 -3
- package/dist/approvals.d.ts +508 -0
- package/dist/approvals.js +1445 -0
- package/dist/argpath.d.ts +75 -0
- package/dist/argpath.js +189 -0
- package/dist/audit.d.ts +289 -0
- package/dist/audit.js +512 -0
- package/dist/canonical.d.ts +78 -0
- package/dist/canonical.js +146 -0
- package/dist/classify-held.d.ts +47 -0
- package/dist/classify-held.js +60 -0
- package/dist/classify-name.d.ts +55 -0
- package/dist/classify-name.js +255 -0
- package/dist/decide.d.ts +168 -0
- package/dist/decide.js +518 -0
- package/dist/egress.d.ts +123 -0
- package/dist/egress.js +544 -0
- package/dist/enforce.d.ts +78 -0
- package/dist/enforce.js +231 -0
- package/dist/exposure-apply.d.ts +51 -0
- package/dist/exposure-apply.js +69 -0
- package/dist/exposure.d.ts +81 -0
- package/dist/exposure.js +65 -0
- package/dist/file-lock.d.ts +70 -0
- package/dist/file-lock.js +165 -0
- package/dist/glob.d.ts +11 -0
- package/dist/glob.js +55 -0
- package/dist/grants.d.ts +204 -0
- package/dist/grants.js +372 -0
- package/dist/http-util.d.ts +52 -0
- package/dist/http-util.js +81 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +36 -0
- package/dist/inventory.d.ts +100 -0
- package/dist/inventory.js +88 -0
- package/dist/ip.d.ts +15 -0
- package/dist/ip.js +186 -0
- package/dist/jwt.d.ts +57 -0
- package/dist/jwt.js +199 -0
- package/dist/live-state.d.ts +76 -0
- package/dist/live-state.js +57 -0
- package/dist/migrate.d.ts +22 -0
- package/dist/migrate.js +318 -0
- package/dist/pins.d.ts +119 -0
- package/dist/pins.js +125 -0
- package/dist/policy-batch.d.ts +89 -0
- package/dist/policy-batch.js +340 -0
- package/dist/policy-source.d.ts +161 -0
- package/dist/policy-source.js +299 -0
- package/dist/policy-write.d.ts +627 -0
- package/dist/policy-write.js +1697 -0
- package/dist/policy.d.ts +456 -0
- package/dist/policy.js +448 -0
- package/dist/redact.d.ts +65 -0
- package/dist/redact.js +191 -0
- package/dist/registry.d.ts +41 -0
- package/dist/registry.js +121 -0
- package/dist/remember-defaults.d.ts +9 -0
- package/dist/remember-defaults.js +9 -0
- package/dist/remember.d.ts +91 -0
- package/dist/remember.js +371 -0
- package/dist/resolve.d.ts +22 -0
- package/dist/resolve.js +34 -0
- package/dist/runtime-state.d.ts +65 -0
- package/dist/runtime-state.js +35 -0
- package/dist/secrets.d.ts +159 -0
- package/dist/secrets.js +361 -0
- package/dist/tenant.d.ts +83 -0
- package/dist/tenant.js +182 -0
- package/dist/unicode.d.ts +40 -0
- package/dist/unicode.js +103 -0
- package/dist/within-host.d.ts +33 -0
- package/dist/within-host.js +91 -0
- package/dist/within-real.d.ts +7 -0
- package/dist/within-real.js +28 -0
- package/package.json +33 -4
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { parse as parseYaml } from "yaml";
|
|
3
|
+
import { canonicalJson } from "./canonical.js";
|
|
4
|
+
import { expandPath, parsePolicy } from "./policy.js";
|
|
5
|
+
import { policyDigest } from "./runtime-state.js";
|
|
6
|
+
/**
|
|
7
|
+
* The policy a running gateway is enforcing, and the one narrow way it can
|
|
8
|
+
* change without a restart.
|
|
9
|
+
*
|
|
10
|
+
* The problem this solves is an asymmetry: the console's exposure switch takes
|
|
11
|
+
* effect on the next call, because it is a row in a database both processes
|
|
12
|
+
* read, while tightening an argument bound meant editing `policy.yaml` and
|
|
13
|
+
* restarting. So the fastest control was the blunt one -- switch the whole tool
|
|
14
|
+
* off -- and the precise one was the slow one. An operator reacting to
|
|
15
|
+
* something in production reaches for whichever works now.
|
|
16
|
+
*
|
|
17
|
+
* Three properties make this safe enough to be on by default:
|
|
18
|
+
*
|
|
19
|
+
* 1. **It is all-or-nothing.** A reload either replaces the policy or keeps the
|
|
20
|
+
* one already in force. There is no state in which half of a policy file is
|
|
21
|
+
* being enforced.
|
|
22
|
+
* 2. **It fails closed.** A policy file that does not parse, or does not
|
|
23
|
+
* validate, leaves the previous policy enforcing. An empty or half-written
|
|
24
|
+
* file -- which is what a reader sees mid-save -- can never be read as
|
|
25
|
+
* "allow everything", because it is not a valid policy at all.
|
|
26
|
+
* 3. **It refuses what it cannot actually apply.** Most of a policy is consumed
|
|
27
|
+
* once, at startup: the audit log holds `audit`, the approvals database is opened from
|
|
28
|
+
* `approvals.store_path`, the tenant is resolved from `tenant`. Swapping the
|
|
29
|
+
* policy object would leave those reading the old values while the rules
|
|
30
|
+
* read the new ones, and the console would show the new digest for a process
|
|
31
|
+
* enforcing a mixture. So a change to any of them is refused in full and
|
|
32
|
+
* reported as needing a restart, rather than partly applied.
|
|
33
|
+
*
|
|
34
|
+
* What is left -- the live set below -- is the posture, the rules, the
|
|
35
|
+
* approval mode and the network rules. Which is to say: every argument bound,
|
|
36
|
+
* what happens to a call outside one, and where the gateway may connect.
|
|
37
|
+
*
|
|
38
|
+
* `egress` joined the live set so that adding a remote server from the console
|
|
39
|
+
* takes effect without restarting every AI app. It is live only because the
|
|
40
|
+
* holder of the guard applies it: on an `applied` reload with `egressChanged`,
|
|
41
|
+
* the gateway calls `EgressGuard.reconfigure(policy.egress)` in the same
|
|
42
|
+
* synchronous step that adopts the policy, so no call sees the new rules with
|
|
43
|
+
* the old allowlist or the other way round.
|
|
44
|
+
*
|
|
45
|
+
* Since classification landed, this file has writers other than a person in an
|
|
46
|
+
* editor: the console and `hmcp-gateway rule` write a rule through
|
|
47
|
+
* `policy-write.ts`. That is what the atomic rename in `writeAtomically` and
|
|
48
|
+
* the inode in `stamp()` below are an agreement about -- the safe way to
|
|
49
|
+
* replace a config file is a temporary file and a rename, and a watch that
|
|
50
|
+
* followed the inode would be left pointing at a file nobody writes again.
|
|
51
|
+
*/
|
|
52
|
+
/**
|
|
53
|
+
* The fields a reload may change, because every consumer of them reads them per
|
|
54
|
+
* call rather than at startup.
|
|
55
|
+
*
|
|
56
|
+
* This list is the contract. Adding a field here is a claim that nothing
|
|
57
|
+
* captures it at construction time. "The live/pinned contract" in
|
|
58
|
+
* `policy-source.test.ts` keeps that claim honest in both directions: it
|
|
59
|
+
* requires a case for every field named here, and it checks that every other
|
|
60
|
+
* field refuses a reload instead of being half-applied.
|
|
61
|
+
*/
|
|
62
|
+
export const LIVE_POLICY_FIELDS = [
|
|
63
|
+
"enforcement",
|
|
64
|
+
"defaults",
|
|
65
|
+
"rules",
|
|
66
|
+
"approvals.mode",
|
|
67
|
+
"approvals.ttl_seconds",
|
|
68
|
+
"approvals.single_use",
|
|
69
|
+
"egress"
|
|
70
|
+
];
|
|
71
|
+
/**
|
|
72
|
+
* Everything a reload may *not* change, as a comparable value: the policy with
|
|
73
|
+
* the live fields removed. Two policies with the same projection differ only in
|
|
74
|
+
* ways this process can actually adopt.
|
|
75
|
+
*/
|
|
76
|
+
function pinnedProjection(policy) {
|
|
77
|
+
const { enforcement: _enforcement, defaults: _defaults, rules: _rules, egress: _egress, approvals, ...rest } = policy;
|
|
78
|
+
const { mode: _mode, ttl_seconds: _ttl, single_use: _single, ...pinnedApprovals } = approvals;
|
|
79
|
+
return { ...rest, approvals: pinnedApprovals };
|
|
80
|
+
}
|
|
81
|
+
/** Which pinned areas differ, named the way an operator would name them. */
|
|
82
|
+
function pinnedDifferences(a, b) {
|
|
83
|
+
const left = pinnedProjection(a);
|
|
84
|
+
const right = pinnedProjection(b);
|
|
85
|
+
const keys = new Set([...Object.keys(left), ...Object.keys(right)]);
|
|
86
|
+
const changed = [];
|
|
87
|
+
for (const key of [...keys].sort()) {
|
|
88
|
+
if (canonicalJson(left[key]) !== canonicalJson(right[key]))
|
|
89
|
+
changed.push(key);
|
|
90
|
+
}
|
|
91
|
+
return changed;
|
|
92
|
+
}
|
|
93
|
+
/** The administrative act of adopting a new policy. In the hash chain, so stable. */
|
|
94
|
+
export const POLICY_RELOAD_RULE = "policy.reload";
|
|
95
|
+
/**
|
|
96
|
+
* A reload changed how one or more tools are classified.
|
|
97
|
+
*
|
|
98
|
+
* Separate from `policy.reload` because it is a different fact: the reload
|
|
99
|
+
* record says a process adopted a file, this one says the adoption moved a
|
|
100
|
+
* tool out of (or into) the refused-by-default state. For the common case --
|
|
101
|
+
* someone classified a tool nothing classified -- this is the record that
|
|
102
|
+
* shows the tool becoming reachable.
|
|
103
|
+
*/
|
|
104
|
+
export const POLICY_RECLASSIFY_RULE = "policy.reclassify";
|
|
105
|
+
/** A policy change this process read and did not adopt, with the reason why. */
|
|
106
|
+
export const POLICY_RELOAD_REFUSED_RULE = "policy.reload-refused";
|
|
107
|
+
/** A one-line description of what changed, for the log and the audit record. */
|
|
108
|
+
function summarize(from, to) {
|
|
109
|
+
const parts = [];
|
|
110
|
+
// First, because nothing else in a reload changes as much as this does.
|
|
111
|
+
if (from.enforcement !== to.enforcement) {
|
|
112
|
+
parts.push(to.enforcement === "observe"
|
|
113
|
+
? "ENFORCEMENT OFF: now observing only, every call runs"
|
|
114
|
+
: "enforcement on: the policy is now applied to every call");
|
|
115
|
+
}
|
|
116
|
+
if (from.defaults.mode !== to.defaults.mode) {
|
|
117
|
+
parts.push(`posture ${from.defaults.mode} → ${to.defaults.mode}`);
|
|
118
|
+
}
|
|
119
|
+
if (from.defaults.on_unclassified !== to.defaults.on_unclassified) {
|
|
120
|
+
parts.push(`unclassified ${from.defaults.on_unclassified} → ${to.defaults.on_unclassified}`);
|
|
121
|
+
}
|
|
122
|
+
const before = new Map(from.rules.map((r) => [r.id, canonicalJson(r)]));
|
|
123
|
+
const after = new Map(to.rules.map((r) => [r.id, canonicalJson(r)]));
|
|
124
|
+
const added = [...after.keys()].filter((id) => !before.has(id));
|
|
125
|
+
const removed = [...before.keys()].filter((id) => !after.has(id));
|
|
126
|
+
const altered = [...after.keys()].filter((id) => before.has(id) && before.get(id) !== after.get(id));
|
|
127
|
+
if (added.length > 0)
|
|
128
|
+
parts.push(`rules added: ${added.join(", ")}`);
|
|
129
|
+
if (removed.length > 0)
|
|
130
|
+
parts.push(`rules removed: ${removed.join(", ")}`);
|
|
131
|
+
if (altered.length > 0)
|
|
132
|
+
parts.push(`rules changed: ${altered.join(", ")}`);
|
|
133
|
+
// Order matters: the first matching rule wins, so a reorder changes outcomes
|
|
134
|
+
// even when every rule is individually identical.
|
|
135
|
+
if (added.length === 0 &&
|
|
136
|
+
removed.length === 0 &&
|
|
137
|
+
from.rules.map((r) => r.id).join(",") !== to.rules.map((r) => r.id).join(",")) {
|
|
138
|
+
parts.push("rules reordered");
|
|
139
|
+
}
|
|
140
|
+
if (from.approvals.mode !== to.approvals.mode) {
|
|
141
|
+
parts.push(`approvals ${from.approvals.mode} → ${to.approvals.mode}`);
|
|
142
|
+
}
|
|
143
|
+
if (from.approvals.ttl_seconds !== to.approvals.ttl_seconds) {
|
|
144
|
+
parts.push(`approval ttl ${from.approvals.ttl_seconds}s → ${to.approvals.ttl_seconds}s`);
|
|
145
|
+
}
|
|
146
|
+
if (from.approvals.single_use !== to.approvals.single_use) {
|
|
147
|
+
parts.push(`single-use ${from.approvals.single_use} → ${to.approvals.single_use}`);
|
|
148
|
+
}
|
|
149
|
+
if (canonicalJson(from.egress) !== canonicalJson(to.egress)) {
|
|
150
|
+
const added = to.egress.allow.filter((h) => !from.egress.allow.includes(h));
|
|
151
|
+
const removed = from.egress.allow.filter((h) => !to.egress.allow.includes(h));
|
|
152
|
+
const bits = [];
|
|
153
|
+
if (added.length > 0)
|
|
154
|
+
bits.push(`allowed ${added.join(", ")}`);
|
|
155
|
+
if (removed.length > 0)
|
|
156
|
+
bits.push(`no longer allowed ${removed.join(", ")}`);
|
|
157
|
+
const { allow: _a, ...restFrom } = from.egress;
|
|
158
|
+
const { allow: _b, ...restTo } = to.egress;
|
|
159
|
+
for (const key of Object.keys(restTo)) {
|
|
160
|
+
if (canonicalJson(restFrom[key]) !== canonicalJson(restTo[key])) {
|
|
161
|
+
bits.push(`${key} ${JSON.stringify(restFrom[key])} → ${JSON.stringify(restTo[key])}`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
parts.push(`egress: ${bits.join(", ")}`);
|
|
165
|
+
}
|
|
166
|
+
return parts.length > 0 ? parts.join("; ") : "no change to any enforced field";
|
|
167
|
+
}
|
|
168
|
+
function stamp(path) {
|
|
169
|
+
try {
|
|
170
|
+
const s = statSync(path);
|
|
171
|
+
return { mtimeMs: s.mtimeMs, size: s.size, ino: Number(s.ino) };
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function sameStamp(a, b) {
|
|
178
|
+
if (a === null || b === null)
|
|
179
|
+
return a === b;
|
|
180
|
+
return a.mtimeMs === b.mtimeMs && a.size === b.size && a.ino === b.ino;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Holds the policy in force and reloads it from disk on request.
|
|
184
|
+
*
|
|
185
|
+
* `current()` returns a snapshot. Callers deciding one call take that snapshot
|
|
186
|
+
* once and use it throughout, so a reload landing mid-call cannot have the
|
|
187
|
+
* classification read one policy and the rules another.
|
|
188
|
+
*/
|
|
189
|
+
export class PolicySource {
|
|
190
|
+
policy;
|
|
191
|
+
file;
|
|
192
|
+
last;
|
|
193
|
+
timer = null;
|
|
194
|
+
constructor(initial, options = {}) {
|
|
195
|
+
this.policy = initial;
|
|
196
|
+
this.file = options.path ? expandPath(options.path, options.cwd ?? process.cwd()) : null;
|
|
197
|
+
this.last = this.file ? stamp(this.file) : null;
|
|
198
|
+
}
|
|
199
|
+
/** The policy in force. A snapshot: safe to hold for the length of one call. */
|
|
200
|
+
current() {
|
|
201
|
+
return this.policy;
|
|
202
|
+
}
|
|
203
|
+
digest() {
|
|
204
|
+
return policyDigest(this.policy);
|
|
205
|
+
}
|
|
206
|
+
/** The file being watched, or null for an inline policy. */
|
|
207
|
+
get path() {
|
|
208
|
+
return this.file;
|
|
209
|
+
}
|
|
210
|
+
/** True when this policy could be reloaded at all. */
|
|
211
|
+
get reloadable() {
|
|
212
|
+
return this.file !== null;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Re-reads the file and adopts it if it is valid and adoptable.
|
|
216
|
+
*
|
|
217
|
+
* Parsing happens before anything is replaced, so an invalid file costs
|
|
218
|
+
* nothing: `this.policy` is only assigned once there is a complete,
|
|
219
|
+
* validated policy to assign.
|
|
220
|
+
*/
|
|
221
|
+
reload() {
|
|
222
|
+
if (!this.file)
|
|
223
|
+
return { kind: "unchanged" };
|
|
224
|
+
const seen = stamp(this.file);
|
|
225
|
+
this.last = seen;
|
|
226
|
+
let candidate;
|
|
227
|
+
try {
|
|
228
|
+
candidate = parsePolicy(parseYaml(readFileSync(this.file, "utf8")), this.file);
|
|
229
|
+
}
|
|
230
|
+
catch (err) {
|
|
231
|
+
return {
|
|
232
|
+
kind: "invalid",
|
|
233
|
+
error: err instanceof Error ? err.message : String(err)
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
const from = policyDigest(this.policy);
|
|
237
|
+
const to = policyDigest(candidate);
|
|
238
|
+
if (from === to)
|
|
239
|
+
return { kind: "unchanged" };
|
|
240
|
+
const fields = pinnedDifferences(this.policy, candidate);
|
|
241
|
+
if (fields.length > 0)
|
|
242
|
+
return { kind: "needs-restart", fields };
|
|
243
|
+
const summary = summarize(this.policy, candidate);
|
|
244
|
+
const egressChanged = canonicalJson(this.policy.egress) !== canonicalJson(candidate.egress);
|
|
245
|
+
this.policy = candidate;
|
|
246
|
+
return { kind: "applied", from, to, summary, egressChanged };
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Polls for a change and reloads when one appears.
|
|
250
|
+
*
|
|
251
|
+
* Polling rather than `fs.watch`: a watch on a *file* follows the inode, and
|
|
252
|
+
* the safe way to write a config file is to write a temporary one and rename
|
|
253
|
+
* it over the original -- which is what every editor and every deploy tool
|
|
254
|
+
* does, and which leaves a watch pointing at a file nobody will write again.
|
|
255
|
+
* A stat compares the inode too, so a rename is a change here rather than
|
|
256
|
+
* silence.
|
|
257
|
+
*
|
|
258
|
+
* The timer is unreferenced, so watching never keeps the process alive on its
|
|
259
|
+
* own.
|
|
260
|
+
*/
|
|
261
|
+
watch(onReload, intervalMs = 1000) {
|
|
262
|
+
if (!this.file || this.timer)
|
|
263
|
+
return () => this.stop();
|
|
264
|
+
this.timer = setInterval(() => {
|
|
265
|
+
const seen = stamp(this.file);
|
|
266
|
+
if (sameStamp(seen, this.last))
|
|
267
|
+
return;
|
|
268
|
+
// A file being rewritten in place can be stat-visible before it is
|
|
269
|
+
// complete. The reload parses before it adopts, so a half-written file is
|
|
270
|
+
// an `invalid` result and the previous policy stays in force; the next
|
|
271
|
+
// tick picks up the finished file.
|
|
272
|
+
const result = this.reload();
|
|
273
|
+
if (result.kind !== "unchanged")
|
|
274
|
+
onReload(result);
|
|
275
|
+
}, intervalMs);
|
|
276
|
+
this.timer.unref?.();
|
|
277
|
+
return () => this.stop();
|
|
278
|
+
}
|
|
279
|
+
stop() {
|
|
280
|
+
if (this.timer) {
|
|
281
|
+
clearInterval(this.timer);
|
|
282
|
+
this.timer = null;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/** Rendered for a log line or an audit reason. */
|
|
287
|
+
export function describeReload(result) {
|
|
288
|
+
switch (result.kind) {
|
|
289
|
+
case "unchanged":
|
|
290
|
+
return "policy unchanged";
|
|
291
|
+
case "applied":
|
|
292
|
+
return `policy reloaded: ${result.summary}`;
|
|
293
|
+
case "invalid":
|
|
294
|
+
return `policy file is invalid and was not adopted; the previous policy is still in force: ${result.error}`;
|
|
295
|
+
case "needs-restart":
|
|
296
|
+
return (`policy change to ${result.fields.join(", ")} cannot be applied to a running gateway and was not ` +
|
|
297
|
+
`adopted; the previous policy is still in force. Restart to pick it up.`);
|
|
298
|
+
}
|
|
299
|
+
}
|