@dads-framework/postgres-access 0.0.0-stage → 0.2.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/BENCHMARK.md +275 -0
- package/LICENSE.md +21 -0
- package/README.md +275 -2
- package/dist/compile-access-policy.d.ts +9 -0
- package/dist/compile-access-policy.d.ts.map +1 -0
- package/dist/compile-access-policy.js +77 -0
- package/dist/compile-access-policy.js.map +1 -0
- package/dist/drizzle-executor.d.ts +5 -0
- package/dist/drizzle-executor.d.ts.map +1 -0
- package/dist/drizzle-executor.js +25 -0
- package/dist/drizzle-executor.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/lookup-primary-pool.d.ts +5 -0
- package/dist/lookup-primary-pool.d.ts.map +1 -0
- package/dist/lookup-primary-pool.js +24 -0
- package/dist/lookup-primary-pool.js.map +1 -0
- package/dist/lookup-revision-query.d.ts +7 -0
- package/dist/lookup-revision-query.d.ts.map +1 -0
- package/dist/lookup-revision-query.js +73 -0
- package/dist/lookup-revision-query.js.map +1 -0
- package/dist/lookup-revision-schema.d.ts +10 -0
- package/dist/lookup-revision-schema.d.ts.map +1 -0
- package/dist/lookup-revision-schema.js +75 -0
- package/dist/lookup-revision-schema.js.map +1 -0
- package/dist/lower-permissions.d.ts +8 -0
- package/dist/lower-permissions.d.ts.map +1 -0
- package/dist/lower-permissions.js +76 -0
- package/dist/lower-permissions.js.map +1 -0
- package/dist/policy/compile-linear-batch.d.ts +15 -0
- package/dist/policy/compile-linear-batch.d.ts.map +1 -0
- package/dist/policy/compile-linear-batch.js +34 -0
- package/dist/policy/compile-linear-batch.js.map +1 -0
- package/dist/policy/compile-linear-check.d.ts +5 -0
- package/dist/policy/compile-linear-check.d.ts.map +1 -0
- package/dist/policy/compile-linear-check.js +71 -0
- package/dist/policy/compile-linear-check.js.map +1 -0
- package/dist/policy/compile-postgres-policy.d.ts +28 -0
- package/dist/policy/compile-postgres-policy.d.ts.map +1 -0
- package/dist/policy/compile-postgres-policy.js +189 -0
- package/dist/policy/compile-postgres-policy.js.map +1 -0
- package/dist/policy/compile-reverse-policy.d.ts +5 -0
- package/dist/policy/compile-reverse-policy.d.ts.map +1 -0
- package/dist/policy/compile-reverse-policy.js +60 -0
- package/dist/policy/compile-reverse-policy.js.map +1 -0
- package/dist/policy/compile-shared-point-check.d.ts +18 -0
- package/dist/policy/compile-shared-point-check.d.ts.map +1 -0
- package/dist/policy/compile-shared-point-check.js +126 -0
- package/dist/policy/compile-shared-point-check.js.map +1 -0
- package/dist/policy/policy-model.d.ts +86 -0
- package/dist/policy/policy-model.d.ts.map +1 -0
- package/dist/policy/policy-model.js +2 -0
- package/dist/policy/policy-model.js.map +1 -0
- package/dist/policy/policy-sql.d.ts +20 -0
- package/dist/policy/policy-sql.d.ts.map +1 -0
- package/dist/policy/policy-sql.js +59 -0
- package/dist/policy/policy-sql.js.map +1 -0
- package/dist/postgres-access.tsbuildinfo +1 -0
- package/dist/postgres-authorization.d.ts +35 -0
- package/dist/postgres-authorization.d.ts.map +1 -0
- package/dist/postgres-authorization.js +170 -0
- package/dist/postgres-authorization.js.map +1 -0
- package/dist/postgres-response.d.ts +4 -0
- package/dist/postgres-response.d.ts.map +1 -0
- package/dist/postgres-response.js +13 -0
- package/dist/postgres-response.js.map +1 -0
- package/dist/postgres-serialization-conflict.d.ts +11 -0
- package/dist/postgres-serialization-conflict.d.ts.map +1 -0
- package/dist/postgres-serialization-conflict.js +29 -0
- package/dist/postgres-serialization-conflict.js.map +1 -0
- package/dist/postgres-tuples.d.ts +19 -0
- package/dist/postgres-tuples.d.ts.map +1 -0
- package/dist/postgres-tuples.js +135 -0
- package/dist/postgres-tuples.js.map +1 -0
- package/dist/postgres-types.d.ts +17 -0
- package/dist/postgres-types.d.ts.map +1 -0
- package/dist/postgres-types.js +10 -0
- package/dist/postgres-types.js.map +1 -0
- package/dist/prepared-check-executor.d.ts +12 -0
- package/dist/prepared-check-executor.d.ts.map +1 -0
- package/dist/prepared-check-executor.js +39 -0
- package/dist/prepared-check-executor.js.map +1 -0
- package/dist/protected-authorization-scope.d.ts +10 -0
- package/dist/protected-authorization-scope.d.ts.map +1 -0
- package/dist/protected-authorization-scope.js +41 -0
- package/dist/protected-authorization-scope.js.map +1 -0
- package/dist/relationship-schema.d.ts +11 -0
- package/dist/relationship-schema.d.ts.map +1 -0
- package/dist/relationship-schema.js +36 -0
- package/dist/relationship-schema.js.map +1 -0
- package/dist/revisioned-lookup.d.ts +13 -0
- package/dist/revisioned-lookup.d.ts.map +1 -0
- package/dist/revisioned-lookup.js +129 -0
- package/dist/revisioned-lookup.js.map +1 -0
- package/dist/transactional-access-changes.d.ts +19 -0
- package/dist/transactional-access-changes.d.ts.map +1 -0
- package/dist/transactional-access-changes.js +35 -0
- package/dist/transactional-access-changes.js.map +1 -0
- package/dist/with-protected-postgres-transaction.d.ts +22 -0
- package/dist/with-protected-postgres-transaction.d.ts.map +1 -0
- package/dist/with-protected-postgres-transaction.js +47 -0
- package/dist/with-protected-postgres-transaction.js.map +1 -0
- package/package.json +41 -4
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import {} from "./policy-model.js";
|
|
2
|
+
import { readLinearPath } from "./compile-linear-check.js";
|
|
3
|
+
import { compileLinearBatchBranch } from "./compile-linear-batch.js";
|
|
4
|
+
import { compileSharedPointCheck } from "./compile-shared-point-check.js";
|
|
5
|
+
import { compileReversePermission } from "./compile-reverse-policy.js";
|
|
6
|
+
import { QueryBuilder, relatedEntity, subjectReachability, TUPLES, } from "./policy-sql.js";
|
|
7
|
+
/** Fixed outer binding used only by named integration adapters. */
|
|
8
|
+
export const POSTGRES_AUTHORIZATION_CANDIDATE_BINDING = Object.freeze({
|
|
9
|
+
tableAlias: "dad_authorization_candidate",
|
|
10
|
+
resourceIdColumn: "resource_id",
|
|
11
|
+
});
|
|
12
|
+
/** Compiles one permission decision into a parameterized Postgres Boolean query. */
|
|
13
|
+
export function compilePostgresCheck(model, namespace, request) {
|
|
14
|
+
const builder = new QueryBuilder();
|
|
15
|
+
const namespaceSql = builder.value(namespace);
|
|
16
|
+
const expression = compilePermission(model, builder, namespaceSql, {
|
|
17
|
+
type: builder.value(request.subject.type),
|
|
18
|
+
id: builder.value(request.subject.id),
|
|
19
|
+
entity: request.subject.type,
|
|
20
|
+
}, {
|
|
21
|
+
type: builder.value(request.resource.type),
|
|
22
|
+
id: builder.value(request.resource.id),
|
|
23
|
+
entity: request.resource.type,
|
|
24
|
+
}, request.permission, new Set());
|
|
25
|
+
if (Buffer.byteLength(expression) > 64 * 1024)
|
|
26
|
+
return compileSharedPointCheck(model, namespace, request);
|
|
27
|
+
return Object.freeze({
|
|
28
|
+
kind: "check",
|
|
29
|
+
text: `SELECT (${expression}) AS allowed`,
|
|
30
|
+
values: builder.values,
|
|
31
|
+
// Five bindings describe the namespace and endpoints. Each remaining
|
|
32
|
+
// binding adds one relation or arrow to the ordinary permission plan.
|
|
33
|
+
canPrepare: builder.values.length <= 9 && builder.throughCount <= 2,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
/** One ordinal input relation and one SQL statement preserve input ordering and duplicates. */
|
|
37
|
+
export function compilePostgresCheckMany(model, namespace, requests) {
|
|
38
|
+
const builder = new QueryBuilder();
|
|
39
|
+
const namespaceSql = builder.value(namespace);
|
|
40
|
+
const tuples = requests.map((request, ordinal) => `(${builder.value(ordinal)}, ${builder.value(request.subject.type)}, ${builder.value(request.subject.id)}, ${builder.value(request.resource.type)}, ${builder.value(request.resource.id)}, ${builder.value(request.permission)})`);
|
|
41
|
+
const shared = [];
|
|
42
|
+
const branches = [
|
|
43
|
+
...new Set(requests.map((request) => `${request.resource.type}\u0000${request.permission}`)),
|
|
44
|
+
].map((identity) => {
|
|
45
|
+
const [resourceType, permission] = splitIdentity(identity);
|
|
46
|
+
if (readLinearPath(model, resourceType, permission) !== undefined) {
|
|
47
|
+
const branch = compileLinearBatchBranch(model, builder, {
|
|
48
|
+
namespace: namespaceSql,
|
|
49
|
+
resourceType,
|
|
50
|
+
permission,
|
|
51
|
+
index: shared.length,
|
|
52
|
+
});
|
|
53
|
+
shared.push(branch.hits);
|
|
54
|
+
return branch.decision;
|
|
55
|
+
}
|
|
56
|
+
const expression = compilePermission(model, builder, namespaceSql, { type: "i.subject_kind", id: "i.subject_id", entity: resourceType }, { type: "i.resource_kind", id: "i.resource_id", entity: resourceType }, permission, new Set());
|
|
57
|
+
return `WHEN i.resource_kind = ${builder.value(resourceType)} AND i.permission = ${builder.value(permission)} THEN (${expression})`;
|
|
58
|
+
});
|
|
59
|
+
return Object.freeze({
|
|
60
|
+
kind: "check-many",
|
|
61
|
+
text: `WITH input(ordinal, subject_kind, subject_id, resource_kind, resource_id, permission) AS (VALUES ${tuples.join(", ")})${shared.map((query) => `, ${query}`).join("")} SELECT i.ordinal::integer AS ordinal, CASE ${branches.join(" ")} ELSE false END AS allowed FROM input i ORDER BY i.ordinal::integer`,
|
|
62
|
+
values: builder.values,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Computes allowed IDs actor-first from the reverse subject index. It never
|
|
67
|
+
* enumerates every resource of a kind in application memory or SQL.
|
|
68
|
+
*/
|
|
69
|
+
export function compilePostgresLookup(model, namespace, request) {
|
|
70
|
+
const builder = new QueryBuilder();
|
|
71
|
+
const namespaceSql = builder.value(namespace);
|
|
72
|
+
const subject = {
|
|
73
|
+
type: builder.value(request.subject.type),
|
|
74
|
+
id: builder.value(request.subject.id),
|
|
75
|
+
entity: request.subject.type,
|
|
76
|
+
};
|
|
77
|
+
const allowedResourceIds = compileReversePermission(model, builder, namespaceSql, request.resourceType, request.permission, new Set());
|
|
78
|
+
// Keep the recursive work table on the outer side of each indexed hop.
|
|
79
|
+
// Flattening the LATERAL probe can scan the namespace once per iteration.
|
|
80
|
+
return Object.freeze({
|
|
81
|
+
kind: "lookup",
|
|
82
|
+
text: `WITH RECURSIVE ${subjectReachability(namespaceSql, subject.type, subject.id)} SELECT DISTINCT allowed_resource.resource_id FROM (${allowedResourceIds}) allowed_resource ORDER BY allowed_resource.resource_id`,
|
|
83
|
+
values: builder.values,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Compiles one descriptor's permission against the fixed integration candidate
|
|
88
|
+
* binding. The caller supplies neither SQL text nor an identifier.
|
|
89
|
+
*/
|
|
90
|
+
export function compilePostgresCandidatePredicate(model, namespace, request) {
|
|
91
|
+
const builder = new QueryBuilder();
|
|
92
|
+
const namespaceSql = builder.value(namespace);
|
|
93
|
+
const expression = compilePermission(model, builder, namespaceSql, {
|
|
94
|
+
type: builder.value(request.subject.type),
|
|
95
|
+
id: builder.value(request.subject.id),
|
|
96
|
+
entity: request.subject.type,
|
|
97
|
+
}, {
|
|
98
|
+
type: builder.value(request.resourceType),
|
|
99
|
+
id: `${POSTGRES_AUTHORIZATION_CANDIDATE_BINDING.tableAlias}.${POSTGRES_AUTHORIZATION_CANDIDATE_BINDING.resourceIdColumn}`,
|
|
100
|
+
entity: request.resourceType,
|
|
101
|
+
}, request.permission, new Set());
|
|
102
|
+
return Object.freeze({
|
|
103
|
+
kind: "candidate-predicate",
|
|
104
|
+
text: `(${expression})`,
|
|
105
|
+
values: builder.values,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
function compilePermission(model, builder, namespace, subject, resource, permissionName, active) {
|
|
109
|
+
const definition = model.permissions.find((candidate) => candidate.wireName === permissionName &&
|
|
110
|
+
candidate.entity === resource.entity);
|
|
111
|
+
if (!definition)
|
|
112
|
+
throw new Error(`Postgres policy cannot find permission ${permissionName}.`);
|
|
113
|
+
const identity = `${resource.entity}\u0000${permissionName}`;
|
|
114
|
+
if (active.has(identity))
|
|
115
|
+
throw new Error("Postgres SQL compilation does not yet support a cycle through permissions. Subject-set cycles are supported by recursive relation SQL; rewrite this permission-through cycle or add a dedicated recursive permission compiler.");
|
|
116
|
+
active.add(identity);
|
|
117
|
+
try {
|
|
118
|
+
return compileExpression(model, builder, namespace, subject, resource, definition.expression, active);
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
active.delete(identity);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function compileExpression(model, builder, namespace, subject, resource, expression, active) {
|
|
125
|
+
if (expression.kind === "relation")
|
|
126
|
+
return compileRelationMembership(builder, namespace, subject, resource, expression.relation);
|
|
127
|
+
if (expression.kind === "through") {
|
|
128
|
+
const relationSql = builder.value(expression.relation);
|
|
129
|
+
const throughAlias = builder.throughAlias();
|
|
130
|
+
const related = {
|
|
131
|
+
type: `${throughAlias}.subject_kind`,
|
|
132
|
+
id: `${throughAlias}.subject_id`,
|
|
133
|
+
entity: relatedEntity(model, resource.entity, expression.relation),
|
|
134
|
+
};
|
|
135
|
+
const child = compilePermissionForRelated(model, builder, namespace, subject, related, expression.permission, active);
|
|
136
|
+
// OFFSET 0 keeps each arrow a correlated subplan. Pulling arrows up
|
|
137
|
+
// into semijoins can repeatedly execute the nested membership query.
|
|
138
|
+
return `EXISTS (SELECT 1 FROM ${TUPLES} ${throughAlias} WHERE ${throughAlias}.namespace = ${namespace} AND ${throughAlias}.resource_kind = ${resource.type} AND ${throughAlias}.resource_id = ${resource.id} AND ${throughAlias}.relation = ${relationSql} AND ${throughAlias}.subject_is_wildcard = false AND ${throughAlias}.subject_relation = '' AND (${child}) OFFSET 0)`;
|
|
139
|
+
}
|
|
140
|
+
if (expression.kind === "exclude")
|
|
141
|
+
return `((${compileExpression(model, builder, namespace, subject, resource, expression.include, active)}) AND NOT (${compileExpression(model, builder, namespace, subject, resource, expression.exclude, active)}))`;
|
|
142
|
+
const joiner = expression.kind === "any-of" ? " OR " : " AND ";
|
|
143
|
+
return `(${expression.expressions.map((child) => `(${compileExpression(model, builder, namespace, subject, resource, child, active)})`).join(joiner)})`;
|
|
144
|
+
}
|
|
145
|
+
function compilePermissionForRelated(model, builder, namespace, subject, resource, permission, active) {
|
|
146
|
+
const definition = model.permissions.find((candidate) => candidate.wireName === permission && candidate.entity === resource.entity);
|
|
147
|
+
if (!definition)
|
|
148
|
+
throw new Error("Postgres policy is missing a through permission.");
|
|
149
|
+
return compilePermission(model, builder, namespace, subject, resource, permission, active);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The recursive UNION deduplicates usersets, including cycles and diamonds.
|
|
153
|
+
* Keep each hop correlated so stale statistics cannot select a repeated
|
|
154
|
+
* namespace scan in place of a tuple key lookup.
|
|
155
|
+
*/
|
|
156
|
+
function compileRelationMembership(builder, namespace, subject, resource, relation) {
|
|
157
|
+
const relationSql = builder.value(relation);
|
|
158
|
+
return `EXISTS (WITH RECURSIVE requested(resource_kind, resource_id, relation) AS (
|
|
159
|
+
VALUES (${resource.type}, ${resource.id}, ${relationSql})
|
|
160
|
+
UNION
|
|
161
|
+
SELECT child.subject_kind, child.subject_id, child.subject_relation
|
|
162
|
+
FROM requested requested_relation
|
|
163
|
+
CROSS JOIN LATERAL (
|
|
164
|
+
SELECT tuple.subject_kind, tuple.subject_id, tuple.subject_relation
|
|
165
|
+
FROM ${TUPLES} tuple
|
|
166
|
+
WHERE tuple.namespace = ${namespace}
|
|
167
|
+
AND tuple.resource_kind = requested_relation.resource_kind
|
|
168
|
+
AND tuple.resource_id = requested_relation.resource_id
|
|
169
|
+
AND tuple.relation = requested_relation.relation
|
|
170
|
+
AND tuple.subject_is_wildcard = false
|
|
171
|
+
AND tuple.subject_relation <> ''
|
|
172
|
+
OFFSET 0
|
|
173
|
+
) child
|
|
174
|
+
)
|
|
175
|
+
SELECT 1
|
|
176
|
+
FROM ${TUPLES} tuple
|
|
177
|
+
JOIN requested requested_relation
|
|
178
|
+
ON requested_relation.resource_kind = tuple.resource_kind
|
|
179
|
+
AND requested_relation.resource_id = tuple.resource_id
|
|
180
|
+
AND requested_relation.relation = tuple.relation
|
|
181
|
+
WHERE tuple.namespace = ${namespace}
|
|
182
|
+
AND ((tuple.subject_is_wildcard = false AND tuple.subject_relation = '' AND tuple.subject_kind = ${subject.type} AND tuple.subject_id = ${subject.id}) OR (tuple.subject_is_wildcard = true AND tuple.subject_kind = ${subject.type}))
|
|
183
|
+
LIMIT 1)`;
|
|
184
|
+
}
|
|
185
|
+
function splitIdentity(value) {
|
|
186
|
+
const index = value.indexOf("\u0000");
|
|
187
|
+
return [value.slice(0, index), value.slice(index + 1)];
|
|
188
|
+
}
|
|
189
|
+
//# sourceMappingURL=compile-postgres-policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile-postgres-policy.js","sourceRoot":"","sources":["../../src/policy/compile-postgres-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,EAAE,wBAAwB,EAAE,MAAM,6BAA6B,CAAC;AACvE,OAAO,EACL,YAAY,EACZ,aAAa,EACb,mBAAmB,EACnB,MAAM,GACP,MAAM,iBAAiB,CAAC;AAWzB,mEAAmE;AACnE,MAAM,CAAC,MAAM,wCAAwC,GACnD,MAAM,CAAC,MAAM,CAAC;IACZ,UAAU,EAAE,6BAA6B;IACzC,gBAAgB,EAAE,aAAa;CAChC,CAAC,CAAC;AAQL,oFAAoF;AACpF,MAAM,UAAU,oBAAoB,CAClC,KAAmC,EACnC,SAAiB,EACjB,OAAkC;IAElC,MAAM,OAAO,GAAG,IAAI,YAAY,EAAE,CAAC;IACnC,MAAM,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IAC9C,MAAM,UAAU,GAAG,iBAAiB,CAClC,KAAK,EACL,OAAO,EACP,YAAY,EACZ;QACE,IAAI,EAAE,OAAO,CAAC,KAAK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./policy-sql.js";
|
|
3
|
+
/** Compiles the set of resource IDs a subject may use for one permission. */
|
|
4
|
+
export declare function compileReversePermission(model: AuthorizationModelDefinition, builder: QueryBuilder, namespace: string, entity: string, permissionName: string, active: Set<string>): string;
|
|
5
|
+
//# sourceMappingURL=compile-reverse-policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile-reverse-policy.d.ts","sourceRoot":"","sources":["../../src/policy/compile-reverse-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,4BAA4B,EAElC,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAyB,KAAK,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE3E,6EAA6E;AAC7E,wBAAgB,wBAAwB,CACtC,KAAK,EAAE,4BAA4B,EACnC,OAAO,EAAE,YAAY,EACrB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,MAAM,EACtB,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,GAClB,MAAM,CA2BR"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import {} from "./policy-model.js";
|
|
2
|
+
import { relatedEntity, TUPLES } from "./policy-sql.js";
|
|
3
|
+
/** Compiles the set of resource IDs a subject may use for one permission. */
|
|
4
|
+
export function compileReversePermission(model, builder, namespace, entity, permissionName, active) {
|
|
5
|
+
const definition = model.permissions.find((candidate) => candidate.wireName === permissionName && candidate.entity === entity);
|
|
6
|
+
if (!definition)
|
|
7
|
+
throw new Error(`Postgres policy cannot find permission ${permissionName}.`);
|
|
8
|
+
const identity = `${entity}\u0000${permissionName}`;
|
|
9
|
+
if (active.has(identity))
|
|
10
|
+
throw new Error("Postgres SQL compilation does not yet support a cycle through permissions. Subject-set cycles are supported by recursive relation SQL; rewrite this permission-through cycle or add a dedicated recursive permission compiler.");
|
|
11
|
+
active.add(identity);
|
|
12
|
+
try {
|
|
13
|
+
return compileReverseExpression(model, builder, namespace, entity, definition.expression, active);
|
|
14
|
+
}
|
|
15
|
+
finally {
|
|
16
|
+
active.delete(identity);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function compileReverseExpression(model, builder, namespace, entity, expression, active) {
|
|
20
|
+
if (expression.kind === "relation") {
|
|
21
|
+
const resourceKind = builder.value(entity);
|
|
22
|
+
const relation = builder.value(expression.relation);
|
|
23
|
+
return `SELECT subject_reachable.resource_id FROM subject_reachable WHERE subject_reachable.resource_kind = ${resourceKind} AND subject_reachable.relation = ${relation}`;
|
|
24
|
+
}
|
|
25
|
+
if (expression.kind === "through") {
|
|
26
|
+
const throughAlias = builder.throughAlias();
|
|
27
|
+
const related = relatedEntity(model, entity, expression.relation);
|
|
28
|
+
const relatedResourceIds = compileReversePermission(model, builder, namespace, related, expression.permission, active);
|
|
29
|
+
const resourceKind = builder.value(entity);
|
|
30
|
+
const relation = builder.value(expression.relation);
|
|
31
|
+
const relatedKind = builder.value(related);
|
|
32
|
+
// Keep the reached IDs on the outer side of this hop. Without the
|
|
33
|
+
// fence Postgres can re-run the inner permission chain for each found
|
|
34
|
+
// resource, particularly when tuple statistics lag writes.
|
|
35
|
+
return `SELECT DISTINCT match.resource_id
|
|
36
|
+
FROM (${relatedResourceIds}) related_resource
|
|
37
|
+
CROSS JOIN LATERAL (
|
|
38
|
+
SELECT ${throughAlias}.resource_id
|
|
39
|
+
FROM ${TUPLES} ${throughAlias}
|
|
40
|
+
WHERE ${throughAlias}.namespace = ${namespace}
|
|
41
|
+
AND ${throughAlias}.resource_kind = ${resourceKind}
|
|
42
|
+
AND ${throughAlias}.relation = ${relation}
|
|
43
|
+
AND ${throughAlias}.subject_kind = ${relatedKind}
|
|
44
|
+
AND ${throughAlias}.subject_id = related_resource.resource_id
|
|
45
|
+
AND ${throughAlias}.subject_is_wildcard = false
|
|
46
|
+
AND ${throughAlias}.subject_relation = ''
|
|
47
|
+
OFFSET 0
|
|
48
|
+
) match`;
|
|
49
|
+
}
|
|
50
|
+
if (expression.kind === "exclude") {
|
|
51
|
+
const include = compileReverseExpression(model, builder, namespace, entity, expression.include, active);
|
|
52
|
+
const exclude = compileReverseExpression(model, builder, namespace, entity, expression.exclude, active);
|
|
53
|
+
return `(${include}) EXCEPT (${exclude})`;
|
|
54
|
+
}
|
|
55
|
+
const operator = expression.kind === "any-of" ? "UNION" : "INTERSECT";
|
|
56
|
+
return expression.expressions
|
|
57
|
+
.map((child) => `(${compileReverseExpression(model, builder, namespace, entity, child, active)})`)
|
|
58
|
+
.join(` ${operator} `);
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=compile-reverse-policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile-reverse-policy.js","sourceRoot":"","sources":["../../src/policy/compile-reverse-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,aAAa,EAAE,MAAM,EAAqB,MAAM,iBAAiB,CAAC;AAE3E,6EAA6E;AAC7E,MAAM,UAAU,wBAAwB,CACtC,KAAmC,EACnC,OAAqB,EACrB,SAAiB,EACjB,MAAc,EACd,cAAsB,EACtB,MAAmB;IAEnB,MAAM,UAAU,GAAG,KAAK,CAAC,WAAW,CAAC,IAAI,CACvC,CAAC,SAAS,EAAE,EAAE,CACZ,SAAS,CAAC,QAAQ,KAAK,cAAc,IAAI,SAAS,CAAC,MAAM,KAAK,MAAM,CACvE,CAAC;IACF,IAAI,CAAC,UAAU;QACb,MAAM,IAAI,KAAK,CACb,0CAA0C,cAAc,GAAG,CAC5D,CAAC;IACJ,MAAM,QAAQ,GAAG,GAAG,MAAM,SAAS,cAAc,EAAE,CAAC;IACpD,IAAI,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;QACtB,MAAM,IAAI,KAAK,CACb,gOAAgO,CACjO,CAAC;IACJ,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACrB,IAAI,CAAC;QACH,OAAO,wBAAwB,CAC7B,KA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,oBAAoB,YAAY;gBAC5C,YAAY,eAAe,QAAQ;gBACnC,YAAY,mBAAmB,WAAW;gBAC1C,YAAY;gBACZ,YAAY;gBACZ,YAAY;;cAEd,CAAC;IACb,CAAC;IACD,IAAI,UAAU,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;QAClC,MAAM,OAAO,GAAG,wBAAwB,CACtC,KAAK,EACL,OAAO,EACP,SAAS,EACT,MAAM,EACN,UAAU,CAAC,OAAO,EAClB,MAAM,CACP,CAAC;QACF,MAAM,OAAO,GAAG,wBAAwB,CACtC,KAAK,EACL,OAAO,EACP,SAAS,EACT,MAAM,EACN,UAAU,CAAC,OAAO,EAClB,MAAM,CACP,CAAC;QACF,OAAO,IAAI,OAAO,aAAa,OAAO,GAAG,CAAC;IAC5C,CAAC;IACD,MAAM,QAAQ,GAAG,UAAU,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC;IACtE,OAAO,UAAU,CAAC,WAAW;SAC1B,GAAG,CACF,CAAC,KAAK,EAAE,EAAE,CACR,IAAI,wBAAwB,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,GAAG,CACpF;SACA,IAAI,CAAC,IAAI,QAAQ,GAAG,CAAC,CAAC;AAC3B,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CompiledAuthorizationQuery } from "./compile-postgres-policy.js";
|
|
2
|
+
import type { AuthorizationModelDefinition, NormalizedPermissionCheck } from "./policy-model.js";
|
|
3
|
+
import { QueryBuilder } from "./policy-sql.js";
|
|
4
|
+
/** Compact Boolean point evaluation for a policy whose forward expansion is large. */
|
|
5
|
+
export declare function compileSharedPointCheck(model: AuthorizationModelDefinition, namespace: string, check: NormalizedPermissionCheck): CompiledAuthorizationQuery & {
|
|
6
|
+
readonly canPrepare: false;
|
|
7
|
+
};
|
|
8
|
+
type SharedPointBinding = {
|
|
9
|
+
readonly namespace: string;
|
|
10
|
+
readonly subjectKind: string;
|
|
11
|
+
readonly subjectId: string;
|
|
12
|
+
readonly resourceEntity: string;
|
|
13
|
+
readonly permission: string;
|
|
14
|
+
};
|
|
15
|
+
/** Rows retain the actual persisted kind, including on nested arrow edges. */
|
|
16
|
+
export declare function compileSharedPointRows(model: AuthorizationModelDefinition, builder: QueryBuilder, binding: SharedPointBinding): string;
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=compile-shared-point-check.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile-shared-point-check.d.ts","sourceRoot":"","sources":["../../src/policy/compile-shared-point-check.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AAC/E,OAAO,KAAK,EACV,4BAA4B,EAE5B,yBAAyB,EAC1B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,YAAY,EAIb,MAAM,iBAAiB,CAAC;AAEzB,sFAAsF;AACtF,wBAAgB,uBAAuB,CACrC,KAAK,EAAE,4BAA4B,EACnC,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,yBAAyB,GAC/B,0BAA0B,GAAG;IAAE,QAAQ,CAAC,UAAU,EAAE,KAAK,CAAA;CAAE,CAwB7D;AAED,KAAK,kBAAkB,GAAG;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF,8EAA8E;AAC9E,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,4BAA4B,EACnC,OAAO,EAAE,YAAY,EACrB,OAAO,EAAE,kBAAkB,GAC1B,MAAM,CAIR"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { QueryBuilder, relatedEntity, subjectReachability, TUPLES, } from "./policy-sql.js";
|
|
2
|
+
/** Compact Boolean point evaluation for a policy whose forward expansion is large. */
|
|
3
|
+
export function compileSharedPointCheck(model, namespace, check) {
|
|
4
|
+
const builder = new QueryBuilder();
|
|
5
|
+
const namespaceSql = builder.value(namespace);
|
|
6
|
+
const subjectKind = builder.value(check.subject.type);
|
|
7
|
+
const subjectId = builder.value(check.subject.id);
|
|
8
|
+
const resourceKind = builder.value(check.resource.type);
|
|
9
|
+
const resourceId = builder.value(check.resource.id);
|
|
10
|
+
const compiled = compileSharedPointPlan(model, builder, {
|
|
11
|
+
namespace: namespaceSql,
|
|
12
|
+
subjectKind,
|
|
13
|
+
subjectId,
|
|
14
|
+
resourceEntity: check.resource.type,
|
|
15
|
+
permission: check.permission,
|
|
16
|
+
});
|
|
17
|
+
return {
|
|
18
|
+
kind: "check",
|
|
19
|
+
canPrepare: false,
|
|
20
|
+
text: `${compiled.withClause}
|
|
21
|
+
SELECT EXISTS (
|
|
22
|
+
SELECT 1 FROM ${compiled.allowed}
|
|
23
|
+
WHERE resource_kind = ${resourceKind} AND resource_id = ${resourceId}
|
|
24
|
+
) AS allowed`,
|
|
25
|
+
values: builder.values,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** Rows retain the actual persisted kind, including on nested arrow edges. */
|
|
29
|
+
export function compileSharedPointRows(model, builder, binding) {
|
|
30
|
+
const compiled = compileSharedPointPlan(model, builder, binding);
|
|
31
|
+
return `${compiled.withClause}
|
|
32
|
+
SELECT resource_kind, resource_id FROM ${compiled.allowed}`;
|
|
33
|
+
}
|
|
34
|
+
function compileSharedPointPlan(model, builder, binding) {
|
|
35
|
+
const compiler = new SharedPointCompiler(model, builder, binding.namespace);
|
|
36
|
+
const allowed = compiler.permission(binding.resourceEntity, binding.permission);
|
|
37
|
+
const reachable = subjectReachability(binding.namespace, binding.subjectKind, binding.subjectId);
|
|
38
|
+
return {
|
|
39
|
+
withClause: `WITH RECURSIVE ${reachable}, ${compiler.queries.join(",\n")}`,
|
|
40
|
+
allowed,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
class SharedPointCompiler {
|
|
44
|
+
model;
|
|
45
|
+
builder;
|
|
46
|
+
namespace;
|
|
47
|
+
queries = [];
|
|
48
|
+
#compiled = new Map();
|
|
49
|
+
#active = new Set();
|
|
50
|
+
constructor(model, builder, namespace) {
|
|
51
|
+
this.model = model;
|
|
52
|
+
this.builder = builder;
|
|
53
|
+
this.namespace = namespace;
|
|
54
|
+
}
|
|
55
|
+
permission(entity, name) {
|
|
56
|
+
const definition = this.model.permissions.find((candidate) => candidate.entity === entity && candidate.wireName === name);
|
|
57
|
+
if (!definition)
|
|
58
|
+
throw new Error(`Postgres policy cannot find permission ${name}.`);
|
|
59
|
+
const identity = `${entity}\u0000${name}`;
|
|
60
|
+
if (this.#active.has(identity))
|
|
61
|
+
throw new Error("Postgres SQL compilation does not support a cycle through permissions.");
|
|
62
|
+
this.#active.add(identity);
|
|
63
|
+
try {
|
|
64
|
+
return this.#expression(entity, definition.expression);
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
this.#active.delete(identity);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
#expression(entity, expression) {
|
|
71
|
+
let expressions = this.#compiled.get(entity);
|
|
72
|
+
if (expressions === undefined) {
|
|
73
|
+
expressions = new Map();
|
|
74
|
+
this.#compiled.set(entity, expressions);
|
|
75
|
+
}
|
|
76
|
+
const compiled = expressions.get(expression);
|
|
77
|
+
if (compiled !== undefined)
|
|
78
|
+
return compiled;
|
|
79
|
+
const body = this.#body(entity, expression);
|
|
80
|
+
const alias = `point_policy_${String(this.queries.length)}`;
|
|
81
|
+
this.queries.push(`${alias}(resource_kind, resource_id) AS MATERIALIZED (${body})`);
|
|
82
|
+
expressions.set(expression, alias);
|
|
83
|
+
return alias;
|
|
84
|
+
}
|
|
85
|
+
#body(entity, expression) {
|
|
86
|
+
if (expression.kind === "relation") {
|
|
87
|
+
const relation = this.builder.value(expression.relation);
|
|
88
|
+
return `SELECT resource_kind, resource_id FROM subject_reachable WHERE relation = ${relation}`;
|
|
89
|
+
}
|
|
90
|
+
if (expression.kind === "through") {
|
|
91
|
+
return this.#through(entity, expression.relation, expression.permission);
|
|
92
|
+
}
|
|
93
|
+
if (expression.kind === "exclude") {
|
|
94
|
+
const include = this.#expression(entity, expression.include);
|
|
95
|
+
const exclude = this.#expression(entity, expression.exclude);
|
|
96
|
+
return `SELECT * FROM ${include} EXCEPT SELECT * FROM ${exclude}`;
|
|
97
|
+
}
|
|
98
|
+
const operator = expression.kind === "any-of" ? "UNION" : "INTERSECT";
|
|
99
|
+
const children = new Set(expression.expressions.map((child) => this.#expression(entity, child)));
|
|
100
|
+
return [...children]
|
|
101
|
+
.map((child) => `SELECT * FROM ${child}`)
|
|
102
|
+
.join(` ${operator} `);
|
|
103
|
+
}
|
|
104
|
+
#through(entity, name, permission) {
|
|
105
|
+
const related = relatedEntity(this.model, entity, name);
|
|
106
|
+
const child = this.permission(related, permission);
|
|
107
|
+
const relation = this.builder.value(name);
|
|
108
|
+
// A stored direct edge determines the actual target kind in a point
|
|
109
|
+
// check. Keep both columns through the set algebra and constrain the
|
|
110
|
+
// final kind only at the requested resource.
|
|
111
|
+
return `SELECT DISTINCT parent.resource_kind, parent.resource_id
|
|
112
|
+
FROM ${child} reached
|
|
113
|
+
CROSS JOIN LATERAL (
|
|
114
|
+
SELECT tuple.resource_kind, tuple.resource_id
|
|
115
|
+
FROM ${TUPLES} tuple
|
|
116
|
+
WHERE tuple.namespace = ${this.namespace}
|
|
117
|
+
AND tuple.subject_kind = reached.resource_kind
|
|
118
|
+
AND tuple.subject_id = reached.resource_id
|
|
119
|
+
AND tuple.relation = ${relation}
|
|
120
|
+
AND tuple.subject_is_wildcard = false
|
|
121
|
+
AND tuple.subject_relation = ''
|
|
122
|
+
OFFSET 0
|
|
123
|
+
) parent`;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=compile-shared-point-check.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile-shared-point-check.js","sourceRoot":"","sources":["../../src/policy/compile-shared-point-check.ts"],"names":[],"mappings":"AAMA,OAAO,EACL,YAAY,EACZ,aAAa,EACb,mBAAmB,EACnB,MAAM,GACP,MAAM,iBAAiB,CAAC;AAEzB,sFAAsF;AACtF,MAAM,UAAU,uBAAuB,CACrC,KAAmC,EACnC,SAAiB,EACjB,KAAgC;IAEhC,MAAM,OAAO,GAAG,IAAI,YAAY,EAAE,CAAC;IACnC,MAAM,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IAC9C,MAAM,WAAW,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACtD,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAClD,MAAM,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACxD,MAAM,UAAU,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IACpD,MAAM,QAAQ,GAAG,sBAAsB,CAAC,KAAK,EAAE,OAAO,EAAE;QACtD,SAAS,EAAE,YAAY;QACvB,WAAW;QACX,SAAS;QACT,cAAc,EAAE,KAAK,CAAC,QAAQ,CAAC,IAAI;QACnC,UAAU,EAAE,KAAK,CAAC,UAAU;KAC7B,CAAC,CAAC;IACH,OAAO;QACL,IAAI,EAAE,OAAO;QACb,UAAU,EAAE,KAAK;QACjB,IAAI,EAAE,GAAG,QAAQ,CAAC,UAAU;;wBAER,QAAQ,CAAC,OAAO;gCACR,YAAY,sBAAsB,UAAU;mBACzD;QACf,MAAM,EAA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C,IAAI,CAAC,IAAI,QAAQ,GAAG,CAAC,CAAC;IAC3B,CAAC;IAED,QAAQ,CAAC,MAAc,EAAE,IAAY,EAAE,UAAkB;QACvD,MAAM,OAAO,GAAG,aAAa,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;QACxD,MAAM,KAAK,GAAG,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;QACnD,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAE1C,oEAAoE;QACpE,qEAAqE;QACrE,6CAA6C;QAC7C,OAAO;aACE,KAAK;;;eAGH,MAAM;kCACa,IAAI,CAAC,SAAS;;;iCAGf,QAAQ;;;;eAI1B,CAAC;IACd,CAAC;CACF"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export type CompiledPermissionExpression = {
|
|
2
|
+
readonly kind: "relation";
|
|
3
|
+
readonly relation: string;
|
|
4
|
+
} | {
|
|
5
|
+
readonly kind: "any-of" | "all-of";
|
|
6
|
+
readonly expressions: readonly CompiledPermissionExpression[];
|
|
7
|
+
} | {
|
|
8
|
+
readonly kind: "through";
|
|
9
|
+
readonly relation: string;
|
|
10
|
+
readonly permission: string;
|
|
11
|
+
} | {
|
|
12
|
+
readonly kind: "exclude";
|
|
13
|
+
readonly include: CompiledPermissionExpression;
|
|
14
|
+
readonly exclude: CompiledPermissionExpression;
|
|
15
|
+
};
|
|
16
|
+
export type CompiledAuthorizationEntity = {
|
|
17
|
+
readonly sourceName: string;
|
|
18
|
+
readonly wireName: string;
|
|
19
|
+
};
|
|
20
|
+
export type CompiledAuthorizationRelationTarget = {
|
|
21
|
+
readonly kind: "entity";
|
|
22
|
+
readonly entity: string;
|
|
23
|
+
} | {
|
|
24
|
+
readonly kind: "wildcard";
|
|
25
|
+
readonly entity: string;
|
|
26
|
+
} | {
|
|
27
|
+
readonly kind: "subject-set";
|
|
28
|
+
readonly entity: string;
|
|
29
|
+
readonly relation: string;
|
|
30
|
+
};
|
|
31
|
+
export type CompiledAuthorizationRelation = {
|
|
32
|
+
readonly authority: "system" | "grantable";
|
|
33
|
+
readonly entity: string;
|
|
34
|
+
readonly sourceName: string;
|
|
35
|
+
readonly wireName: string;
|
|
36
|
+
readonly targets: readonly CompiledAuthorizationRelationTarget[];
|
|
37
|
+
/** Present only for RBAC role declarations; absence remains an ordinary relation. */
|
|
38
|
+
readonly definitionType?: "role";
|
|
39
|
+
readonly grantPermission?: CompiledGrantPermission;
|
|
40
|
+
readonly revokePermission?: CompiledGrantPermission;
|
|
41
|
+
readonly grantScopeRelation?: string;
|
|
42
|
+
};
|
|
43
|
+
export type CompiledGrantPermission = {
|
|
44
|
+
readonly entity: string;
|
|
45
|
+
readonly permission: string;
|
|
46
|
+
};
|
|
47
|
+
export type CompiledAuthorizationPermission = {
|
|
48
|
+
readonly entity: string;
|
|
49
|
+
readonly sourceName: string;
|
|
50
|
+
readonly wireName: string;
|
|
51
|
+
readonly expression: CompiledPermissionExpression;
|
|
52
|
+
};
|
|
53
|
+
/** Internal, lowered SQL model. Access-kit definitions are the public model. */
|
|
54
|
+
export type AuthorizationModelDefinition = {
|
|
55
|
+
readonly entities: readonly CompiledAuthorizationEntity[];
|
|
56
|
+
readonly relations: readonly CompiledAuthorizationRelation[];
|
|
57
|
+
readonly permissions: readonly CompiledAuthorizationPermission[];
|
|
58
|
+
readonly lifecycle: readonly {
|
|
59
|
+
readonly name: string;
|
|
60
|
+
}[];
|
|
61
|
+
};
|
|
62
|
+
export type NormalizedAuthorizationReference = {
|
|
63
|
+
readonly type: string;
|
|
64
|
+
readonly id: string;
|
|
65
|
+
};
|
|
66
|
+
export type NormalizedRelationship = {
|
|
67
|
+
readonly subject: (NormalizedAuthorizationReference & {
|
|
68
|
+
readonly relation?: string;
|
|
69
|
+
}) | {
|
|
70
|
+
readonly type: string;
|
|
71
|
+
readonly wildcard: true;
|
|
72
|
+
};
|
|
73
|
+
readonly relation: string;
|
|
74
|
+
readonly resource: NormalizedAuthorizationReference;
|
|
75
|
+
};
|
|
76
|
+
export type NormalizedPermissionCheck = {
|
|
77
|
+
readonly subject: NormalizedAuthorizationReference;
|
|
78
|
+
readonly permission: string;
|
|
79
|
+
readonly resource: NormalizedAuthorizationReference;
|
|
80
|
+
};
|
|
81
|
+
export type NormalizedResourceLookup = {
|
|
82
|
+
readonly subject: NormalizedAuthorizationReference;
|
|
83
|
+
readonly permission: string;
|
|
84
|
+
readonly resourceType: string;
|
|
85
|
+
};
|
|
86
|
+
//# sourceMappingURL=policy-model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy-model.d.ts","sourceRoot":"","sources":["../../src/policy/policy-model.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,4BAA4B,GACpC;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GACxD;IACE,QAAQ,CAAC,IAAI,EAAE,QAAQ,GAAG,QAAQ,CAAC;IACnC,QAAQ,CAAC,WAAW,EAAE,SAAS,4BAA4B,EAAE,CAAC;CAC/D,GACD;IACE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B,GACD;IACE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,OAAO,EAAE,4BAA4B,CAAC;IAC/C,QAAQ,CAAC,OAAO,EAAE,4BAA4B,CAAC;CAChD,CAAC;AAEN,MAAM,MAAM,2BAA2B,GAAG;IACxC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,mCAAmC,GAC3C;IAAE,QAAQ,CAAC,IA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y-model.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type AuthorizationModelDefinition } from "./policy-model.js";
|
|
2
|
+
import type { PostgresValue } from "../postgres-types.js";
|
|
3
|
+
/** The single relationship table every compiled policy statement reads. */
|
|
4
|
+
export declare const TUPLES = "dad_authorization_relationship";
|
|
5
|
+
/**
|
|
6
|
+
* Collects every literal a compiled statement needs as an ordinal parameter and
|
|
7
|
+
* hands out the distinct table aliases a nested `through` traversal joins on.
|
|
8
|
+
*/
|
|
9
|
+
export declare class QueryBuilder {
|
|
10
|
+
#private;
|
|
11
|
+
readonly values: PostgresValue[];
|
|
12
|
+
value(value: PostgresValue): string;
|
|
13
|
+
get throughCount(): number;
|
|
14
|
+
throughAlias(): string;
|
|
15
|
+
}
|
|
16
|
+
/** Resolves the one entity a `through` relation may hop to. */
|
|
17
|
+
export declare function relatedEntity(model: AuthorizationModelDefinition, entity: string, relation: string): string;
|
|
18
|
+
/** Reach all relationship usersets containing a concrete subject. */
|
|
19
|
+
export declare function subjectReachability(namespace: string, subjectKind: string, subjectId: string): string;
|
|
20
|
+
//# sourceMappingURL=policy-sql.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy-sql.d.ts","sourceRoot":"","sources":["../../src/policy/policy-sql.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,4BAA4B,EAAE,MAAM,mBAAmB,CAAC;AACtE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE1D,2EAA2E;AAC3E,eAAO,MAAM,MAAM,mCAAmC,CAAC;AAEvD;;;GAGG;AACH,qBAAa,YAAY;;IACvB,QAAQ,CAAC,MAAM,EAAE,aAAa,EAAE,CAAM;IAGtC,KAAK,CAAC,KAAK,EAAE,aAAa,GAAG,MAAM,CAGlC;IAED,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,YAAY,IAAI,MAAM,CAIrB;CACF;AAED,+DAA+D;AAC/D,wBAAgB,aAAa,CAC3B,KAAK,EAAE,4BAA4B,EACnC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,GACf,MAAM,CAgBR;AAED,qEAAqE;AACrE,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,MAAM,GAChB,MAAM,CAsBR"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {} from "./policy-model.js";
|
|
2
|
+
/** The single relationship table every compiled policy statement reads. */
|
|
3
|
+
export const TUPLES = "dad_authorization_relationship";
|
|
4
|
+
/**
|
|
5
|
+
* Collects every literal a compiled statement needs as an ordinal parameter and
|
|
6
|
+
* hands out the distinct table aliases a nested `through` traversal joins on.
|
|
7
|
+
*/
|
|
8
|
+
export class QueryBuilder {
|
|
9
|
+
values = [];
|
|
10
|
+
#throughAliasCount = 0;
|
|
11
|
+
value(value) {
|
|
12
|
+
this.values.push(value);
|
|
13
|
+
return `$${String(this.values.length)}`;
|
|
14
|
+
}
|
|
15
|
+
get throughCount() {
|
|
16
|
+
return this.#throughAliasCount;
|
|
17
|
+
}
|
|
18
|
+
throughAlias() {
|
|
19
|
+
const alias = `through_tuple_${String(this.#throughAliasCount)}`;
|
|
20
|
+
this.#throughAliasCount += 1;
|
|
21
|
+
return alias;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Resolves the one entity a `through` relation may hop to. */
|
|
25
|
+
export function relatedEntity(model, entity, relation) {
|
|
26
|
+
const definition = model.relations.find((candidate) => candidate.entity === entity && candidate.wireName === relation);
|
|
27
|
+
const targets = definition?.targets.filter((target) => target.kind === "entity");
|
|
28
|
+
if (!targets || targets.length !== 1)
|
|
29
|
+
throw new Error(`Postgres policy needs one entity target for through relation ${entity}.${relation}.`);
|
|
30
|
+
const target = targets[0];
|
|
31
|
+
if (!target)
|
|
32
|
+
throw new Error("Postgres policy is missing a through relation target.");
|
|
33
|
+
return target.entity;
|
|
34
|
+
}
|
|
35
|
+
/** Reach all relationship usersets containing a concrete subject. */
|
|
36
|
+
export function subjectReachability(namespace, subjectKind, subjectId) {
|
|
37
|
+
return `subject_reachable(resource_kind, resource_id, relation) AS (
|
|
38
|
+
SELECT direct_tuple.resource_kind, direct_tuple.resource_id, direct_tuple.relation
|
|
39
|
+
FROM ${TUPLES} direct_tuple
|
|
40
|
+
WHERE direct_tuple.namespace = ${namespace}
|
|
41
|
+
AND direct_tuple.subject_relation = ''
|
|
42
|
+
AND ((direct_tuple.subject_is_wildcard = false AND direct_tuple.subject_kind = ${subjectKind} AND direct_tuple.subject_id = ${subjectId}) OR (direct_tuple.subject_is_wildcard = true AND direct_tuple.subject_kind = ${subjectKind}))
|
|
43
|
+
UNION
|
|
44
|
+
SELECT parent.resource_kind, parent.resource_id, parent.relation
|
|
45
|
+
FROM subject_reachable child_relation
|
|
46
|
+
CROSS JOIN LATERAL (
|
|
47
|
+
SELECT tuple.resource_kind, tuple.resource_id, tuple.relation
|
|
48
|
+
FROM ${TUPLES} tuple
|
|
49
|
+
WHERE tuple.namespace = ${namespace}
|
|
50
|
+
AND tuple.subject_kind = child_relation.resource_kind
|
|
51
|
+
AND tuple.subject_id = child_relation.resource_id
|
|
52
|
+
AND tuple.subject_relation = child_relation.relation
|
|
53
|
+
AND tuple.subject_is_wildcard = false
|
|
54
|
+
AND tuple.subject_relation <> ''
|
|
55
|
+
OFFSET 0
|
|
56
|
+
) parent
|
|
57
|
+
)`;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=policy-sql.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy-sql.js","sourceRoot":"","sources":["../../src/policy/policy-sql.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqC,MAAM,mBAAmB,CAAC;AAGtE,2EAA2E;AAC3E,MAAM,CAAC,MAAM,MAAM,GAAG,gCAAgC,CAAC;AAEvD;;;GAGG;AACH,MAAM,OAAO,YAAY;IACd,MA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