@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,307 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// The data of a rule a caller writes, checked against its Authz node's closed parameter_schema before anything changes.
|
|
3
|
+
// The schema is the registry's, in the form a caller writes it (the blueprint JSON Schema's node data definitions), so
|
|
4
|
+
// this check and the backend read the same truth; ajv does the validating.
|
|
5
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
6
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.READ_BUT_NOT_SUPPORTED = exports.jsonCopy = exports.POLICY_ROLES = void 0;
|
|
10
|
+
exports.blueprintValidator = blueprintValidator;
|
|
11
|
+
exports.unsupportedShapes = unsupportedShapes;
|
|
12
|
+
exports.checkRule = checkRule;
|
|
13
|
+
const harness_1 = require("@agentic-kit/harness");
|
|
14
|
+
const ajv_1 = __importDefault(require("ajv"));
|
|
15
|
+
const node_type_registry_1 = require("node-type-registry");
|
|
16
|
+
const policies_1 = require("./policies");
|
|
17
|
+
/**
|
|
18
|
+
* The roles a rule can name: the request roles of the GraphQL API (`authenticated`, `anonymous`), the function lane's
|
|
19
|
+
* floor (`authenticated_client`), and PostgreSQL's `public`, which is every role. The backend creates the policy for
|
|
20
|
+
* the role as given, so another name fails there. An API may name its own request roles, so a caller can pass them.
|
|
21
|
+
*/
|
|
22
|
+
exports.POLICY_ROLES = ['authenticated', 'anonymous', 'authenticated_client', 'public'];
|
|
23
|
+
const COMBINATORS = ['AND', 'OR', 'NOT'];
|
|
24
|
+
const POLICY_TYPES = new Set(node_type_registry_1.policyNodeTypes.map((node) => node.name));
|
|
25
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
26
|
+
let ajv;
|
|
27
|
+
/**
|
|
28
|
+
* A copy to validate: the validator fills registry defaults into what it checks, and a JSON copy shares no object
|
|
29
|
+
* between two places, so a default filled in one rule never lands in another.
|
|
30
|
+
*/
|
|
31
|
+
const jsonCopy = (value) => (value === undefined ? value : JSON.parse(JSON.stringify(value)));
|
|
32
|
+
exports.jsonCopy = jsonCopy;
|
|
33
|
+
const validators = new Map();
|
|
34
|
+
/**
|
|
35
|
+
* A validator of one definition of the registry's blueprint JSON Schema (`BlueprintDefinition`, `AuthzEntityMembershipParams`,
|
|
36
|
+
* ...), with the formats the schema names. Registry defaults fill missing keys before the required keys are checked, as the
|
|
37
|
+
* backend does, so a caller validates a copy.
|
|
38
|
+
*/
|
|
39
|
+
function blueprintValidator(definition) {
|
|
40
|
+
if (!ajv) {
|
|
41
|
+
ajv = new ajv_1.default({ allErrors: true, strict: false, verbose: true, useDefaults: true });
|
|
42
|
+
// A column name cannot be empty: PostgreSQL refuses a zero-length identifier.
|
|
43
|
+
ajv.addFormat('column-ref', (name) => name.length > 0);
|
|
44
|
+
ajv.addFormat('uuid', UUID);
|
|
45
|
+
ajv.addFormat('function-ref', true);
|
|
46
|
+
ajv.addFormat('labeled', true);
|
|
47
|
+
ajv.addSchema(node_type_registry_1.blueprintDefinitionJsonSchema, 'blueprint');
|
|
48
|
+
}
|
|
49
|
+
let validate = validators.get(definition);
|
|
50
|
+
if (!validate) {
|
|
51
|
+
validate = ajv.getSchema(`blueprint#/$defs/${definition}`);
|
|
52
|
+
if (!validate)
|
|
53
|
+
throw new Error(`The blueprint JSON Schema of node-type-registry has no definition ${definition}; rebuild node-type-registry.`);
|
|
54
|
+
validators.set(definition, validate);
|
|
55
|
+
}
|
|
56
|
+
return validate;
|
|
57
|
+
}
|
|
58
|
+
const TYPE_WORDS = {
|
|
59
|
+
string: 'a string',
|
|
60
|
+
integer: 'an integer',
|
|
61
|
+
number: 'a number',
|
|
62
|
+
boolean: 'true or false',
|
|
63
|
+
array: 'a list',
|
|
64
|
+
object: 'an object',
|
|
65
|
+
null: 'null'
|
|
66
|
+
};
|
|
67
|
+
// What a schema allows, in words.
|
|
68
|
+
function allowed(schema) {
|
|
69
|
+
if (schema.enum)
|
|
70
|
+
return `one of ${schema.enum.map((value) => JSON.stringify(value)).join(', ')}`;
|
|
71
|
+
if (schema.const !== undefined)
|
|
72
|
+
return JSON.stringify(schema.const);
|
|
73
|
+
if (schema.anyOf)
|
|
74
|
+
return (0, harness_1.listed)(schema.anyOf.map(allowed), 'or');
|
|
75
|
+
const types = [schema.type ?? []].flat();
|
|
76
|
+
return types.length ? (0, harness_1.listed)(types.map((type) => TYPE_WORDS[type] ?? type), 'or') : 'a value its schema allows';
|
|
77
|
+
}
|
|
78
|
+
// The Authz node a path is in: the last node name on it, or the rule's own type.
|
|
79
|
+
function nodeAt(type, path) {
|
|
80
|
+
return path.split('.').filter((segment) => policies_1.AUTHZ_NODES.has(segment)).pop() ?? type;
|
|
81
|
+
}
|
|
82
|
+
const isExpressionLevel = (schema) => Boolean(schema?.properties?.AND && schema.properties.OR && schema.properties.NOT);
|
|
83
|
+
function callerKeys(node) {
|
|
84
|
+
return Object.entries(policies_1.AUTHZ_NODES.get(node)?.parameter_schema.properties ?? {})
|
|
85
|
+
.filter(([, schema]) => !schema.readOnly)
|
|
86
|
+
.map(([key]) => key)
|
|
87
|
+
.sort();
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Keys a member rule's data can hold that these tools do not support in a rule you write, with what to use instead.
|
|
91
|
+
* A stored rule that holds one is said as a setting the words do not describe.
|
|
92
|
+
*/
|
|
93
|
+
const NOT_SUPPORTED = {
|
|
94
|
+
any_array: 'a team column that holds a list of teams is not supported. Name a column that holds one team id.',
|
|
95
|
+
include_current_user_id: 'counting the person\'s own id as a team is not supported. For rows that name the person, add an AuthzDirectOwner rule.',
|
|
96
|
+
entity_capability_check: 'matching the member list against the row instead of the person is not supported. To ask more of a member, use is_admin, is_owner, capabilities, levels or mask.'
|
|
97
|
+
};
|
|
98
|
+
/** The keys the backend reads from a member rule's data that NOT_SUPPORTED keeps out of a rule you write. */
|
|
99
|
+
exports.READ_BUT_NOT_SUPPORTED = Object.keys(NOT_SUPPORTED);
|
|
100
|
+
function extraKey(type, at, key, parent) {
|
|
101
|
+
const path = (0, policies_1.join)(at, key);
|
|
102
|
+
if (isExpressionLevel(parent)) {
|
|
103
|
+
const takes = `A rule you write takes only Authz nodes and ${(0, harness_1.listed)((0, policies_1.quoted)(COMBINATORS))}.`;
|
|
104
|
+
const same = COMBINATORS.find((name) => name.toLowerCase() === key.toLowerCase());
|
|
105
|
+
if (same)
|
|
106
|
+
return { path, code: 'not-a-node', message: `${type}: "${key}" is not a key the backend reads. Use "${same}": the combinators are in capitals.` };
|
|
107
|
+
if (policies_1.AUTHZ_NODES.has(key))
|
|
108
|
+
return { path, code: 'not-in-expression', message: `${type}: ${key} cannot be built inside an expression. Give it as a rule of its own.` };
|
|
109
|
+
const meant = (0, harness_1.nearestName)(key, [...policies_1.AUTHZ_NODES.keys()]);
|
|
110
|
+
if (meant)
|
|
111
|
+
return { path, code: 'mistyped-node', message: `${type}: "${key}" is not an Authz node; did you mean "${meant}"? ${takes}`, meant };
|
|
112
|
+
return { path, code: 'not-a-node', message: `${type}: "${key}" is not an Authz node: it is a raw expression or a node the registry does not have. ${takes}` };
|
|
113
|
+
}
|
|
114
|
+
const node = nodeAt(type, at);
|
|
115
|
+
// At the node's own data, or in an object nested in it.
|
|
116
|
+
const own = at === '' || at.endsWith(node);
|
|
117
|
+
if (own && policies_1.AUTHZ_NODES.get(node)?.parameter_schema.properties?.[key]?.readOnly) {
|
|
118
|
+
return { path, code: 'stored-only-key', message: `${node}'s "${key}" is set by the backend or the platform, and a rule you write does not give it. Leave it out.` };
|
|
119
|
+
}
|
|
120
|
+
if (own && Object.hasOwn(NOT_SUPPORTED, key))
|
|
121
|
+
return { path, code: 'not-supported', message: `${node}'s "${key}": ${NOT_SUPPORTED[key]}` };
|
|
122
|
+
const keys = own ? callerKeys(node) : Object.keys(parent?.properties ?? {}).sort();
|
|
123
|
+
const meant = (0, harness_1.nearestName)(key, keys);
|
|
124
|
+
return {
|
|
125
|
+
path,
|
|
126
|
+
code: 'unknown-key',
|
|
127
|
+
message: `${node} has no key "${path}"${meant ? `; did you mean "${meant}"?` : '.'} The backend would store it and ignore it, so the rule ` +
|
|
128
|
+
`would let in more people than you meant. ${keys.length ? `It takes ${(0, harness_1.listed)((0, policies_1.quoted)(keys))}.` : 'It takes no keys.'}`,
|
|
129
|
+
...(meant ? { meant } : {})
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
const groupsOf = (branches) => Array.isArray(branches) && branches.every((branch) => (0, policies_1.isObject)(branch) && Array.isArray(branch.required)) ? branches.map((branch) => branch.required) : undefined;
|
|
133
|
+
// A branch of a oneOf or anyOf fails on its own whenever the whole fails; the whole is reported instead.
|
|
134
|
+
const inBranch = (error) => /\/(oneOf|anyOf)\/\d+\//.test(error.schemaPath);
|
|
135
|
+
function problemOf(type, error) {
|
|
136
|
+
if (inBranch(error))
|
|
137
|
+
return undefined;
|
|
138
|
+
const at = error.instancePath.split('/').slice(1).map((segment) => segment.replace(/~1/g, '/').replace(/~0/g, '~')).join('.');
|
|
139
|
+
const label = at ? `${nodeAt(type, at)}'s "${at}"` : `${type}'s data`;
|
|
140
|
+
const schema = error.parentSchema;
|
|
141
|
+
switch (error.keyword) {
|
|
142
|
+
case 'additionalProperties':
|
|
143
|
+
return extraKey(type, at, String(error.params.additionalProperty), schema);
|
|
144
|
+
case 'required': {
|
|
145
|
+
const key = String(error.params.missingProperty);
|
|
146
|
+
const property = schema?.properties?.[key];
|
|
147
|
+
return { path: (0, policies_1.join)(at, key), code: 'required', message: `${nodeAt(type, at)} needs "${(0, policies_1.join)(at, key)}"${property ? ` (${allowed(property)})` : ''}. Add it.` };
|
|
148
|
+
}
|
|
149
|
+
case 'oneOf':
|
|
150
|
+
case 'anyOf': {
|
|
151
|
+
const groups = groupsOf(error.schema);
|
|
152
|
+
if (!groups)
|
|
153
|
+
return { path: at, code: 'type', message: `${label} must be ${allowed(schema ?? {})}.` };
|
|
154
|
+
const options = (0, harness_1.listed)(groups.map((group) => (0, harness_1.listed)((0, policies_1.quoted)(group))), 'or');
|
|
155
|
+
const both = error.keyword === 'oneOf' && Array.isArray(error.params.passingSchemas);
|
|
156
|
+
return {
|
|
157
|
+
path: at,
|
|
158
|
+
code: 'one-of',
|
|
159
|
+
message: both ? `${nodeAt(type, at)} takes exactly one of ${options}, not more. Leave one out.` : `${nodeAt(type, at)} needs ${options}, and the backend refuses the rule without one of them. Add one.`
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
case 'type':
|
|
163
|
+
case 'enum':
|
|
164
|
+
case 'const':
|
|
165
|
+
return { path: at, code: error.keyword, message: `${label} must be ${allowed(schema ?? {})}.` };
|
|
166
|
+
case 'minimum':
|
|
167
|
+
case 'maximum':
|
|
168
|
+
return { path: at, code: 'range', message: `${label} must be ${error.keyword === 'minimum' ? 'at least' : 'at most'} ${error.params.limit}.` };
|
|
169
|
+
case 'minItems':
|
|
170
|
+
return { path: at, code: 'not-empty', message: `${label} must list at least ${error.params.limit} item${error.params.limit === 1 ? '' : 's'}, and the backend refuses an empty list.` };
|
|
171
|
+
case 'minLength':
|
|
172
|
+
return { path: at, code: 'not-empty', message: `${label} cannot be empty.` };
|
|
173
|
+
case 'minProperties':
|
|
174
|
+
case 'maxProperties':
|
|
175
|
+
return {
|
|
176
|
+
path: at,
|
|
177
|
+
code: 'expression',
|
|
178
|
+
message: `${label} must hold exactly one key: ${(0, harness_1.listed)((0, policies_1.quoted)(COMBINATORS))} or one Authz node, as in {"AuthzEntityMembership": {...}}. Put several parts under "AND" or "OR".`
|
|
179
|
+
};
|
|
180
|
+
case 'format':
|
|
181
|
+
return error.params.format === 'column-ref'
|
|
182
|
+
? { path: at, code: 'not-empty', message: `${label} names a column, and a column name cannot be empty. Give the column's name.` }
|
|
183
|
+
: { path: at, code: 'format', message: `${label} must be a ${error.params.format}.` };
|
|
184
|
+
case 'pattern':
|
|
185
|
+
return { path: at, code: 'pattern', message: `${label} must match ${error.params.pattern}.` };
|
|
186
|
+
case 'not':
|
|
187
|
+
return { path: at, code: 'not', message: `${label} cannot be ${allowed(schema?.not ?? {})}.` };
|
|
188
|
+
default:
|
|
189
|
+
// `if` repeats the errors of its `then`, which are reported on their own.
|
|
190
|
+
return error.keyword === 'if' ? undefined : { path: at, code: error.keyword, message: `${label} ${error.message}.` };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function roleProblem(role, roles) {
|
|
194
|
+
if (role === undefined || roles.includes(role))
|
|
195
|
+
return [];
|
|
196
|
+
const meant = (0, harness_1.nearestName)(role, [...roles]);
|
|
197
|
+
return [
|
|
198
|
+
{
|
|
199
|
+
path: 'role',
|
|
200
|
+
code: 'unknown-role',
|
|
201
|
+
message: `The role "${role}" is not a role a rule can name${meant ? `; did you mean "${meant}"?` : '.'} ` +
|
|
202
|
+
`The backend creates the policy for the role as given, and it fails for a role that does not exist. Use ${(0, harness_1.listed)((0, policies_1.quoted)([...roles]), 'or')}.`,
|
|
203
|
+
...(meant ? { meant } : {})
|
|
204
|
+
}
|
|
205
|
+
];
|
|
206
|
+
}
|
|
207
|
+
/** The column names AuthzOrgHierarchy does not support for the columns of its own table. */
|
|
208
|
+
const CHART_COLUMN_NAMES = ['entity_id', 'ancestor_id', 'descendant_id', 'depth'];
|
|
209
|
+
function eachPart(value, path, visit) {
|
|
210
|
+
if (Array.isArray(value))
|
|
211
|
+
return value.forEach((item, i) => eachPart(item, (0, policies_1.join)(path, i), visit));
|
|
212
|
+
if (!(0, policies_1.isObject)(value))
|
|
213
|
+
return;
|
|
214
|
+
for (const [key, inner] of Object.entries(value)) {
|
|
215
|
+
if (COMBINATORS.includes(key))
|
|
216
|
+
eachPart(inner, (0, policies_1.join)(path, key), visit);
|
|
217
|
+
else if (policies_1.AUTHZ_NODES.has(key) && (0, policies_1.isObject)(inner))
|
|
218
|
+
eachLeaf(key, inner, (0, policies_1.join)(path, key), visit);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
// Each Authz node of a rule, nested ones included.
|
|
222
|
+
function eachLeaf(type, data, path, visit) {
|
|
223
|
+
if (type === 'AuthzComposite')
|
|
224
|
+
return eachPart(data, path, visit);
|
|
225
|
+
visit(type, data, path);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* The rule shapes these tools do not support, which the parameter schema cannot say: a module's table outside a
|
|
229
|
+
* blueprint, and the shapes the backend builds into a weaker check than the data says. A stored rule of such a shape
|
|
230
|
+
* is one its words cannot fully state.
|
|
231
|
+
*/
|
|
232
|
+
function unsupportedShapes(type, data, context) {
|
|
233
|
+
const problems = [];
|
|
234
|
+
eachLeaf(type, data, '', (node, given, at) => {
|
|
235
|
+
const values = { ...(0, policies_1.defaultsOf)(node), ...given };
|
|
236
|
+
if (!context.inBlueprint && values.ref_module !== undefined) {
|
|
237
|
+
problems.push({
|
|
238
|
+
path: (0, policies_1.join)(at, 'ref_module'),
|
|
239
|
+
code: 'not-supported',
|
|
240
|
+
message: `${node}'s "ref_module" names a table a module made, which only a blueprint resolves. Give the table as "ref_table_id", or as "ref_schema" and "ref_table".`
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
if (node === 'AuthzOrgHierarchy') {
|
|
244
|
+
for (const param of ['entity_field', 'anchor_field']) {
|
|
245
|
+
const column = values[param];
|
|
246
|
+
if (typeof column !== 'string' || !CHART_COLUMN_NAMES.includes(column))
|
|
247
|
+
continue;
|
|
248
|
+
problems.push({
|
|
249
|
+
path: (0, policies_1.join)(at, param),
|
|
250
|
+
code: 'not-supported',
|
|
251
|
+
message: `${node}'s "${param}" is "${column}"${given[param] === undefined ? ' (its default)' : ''}, and this rule does not support a column named ` +
|
|
252
|
+
`${(0, harness_1.listed)((0, policies_1.quoted)(CHART_COLUMN_NAMES), 'or')}. Name a column with another name.`
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (node === 'AuthzRelatedMemberList' && typeof values.this_object_key === 'string') {
|
|
257
|
+
const columns = context.columnsOf?.({ id: values.owned_table_id, schema: values.owned_schema, name: values.owned_table });
|
|
258
|
+
if (columns?.includes(values.this_object_key)) {
|
|
259
|
+
problems.push({
|
|
260
|
+
path: (0, policies_1.join)(at, 'this_object_key'),
|
|
261
|
+
code: 'not-supported',
|
|
262
|
+
message: `${node}'s "this_object_key" is "${values.this_object_key}", and the related table has a column of that name too, which this rule does not support. ` +
|
|
263
|
+
'Name a column of this table whose name the related table does not have.'
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
return problems;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* The problems of one rule a caller writes for Authz node `type`: its data against the node's closed parameter_schema
|
|
272
|
+
* (nested Authz nodes included), the rule shapes these tools do not support, and the role it names. An empty list
|
|
273
|
+
* means the backend builds the rule as written. A key the backend or the platform sets is `stored-only-key`, a key
|
|
274
|
+
* these tools do not support is `not-supported`, any other key the node does not take is `unknown-key`, and a part
|
|
275
|
+
* that is not an Authz node or AND, OR, NOT is refused.
|
|
276
|
+
*/
|
|
277
|
+
function checkRule(type, data, context = {}) {
|
|
278
|
+
const roles = roleProblem(context.role, context.roles ?? exports.POLICY_ROLES);
|
|
279
|
+
if (policies_1.AUTHZ_NODES.has(type) && !POLICY_TYPES.has(type)) {
|
|
280
|
+
return [
|
|
281
|
+
{
|
|
282
|
+
path: '',
|
|
283
|
+
code: 'not-a-policy',
|
|
284
|
+
message: `${type} is not a policy type a rule can use: it guards writes to columns, and a blueprint gives it in a table's "nodes". Use a policy type for a rule.`
|
|
285
|
+
},
|
|
286
|
+
...roles
|
|
287
|
+
];
|
|
288
|
+
}
|
|
289
|
+
if (!policies_1.AUTHZ_NODES.has(type)) {
|
|
290
|
+
const meant = (0, harness_1.nearestName)(type, [...POLICY_TYPES]);
|
|
291
|
+
return [
|
|
292
|
+
{
|
|
293
|
+
path: '',
|
|
294
|
+
code: meant ? 'mistyped-node' : 'not-a-node',
|
|
295
|
+
message: `"${type}" is not an Authz node${meant ? `; did you mean "${meant}"?` : '.'} Use the name of one, for example "AuthzEntityMembership".`,
|
|
296
|
+
...(meant ? { meant } : {})
|
|
297
|
+
},
|
|
298
|
+
...roles
|
|
299
|
+
];
|
|
300
|
+
}
|
|
301
|
+
if (data !== undefined && !(0, policies_1.isObject)(data))
|
|
302
|
+
return [{ path: '', code: 'type', message: `${type}'s data must be an object of its parameters.` }, ...roles];
|
|
303
|
+
const validate = blueprintValidator(`${type}Params`);
|
|
304
|
+
const problems = validate((0, exports.jsonCopy)(data ?? {})) ? unsupportedShapes(type, (data ?? {}), context) : validate.errors.map((error) => problemOf(type, error)).filter(Boolean);
|
|
305
|
+
// One problem per place: a wrong type there also fails the rules about it.
|
|
306
|
+
return [...roles, ...problems.filter((problem, i) => problems.findIndex((other) => other.path === problem.path) === i)];
|
|
307
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type TableRef } from './rule-check';
|
|
2
|
+
/** The columns of other tables a reader read, by table id and by table name. */
|
|
3
|
+
export type RelatedColumns = Record<string, readonly string[]>;
|
|
4
|
+
/** The lookup a rule check and the words read another table's columns with. */
|
|
5
|
+
export declare const columnsOf: (related: RelatedColumns | undefined) => (table: TableRef) => readonly string[] | undefined;
|
|
6
|
+
/** How to say one rule of an Authz node. `{param}` takes the rule's value for that parameter (registry defaults apply). */
|
|
7
|
+
export interface PolicyWordsTemplate {
|
|
8
|
+
/** One sentence naming who passes. */
|
|
9
|
+
text: string;
|
|
10
|
+
/**
|
|
11
|
+
* Added when the data has a value for `when` (not false, null, an empty string or an empty list) and none for any
|
|
12
|
+
* `unless` key; `covers` names other keys the clause also describes.
|
|
13
|
+
*/
|
|
14
|
+
clauses?: {
|
|
15
|
+
when: string;
|
|
16
|
+
unless?: string[];
|
|
17
|
+
covers?: string[];
|
|
18
|
+
text: string;
|
|
19
|
+
}[];
|
|
20
|
+
/** Words for a parameter's value where `{param}` appears; `*` covers any other value, and `{value}` is the value. */
|
|
21
|
+
values?: Record<string, Record<string, string>>;
|
|
22
|
+
/** The value the backend gives a parameter when the data gives none. */
|
|
23
|
+
assumes?: Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
export declare const POLICY_WORDS: Record<string, PolicyWordsTemplate>;
|
|
26
|
+
/**
|
|
27
|
+
* True when the words of one rule's own data (an Authz node; its nested parts are rules of their own) state all it
|
|
28
|
+
* checks: no key they do not describe, no shape they hedge, and data they can read. Otherwise the rule may let in more
|
|
29
|
+
* or fewer people than its words say, and its words say where.
|
|
30
|
+
*/
|
|
31
|
+
export declare function statesAll(type: string, data: Record<string, unknown>, related?: RelatedColumns): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* One rule in plain words: the node's sentence filled from `data` (registry defaults apply), then each clause that
|
|
34
|
+
* applies, then one sentence for every key of `data` that none of those parts describes, and one for each shape the
|
|
35
|
+
* backend may build into another check. A nested Authz node or AND/OR/NOT part inside the data is explained in turn.
|
|
36
|
+
* `related` holds the columns of the other tables the rule names, when the reader read them.
|
|
37
|
+
*/
|
|
38
|
+
export declare function explainPolicy(type: string, data: Record<string, unknown>, related?: RelatedColumns): string;
|