@canopy-io/node 0.15.0 → 0.16.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/CHANGELOG.md +15 -0
- package/README.md +26 -10
- package/dist/index.cjs +191 -56
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5777 -2320
- package/dist/index.d.ts +5777 -2320
- package/dist/index.js +191 -56
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# @canopy-io/node
|
|
2
2
|
|
|
3
|
+
## 0.16.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 7d31f2f: `LocalAuthorizer` understands authorization model version 2, which Canopy answers in an Environment that holds a deny. A deny refuses a permission to an identity, a machine client or a role's holders at a node and everything beneath it, whatever the grants allow. The grants read carries the subject's deny roots beside its grant roots, already resolved by Canopy (the roles it holds, the exception roles that spare it), and the authorizer applies them from the same cache and TTL:
|
|
8
|
+
|
|
9
|
+
- `evaluate` refuses with the new reason `denied` when a deny root for the permission is at the node or above it, before any grant is looked at. An `app_wide` check holds while one grant in effect stands clear of every deny, and is `denied` when none does. A check on a permission with a deny reads the tree even for a grant at the node itself.
|
|
10
|
+
- `grantRoots` and `accessibleNodes` leave out a root a deny refuses and name `excludedRoots`, the deny roots beneath a root, each cutting out its subtree. `accessibleNodes` leaves those subtrees out of `nodes`, and `envWide` is false while anything is excluded. A filter by roots must exclude `excludedRoots` too.
|
|
11
|
+
- `holdsAnywhereUnder` answers `{ held, roots, excludedRoots }`; a deny at the subtree's root or above it refuses the whole subtree.
|
|
12
|
+
- `permissionsAt` and `permissionsAtMany` leave out a permission a deny refuses at the node.
|
|
13
|
+
|
|
14
|
+
`LocalDenialReason` gains `denied`, so an exhaustive `switch` over it needs the new case. A version 1 answer, from a Canopy without denies, carries none and is read as before. The conformance bundle gains the `denies` vector, which the authorizer answers, and is published at model version 2.
|
|
15
|
+
|
|
16
|
+
The spec now has 197 operations. Generated types cover the Denies routes (`/api/v1/denies`: list, get, create, preview, reach, remove), unwrapped like the other management routes, `deny_id` on an evaluation refused by a deny, and the reads and previews released beside them.
|
|
17
|
+
|
|
3
18
|
## 0.15.0
|
|
4
19
|
|
|
5
20
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -327,13 +327,24 @@ every request then finds the cache expired and refetches.
|
|
|
327
327
|
|
|
328
328
|
Each answer carries a reason when it is no, from what the grant roots and the
|
|
329
329
|
tree can say: `permission_not_held`, `outside_grants`, `expired`,
|
|
330
|
-
`not_yet_effective`, or `node_missing` for a `node` check with no
|
|
330
|
+
`not_yet_effective`, `denied`, or `node_missing` for a `node` check with no
|
|
331
|
+
node. The
|
|
331
332
|
grant roots are read with their windows, so a grant that ends sooner than the
|
|
332
333
|
TTL is dropped at its boundary, not at the next expiry. `onDecision` and
|
|
333
334
|
`onDenied` hooks hear every verdict after it is reached, never awaited and
|
|
334
335
|
never able to change it, for audit trails and alerting without a fork of the
|
|
335
336
|
guard.
|
|
336
337
|
|
|
338
|
+
A deny refuses a permission at a node and everything beneath it, whatever the
|
|
339
|
+
grants allow. The grants read carries the identity's deny roots beside its
|
|
340
|
+
grant roots, already resolved by Canopy (the roles it holds, the exception
|
|
341
|
+
roles that spare it), so a check refuses with `denied` when a deny root for
|
|
342
|
+
the permission is at the node or above it, before any grant is looked at. An
|
|
343
|
+
`app_wide` check holds while one grant in effect stands clear of every deny.
|
|
344
|
+
Denies ride the same cache and TTL as grants: a deny created or deleted
|
|
345
|
+
reaches this authorizer within `ttlMs`, and `invalidate()` with no argument
|
|
346
|
+
picks it up at once, since a deny on a role binds everyone holding it.
|
|
347
|
+
|
|
337
348
|
A machine client's grants are its own, read from
|
|
338
349
|
`GET /api/v1/oauth-clients/:id/grants`. Pass `principal_type: "service"` with
|
|
339
350
|
the client's `client_id` as `identity_id`; the list reads and `invalidate` take
|
|
@@ -363,13 +374,15 @@ const authorizer = new LocalAuthorizer(canopy.client, {
|
|
|
363
374
|
A list needs the other direction: which nodes may this identity see. The same
|
|
364
375
|
cache answers it, in the shape your query wants. `grantRoots` names the nodes
|
|
365
376
|
the permission is granted at, in effect now, minus the ones another root
|
|
366
|
-
already covers, and says whether one is the Environment's
|
|
367
|
-
every node qualifies, so no filter). `
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
377
|
+
already covers or a deny refuses, and says whether one is the Environment's
|
|
378
|
+
root (`envWide`: every node qualifies, so no filter). `excludedRoots` names
|
|
379
|
+
the deny roots beneath those roots; each cuts out its subtree, and `envWide`
|
|
380
|
+
is false while there are any. `accessibleNodes` expands the roots through
|
|
381
|
+
their descendants, leaving the excluded subtrees out, into a `Set` for an
|
|
382
|
+
`IN (...)` filter, capped at `maxNodes` (10,000 by default, per authorizer or
|
|
383
|
+
per call); past the cap it hands back the roots with `nodes: null` and
|
|
384
|
+
`complete: false`, so an application with a closure table or a materialised
|
|
385
|
+
path filters by "beneath a root and not beneath an excluded root" instead. `permissionsAt` lists what the identity holds at one node, for
|
|
373
386
|
rendering a screen's controls, and `permissionsAtMany` answers several nodes
|
|
374
387
|
from one read, for the rows of a list.
|
|
375
388
|
|
|
@@ -381,7 +394,8 @@ if (access.envWide) {
|
|
|
381
394
|
}
|
|
382
395
|
|
|
383
396
|
if (!access.complete) {
|
|
384
|
-
// Too large for an IN (...) list: filter beneath access.roots
|
|
397
|
+
// Too large for an IN (...) list: filter beneath access.roots, and not
|
|
398
|
+
// beneath access.excludedRoots, instead.
|
|
385
399
|
}
|
|
386
400
|
|
|
387
401
|
return db.reports.findMany({ where: { nodeId: { in: [...access.nodes] } } });
|
|
@@ -392,7 +406,9 @@ hold the permission anywhere in it? `app_wide` cannot say, since it covers the
|
|
|
392
406
|
whole Environment, so a grant in another organization would show the menu
|
|
393
407
|
here too. `holdsAnywhereUnder` answers for one subtree, with `roots` as the
|
|
394
408
|
subtree root alone when a grant at or above it reaches all of it, and
|
|
395
|
-
otherwise the topmost granted nodes inside it
|
|
409
|
+
otherwise the topmost granted nodes inside it, with `excludedRoots` as the
|
|
410
|
+
deny roots inside those. A deny at the subtree's root or above it refuses the
|
|
411
|
+
whole subtree:
|
|
396
412
|
|
|
397
413
|
```ts
|
|
398
414
|
const { held, roots } = await authorizer.holdsAnywhereUnder(
|
package/dist/index.cjs
CHANGED
|
@@ -1201,7 +1201,7 @@ var MAX_WALK_DEPTH = 256;
|
|
|
1201
1201
|
var PAGE_SIZE = 100;
|
|
1202
1202
|
var MAX_PAGES = 1e4;
|
|
1203
1203
|
var MAX_TREE_WALKS = 3;
|
|
1204
|
-
var KNOWN_MODEL_VERSIONS = /* @__PURE__ */ new Set([1]);
|
|
1204
|
+
var KNOWN_MODEL_VERSIONS = /* @__PURE__ */ new Set([1, 2]);
|
|
1205
1205
|
function understood(page, path) {
|
|
1206
1206
|
if (!KNOWN_MODEL_VERSIONS.has(page.model_version)) {
|
|
1207
1207
|
throw new CanopyAuthorizerError(
|
|
@@ -1220,6 +1220,17 @@ function inEffect(window, now) {
|
|
|
1220
1220
|
function reasonAmong(windows, now) {
|
|
1221
1221
|
return windows.some((window) => window.to !== null && window.to <= now) ? "expired" : "not_yet_effective";
|
|
1222
1222
|
}
|
|
1223
|
+
function refusedOn(denied, lineage) {
|
|
1224
|
+
if (!denied) {
|
|
1225
|
+
return false;
|
|
1226
|
+
}
|
|
1227
|
+
for (const node of lineage) {
|
|
1228
|
+
if (denied.has(node)) {
|
|
1229
|
+
return true;
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
return false;
|
|
1233
|
+
}
|
|
1223
1234
|
var LocalAuthorizer = class {
|
|
1224
1235
|
client;
|
|
1225
1236
|
ttlMs;
|
|
@@ -1317,55 +1328,63 @@ var LocalAuthorizer = class {
|
|
|
1317
1328
|
* that keeps a closure table or a materialised path: it filters by "beneath
|
|
1318
1329
|
* one of these" without an expanded set.
|
|
1319
1330
|
*
|
|
1331
|
+
* A grant a deny refuses (a deny root at its node or above it) is left out,
|
|
1332
|
+
* and a deny root beneath a root is named in `excludedRoots`, so the filter
|
|
1333
|
+
* becomes "beneath one of the roots and not beneath an excluded one".
|
|
1334
|
+
* `envWide` holds only while nothing is excluded.
|
|
1335
|
+
*
|
|
1320
1336
|
* Reads the tree, since minimality and `envWide` are questions about it; a
|
|
1321
1337
|
* root the readable hierarchy does not contain raises
|
|
1322
1338
|
* `authorizer.hierarchy_incomplete`, as a check at such a node does.
|
|
1323
1339
|
*/
|
|
1324
1340
|
async grantRoots(identityId, permission, subject = {}) {
|
|
1325
1341
|
const now = this.now();
|
|
1326
|
-
const
|
|
1342
|
+
const holdings = await this.grantRootsFor(
|
|
1327
1343
|
identityId,
|
|
1328
1344
|
subject.principalType ?? "identity"
|
|
1329
|
-
)
|
|
1345
|
+
);
|
|
1330
1346
|
const granted = new Set(
|
|
1331
|
-
|
|
1347
|
+
(holdings.roots.get(permission) ?? []).filter((window) => inEffect(window, now)).map((w) => w.node)
|
|
1332
1348
|
);
|
|
1333
1349
|
if (granted.size === 0) {
|
|
1334
|
-
return { roots: [], envWide: false };
|
|
1350
|
+
return { roots: [], excludedRoots: [], envWide: false };
|
|
1335
1351
|
}
|
|
1352
|
+
const denied = holdings.denies.get(permission);
|
|
1336
1353
|
const { parents } = await this.treeEntry();
|
|
1337
1354
|
const roots = [];
|
|
1338
|
-
let
|
|
1355
|
+
let rootGranted = false;
|
|
1339
1356
|
for (const node of granted) {
|
|
1340
1357
|
if (!parents.has(node)) {
|
|
1341
1358
|
throw this.hierarchyIncomplete(node);
|
|
1342
1359
|
}
|
|
1343
|
-
|
|
1344
|
-
|
|
1360
|
+
const path = this.lineage(parents, node);
|
|
1361
|
+
if (refusedOn(denied, path)) {
|
|
1362
|
+
continue;
|
|
1345
1363
|
}
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
for (let current = parents.get(node); current && depth < MAX_WALK_DEPTH; current = parents.get(current), depth += 1) {
|
|
1349
|
-
if (granted.has(current)) {
|
|
1350
|
-
covered = true;
|
|
1351
|
-
break;
|
|
1352
|
-
}
|
|
1364
|
+
if (parents.get(node) === null) {
|
|
1365
|
+
rootGranted = true;
|
|
1353
1366
|
}
|
|
1354
|
-
if (!
|
|
1367
|
+
if (!path.slice(1).some((n) => granted.has(n))) {
|
|
1355
1368
|
roots.push(node);
|
|
1356
1369
|
}
|
|
1357
1370
|
}
|
|
1358
|
-
|
|
1371
|
+
const excludedRoots = this.excludedBeneath(parents, denied, roots);
|
|
1372
|
+
return {
|
|
1373
|
+
roots,
|
|
1374
|
+
excludedRoots,
|
|
1375
|
+
envWide: rootGranted && excludedRoots.length === 0
|
|
1376
|
+
};
|
|
1359
1377
|
}
|
|
1360
1378
|
/**
|
|
1361
1379
|
* Where the identity may act with the permission, as the set a list query
|
|
1362
|
-
* filters by: {@link grantRoots} expanded through their descendants,
|
|
1363
|
-
* `
|
|
1380
|
+
* filters by: {@link grantRoots} expanded through their descendants, the
|
|
1381
|
+
* subtrees beneath `excludedRoots` left out, up to `maxNodes` (the
|
|
1382
|
+
* authorizer's default unless given here).
|
|
1364
1383
|
*
|
|
1365
1384
|
* Past the cap `nodes` is null and `complete` false: the set is too large
|
|
1366
|
-
* to be worth an `IN (...)` list, and the caller filters by `roots`
|
|
1367
|
-
* instead. An `envWide` answer carries no set either, since
|
|
1368
|
-
* qualifies and a filter would only slow the query down.
|
|
1385
|
+
* to be worth an `IN (...)` list, and the caller filters by `roots` and
|
|
1386
|
+
* `excludedRoots` instead. An `envWide` answer carries no set either, since
|
|
1387
|
+
* every node qualifies and a filter would only slow the query down.
|
|
1369
1388
|
*
|
|
1370
1389
|
* Computed from the same cached grant roots and tree a check uses, so a
|
|
1371
1390
|
* list filtered by it and the check on a write cannot disagree, and it goes
|
|
@@ -1373,32 +1392,55 @@ var LocalAuthorizer = class {
|
|
|
1373
1392
|
*/
|
|
1374
1393
|
async accessibleNodes(identityId, permission, options = {}) {
|
|
1375
1394
|
const cap = options.maxNodes ?? this.maxNodes;
|
|
1376
|
-
const { roots, envWide } = await this.grantRoots(
|
|
1377
|
-
|
|
1378
|
-
|
|
1395
|
+
const { roots, excludedRoots, envWide } = await this.grantRoots(
|
|
1396
|
+
identityId,
|
|
1397
|
+
permission,
|
|
1398
|
+
{
|
|
1399
|
+
...options.principalType ? { principalType: options.principalType } : {}
|
|
1400
|
+
}
|
|
1401
|
+
);
|
|
1379
1402
|
if (envWide) {
|
|
1380
|
-
return {
|
|
1403
|
+
return {
|
|
1404
|
+
roots,
|
|
1405
|
+
excludedRoots,
|
|
1406
|
+
envWide: true,
|
|
1407
|
+
nodes: null,
|
|
1408
|
+
complete: true
|
|
1409
|
+
};
|
|
1381
1410
|
}
|
|
1382
1411
|
if (roots.length === 0) {
|
|
1383
|
-
return {
|
|
1412
|
+
return {
|
|
1413
|
+
roots,
|
|
1414
|
+
excludedRoots,
|
|
1415
|
+
envWide: false,
|
|
1416
|
+
nodes: /* @__PURE__ */ new Set(),
|
|
1417
|
+
complete: true
|
|
1418
|
+
};
|
|
1384
1419
|
}
|
|
1385
1420
|
const { children } = await this.treeEntry();
|
|
1421
|
+
const excluded = new Set(excludedRoots);
|
|
1386
1422
|
const nodes = /* @__PURE__ */ new Set();
|
|
1387
1423
|
const pending = [...roots];
|
|
1388
1424
|
while (pending.length > 0) {
|
|
1389
1425
|
const node = pending.pop();
|
|
1390
|
-
if (nodes.has(node)) {
|
|
1426
|
+
if (nodes.has(node) || excluded.has(node)) {
|
|
1391
1427
|
continue;
|
|
1392
1428
|
}
|
|
1393
1429
|
nodes.add(node);
|
|
1394
1430
|
if (nodes.size > cap) {
|
|
1395
|
-
return {
|
|
1431
|
+
return {
|
|
1432
|
+
roots,
|
|
1433
|
+
excludedRoots,
|
|
1434
|
+
envWide: false,
|
|
1435
|
+
nodes: null,
|
|
1436
|
+
complete: false
|
|
1437
|
+
};
|
|
1396
1438
|
}
|
|
1397
1439
|
for (const child of children.get(node) ?? []) {
|
|
1398
1440
|
pending.push(child);
|
|
1399
1441
|
}
|
|
1400
1442
|
}
|
|
1401
|
-
return { roots, envWide: false, nodes, complete: true };
|
|
1443
|
+
return { roots, excludedRoots, envWide: false, nodes, complete: true };
|
|
1402
1444
|
}
|
|
1403
1445
|
/**
|
|
1404
1446
|
* Whether the identity holds the permission anywhere at or beneath a node,
|
|
@@ -1409,7 +1451,9 @@ var LocalAuthorizer = class {
|
|
|
1409
1451
|
* `roots` is `[subtreeRootId]` when a grant at the node or above it reaches
|
|
1410
1452
|
* the whole subtree; otherwise the topmost granted nodes inside it, the
|
|
1411
1453
|
* ones no other granted node inside it already covers. A list query filters
|
|
1412
|
-
* by "beneath one of these"
|
|
1454
|
+
* by "beneath one of these and not beneath one of `excludedRoots`", the
|
|
1455
|
+
* deny roots inside them. A deny at the node or above it refuses the whole
|
|
1456
|
+
* subtree, and a grant a deny refuses counts for nothing.
|
|
1413
1457
|
*
|
|
1414
1458
|
* **A read for menus and lists, never a guard.** Holding the permission
|
|
1415
1459
|
* somewhere in the subtree says nothing about the record a write touches:
|
|
@@ -1422,34 +1466,48 @@ var LocalAuthorizer = class {
|
|
|
1422
1466
|
*/
|
|
1423
1467
|
async holdsAnywhereUnder(identityId, permission, subtreeRootId, subject = {}) {
|
|
1424
1468
|
const now = this.now();
|
|
1425
|
-
const
|
|
1469
|
+
const holdings = await this.grantRootsFor(
|
|
1426
1470
|
identityId,
|
|
1427
1471
|
subject.principalType ?? "identity"
|
|
1428
|
-
)
|
|
1472
|
+
);
|
|
1429
1473
|
const granted = new Set(
|
|
1430
|
-
|
|
1474
|
+
(holdings.roots.get(permission) ?? []).filter((window) => inEffect(window, now)).map((w) => w.node)
|
|
1431
1475
|
);
|
|
1476
|
+
const denied = holdings.denies.get(permission);
|
|
1432
1477
|
const { parents } = await this.treeEntry();
|
|
1433
1478
|
if (!parents.has(subtreeRootId)) {
|
|
1434
1479
|
throw this.hierarchyIncomplete(subtreeRootId);
|
|
1435
1480
|
}
|
|
1436
|
-
|
|
1437
|
-
|
|
1481
|
+
const above = this.lineage(parents, subtreeRootId);
|
|
1482
|
+
if (granted.size === 0 || refusedOn(denied, above)) {
|
|
1483
|
+
return { held: false, roots: [], excludedRoots: [] };
|
|
1438
1484
|
}
|
|
1439
|
-
if (
|
|
1440
|
-
return {
|
|
1485
|
+
if (above.some((n) => granted.has(n))) {
|
|
1486
|
+
return {
|
|
1487
|
+
held: true,
|
|
1488
|
+
roots: [subtreeRootId],
|
|
1489
|
+
excludedRoots: this.excludedBeneath(parents, denied, [subtreeRootId])
|
|
1490
|
+
};
|
|
1441
1491
|
}
|
|
1442
1492
|
const roots = [];
|
|
1443
1493
|
for (const node of granted) {
|
|
1444
1494
|
if (!parents.has(node)) {
|
|
1445
1495
|
throw this.hierarchyIncomplete(node);
|
|
1446
1496
|
}
|
|
1447
|
-
const
|
|
1448
|
-
if (
|
|
1497
|
+
const path = this.lineage(parents, node);
|
|
1498
|
+
if (refusedOn(denied, path)) {
|
|
1499
|
+
continue;
|
|
1500
|
+
}
|
|
1501
|
+
const ancestors = path.slice(1);
|
|
1502
|
+
if (ancestors.includes(subtreeRootId) && !ancestors.some((n) => granted.has(n))) {
|
|
1449
1503
|
roots.push(node);
|
|
1450
1504
|
}
|
|
1451
1505
|
}
|
|
1452
|
-
return {
|
|
1506
|
+
return {
|
|
1507
|
+
held: roots.length > 0,
|
|
1508
|
+
roots,
|
|
1509
|
+
excludedRoots: this.excludedBeneath(parents, denied, roots)
|
|
1510
|
+
};
|
|
1453
1511
|
}
|
|
1454
1512
|
/** The node and every ancestor up to the root, nearest first. */
|
|
1455
1513
|
lineage(parents, nodeId) {
|
|
@@ -1460,10 +1518,32 @@ var LocalAuthorizer = class {
|
|
|
1460
1518
|
}
|
|
1461
1519
|
return path;
|
|
1462
1520
|
}
|
|
1521
|
+
/**
|
|
1522
|
+
* The deny roots strictly beneath one of `roots`: the subtrees a filter by
|
|
1523
|
+
* those roots has to cut out. A deny root at a root or above it never
|
|
1524
|
+
* reaches here, because that root was refused and left out.
|
|
1525
|
+
*/
|
|
1526
|
+
excludedBeneath(parents, denied, roots) {
|
|
1527
|
+
if (!denied || roots.length === 0) {
|
|
1528
|
+
return [];
|
|
1529
|
+
}
|
|
1530
|
+
const rootSet = new Set(roots);
|
|
1531
|
+
const excluded = [];
|
|
1532
|
+
for (const node of denied) {
|
|
1533
|
+
if (!parents.has(node)) {
|
|
1534
|
+
throw this.hierarchyIncomplete(node);
|
|
1535
|
+
}
|
|
1536
|
+
if (this.lineage(parents, node).slice(1).some((n) => rootSet.has(n))) {
|
|
1537
|
+
excluded.push(node);
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
return excluded;
|
|
1541
|
+
}
|
|
1463
1542
|
/**
|
|
1464
1543
|
* Every permission the identity holds at the node, by grant or by
|
|
1465
|
-
* inheritance: the list a screen renders
|
|
1466
|
-
* the same cached grant roots and tree a
|
|
1544
|
+
* inheritance, less those a deny refuses there: the list a screen renders
|
|
1545
|
+
* its controls from, answered from the same cached grant roots and tree a
|
|
1546
|
+
* check uses.
|
|
1467
1547
|
*/
|
|
1468
1548
|
async permissionsAt(identityId, nodeId, subject = {}) {
|
|
1469
1549
|
const answers = await this.permissionsAtMany(identityId, [nodeId], subject);
|
|
@@ -1476,7 +1556,7 @@ var LocalAuthorizer = class {
|
|
|
1476
1556
|
*/
|
|
1477
1557
|
async permissionsAtMany(identityId, nodeIds, subject = {}) {
|
|
1478
1558
|
const now = this.now();
|
|
1479
|
-
const
|
|
1559
|
+
const holdings = await this.grantRootsFor(
|
|
1480
1560
|
identityId,
|
|
1481
1561
|
subject.principalType ?? "identity"
|
|
1482
1562
|
);
|
|
@@ -1484,7 +1564,10 @@ var LocalAuthorizer = class {
|
|
|
1484
1564
|
for (const nodeId of new Set(nodeIds)) {
|
|
1485
1565
|
const path = await this.lineageOf(nodeId);
|
|
1486
1566
|
const held = [];
|
|
1487
|
-
for (const [permission, windows] of roots) {
|
|
1567
|
+
for (const [permission, windows] of holdings.roots) {
|
|
1568
|
+
if (refusedOn(holdings.denies.get(permission), path)) {
|
|
1569
|
+
continue;
|
|
1570
|
+
}
|
|
1488
1571
|
if (windows.some(
|
|
1489
1572
|
(window) => path.has(window.node) && inEffect(window, now)
|
|
1490
1573
|
)) {
|
|
@@ -1497,19 +1580,34 @@ var LocalAuthorizer = class {
|
|
|
1497
1580
|
}
|
|
1498
1581
|
async decide(query) {
|
|
1499
1582
|
const now = this.now();
|
|
1500
|
-
const
|
|
1583
|
+
const holdings = await this.grantRootsFor(
|
|
1501
1584
|
query.identity_id,
|
|
1502
1585
|
query.principal_type ?? "identity"
|
|
1503
|
-
)
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
}
|
|
1586
|
+
);
|
|
1587
|
+
const held = holdings.roots.get(query.permission) ?? [];
|
|
1588
|
+
const denied = holdings.denies.get(query.permission);
|
|
1507
1589
|
if ((query.scope ?? "node") === "app_wide") {
|
|
1508
|
-
|
|
1590
|
+
if (held.length === 0) {
|
|
1591
|
+
return { allowed: false, reason: "permission_not_held" };
|
|
1592
|
+
}
|
|
1593
|
+
const standing = held.filter((window) => inEffect(window, now));
|
|
1594
|
+
if (standing.length === 0) {
|
|
1595
|
+
return { allowed: false, reason: reasonAmong(held, now) };
|
|
1596
|
+
}
|
|
1597
|
+
return await this.anyClearOf(denied, standing) ? { allowed: true, reason: null } : { allowed: false, reason: "denied" };
|
|
1598
|
+
}
|
|
1599
|
+
if (held.length === 0 && !denied) {
|
|
1600
|
+
return { allowed: false, reason: "permission_not_held" };
|
|
1509
1601
|
}
|
|
1510
1602
|
if (!query.node_id) {
|
|
1511
1603
|
return { allowed: false, reason: "node_missing" };
|
|
1512
1604
|
}
|
|
1605
|
+
if (denied && refusedOn(denied, await this.lineageOf(query.node_id))) {
|
|
1606
|
+
return { allowed: false, reason: "denied" };
|
|
1607
|
+
}
|
|
1608
|
+
if (held.length === 0) {
|
|
1609
|
+
return { allowed: false, reason: "permission_not_held" };
|
|
1610
|
+
}
|
|
1513
1611
|
if (held.some(
|
|
1514
1612
|
(window) => window.node === query.node_id && inEffect(window, now)
|
|
1515
1613
|
)) {
|
|
@@ -1525,6 +1623,22 @@ var LocalAuthorizer = class {
|
|
|
1525
1623
|
}
|
|
1526
1624
|
return { allowed: false, reason: reasonAmong(reachable, now) };
|
|
1527
1625
|
}
|
|
1626
|
+
/**
|
|
1627
|
+
* Whether one of the grants stands clear of every deny root: none at its
|
|
1628
|
+
* node or above it. A grant with a deny strictly beneath it still reaches
|
|
1629
|
+
* the rest of its subtree. Reads the tree only when there is a deny.
|
|
1630
|
+
*/
|
|
1631
|
+
async anyClearOf(denied, windows) {
|
|
1632
|
+
if (!denied) {
|
|
1633
|
+
return true;
|
|
1634
|
+
}
|
|
1635
|
+
for (const window of windows) {
|
|
1636
|
+
if (!refusedOn(denied, await this.lineageOf(window.node))) {
|
|
1637
|
+
return true;
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
return false;
|
|
1641
|
+
}
|
|
1528
1642
|
/** Counters for observability — how much traffic the cache is actually saving. */
|
|
1529
1643
|
snapshot() {
|
|
1530
1644
|
return { ...this.stats };
|
|
@@ -1542,7 +1656,9 @@ var LocalAuthorizer = class {
|
|
|
1542
1656
|
* With no argument, everything goes: grants and the hierarchy tree. Not
|
|
1543
1657
|
* needed in normal operation, where entries expire on their own; useful in
|
|
1544
1658
|
* tests and after a change whose reach you cannot name (a role's
|
|
1545
|
-
* permissions edited, a role deactivated or reactivated, a node moved
|
|
1659
|
+
* permissions edited, a role deactivated or reactivated, a node moved, a
|
|
1660
|
+
* deny created or deleted, since a deny on a role binds everyone holding
|
|
1661
|
+
* it). A
|
|
1546
1662
|
* deactivated role grants nothing to anyone holding it, so until the cache
|
|
1547
1663
|
* is cleared or expires, its holders keep being allowed locally.
|
|
1548
1664
|
*
|
|
@@ -1604,7 +1720,7 @@ var LocalAuthorizer = class {
|
|
|
1604
1720
|
const key = subjectKey(type, identityId);
|
|
1605
1721
|
const cached = this.grants.get(key);
|
|
1606
1722
|
if (cached && cached.expiresAt > this.now()) {
|
|
1607
|
-
return cached.
|
|
1723
|
+
return cached.holdings;
|
|
1608
1724
|
}
|
|
1609
1725
|
const pending = this.pendingGrants.get(key);
|
|
1610
1726
|
if (pending) {
|
|
@@ -1630,17 +1746,35 @@ var LocalAuthorizer = class {
|
|
|
1630
1746
|
* `expired` or `not_yet_effective`, and the entry expires at the nearest
|
|
1631
1747
|
* window boundary when that comes before the TTL, so a grant takes effect
|
|
1632
1748
|
* or ends on time rather than up to `ttlMs` late.
|
|
1749
|
+
*
|
|
1750
|
+
* Every page carries the subject's deny roots whole, beside its rows; they
|
|
1751
|
+
* are gathered from each page, so a read is one snapshot of both.
|
|
1633
1752
|
*/
|
|
1634
1753
|
async fetchGrantRoots(identityId, type) {
|
|
1635
1754
|
this.stats.grantFetches += 1;
|
|
1636
1755
|
const path = GRANTS_PATH(type, identityId);
|
|
1756
|
+
const denies = /* @__PURE__ */ new Map();
|
|
1637
1757
|
const rows = paginate(
|
|
1638
1758
|
(query) => {
|
|
1639
1759
|
this.stats.grantPages += 1;
|
|
1640
1760
|
return this.client.request("GET", path, {
|
|
1641
1761
|
...this.readOptions,
|
|
1642
1762
|
query: { ...query, include: "windows" }
|
|
1643
|
-
}).then((page) =>
|
|
1763
|
+
}).then((page) => {
|
|
1764
|
+
const read = understood(
|
|
1765
|
+
page,
|
|
1766
|
+
path
|
|
1767
|
+
).denies;
|
|
1768
|
+
for (const deny of read ?? []) {
|
|
1769
|
+
const nodes = denies.get(deny.permission);
|
|
1770
|
+
if (nodes) {
|
|
1771
|
+
nodes.add(deny.node_id);
|
|
1772
|
+
} else {
|
|
1773
|
+
denies.set(deny.permission, /* @__PURE__ */ new Set([deny.node_id]));
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
return page;
|
|
1777
|
+
});
|
|
1644
1778
|
},
|
|
1645
1779
|
{ page: 1, take: PAGE_SIZE },
|
|
1646
1780
|
{ maxPages: MAX_PAGES }
|
|
@@ -1666,11 +1800,12 @@ var LocalAuthorizer = class {
|
|
|
1666
1800
|
roots.set(row.permission, [window]);
|
|
1667
1801
|
}
|
|
1668
1802
|
}
|
|
1803
|
+
const holdings = { roots, denies };
|
|
1669
1804
|
this.grants.set(subjectKey(type, identityId), {
|
|
1670
|
-
|
|
1805
|
+
holdings,
|
|
1671
1806
|
expiresAt: Math.min(now + this.ttlMs, boundary)
|
|
1672
1807
|
});
|
|
1673
|
-
return
|
|
1808
|
+
return holdings;
|
|
1674
1809
|
}
|
|
1675
1810
|
async treeEntry() {
|
|
1676
1811
|
if (this.tree && this.tree.expiresAt > this.now()) {
|