@devfellowship/access 0.0.0-stage → 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 +130 -2
- package/capabilities.json +37126 -0
- package/db-surface.json +919 -0
- package/dist/can.d.ts +25 -0
- package/dist/can.js +113 -0
- package/dist/capability.d.ts +20 -0
- package/dist/capability.js +51 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +6 -0
- package/dist/line.d.ts +22 -0
- package/dist/line.js +46 -0
- package/dist/principal.d.ts +49 -0
- package/dist/principal.js +73 -0
- package/dist/rule.d.ts +103 -0
- package/dist/rule.js +191 -0
- package/dist/runtime.d.ts +20 -0
- package/dist/runtime.js +19 -0
- package/dist/types.d.ts +86 -0
- package/dist/types.js +8 -0
- package/package.json +63 -5
- package/parity.json +663 -0
- package/runtime.pin.json +26 -0
package/dist/can.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* can(): the one fleet access decision (parent plan
|
|
3
|
+
* 20261115-fleet-access-iam-gateway-flows-rules, ADR-5 and ADR-16).
|
|
4
|
+
*
|
|
5
|
+
* Rules:
|
|
6
|
+
* 1. Deny wins. Among the lines in force that match, deny beats ask and ask
|
|
7
|
+
* beats allow. No matching line: deny (default deny).
|
|
8
|
+
* 2. A line matches when its capability (exact or pattern) covers the
|
|
9
|
+
* request and its scope covers the target.
|
|
10
|
+
* 3. A revoked or expired line is ignored.
|
|
11
|
+
* 4. An agent never exceeds its owner:
|
|
12
|
+
* effective(agent) = (agent role bundles ∪ agent lines) ∩ effective(owner).
|
|
13
|
+
* Deny wins at each layer; an ask on either side gives ask.
|
|
14
|
+
*
|
|
15
|
+
* Pure: no I/O, no clock read, no input mutation.
|
|
16
|
+
*/
|
|
17
|
+
import type { CanInput, Decision, PermissionLine, Principal } from "./types.js";
|
|
18
|
+
/** The owner chain is at most this long (agent → owner → ...). A longer chain is denied. */
|
|
19
|
+
export declare const MAX_OWNER_DEPTH = 4;
|
|
20
|
+
/** The lines that belong to this principal: its own lines and the lines of its role bundles. */
|
|
21
|
+
export declare function linesOf(principal: Principal, lines: readonly PermissionLine[]): PermissionLine[];
|
|
22
|
+
/** The lines of this principal that are in force and cover the capability and the target. */
|
|
23
|
+
export declare function matchingLines(principal: Principal, input: Omit<CanInput, "principal">): PermissionLine[];
|
|
24
|
+
/** Can this principal do this capability on this target, at `now`? */
|
|
25
|
+
export declare function can(input: CanInput): Decision;
|
package/dist/can.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* can(): the one fleet access decision (parent plan
|
|
3
|
+
* 20261115-fleet-access-iam-gateway-flows-rules, ADR-5 and ADR-16).
|
|
4
|
+
*
|
|
5
|
+
* Rules:
|
|
6
|
+
* 1. Deny wins. Among the lines in force that match, deny beats ask and ask
|
|
7
|
+
* beats allow. No matching line: deny (default deny).
|
|
8
|
+
* 2. A line matches when its capability (exact or pattern) covers the
|
|
9
|
+
* request and its scope covers the target.
|
|
10
|
+
* 3. A revoked or expired line is ignored.
|
|
11
|
+
* 4. An agent never exceeds its owner:
|
|
12
|
+
* effective(agent) = (agent role bundles ∪ agent lines) ∩ effective(owner).
|
|
13
|
+
* Deny wins at each layer; an ask on either side gives ask.
|
|
14
|
+
*
|
|
15
|
+
* Pure: no I/O, no clock read, no input mutation.
|
|
16
|
+
*/
|
|
17
|
+
import { isValidCapability, matchesCapability } from "./capability.js";
|
|
18
|
+
import { isLineActive, matchesScope } from "./line.js";
|
|
19
|
+
/** The owner chain is at most this long (agent → owner → ...). A longer chain is denied. */
|
|
20
|
+
export const MAX_OWNER_DEPTH = 4;
|
|
21
|
+
/** The order in which effects decide. The first effect with a matching line wins. */
|
|
22
|
+
const PRECEDENCE = ["deny", "ask", "allow"];
|
|
23
|
+
const REASON_BY_EFFECT = {
|
|
24
|
+
deny: "deny_line",
|
|
25
|
+
ask: "ask_line",
|
|
26
|
+
allow: "allow_line",
|
|
27
|
+
};
|
|
28
|
+
/** The lines that belong to this principal: its own lines and the lines of its role bundles. */
|
|
29
|
+
export function linesOf(principal, lines) {
|
|
30
|
+
const roles = new Set(principal.role_ids ?? []);
|
|
31
|
+
return lines.filter((line) => (line.principal_actor_id != null &&
|
|
32
|
+
line.principal_actor_id === principal.actor_id) ||
|
|
33
|
+
(line.principal_role_id != null && roles.has(line.principal_role_id)));
|
|
34
|
+
}
|
|
35
|
+
/** The lines of this principal that are in force and cover the capability and the target. */
|
|
36
|
+
export function matchingLines(principal, input) {
|
|
37
|
+
return linesOf(principal, input.lines).filter((line) => isLineActive(line, input.now) &&
|
|
38
|
+
matchesCapability(line.capability, input.capability) &&
|
|
39
|
+
matchesScope(line, input.target));
|
|
40
|
+
}
|
|
41
|
+
/** Lowest id first, so the deciding line does not depend on the input order. */
|
|
42
|
+
function first(lines) {
|
|
43
|
+
return [...lines].sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))[0];
|
|
44
|
+
}
|
|
45
|
+
/** One principal alone, without the owner cap. */
|
|
46
|
+
function decideLayer(principal, input) {
|
|
47
|
+
const matched = matchingLines(principal, input);
|
|
48
|
+
for (const effect of PRECEDENCE) {
|
|
49
|
+
const line = first(matched.filter((l) => l.effect === effect));
|
|
50
|
+
if (line) {
|
|
51
|
+
return {
|
|
52
|
+
outcome: effect,
|
|
53
|
+
decided_by: `permission:${line.id}`,
|
|
54
|
+
reason: REASON_BY_EFFECT[effect],
|
|
55
|
+
line,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return { outcome: "deny", decided_by: null, reason: "no_match" };
|
|
60
|
+
}
|
|
61
|
+
/** agent ∩ owner. Deny wins over ask; ask wins over allow. */
|
|
62
|
+
function capByOwner(agent, owner) {
|
|
63
|
+
if (agent.outcome === "deny")
|
|
64
|
+
return { ...agent, owner };
|
|
65
|
+
if (owner.outcome === "deny") {
|
|
66
|
+
return {
|
|
67
|
+
outcome: "deny",
|
|
68
|
+
decided_by: "owner_cap",
|
|
69
|
+
reason: "owner_cap",
|
|
70
|
+
owner,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
if (agent.outcome === "ask")
|
|
74
|
+
return { ...agent, owner };
|
|
75
|
+
if (owner.outcome === "ask") {
|
|
76
|
+
return {
|
|
77
|
+
outcome: "ask",
|
|
78
|
+
decided_by: "owner_cap",
|
|
79
|
+
reason: "owner_cap",
|
|
80
|
+
owner,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
return { ...agent, owner };
|
|
84
|
+
}
|
|
85
|
+
function decide(principal, input, depth) {
|
|
86
|
+
if (depth > MAX_OWNER_DEPTH) {
|
|
87
|
+
return {
|
|
88
|
+
outcome: "deny",
|
|
89
|
+
decided_by: "owner_cap",
|
|
90
|
+
reason: "owner_depth_exceeded",
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
const own = decideLayer(principal, input);
|
|
94
|
+
if (principal.kind !== "agent")
|
|
95
|
+
return own;
|
|
96
|
+
if (!principal.owner) {
|
|
97
|
+
return {
|
|
98
|
+
outcome: "deny",
|
|
99
|
+
decided_by: "owner_cap",
|
|
100
|
+
reason: "owner_missing",
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
const owner = decide(principal.owner, input, depth + 1);
|
|
104
|
+
return capByOwner(own, owner);
|
|
105
|
+
}
|
|
106
|
+
/** Can this principal do this capability on this target, at `now`? */
|
|
107
|
+
export function can(input) {
|
|
108
|
+
if (!isValidCapability(input.capability)) {
|
|
109
|
+
return { outcome: "deny", decided_by: null, reason: "invalid_capability" };
|
|
110
|
+
}
|
|
111
|
+
const { principal, ...rest } = input;
|
|
112
|
+
return decide(principal, rest, 0);
|
|
113
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capability names and patterns.
|
|
3
|
+
*
|
|
4
|
+
* A name is dot-separated segments: `<domain>.<entity>.<verb>`, for example
|
|
5
|
+
* `work.task.create` (or `work.tasks.insert` for a table write that has no
|
|
6
|
+
* command yet). A segment is lower-case letters, digits, `_` or `-`.
|
|
7
|
+
*
|
|
8
|
+
* A pattern is a name where a segment may be `*`:
|
|
9
|
+
* - a `*` in the last position matches ONE OR MORE remaining segments
|
|
10
|
+
* (`work.*` matches `work.task.create`, but not `work`);
|
|
11
|
+
* - a `*` in any other position matches exactly one segment
|
|
12
|
+
* (`work.*.create` matches `work.task.create`);
|
|
13
|
+
* - `*` alone matches every capability.
|
|
14
|
+
* A malformed pattern matches nothing. The access module (S2-T4) refuses
|
|
15
|
+
* to write one, so a malformed line cannot exist in `iam.permissions`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function isValidCapability(name: string): boolean;
|
|
18
|
+
export declare function isValidCapabilityPattern(pattern: string): boolean;
|
|
19
|
+
/** True when the line capability (exact or pattern) covers the requested capability. */
|
|
20
|
+
export declare function matchesCapability(pattern: string, capability: string): boolean;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capability names and patterns.
|
|
3
|
+
*
|
|
4
|
+
* A name is dot-separated segments: `<domain>.<entity>.<verb>`, for example
|
|
5
|
+
* `work.task.create` (or `work.tasks.insert` for a table write that has no
|
|
6
|
+
* command yet). A segment is lower-case letters, digits, `_` or `-`.
|
|
7
|
+
*
|
|
8
|
+
* A pattern is a name where a segment may be `*`:
|
|
9
|
+
* - a `*` in the last position matches ONE OR MORE remaining segments
|
|
10
|
+
* (`work.*` matches `work.task.create`, but not `work`);
|
|
11
|
+
* - a `*` in any other position matches exactly one segment
|
|
12
|
+
* (`work.*.create` matches `work.task.create`);
|
|
13
|
+
* - `*` alone matches every capability.
|
|
14
|
+
* A malformed pattern matches nothing. The access module (S2-T4) refuses
|
|
15
|
+
* to write one, so a malformed line cannot exist in `iam.permissions`.
|
|
16
|
+
*/
|
|
17
|
+
const SEGMENT = /^[a-z0-9_-]+$/;
|
|
18
|
+
export function isValidCapability(name) {
|
|
19
|
+
if (typeof name !== "string" || name.length === 0)
|
|
20
|
+
return false;
|
|
21
|
+
return name.split(".").every((segment) => SEGMENT.test(segment));
|
|
22
|
+
}
|
|
23
|
+
export function isValidCapabilityPattern(pattern) {
|
|
24
|
+
if (typeof pattern !== "string" || pattern.length === 0)
|
|
25
|
+
return false;
|
|
26
|
+
return pattern
|
|
27
|
+
.split(".")
|
|
28
|
+
.every((segment) => segment === "*" || SEGMENT.test(segment));
|
|
29
|
+
}
|
|
30
|
+
/** True when the line capability (exact or pattern) covers the requested capability. */
|
|
31
|
+
export function matchesCapability(pattern, capability) {
|
|
32
|
+
if (!isValidCapabilityPattern(pattern) || !isValidCapability(capability)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const p = pattern.split(".");
|
|
36
|
+
const c = capability.split(".");
|
|
37
|
+
for (let i = 0; i < p.length; i++) {
|
|
38
|
+
const segment = p[i];
|
|
39
|
+
if (segment === "*") {
|
|
40
|
+
const isLast = i === p.length - 1;
|
|
41
|
+
if (isLast)
|
|
42
|
+
return c.length > i;
|
|
43
|
+
if (c[i] === undefined)
|
|
44
|
+
return false;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (segment !== c[i])
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
return p.length === c.length;
|
|
51
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { can, linesOf, MAX_OWNER_DEPTH, matchingLines } from "./can.js";
|
|
2
|
+
export { isValidCapability, isValidCapabilityPattern, matchesCapability, } from "./capability.js";
|
|
3
|
+
export { isLineActive, matchesScope } from "./line.js";
|
|
4
|
+
export type { ActorRow, ActorType, OwnerResolution, PrincipalReads, ResolvedPrincipal, } from "./principal.js";
|
|
5
|
+
export { resolvePrincipal } from "./principal.js";
|
|
6
|
+
export type { InputSource, RuleDecision, RuleReason, RuleRef, RuleRequest, VerifiedContext, WriteDecision, } from "./rule.js";
|
|
7
|
+
export { buildRuleInput, decideWrite, evaluateRule } from "./rule.js";
|
|
8
|
+
export type { DecisionTable, DmnDefinition, DmnResult, HitPolicy, InputClause, MatchedRule, OutputClause, } from "./runtime.js";
|
|
9
|
+
export { DmnError, evaluateDefinition } from "./runtime.js";
|
|
10
|
+
export type { CanInput, Decision, DecisionReason, Effect, Instant, Outcome, PermissionLine, Principal, ScopeKind, Target, } from "./types.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { can, linesOf, MAX_OWNER_DEPTH, matchingLines } from "./can.js";
|
|
2
|
+
export { isValidCapability, isValidCapabilityPattern, matchesCapability, } from "./capability.js";
|
|
3
|
+
export { isLineActive, matchesScope } from "./line.js";
|
|
4
|
+
export { resolvePrincipal } from "./principal.js";
|
|
5
|
+
export { buildRuleInput, decideWrite, evaluateRule } from "./rule.js";
|
|
6
|
+
export { DmnError, evaluateDefinition } from "./runtime.js";
|
package/dist/line.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-line checks: is the line still in force, and does its scope cover the target.
|
|
3
|
+
*/
|
|
4
|
+
import type { PermissionLine, Target } from "./types.js";
|
|
5
|
+
/**
|
|
6
|
+
* A line is in force when it is not revoked and not expired.
|
|
7
|
+
*
|
|
8
|
+
* - `revoked_at` set: ignored (a revoke is immediate; the row stays for history).
|
|
9
|
+
* - `expires_at` at or before `now`: ignored.
|
|
10
|
+
* - `expires_at` unparseable: a restrictive line (deny, ask) stays in force and an
|
|
11
|
+
* allow line is ignored. A bad timestamp never opens access.
|
|
12
|
+
*/
|
|
13
|
+
export declare function isLineActive(line: PermissionLine, now: Date): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* True when the line scope covers the target.
|
|
16
|
+
*
|
|
17
|
+
* - `all`: every target.
|
|
18
|
+
* - `business_unit`: the target is in that business unit.
|
|
19
|
+
* - `item`: the target is that item.
|
|
20
|
+
* A scoped line without `scope_id`, or an unknown scope kind, covers nothing.
|
|
21
|
+
*/
|
|
22
|
+
export declare function matchesScope(line: PermissionLine, target: Target | undefined): boolean;
|
package/dist/line.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-line checks: is the line still in force, and does its scope cover the target.
|
|
3
|
+
*/
|
|
4
|
+
function toMillis(value) {
|
|
5
|
+
const ms = value instanceof Date ? value.getTime() : Date.parse(value);
|
|
6
|
+
return Number.isNaN(ms) ? null : ms;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* A line is in force when it is not revoked and not expired.
|
|
10
|
+
*
|
|
11
|
+
* - `revoked_at` set: ignored (a revoke is immediate; the row stays for history).
|
|
12
|
+
* - `expires_at` at or before `now`: ignored.
|
|
13
|
+
* - `expires_at` unparseable: a restrictive line (deny, ask) stays in force and an
|
|
14
|
+
* allow line is ignored. A bad timestamp never opens access.
|
|
15
|
+
*/
|
|
16
|
+
export function isLineActive(line, now) {
|
|
17
|
+
if (line.revoked_at != null)
|
|
18
|
+
return false;
|
|
19
|
+
if (line.expires_at == null)
|
|
20
|
+
return true;
|
|
21
|
+
const expiresAt = toMillis(line.expires_at);
|
|
22
|
+
if (expiresAt === null)
|
|
23
|
+
return line.effect !== "allow";
|
|
24
|
+
const isExpired = expiresAt <= now.getTime();
|
|
25
|
+
return !isExpired;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* True when the line scope covers the target.
|
|
29
|
+
*
|
|
30
|
+
* - `all`: every target.
|
|
31
|
+
* - `business_unit`: the target is in that business unit.
|
|
32
|
+
* - `item`: the target is that item.
|
|
33
|
+
* A scoped line without `scope_id`, or an unknown scope kind, covers nothing.
|
|
34
|
+
*/
|
|
35
|
+
export function matchesScope(line, target) {
|
|
36
|
+
switch (line.scope_kind) {
|
|
37
|
+
case "all":
|
|
38
|
+
return true;
|
|
39
|
+
case "business_unit":
|
|
40
|
+
return (line.scope_id != null && target?.business_unit_id === line.scope_id);
|
|
41
|
+
case "item":
|
|
42
|
+
return line.scope_id != null && target?.item_id === line.scope_id;
|
|
43
|
+
default:
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* resolvePrincipal(): the `Principal` of `can()` from the rows a caller can read.
|
|
3
|
+
*
|
|
4
|
+
* Moved from dfl-os-commands (S2-T4) into the package (plan
|
|
5
|
+
* 20261116-fleet-access-s3-gateway-decision, ADR-1), so the gateway and the
|
|
6
|
+
* service build the principal with one copy of the rules: the actor, its role
|
|
7
|
+
* bundles, and for an agent its owner (the delegator of its one ACTIVE
|
|
8
|
+
* delegation). The chain is read up to MAX_OWNER_DEPTH layers; a deeper owner
|
|
9
|
+
* is a stub that `can()` refuses.
|
|
10
|
+
*
|
|
11
|
+
* No I/O of its own: the caller passes the four reads (SQL in a caller
|
|
12
|
+
* transaction in the service, PostgREST with the caller's token in the
|
|
13
|
+
* gateway, an in-memory snapshot in the tests).
|
|
14
|
+
*/
|
|
15
|
+
import type { PermissionLine, Principal } from "./types.js";
|
|
16
|
+
/** `public.actors.type`. */
|
|
17
|
+
export type ActorType = "human" | "agent" | "service";
|
|
18
|
+
/** One row of `public.actors`. */
|
|
19
|
+
export interface ActorRow {
|
|
20
|
+
id: string;
|
|
21
|
+
type: ActorType;
|
|
22
|
+
display_name: string | null;
|
|
23
|
+
}
|
|
24
|
+
/** How the owner of one layer was found. */
|
|
25
|
+
export type OwnerResolution = "not_agent" | "one" | "none" | "ambiguous";
|
|
26
|
+
/** The four reads of resolvePrincipal. Each runs with the caller's own token. */
|
|
27
|
+
export interface PrincipalReads<L extends PermissionLine = PermissionLine> {
|
|
28
|
+
readActor(id: string): Promise<ActorRow | null>;
|
|
29
|
+
/** The `iam.user_roles` role ids of the auth users linked to the actor. */
|
|
30
|
+
readActorRoles(actorId: string): Promise<string[]>;
|
|
31
|
+
/** The delegator actors of the ACTIVE delegations to this actor. */
|
|
32
|
+
readActorOwners(actorId: string): Promise<string[]>;
|
|
33
|
+
/** Unrevoked lines of these actors and these roles (expired lines included). */
|
|
34
|
+
readLines(actorIds: readonly string[], roleIds: readonly string[]): Promise<L[]>;
|
|
35
|
+
}
|
|
36
|
+
export interface ResolvedPrincipal<L extends PermissionLine = PermissionLine> {
|
|
37
|
+
principal: Principal;
|
|
38
|
+
/** One entry per layer: the principal, then its owner chain. */
|
|
39
|
+
layers: {
|
|
40
|
+
actor: ActorRow;
|
|
41
|
+
role_ids: string[];
|
|
42
|
+
owner_resolution: OwnerResolution;
|
|
43
|
+
owner_candidates?: string[];
|
|
44
|
+
}[];
|
|
45
|
+
/** The unrevoked lines of every layer (own lines and role lines). */
|
|
46
|
+
lines: L[];
|
|
47
|
+
}
|
|
48
|
+
/** Null when the actor is not visible to the caller. */
|
|
49
|
+
export declare function resolvePrincipal<L extends PermissionLine>(reads: PrincipalReads<L>, actorId: string): Promise<ResolvedPrincipal<L> | null>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* resolvePrincipal(): the `Principal` of `can()` from the rows a caller can read.
|
|
3
|
+
*
|
|
4
|
+
* Moved from dfl-os-commands (S2-T4) into the package (plan
|
|
5
|
+
* 20261116-fleet-access-s3-gateway-decision, ADR-1), so the gateway and the
|
|
6
|
+
* service build the principal with one copy of the rules: the actor, its role
|
|
7
|
+
* bundles, and for an agent its owner (the delegator of its one ACTIVE
|
|
8
|
+
* delegation). The chain is read up to MAX_OWNER_DEPTH layers; a deeper owner
|
|
9
|
+
* is a stub that `can()` refuses.
|
|
10
|
+
*
|
|
11
|
+
* No I/O of its own: the caller passes the four reads (SQL in a caller
|
|
12
|
+
* transaction in the service, PostgREST with the caller's token in the
|
|
13
|
+
* gateway, an in-memory snapshot in the tests).
|
|
14
|
+
*/
|
|
15
|
+
import { MAX_OWNER_DEPTH } from "./can.js";
|
|
16
|
+
const KIND = {
|
|
17
|
+
human: "person",
|
|
18
|
+
agent: "agent",
|
|
19
|
+
service: "service",
|
|
20
|
+
};
|
|
21
|
+
/** Null when the actor is not visible to the caller. */
|
|
22
|
+
export async function resolvePrincipal(reads, actorId) {
|
|
23
|
+
const layers = [];
|
|
24
|
+
const seen = new Set();
|
|
25
|
+
const build = async (id, depth) => {
|
|
26
|
+
const actor = await reads.readActor(id);
|
|
27
|
+
if (!actor)
|
|
28
|
+
return null;
|
|
29
|
+
const roleIds = await reads.readActorRoles(id);
|
|
30
|
+
const layer = {
|
|
31
|
+
actor,
|
|
32
|
+
role_ids: roleIds,
|
|
33
|
+
owner_resolution: "not_agent",
|
|
34
|
+
};
|
|
35
|
+
layers.push(layer);
|
|
36
|
+
seen.add(id);
|
|
37
|
+
const principal = {
|
|
38
|
+
actor_id: actor.id,
|
|
39
|
+
kind: KIND[actor.type] ?? "service",
|
|
40
|
+
role_ids: roleIds,
|
|
41
|
+
};
|
|
42
|
+
if (actor.type !== "agent")
|
|
43
|
+
return principal;
|
|
44
|
+
const owners = (await reads.readActorOwners(id)).filter((o) => o !== id);
|
|
45
|
+
if (owners.length === 0) {
|
|
46
|
+
layer.owner_resolution = "none";
|
|
47
|
+
principal.owner = null;
|
|
48
|
+
}
|
|
49
|
+
else if (owners.length > 1) {
|
|
50
|
+
layer.owner_resolution = "ambiguous";
|
|
51
|
+
layer.owner_candidates = owners;
|
|
52
|
+
principal.owner = null;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
layer.owner_resolution = "one";
|
|
56
|
+
const ownerId = owners[0];
|
|
57
|
+
// Past the limit, or a cycle: a stub owner without rows. can() denies it
|
|
58
|
+
// (owner_depth_exceeded past the limit, owner_missing on a cycle). No more reads.
|
|
59
|
+
principal.owner =
|
|
60
|
+
depth + 1 > MAX_OWNER_DEPTH || seen.has(ownerId)
|
|
61
|
+
? { actor_id: ownerId, kind: "agent", owner: null }
|
|
62
|
+
: await build(ownerId, depth + 1);
|
|
63
|
+
}
|
|
64
|
+
return principal;
|
|
65
|
+
};
|
|
66
|
+
const principal = await build(actorId, 0);
|
|
67
|
+
if (!principal)
|
|
68
|
+
return null;
|
|
69
|
+
const actorIds = layers.map((l) => l.actor.id);
|
|
70
|
+
const roleIds = [...new Set(layers.flatMap((l) => l.role_ids))];
|
|
71
|
+
const lines = await reads.readLines(actorIds, roleIds);
|
|
72
|
+
return { principal, layers, lines };
|
|
73
|
+
}
|
package/dist/rule.d.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Level 2 of a write: the rule (parent plan ADR-5, ADR-16; S2-T3).
|
|
3
|
+
*
|
|
4
|
+
* A rule is a decision table of dfl-flows-definitions (`policies/**.dmn.json`).
|
|
5
|
+
* Its output field `outcome` says `allow`, `deny` or `ask`.
|
|
6
|
+
*
|
|
7
|
+
* The inputs of a rule come ONLY from two sources, named per input in the
|
|
8
|
+
* rule binding (`RuleRef.inputs`):
|
|
9
|
+
* - `verified`: the context that the caller built from the verified token and
|
|
10
|
+
* from rows it read with the user's own token (for example the global role);
|
|
11
|
+
* - `body`: the request body.
|
|
12
|
+
* A body field is read only when the binding names it with `from: "body"`.
|
|
13
|
+
* A `role` (or any other field) that the client puts in the body is ignored
|
|
14
|
+
* when the binding reads that input from `verified` (fixes parent F22).
|
|
15
|
+
*
|
|
16
|
+
* Fail closed: a missing binding, a missing or mistyped input, an evaluation
|
|
17
|
+
* error, no matching rule, or an output that is not `allow`/`deny`/`ask`
|
|
18
|
+
* gives `deny` (parent F23: no rule match must never mean "no objection").
|
|
19
|
+
*
|
|
20
|
+
* Pure: no I/O, no clock read, no input mutation.
|
|
21
|
+
*/
|
|
22
|
+
import { type DmnDefinition } from "./runtime.js";
|
|
23
|
+
import type { CanInput, Decision, Outcome } from "./types.js";
|
|
24
|
+
/** Where one rule input comes from. */
|
|
25
|
+
export interface InputSource {
|
|
26
|
+
from: "verified" | "body";
|
|
27
|
+
/** A dotted path in the source, for example `role` or `task.points`. */
|
|
28
|
+
path: string;
|
|
29
|
+
/** When set, a value of another type is refused (deny). */
|
|
30
|
+
type?: "string" | "number" | "boolean";
|
|
31
|
+
}
|
|
32
|
+
/** One rule, as the caller loaded it from dfl-flows-definitions at a pinned commit. */
|
|
33
|
+
export interface RuleRef {
|
|
34
|
+
/** Repo path of the rule file, for example `policies/work/task_create_points.dmn.json`. */
|
|
35
|
+
path: string;
|
|
36
|
+
/** The file version: the short git blob id (the engine loader `dmnFileVersion`). */
|
|
37
|
+
version: string;
|
|
38
|
+
definition: DmnDefinition;
|
|
39
|
+
decision_id: string;
|
|
40
|
+
/** One source per input expression of the decision table. A table input without a source: deny. */
|
|
41
|
+
inputs: Readonly<Record<string, InputSource>>;
|
|
42
|
+
/** The output field that holds the outcome. Default `outcome`. */
|
|
43
|
+
outcome_field?: string;
|
|
44
|
+
}
|
|
45
|
+
/** What the caller knows from the verified token and from rows read with the user's token. */
|
|
46
|
+
export type VerifiedContext = Readonly<Record<string, unknown>>;
|
|
47
|
+
export interface RuleRequest {
|
|
48
|
+
rule: RuleRef;
|
|
49
|
+
verified: VerifiedContext;
|
|
50
|
+
/** The request body, as the client sent it. Untrusted. */
|
|
51
|
+
body: unknown;
|
|
52
|
+
}
|
|
53
|
+
export type RuleReason =
|
|
54
|
+
/** The matched rule row gave this outcome. */
|
|
55
|
+
"rule_allow" | "rule_deny" | "rule_ask"
|
|
56
|
+
/** No rule row matched: deny. */
|
|
57
|
+
| "rule_no_match"
|
|
58
|
+
/** A table input has no source in the binding: deny. */
|
|
59
|
+
| "rule_binding_invalid"
|
|
60
|
+
/** A bound input is absent or has the wrong type: deny. */
|
|
61
|
+
| "rule_input_invalid"
|
|
62
|
+
/** The evaluator threw: deny. */
|
|
63
|
+
| "rule_error"
|
|
64
|
+
/** The output is not one of allow/deny/ask, or the table is not single-hit: deny. */
|
|
65
|
+
| "rule_output_invalid";
|
|
66
|
+
export interface RuleDecision {
|
|
67
|
+
outcome: Outcome;
|
|
68
|
+
/** `rule:<path>@<version>` (parent ADR-7), also on a fail-closed deny. */
|
|
69
|
+
decided_by: string;
|
|
70
|
+
reason: RuleReason;
|
|
71
|
+
/** The id of the matched rule row, when one matched. */
|
|
72
|
+
rule_row?: string;
|
|
73
|
+
/** The full output of the matched row (for example `approver_role` on ask). */
|
|
74
|
+
output?: Record<string, unknown>;
|
|
75
|
+
/** The input the table saw. Built from the binding only. */
|
|
76
|
+
input?: Record<string, unknown>;
|
|
77
|
+
/** Why a fail-closed deny happened, for the log. */
|
|
78
|
+
detail?: string;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Build the input of the decision table from the binding only.
|
|
82
|
+
* Throws `RuleInputError` when a table input has no source, or a bound value
|
|
83
|
+
* is absent or has the wrong type (an `optional` table input may be absent: null).
|
|
84
|
+
*/
|
|
85
|
+
export declare function buildRuleInput(request: RuleRequest): Record<string, unknown>;
|
|
86
|
+
/** Evaluate one rule. Never throws: every failure is a deny with a reason. */
|
|
87
|
+
export declare function evaluateRule(request: RuleRequest): RuleDecision;
|
|
88
|
+
export interface WriteDecision {
|
|
89
|
+
outcome: Outcome;
|
|
90
|
+
/** The value for `mcp_tool_calls.decided_by`: the stricter level; the rule on a tie. */
|
|
91
|
+
decided_by: string | null;
|
|
92
|
+
/** Level 1: the grant. */
|
|
93
|
+
grant: Decision;
|
|
94
|
+
/** Level 2: the rule. Absent when the grant denied or the write has no rule. */
|
|
95
|
+
rule?: RuleDecision;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The two levels of a write (parent plan "Two levels on a write"):
|
|
99
|
+
* 1. the grant, `can()`; a deny stops here (the rule is not evaluated);
|
|
100
|
+
* 2. the rule, when the write has one.
|
|
101
|
+
* The stricter outcome wins: deny over ask over allow.
|
|
102
|
+
*/
|
|
103
|
+
export declare function decideWrite(grant: CanInput, rule?: RuleRequest): WriteDecision;
|