@agentic-kit/db-tools 0.12.46 → 0.13.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 +18 -4
- package/access/findings.d.ts +93 -0
- package/access/findings.js +434 -0
- package/access/index.d.ts +5 -0
- package/access/index.js +27 -0
- package/access/policies.d.ts +142 -0
- package/access/policies.js +338 -0
- package/access/provisioning.d.ts +121 -0
- package/access/provisioning.js +381 -0
- package/access/rule-check.d.ts +52 -0
- package/access/rule-check.js +307 -0
- package/access/words.d.ts +38 -0
- package/access/words.js +530 -0
- package/app-workspace.d.ts +1 -0
- package/app-workspace.js +7 -9
- package/blueprint/blueprint-defaults.d.ts +9 -0
- package/blueprint/blueprint-defaults.js +77 -0
- package/blueprint/blueprint-schema.d.ts +68 -0
- package/blueprint/blueprint-schema.js +139 -0
- package/blueprint/field-type.d.ts +4 -0
- package/blueprint/field-type.js +100 -0
- package/blueprint/internal-policies.d.ts +1 -0
- package/blueprint/internal-policies.js +10 -0
- package/blueprint-construction.d.ts +10 -6
- package/blueprint-construction.js +34 -25
- package/context.d.ts +15 -0
- package/context.js +132 -69
- package/db-probe.d.ts +1 -1
- package/db-probe.js +19 -4
- package/esm/access/findings.d.ts +93 -0
- package/esm/access/findings.js +428 -0
- package/esm/access/index.d.ts +5 -0
- package/esm/access/index.js +5 -0
- package/esm/access/policies.d.ts +142 -0
- package/esm/access/policies.js +315 -0
- package/esm/access/provisioning.d.ts +121 -0
- package/esm/access/provisioning.js +375 -0
- package/esm/access/rule-check.d.ts +52 -0
- package/esm/access/rule-check.js +297 -0
- package/esm/access/words.d.ts +38 -0
- package/esm/access/words.js +524 -0
- package/esm/app-workspace.d.ts +1 -0
- package/esm/app-workspace.js +7 -10
- package/esm/blueprint/blueprint-defaults.d.ts +9 -0
- package/esm/blueprint/blueprint-defaults.js +74 -0
- package/esm/blueprint/blueprint-schema.d.ts +68 -0
- package/esm/blueprint/blueprint-schema.js +134 -0
- package/esm/blueprint/field-type.d.ts +4 -0
- package/esm/blueprint/field-type.js +95 -0
- package/esm/blueprint/internal-policies.d.ts +1 -0
- package/esm/blueprint/internal-policies.js +7 -0
- package/esm/blueprint-construction.d.ts +10 -6
- package/esm/blueprint-construction.js +32 -24
- package/esm/context.d.ts +15 -0
- package/esm/context.js +133 -70
- package/esm/db-probe.d.ts +1 -1
- package/esm/db-probe.js +21 -6
- package/esm/failure.d.ts +89 -0
- package/esm/failure.js +362 -0
- package/esm/gate.d.ts +6 -4
- package/esm/gate.js +42 -14
- package/esm/graphql-http.d.ts +27 -6
- package/esm/graphql-http.js +96 -24
- package/esm/host.d.ts +33 -8
- package/esm/host.js +42 -2
- package/esm/index.d.ts +22 -12
- package/esm/index.js +21 -39
- package/esm/provision-database/env-file.d.ts +7 -0
- package/esm/provision-database/env-file.js +30 -0
- package/esm/provision-database/preset-match.js +1 -2
- package/esm/provision-database/provision.d.ts +21 -4
- package/esm/provision-database/provision.js +330 -85
- package/esm/provision-database/request-database.d.ts +22 -3
- package/esm/provision-database/request-database.js +46 -13
- package/esm/ready.d.ts +20 -0
- package/esm/ready.js +45 -0
- package/esm/run-codegen/barrels.js +1 -1
- package/esm/run-codegen/endpoints.js +5 -6
- package/esm/schema-model/normalize.js +1 -1
- package/esm/schema-model.js +4 -5
- package/esm/schema-resolve.d.ts +50 -0
- package/esm/schema-resolve.js +200 -14
- package/esm/table-access.d.ts +116 -0
- package/esm/table-access.js +418 -0
- package/esm/tools/access.d.ts +218 -0
- package/esm/tools/access.js +651 -0
- package/esm/tools/add-policies.d.ts +44 -12
- package/esm/tools/add-policies.js +84 -84
- package/esm/tools/add-records.d.ts +23 -2
- package/esm/tools/add-records.js +113 -75
- package/esm/tools/add-relation-schema.d.ts +5 -5
- package/esm/tools/add-relation-schema.js +13 -14
- package/esm/tools/add-relation.d.ts +11 -2
- package/esm/tools/add-relation.js +68 -31
- package/esm/tools/create-api-key.d.ts +18 -4
- package/esm/tools/create-api-key.js +105 -54
- package/esm/tools/describe-schema.d.ts +5 -7
- package/esm/tools/describe-schema.js +41 -118
- package/esm/tools/entity-types.d.ts +54 -0
- package/esm/tools/entity-types.js +233 -0
- package/esm/tools/index.d.ts +26 -0
- package/esm/tools/index.js +151 -0
- package/esm/tools/mutations.d.ts +36 -7
- package/esm/tools/mutations.js +245 -99
- package/esm/tools/provision-blueprint.d.ts +25 -1
- package/esm/tools/provision-blueprint.js +184 -42
- package/esm/tools/provision-database.d.ts +7 -5
- package/esm/tools/provision-database.js +31 -17
- package/esm/tools/run-codegen.d.ts +27 -4
- package/esm/tools/run-codegen.js +143 -76
- package/esm/tools/templates.d.ts +30 -7
- package/esm/tools/templates.js +166 -95
- package/failure.d.ts +89 -0
- package/failure.js +375 -0
- package/gate.d.ts +6 -4
- package/gate.js +41 -13
- package/graphql-http.d.ts +27 -6
- package/graphql-http.js +105 -25
- package/host.d.ts +33 -8
- package/host.js +45 -2
- package/index.d.ts +22 -12
- package/index.js +86 -43
- package/package.json +6 -5
- package/provision-database/env-file.d.ts +7 -0
- package/provision-database/env-file.js +32 -1
- package/provision-database/preset-match.js +1 -2
- package/provision-database/provision.d.ts +21 -4
- package/provision-database/provision.js +327 -81
- package/provision-database/request-database.d.ts +22 -3
- package/provision-database/request-database.js +49 -14
- package/ready.d.ts +20 -0
- package/ready.js +52 -0
- package/run-codegen/barrels.js +1 -1
- package/run-codegen/endpoints.js +5 -6
- package/schema-model/normalize.js +4 -4
- package/schema-model.js +4 -5
- package/schema-resolve.d.ts +50 -0
- package/schema-resolve.js +209 -14
- package/table-access.d.ts +116 -0
- package/table-access.js +437 -0
- package/tools/access.d.ts +218 -0
- package/tools/access.js +669 -0
- package/tools/add-policies.d.ts +44 -12
- package/tools/add-policies.js +86 -84
- package/tools/add-records.d.ts +23 -2
- package/tools/add-records.js +113 -74
- package/tools/add-relation-schema.d.ts +5 -5
- package/tools/add-relation-schema.js +13 -14
- package/tools/add-relation.d.ts +11 -2
- package/tools/add-relation.js +69 -31
- package/tools/create-api-key.d.ts +18 -4
- package/tools/create-api-key.js +104 -53
- package/tools/describe-schema.d.ts +5 -7
- package/tools/describe-schema.js +40 -117
- package/tools/entity-types.d.ts +54 -0
- package/tools/entity-types.js +240 -0
- package/tools/index.d.ts +26 -0
- package/tools/index.js +154 -0
- package/tools/mutations.d.ts +36 -7
- package/tools/mutations.js +252 -101
- package/tools/provision-blueprint.d.ts +25 -1
- package/tools/provision-blueprint.js +186 -43
- package/tools/provision-database.d.ts +7 -5
- package/tools/provision-database.js +33 -18
- package/tools/run-codegen.d.ts +27 -4
- package/tools/run-codegen.js +141 -73
- package/tools/templates.d.ts +30 -7
- package/tools/templates.js +167 -94
- package/esm/policy/add-policies-to-table.d.ts +0 -26
- package/esm/policy/add-policies-to-table.js +0 -66
- package/esm/policy/provision-helpers.d.ts +0 -38
- package/esm/policy/provision-helpers.js +0 -50
- package/esm/tools/manage-entity-types.d.ts +0 -40
- package/esm/tools/manage-entity-types.js +0 -201
- package/policy/add-policies-to-table.d.ts +0 -26
- package/policy/add-policies-to-table.js +0 -71
- package/policy/provision-helpers.d.ts +0 -38
- package/policy/provision-helpers.js +0 -58
- package/tools/manage-entity-types.d.ts +0 -40
- package/tools/manage-entity-types.js +0 -206
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
// The rules of one table in one shape: stored policies and a caller's blueprint policies, their normal form (registry
|
|
2
|
+
// defaults in, keys the backend derives out), their expression trees, and the comparison the read-back after an
|
|
3
|
+
// access change makes.
|
|
4
|
+
import { listed, nearestName } from '@agentic-kit/harness';
|
|
5
|
+
import { allNodeTypes } from 'node-type-registry';
|
|
6
|
+
export const OPERATIONS = ['read', 'create', 'update', 'delete'];
|
|
7
|
+
export const PRIVILEGE = { read: 'select', create: 'insert', update: 'update', delete: 'delete' };
|
|
8
|
+
export const PRIVILEGES = OPERATIONS.map((operation) => PRIVILEGE[operation]);
|
|
9
|
+
export const SIGNED_IN = 'authenticated';
|
|
10
|
+
/** PostgreSQL's PUBLIC: a grant or a policy for it applies to every role. */
|
|
11
|
+
export const PUBLIC = 'public';
|
|
12
|
+
export const AUTHZ_NODES = new Map(allNodeTypes.filter((node) => node.category === 'authz').map((node) => [node.name, node]));
|
|
13
|
+
const COMPOSITE = 'AuthzComposite';
|
|
14
|
+
const COMBINATORS = ['AND', 'OR', 'NOT'];
|
|
15
|
+
const BOOL_OPS = { AND_EXPR: 'and', OR_EXPR: 'or', NOT_EXPR: 'not' };
|
|
16
|
+
const TEAM_KIND = ['membership_type', 'entity_type'];
|
|
17
|
+
const SPRT = ['sprt_schema', 'sprt_table'];
|
|
18
|
+
// The policy parser removes these from every rule before it stores it.
|
|
19
|
+
const STRIPPED_KEYS = new Set(['rls_role_schema', 'rls_role', 'current_user_ast']);
|
|
20
|
+
/**
|
|
21
|
+
* Keys the backend writes from other keys when it stores a rule. They carry no meaning of their own, so two rules that
|
|
22
|
+
* differ only in them mean the same. The member list (sprt_*) comes from the team kind, and only when a rule gives one;
|
|
23
|
+
* without it the backend keeps a given member list, which then decides who passes. An app rule's membership_type is
|
|
24
|
+
* derived only when it is the app's own (1): another one names the member list the rule reads.
|
|
25
|
+
*/
|
|
26
|
+
export const DERIVED_KEYS = {
|
|
27
|
+
AuthzAppMemberOwner: [...SPRT, 'bitlen', 'membership_type'],
|
|
28
|
+
AuthzAppMembership: [...SPRT, 'bitlen', 'membership_type'],
|
|
29
|
+
AuthzEntityMembership: [...SPRT, 'bitlen', 'mask_table'],
|
|
30
|
+
AuthzMemberOwner: [...SPRT, 'bitlen'],
|
|
31
|
+
AuthzNotReadOnly: SPRT,
|
|
32
|
+
AuthzOrgHierarchy: SPRT,
|
|
33
|
+
AuthzPeerOwnership: [...SPRT, 'bitlen'],
|
|
34
|
+
AuthzRelatedEntityMembership: [...SPRT, 'bitlen'],
|
|
35
|
+
AuthzRelatedMemberOwner: [...SPRT, 'bitlen'],
|
|
36
|
+
AuthzRelatedPeerOwnership: [...SPRT, 'bitlen'],
|
|
37
|
+
AuthzValueExists: ['protected_table_name'],
|
|
38
|
+
AuthzValueMatch: ['protected_table_name']
|
|
39
|
+
};
|
|
40
|
+
// A table or column a rule names by id: the backend writes its schema and name beside the id when it stores the rule.
|
|
41
|
+
const NAMED_BY_ID = [
|
|
42
|
+
['obj_table_id', ['obj_schema', 'obj_table']],
|
|
43
|
+
['obj_field_id', ['obj_field']],
|
|
44
|
+
['ref_table_id', ['ref_schema', 'ref_table']],
|
|
45
|
+
['owned_table_id', ['owned_schema', 'owned_table']],
|
|
46
|
+
['shares_table_id', ['shares_schema', 'shares_table']],
|
|
47
|
+
['files_table_id', ['files_schema', 'files_table']]
|
|
48
|
+
];
|
|
49
|
+
// The conditions of a value rule: the backend stores them compiled (conditions_ast), so a plan and its stored rule share
|
|
50
|
+
// only that the rule has some.
|
|
51
|
+
const CONDITIONS = ['conditions', 'conditions_ast'];
|
|
52
|
+
const COMPILED_CONDITIONS = { compiled: true };
|
|
53
|
+
const VALUE_RULES = new Set(['AuthzValueExists', 'AuthzValueMatch']);
|
|
54
|
+
const NEEDS_TEAM_KIND = new Set(['AuthzEntityMembership', 'AuthzMemberOwner', 'AuthzNotReadOnly', 'AuthzPeerOwnership', 'AuthzRelatedEntityMembership', 'AuthzRelatedMemberOwner', 'AuthzRelatedPeerOwnership']);
|
|
55
|
+
const APP_RULES = new Set(['AuthzAppMembership', 'AuthzAppMemberOwner']);
|
|
56
|
+
export const APP_MEMBERSHIP_TYPE = 1;
|
|
57
|
+
/**
|
|
58
|
+
* The keys of this rule's data the backend derived; a member list counts as derived only beside a team kind, an app
|
|
59
|
+
* rule's membership_type only when it is the app's, and a table's schema and name only beside the table's id.
|
|
60
|
+
*/
|
|
61
|
+
export function derivedKeys(type, data) {
|
|
62
|
+
const teamKind = TEAM_KIND.some((key) => data?.[key] !== undefined && data?.[key] !== null);
|
|
63
|
+
const appKind = data?.membership_type === undefined || data?.membership_type === APP_MEMBERSHIP_TYPE;
|
|
64
|
+
const listed = (DERIVED_KEYS[type] ?? []).filter((key) => {
|
|
65
|
+
if (key === 'membership_type' && APP_RULES.has(type))
|
|
66
|
+
return appKind;
|
|
67
|
+
return !SPRT.includes(key) || !NEEDS_TEAM_KIND.has(type) || teamKind;
|
|
68
|
+
});
|
|
69
|
+
const named = NAMED_BY_ID.flatMap(([id, keys]) => (data?.[id] !== undefined && data?.[id] !== null ? keys : []));
|
|
70
|
+
return new Set([...listed, ...named]);
|
|
71
|
+
}
|
|
72
|
+
// A value rule's data, or its nested `exists`, with its conditions in the one form a plan and its stored rule share.
|
|
73
|
+
function sharedConditions(data) {
|
|
74
|
+
const kept = Object.fromEntries(Object.entries(data).filter(([key]) => !CONDITIONS.includes(key)));
|
|
75
|
+
return CONDITIONS.some((key) => data[key] !== undefined && data[key] !== null) ? { ...kept, conditions: COMPILED_CONDITIONS } : kept;
|
|
76
|
+
}
|
|
77
|
+
// A value rule's nested `exists` without the keys the backend derives in it.
|
|
78
|
+
const normalExists = (value) => {
|
|
79
|
+
const derived = NAMED_BY_ID.flatMap(([id, keys]) => (value[id] !== undefined && value[id] !== null ? keys : []));
|
|
80
|
+
return sharedConditions(Object.fromEntries(Object.entries(value).filter(([key]) => !derived.includes(key))));
|
|
81
|
+
};
|
|
82
|
+
export function isObject(value) {
|
|
83
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
84
|
+
}
|
|
85
|
+
export function sorted(value) {
|
|
86
|
+
if (Array.isArray(value))
|
|
87
|
+
return value.map(sorted);
|
|
88
|
+
if (!isObject(value))
|
|
89
|
+
return value;
|
|
90
|
+
return Object.fromEntries(Object.keys(value).sort().map((key) => [key, sorted(value[key])]));
|
|
91
|
+
}
|
|
92
|
+
export function unique(items, keyOf) {
|
|
93
|
+
const seen = new Set();
|
|
94
|
+
return items.filter((item) => !seen.has(keyOf(item)) && seen.add(keyOf(item)));
|
|
95
|
+
}
|
|
96
|
+
/** Each parameter's registry default. */
|
|
97
|
+
export function defaultsOf(type) {
|
|
98
|
+
const values = {};
|
|
99
|
+
for (const [param, schema] of Object.entries(AUTHZ_NODES.get(type)?.parameter_schema.properties ?? {})) {
|
|
100
|
+
if (schema.default !== undefined)
|
|
101
|
+
values[param] = schema.default;
|
|
102
|
+
}
|
|
103
|
+
return values;
|
|
104
|
+
}
|
|
105
|
+
/** A blueprint policy as a table policy, with the defaults the provisioner applies. */
|
|
106
|
+
export function tablePolicyFromBlueprint(policy) {
|
|
107
|
+
return {
|
|
108
|
+
type: policy.$type,
|
|
109
|
+
privileges: (policy.privileges ?? PRIVILEGES),
|
|
110
|
+
permissive: policy.permissive ?? true,
|
|
111
|
+
role: policy.policy_role ?? SIGNED_IN,
|
|
112
|
+
data: policy.data ?? {},
|
|
113
|
+
...(policy.with_check ? { with_check: { type: policy.with_check.$type, data: policy.with_check.data ?? {} } } : {})
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/** The policy a rule stands for: a composite whose data is exactly one Authz node is that node. */
|
|
117
|
+
export function effectivePolicy(type, data) {
|
|
118
|
+
const keys = Object.keys(data ?? {});
|
|
119
|
+
if (type !== COMPOSITE || keys.length !== 1 || !AUTHZ_NODES.has(keys[0]) || !isObject(data[keys[0]]))
|
|
120
|
+
return { type, data };
|
|
121
|
+
return effectivePolicy(keys[0], data[keys[0]]);
|
|
122
|
+
}
|
|
123
|
+
// ─── Normal form ─────────────────────────────────────────────────
|
|
124
|
+
/** The two team kinds every database has. */
|
|
125
|
+
export const BUILT_IN_SCOPES = [
|
|
126
|
+
{ scope: 'app', membership_type: 1 },
|
|
127
|
+
{ scope: 'org', membership_type: 2 }
|
|
128
|
+
];
|
|
129
|
+
function normalNested(value) {
|
|
130
|
+
if (Array.isArray(value))
|
|
131
|
+
return value.map(normalNested);
|
|
132
|
+
if (!isObject(value))
|
|
133
|
+
return value;
|
|
134
|
+
const keys = Object.keys(value);
|
|
135
|
+
if (keys.length === 1 && AUTHZ_NODES.has(keys[0]) && isObject(value[keys[0]])) {
|
|
136
|
+
return { [keys[0]]: normalData(keys[0], value[keys[0]]) };
|
|
137
|
+
}
|
|
138
|
+
return Object.fromEntries(keys.map((key) => [key, normalNested(value[key])]));
|
|
139
|
+
}
|
|
140
|
+
// The backend removes entity_type once it has resolved it into membership_type.
|
|
141
|
+
const withoutEntityType = (data) => Object.fromEntries(Object.entries(data).filter(([key]) => key !== 'entity_type'));
|
|
142
|
+
// An entity_type every database has (app, org) is the membership_type the backend stores for it; another one needs the
|
|
143
|
+
// database's team kinds (resolveEntityTypes).
|
|
144
|
+
function builtInScope(data) {
|
|
145
|
+
const scope = BUILT_IN_SCOPES.find((entry) => entry.scope === data.entity_type);
|
|
146
|
+
if (!scope)
|
|
147
|
+
return data;
|
|
148
|
+
return { ...withoutEntityType(data), membership_type: scope.membership_type };
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Registry defaults filled in, and the keys the backend derives or strips left out: a plan and the stored policy it
|
|
152
|
+
* becomes have the same normal form.
|
|
153
|
+
*/
|
|
154
|
+
export function normalData(type, given) {
|
|
155
|
+
const data = builtInScope(given ?? {});
|
|
156
|
+
const derived = derivedKeys(type, data);
|
|
157
|
+
const schema = AUTHZ_NODES.get(type)?.parameter_schema.properties ?? {};
|
|
158
|
+
const normal = defaultsOf(type);
|
|
159
|
+
for (const [key, value] of Object.entries(data)) {
|
|
160
|
+
// The builders test a flag with IS TRUE, so false with no default is the same as leaving it out.
|
|
161
|
+
const off = value === false && schema[key]?.default === undefined;
|
|
162
|
+
if (STRIPPED_KEYS.has(key) || derived.has(key) || off)
|
|
163
|
+
continue;
|
|
164
|
+
normal[key] = key === 'exists' && VALUE_RULES.has(type) && isObject(value) ? normalExists(value) : normalNested(value);
|
|
165
|
+
}
|
|
166
|
+
return VALUE_RULES.has(type) ? sharedConditions(normal) : normal;
|
|
167
|
+
}
|
|
168
|
+
const RAW = { kind: 'raw' };
|
|
169
|
+
export function exprOf(type, data) {
|
|
170
|
+
if (type === COMPOSITE)
|
|
171
|
+
return treeOf(data);
|
|
172
|
+
return { kind: 'leaf', type, data: normalData(type, data ?? {}) };
|
|
173
|
+
}
|
|
174
|
+
// The composite's tree: one key per level. A stored rule may hold a stored boolean expression, read by its operator.
|
|
175
|
+
export function treeOf(value) {
|
|
176
|
+
if (!isObject(value))
|
|
177
|
+
return RAW;
|
|
178
|
+
const keys = Object.keys(value);
|
|
179
|
+
if (keys.length !== 1)
|
|
180
|
+
return RAW;
|
|
181
|
+
const [key] = keys;
|
|
182
|
+
const inner = value[key];
|
|
183
|
+
if (key === 'AND' || key === 'OR') {
|
|
184
|
+
return Array.isArray(inner) && inner.length > 0 ? { kind: key === 'AND' ? 'and' : 'or', args: inner.map(treeOf) } : RAW;
|
|
185
|
+
}
|
|
186
|
+
if (key === 'NOT')
|
|
187
|
+
return isObject(inner) ? { kind: 'not', arg: treeOf(inner) } : RAW;
|
|
188
|
+
if (key === 'BoolExpr' && isObject(inner) && Array.isArray(inner.args)) {
|
|
189
|
+
const op = BOOL_OPS[String(inner.boolop)];
|
|
190
|
+
if (op === 'not')
|
|
191
|
+
return inner.args.length === 1 ? { kind: 'not', arg: treeOf(inner.args[0]) } : RAW;
|
|
192
|
+
return op && inner.args.length > 0 ? { kind: op, args: inner.args.map(treeOf) } : RAW;
|
|
193
|
+
}
|
|
194
|
+
if (AUTHZ_NODES.has(key) && isObject(inner)) {
|
|
195
|
+
const effective = effectivePolicy(key, inner);
|
|
196
|
+
return exprOf(effective.type, effective.data);
|
|
197
|
+
}
|
|
198
|
+
return RAW;
|
|
199
|
+
}
|
|
200
|
+
/** An object that is an Authz expression: one Authz node, or one of the combinators. */
|
|
201
|
+
export const isExpression = (value) => isObject(value) && Object.keys(value).length === 1 && (AUTHZ_NODES.has(Object.keys(value)[0]) || COMBINATORS.includes(Object.keys(value)[0]));
|
|
202
|
+
export function part(policy) {
|
|
203
|
+
const effective = effectivePolicy(policy.type, policy.data ?? {});
|
|
204
|
+
const data = normalData(effective.type, effective.data);
|
|
205
|
+
const role = policy.role ?? SIGNED_IN;
|
|
206
|
+
const meaning = JSON.stringify([effective.type, sorted(data)]);
|
|
207
|
+
const own = policy.with_check ? effectivePolicy(policy.with_check.type, policy.with_check.data ?? {}) : undefined;
|
|
208
|
+
// The backend drops a check that is the same as the policy.
|
|
209
|
+
const check = own && JSON.stringify([own.type, sorted(normalData(own.type, own.data))]) !== meaning ? own : undefined;
|
|
210
|
+
return {
|
|
211
|
+
policy,
|
|
212
|
+
role,
|
|
213
|
+
key: JSON.stringify([effective.type, policy.permissive !== false, role, sorted(data), check ? [check.type, sorted(normalData(check.type, check.data))] : null]),
|
|
214
|
+
expr: exprOf(effective.type, effective.data),
|
|
215
|
+
...(check ? { check_expr: exprOf(check.type, check.data) } : {})
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
export const covers = (policy, privilege) => policy.privileges.map((entry) => entry.toLowerCase()).includes(privilege);
|
|
219
|
+
export function partsFor(policies, privilege) {
|
|
220
|
+
return unique(policies.filter((policy) => covers(policy, privilege)).map(part), (entry) => entry.key);
|
|
221
|
+
}
|
|
222
|
+
export const appliesTo = (item, role) => item.role === role || item.role === PUBLIC;
|
|
223
|
+
export const lets = (item) => item.policy.permissive !== false;
|
|
224
|
+
/**
|
|
225
|
+
* What differs between two rule sets of one table, by meaning: order, duplicates, registry defaults and the keys the
|
|
226
|
+
* backend derives do not count, and a `with_check` equal to its policy is no check. Each policy returned is the given
|
|
227
|
+
* one, with only the privileges that differ. The role counts as it is (`public` also lets the signed-out in), and a
|
|
228
|
+
* stored raw part counts by its exact data (key order aside). The planned rules are a caller's, resolved by
|
|
229
|
+
* `resolveEntityTypes`; the stored ones are the backend's, which never hold an `entity_type`.
|
|
230
|
+
*/
|
|
231
|
+
export function policyDifference(planned, stored) {
|
|
232
|
+
const atomsOf = (policies) => {
|
|
233
|
+
const found = new Map();
|
|
234
|
+
for (const policy of policies) {
|
|
235
|
+
const { key } = part(policy);
|
|
236
|
+
for (const privilege of policy.privileges.map((entry) => entry.toLowerCase())) {
|
|
237
|
+
if (!found.has(`${key}:${privilege}`))
|
|
238
|
+
found.set(`${key}:${privilege}`, { key, policy, privilege });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return found;
|
|
242
|
+
};
|
|
243
|
+
const side = (from, other) => {
|
|
244
|
+
const groups = new Map();
|
|
245
|
+
for (const [atom, { key, policy, privilege }] of from) {
|
|
246
|
+
if (other.has(atom))
|
|
247
|
+
continue;
|
|
248
|
+
const group = groups.get(key) ?? { policy, privileges: new Set() };
|
|
249
|
+
group.privileges.add(privilege);
|
|
250
|
+
groups.set(key, group);
|
|
251
|
+
}
|
|
252
|
+
return [...groups.values()].map(({ policy, privileges }) => ({
|
|
253
|
+
...policy,
|
|
254
|
+
privileges: [...PRIVILEGES.filter((entry) => privileges.has(entry)), ...[...privileges].filter((entry) => !PRIVILEGES.includes(entry))]
|
|
255
|
+
}));
|
|
256
|
+
};
|
|
257
|
+
const plannedAtoms = atomsOf(planned);
|
|
258
|
+
const storedAtoms = atomsOf(stored);
|
|
259
|
+
return { missing: side(plannedAtoms, storedAtoms), unplanned: side(storedAtoms, plannedAtoms) };
|
|
260
|
+
}
|
|
261
|
+
export const quoted = (keys) => keys.map((key) => `"${key}"`);
|
|
262
|
+
export const join = (path, key) => (path ? `${path}.${key}` : String(key));
|
|
263
|
+
// A node's data with its entity_type turned into the membership_type the backend stores (it sets membership_type from
|
|
264
|
+
// the scope and removes entity_type), nested expressions included.
|
|
265
|
+
function resolveNode(type, data, path, scopes, say) {
|
|
266
|
+
if (!AUTHZ_NODES.has(type))
|
|
267
|
+
return data;
|
|
268
|
+
if (type === COMPOSITE)
|
|
269
|
+
return resolveTree(data, path, scopes, say);
|
|
270
|
+
const out = Object.fromEntries(Object.entries(data).map(([key, value]) => [key, isObject(value) ? resolveTree(value, join(path, key), scopes, say) : value]));
|
|
271
|
+
if (out.entity_type === undefined || out.entity_type === null)
|
|
272
|
+
return out;
|
|
273
|
+
const name = String(out.entity_type);
|
|
274
|
+
const found = scopes.find((scope) => scope.scope === name);
|
|
275
|
+
if (!found) {
|
|
276
|
+
const meant = nearestName(name, scopes.map((scope) => scope.scope));
|
|
277
|
+
say({
|
|
278
|
+
path: join(path, 'entity_type'),
|
|
279
|
+
code: 'unknown-entity-type',
|
|
280
|
+
message: `${type}'s "entity_type" is "${name}", which is not a team kind of this database${meant ? `; did you mean "${meant}"?` : '.'} ` +
|
|
281
|
+
`The database has ${listed(quoted(scopes.map((scope) => scope.scope)))}.`,
|
|
282
|
+
...(meant ? { meant } : {})
|
|
283
|
+
});
|
|
284
|
+
return out;
|
|
285
|
+
}
|
|
286
|
+
return { ...withoutEntityType(out), membership_type: found.membership_type };
|
|
287
|
+
}
|
|
288
|
+
function resolveTree(value, path, scopes, say) {
|
|
289
|
+
if (Array.isArray(value))
|
|
290
|
+
return value.map((item, i) => resolveTree(item, join(path, i), scopes, say));
|
|
291
|
+
if (!isObject(value))
|
|
292
|
+
return value;
|
|
293
|
+
const keys = Object.keys(value);
|
|
294
|
+
if (keys.length === 1 && AUTHZ_NODES.has(keys[0]) && isObject(value[keys[0]])) {
|
|
295
|
+
return { [keys[0]]: resolveNode(keys[0], value[keys[0]], join(path, keys[0]), scopes, say) };
|
|
296
|
+
}
|
|
297
|
+
return Object.fromEntries(keys.map((key) => [key, resolveTree(value[key], join(path, key), scopes, say)]));
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* A caller's rules in the form the backend stores them: every `entity_type`, in every node at any depth (and in a
|
|
301
|
+
* `with_check`), becomes its `membership_type`, from the database's team kinds (`scopes`: an entity type's prefix and
|
|
302
|
+
* its `out_membership_type`; `app` and `org` are always there). An `entity_type` the database does not have is a
|
|
303
|
+
* problem, with the index of its rule.
|
|
304
|
+
*/
|
|
305
|
+
export function resolveEntityTypes(policies, scopes) {
|
|
306
|
+
const all = [...BUILT_IN_SCOPES, ...scopes.filter((scope) => !BUILT_IN_SCOPES.some((built) => built.scope === scope.scope))];
|
|
307
|
+
const problems = [];
|
|
308
|
+
const resolved = policies.map((policy, index) => {
|
|
309
|
+
const say = (problem) => problems.push({ ...problem, policy: index });
|
|
310
|
+
const data = resolveNode(policy.type, policy.data ?? {}, '', all, say);
|
|
311
|
+
const check = policy.with_check ? { ...policy.with_check, data: resolveNode(policy.with_check.type, policy.with_check.data ?? {}, 'with_check', all, say) } : policy.with_check;
|
|
312
|
+
return { ...policy, data, ...(policy.with_check === undefined ? {} : { with_check: check }) };
|
|
313
|
+
});
|
|
314
|
+
return { policies: resolved, problems };
|
|
315
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { type FieldDefault } from 'node-type-registry';
|
|
2
|
+
import { type TablePolicy } from './policies';
|
|
3
|
+
/**
|
|
4
|
+
* What an Authz node needs on the table: `has-module` (a Data node adds the columns it reads, `data_node`),
|
|
5
|
+
* `needs-fields` (it reads columns the caller adds), `needs-table` (it reads another table), `no-fields` (it reads no
|
|
6
|
+
* column of the table), `mixed` (it needs what its nested nodes need).
|
|
7
|
+
*/
|
|
8
|
+
export type TableKind = 'has-module' | 'needs-fields' | 'needs-table' | 'no-fields' | 'mixed';
|
|
9
|
+
/** The Data node that adds the columns an Authz node reads. */
|
|
10
|
+
export interface DataNodeNeed {
|
|
11
|
+
type: string;
|
|
12
|
+
/**
|
|
13
|
+
* For each Authz parameter that names such a column: the Data node parameter that takes its value, or the fixed name
|
|
14
|
+
* the Data node always gives the column (the rule must name that column).
|
|
15
|
+
*/
|
|
16
|
+
columns: Record<string, {
|
|
17
|
+
param: string;
|
|
18
|
+
} | {
|
|
19
|
+
name: string;
|
|
20
|
+
}>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The default of a column a Data node adds: `null` for none, `'unknown'` where it has not been checked, a constant (or
|
|
24
|
+
* the node parameter that holds it), a function call, the node parameter that holds the whole default, or an identity
|
|
25
|
+
* or generated column, which takes no default.
|
|
26
|
+
*/
|
|
27
|
+
export type AddedDefault = null | 'unknown' | {
|
|
28
|
+
value: string | number | boolean | {
|
|
29
|
+
param: string;
|
|
30
|
+
};
|
|
31
|
+
} | {
|
|
32
|
+
function: string;
|
|
33
|
+
schema?: string;
|
|
34
|
+
} | {
|
|
35
|
+
param: string;
|
|
36
|
+
} | {
|
|
37
|
+
generated: 'identity' | 'expression';
|
|
38
|
+
};
|
|
39
|
+
/** One column a Data node adds to its table. */
|
|
40
|
+
export interface AddedField {
|
|
41
|
+
/** The column name, or the node parameter whose value names it (registry default applies). */
|
|
42
|
+
name: string | {
|
|
43
|
+
param: string;
|
|
44
|
+
};
|
|
45
|
+
/** The PostgreSQL type, or the node parameter that holds it (a FieldType). */
|
|
46
|
+
type: string | {
|
|
47
|
+
param: string;
|
|
48
|
+
};
|
|
49
|
+
/** NOT NULL, or the node parameter that decides it. */
|
|
50
|
+
required: boolean | {
|
|
51
|
+
param: string;
|
|
52
|
+
};
|
|
53
|
+
/** The default the generator gives a column it creates (a column the table already has keeps its own). */
|
|
54
|
+
default: AddedDefault;
|
|
55
|
+
/** Left out when this node parameter is true. */
|
|
56
|
+
unless?: string;
|
|
57
|
+
}
|
|
58
|
+
export declare const TABLE_NEEDS: Record<string, {
|
|
59
|
+
kind: TableKind;
|
|
60
|
+
data_node?: DataNodeNeed;
|
|
61
|
+
}>;
|
|
62
|
+
/**
|
|
63
|
+
* The columns each node of the data module adds. `[]` when it adds none (a trigger, an index, a CHECK constraint, or
|
|
64
|
+
* columns on another table); `'unknown'` where the backend's answer has not been checked.
|
|
65
|
+
*/
|
|
66
|
+
export declare const ADDED_FIELDS: Record<string, AddedField[] | 'unknown'>;
|
|
67
|
+
/**
|
|
68
|
+
* A column's default with the node's data filled in: `null` for none, `'unknown'` where it has not been checked, a
|
|
69
|
+
* FieldDefault (a constant, a function call, ...), or `{ generated }` for an identity or generated column.
|
|
70
|
+
*/
|
|
71
|
+
export type ColumnDefault = null | 'unknown' | FieldDefault | {
|
|
72
|
+
generated: 'identity' | 'expression';
|
|
73
|
+
};
|
|
74
|
+
export interface AddedColumn {
|
|
75
|
+
name: string;
|
|
76
|
+
type: string;
|
|
77
|
+
nullable: boolean;
|
|
78
|
+
default: ColumnDefault;
|
|
79
|
+
/** The node that adds it. */
|
|
80
|
+
node: string;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Which rows a column's default fills when the backend adds the column: a constant rides ADD COLUMN and fills the rows
|
|
84
|
+
* the table has; any other default is set after SET NOT NULL, so it fills new rows only, and a required column with one
|
|
85
|
+
* fails on a table that has rows. An identity or generated column fills every row.
|
|
86
|
+
*/
|
|
87
|
+
export declare function defaultFills(value: ColumnDefault): {
|
|
88
|
+
existing_rows: boolean | 'unknown';
|
|
89
|
+
new_rows: boolean | 'unknown';
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* The columns a list of blueprint nodes adds to its table, from `ADDED_FIELDS` and each node's data (registry defaults
|
|
93
|
+
* apply). A node whose columns are not known, or whose data leaves a column unnamed, is listed in `unknown` instead.
|
|
94
|
+
*/
|
|
95
|
+
export declare function fieldsAddedByNodes(list: Array<string | {
|
|
96
|
+
$type: string;
|
|
97
|
+
data?: object;
|
|
98
|
+
}>): {
|
|
99
|
+
fields: AddedColumn[];
|
|
100
|
+
unknown: string[];
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* The table's own id: DataId's column. A rule that reads it as its owner or team column reads the row as the person
|
|
104
|
+
* (the users table: each row is a person, and their own organization), and no Data node makes that column.
|
|
105
|
+
*/
|
|
106
|
+
export declare const ROW_ID = "id";
|
|
107
|
+
export interface NeededDataNode {
|
|
108
|
+
$type: string;
|
|
109
|
+
data: Record<string, unknown>;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The Data nodes a table needs for its rules to have their columns: each rule's Data node with its column names, nested
|
|
113
|
+
* nodes of a combination included. A node is left out when another one adds all its columns, and when the rule reads the
|
|
114
|
+
* row's own id (`ROW_ID`), which no Data node makes. A rule that names a column its Data node adds under another name
|
|
115
|
+
* (the node's columns have fixed names) needs no node when the table has every column the rule reads (`columns`); else
|
|
116
|
+
* it is a problem, not a guess, since the node would add columns the rule does not read.
|
|
117
|
+
*/
|
|
118
|
+
export declare function dataNodesForPolicies(policies: Array<Pick<TablePolicy, 'type' | 'data'>>, columns?: ReadonlySet<string>): {
|
|
119
|
+
nodes: NeededDataNode[];
|
|
120
|
+
problems: string[];
|
|
121
|
+
};
|