@canopy-io/node 0.8.0 → 0.9.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 +17 -0
- package/README.md +19 -2
- package/dist/index.cjs +154 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +245 -126
- package/dist/index.d.ts +245 -126
- package/dist/index.js +154 -36
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @canopy-io/node
|
|
2
2
|
|
|
3
|
+
## 0.9.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 436d639: Follows Canopy's paged hierarchy, grant, session and directory group reads. These endpoints now answer `{ items, pagination }`, at most 100 to a page, where they used to answer the whole collection.
|
|
8
|
+
|
|
9
|
+
`LocalAuthorizer`, and so `CanopyGuard`, reads every page of an identity's grant roots (now one row per permission and node) and every page of the hierarchy's parent edges, 100 at a time. Revalidation still costs one request: only the hierarchy's first page is sent with its `ETag`, and a `304` keeps the held copy. When the hierarchy has changed, every page is read again, and a rebuild over several pages is confirmed against the first page's `ETag` so a change made mid-read is not kept half-applied. `snapshot()` gains `grantPages` and `treePages`, the page requests behind `grantFetches` and `treeRequests`. Earlier versions deny every check against a Canopy that answers grants as one row per node, so upgrade before that API reaches you.
|
|
10
|
+
|
|
11
|
+
Caller shapes that changed:
|
|
12
|
+
|
|
13
|
+
- `identities.sessions(id, query?)` returns a `Paginator` instead of a promise of `{ items }`, and takes `page` and `take`. Use `for await`, `.all()` or `.first()`.
|
|
14
|
+
- `organizations.listDirectoryGroups(id, query?)` returns a `Paginator`, and takes `page`, `take`, `q` (display name or external id) and `sort_by` (`name`, the default, or `created`). The item type is exported as `OrganizationDirectoryGroupItem`.
|
|
15
|
+
- `permissions.evaluateBulk` takes between 1 and 100 checks; more is refused with a `400`.
|
|
16
|
+
- The generated types follow the API: `GET /api/v1/nodes` and `GET /api/v1/nodes/:id/tree` answer pages of flat nodes (`ScopedNodeResponseDto`, linked by `parent_node_id`) instead of a nested tree with its scope, `GET /api/v1/nodes/:id/children` is paged, and `IdentityGrantResponseDto` carries `node_id` instead of `nodes`. Several lists' `sort_by` is now typed to the columns each accepts.
|
|
17
|
+
|
|
18
|
+
`@canopy-io/nestjs` picks up the new authorizer; its own options and behaviour are unchanged.
|
|
19
|
+
|
|
3
20
|
## 0.8.0
|
|
4
21
|
|
|
5
22
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -46,11 +46,14 @@ const { allowed } = await canopy.permissions.evaluate({
|
|
|
46
46
|
node_id: nodeId,
|
|
47
47
|
});
|
|
48
48
|
|
|
49
|
-
// Pagination is handled for you, whichever style the endpoint uses.
|
|
49
|
+
// Pagination is handled for you, whichever style the endpoint uses. Every
|
|
50
|
+
// list method returns a Paginator: `for await`, `.all()` or `.first()`.
|
|
50
51
|
for await (const identity of canopy.identities.list({ take: 50 })) {
|
|
51
52
|
console.log(identity.email);
|
|
52
53
|
}
|
|
53
54
|
|
|
55
|
+
const newest = await canopy.identities.sessions(identityId).first();
|
|
56
|
+
|
|
54
57
|
// Errors carry a stable code, not just a message.
|
|
55
58
|
try {
|
|
56
59
|
await canopy.assignments.create({
|
|
@@ -67,6 +70,10 @@ try {
|
|
|
67
70
|
}
|
|
68
71
|
```
|
|
69
72
|
|
|
73
|
+
Lists answer 20 items a page by default and at most 100 (`take`).
|
|
74
|
+
`permissions.evaluateBulk` takes 1 to 100 checks in one request; more is
|
|
75
|
+
refused with a `400` and nothing is evaluated, so send a larger set in batches.
|
|
76
|
+
|
|
70
77
|
Anything without a typed wrapper is reachable the same way, with the same envelope handling and retry policy:
|
|
71
78
|
|
|
72
79
|
```ts
|
|
@@ -243,10 +250,20 @@ const { allowed } = await authorizer.evaluate({
|
|
|
243
250
|
|
|
244
251
|
It holds two things: the identity's grant roots, and one copy of your hierarchy
|
|
245
252
|
— read as parent edges from `GET /api/v1/nodes/parents` rather than the full
|
|
246
|
-
|
|
253
|
+
node list, which is 3.7x smaller — shared across every identity in the process. A check walks up from the node in
|
|
247
254
|
question looking for a grant root — a grant already means _this node and
|
|
248
255
|
everything beneath it_, so the roots are never expanded into their descendants.
|
|
249
256
|
|
|
257
|
+
Both reads are paged, and the authorizer reads every page, 100 rows at a time:
|
|
258
|
+
`GET /api/v1/identities/:id/grants` answers one row per permission and node
|
|
259
|
+
it was granted at, and the hierarchy arrives 100 parent edges to a page. Only
|
|
260
|
+
the hierarchy's first page is revalidated: its `ETag` covers the whole
|
|
261
|
+
hierarchy, so a `304` keeps the copy held without reading any other page. When
|
|
262
|
+
the hierarchy has changed, every page is read again and the copy rebuilt; a
|
|
263
|
+
rebuild over several pages is then confirmed against the first page's `ETag`,
|
|
264
|
+
and read again if the hierarchy changed while its pages were being read. At
|
|
265
|
+
50,000 nodes a rebuild is 501 requests, and a revalidation is one.
|
|
266
|
+
|
|
250
267
|
Both are held for at most 60 seconds, which is the delay between an access
|
|
251
268
|
change and it taking effect, including a moved node. Set `ttlMs` to change it,
|
|
252
269
|
up to five minutes (`300_000`); a longer value throws when the authorizer is
|
package/dist/index.cjs
CHANGED
|
@@ -615,11 +615,23 @@ var Identities = class {
|
|
|
615
615
|
* newest first: when and where it was signed in, when it expires, and the
|
|
616
616
|
* organization it is acting in. Read it back after a revoke to confirm,
|
|
617
617
|
* from your own side, that a sign-out took.
|
|
618
|
+
*
|
|
619
|
+
* Paginated (20 per page by default, `take` up to 100), so this returns a
|
|
620
|
+
* `Paginator`: iterate it for every session, or call `.first()` for the
|
|
621
|
+
* newest. The list takes `page` and `take` only; its order is fixed.
|
|
622
|
+
*
|
|
623
|
+
* ```ts
|
|
624
|
+
* for await (const session of canopy.identities.sessions(id)) { … }
|
|
625
|
+
* ```
|
|
618
626
|
*/
|
|
619
|
-
sessions(id) {
|
|
620
|
-
return
|
|
621
|
-
|
|
622
|
-
|
|
627
|
+
sessions(id, query = {}) {
|
|
628
|
+
return paginate(
|
|
629
|
+
(params) => this.client.request(
|
|
630
|
+
"GET",
|
|
631
|
+
`/api/v1/identities/${encodeURIComponent(id)}/sessions`,
|
|
632
|
+
{ query: params }
|
|
633
|
+
),
|
|
634
|
+
{ ...query }
|
|
623
635
|
);
|
|
624
636
|
}
|
|
625
637
|
/**
|
|
@@ -915,11 +927,25 @@ var Organizations = class {
|
|
|
915
927
|
`/api/v1/organizations/${enc(id)}/directory/activity`
|
|
916
928
|
);
|
|
917
929
|
}
|
|
918
|
-
/**
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
930
|
+
/**
|
|
931
|
+
* The groups the provider has pushed, each with its role mapping.
|
|
932
|
+
*
|
|
933
|
+
* Paginated (20 per page by default, `take` up to 100), so this returns a
|
|
934
|
+
* `Paginator`. Sorted by display name unless `sort_by: "created"`; `q`
|
|
935
|
+
* searches display names and external ids.
|
|
936
|
+
*
|
|
937
|
+
* ```ts
|
|
938
|
+
* for await (const group of canopy.organizations.listDirectoryGroups(id)) { … }
|
|
939
|
+
* ```
|
|
940
|
+
*/
|
|
941
|
+
listDirectoryGroups(id, query = {}) {
|
|
942
|
+
return paginate(
|
|
943
|
+
(params) => this.client.request(
|
|
944
|
+
"GET",
|
|
945
|
+
`/api/v1/organizations/${enc(id)}/directory/groups`,
|
|
946
|
+
{ query: params }
|
|
947
|
+
),
|
|
948
|
+
{ ...query }
|
|
923
949
|
);
|
|
924
950
|
}
|
|
925
951
|
/**
|
|
@@ -981,6 +1007,10 @@ var Permissions = class {
|
|
|
981
1007
|
*
|
|
982
1008
|
* Prefer this to a loop over `evaluate` when rendering a screen: the checks
|
|
983
1009
|
* are answered together instead of paying request latency for each.
|
|
1010
|
+
*
|
|
1011
|
+
* One request carries between 1 and 100 checks. More is refused with a
|
|
1012
|
+
* `400` (`CanopyError`, `statusCode` 400) and nothing is evaluated, so split
|
|
1013
|
+
* a larger set into batches of 100.
|
|
984
1014
|
*/
|
|
985
1015
|
evaluateBulk(input, options = {}) {
|
|
986
1016
|
return this.client.request("POST", "/api/v1/permissions/evaluate/bulk", {
|
|
@@ -1122,6 +1152,11 @@ var Canopy = class {
|
|
|
1122
1152
|
var DEFAULT_TTL_MS = 6e4;
|
|
1123
1153
|
var MAX_TTL_MS = 3e5;
|
|
1124
1154
|
var MAX_WALK_DEPTH = 256;
|
|
1155
|
+
var PAGE_SIZE = 100;
|
|
1156
|
+
var MAX_PAGES = 1e4;
|
|
1157
|
+
var MAX_TREE_WALKS = 3;
|
|
1158
|
+
var GRANTS_PATH = (identityId) => `/api/v1/identities/${encodeURIComponent(identityId)}/grants`;
|
|
1159
|
+
var PARENTS_PATH = "/api/v1/nodes/parents";
|
|
1125
1160
|
var LocalAuthorizer = class {
|
|
1126
1161
|
client;
|
|
1127
1162
|
ttlMs;
|
|
@@ -1141,7 +1176,9 @@ var LocalAuthorizer = class {
|
|
|
1141
1176
|
pendingTree;
|
|
1142
1177
|
stats = {
|
|
1143
1178
|
grantFetches: 0,
|
|
1179
|
+
grantPages: 0,
|
|
1144
1180
|
treeRequests: 0,
|
|
1181
|
+
treePages: 0,
|
|
1145
1182
|
treeNotModified: 0
|
|
1146
1183
|
};
|
|
1147
1184
|
constructor(client, options = {}) {
|
|
@@ -1276,16 +1313,36 @@ var LocalAuthorizer = class {
|
|
|
1276
1313
|
this.pendingGrants.set(identityId, read);
|
|
1277
1314
|
return read;
|
|
1278
1315
|
}
|
|
1316
|
+
/**
|
|
1317
|
+
* Every page of the identity's grant roots, folded into permission → nodes.
|
|
1318
|
+
*
|
|
1319
|
+
* Each row is one permission at one node, so a permission granted at three
|
|
1320
|
+
* nodes arrives as three rows, possibly on different pages. Stopping after
|
|
1321
|
+
* the first page would under-report what the identity holds: a false deny,
|
|
1322
|
+
* the safe direction, but a wrong answer all the same.
|
|
1323
|
+
*/
|
|
1279
1324
|
async fetchGrantRoots(identityId) {
|
|
1280
1325
|
this.stats.grantFetches += 1;
|
|
1281
|
-
const
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1326
|
+
const path = GRANTS_PATH(identityId);
|
|
1327
|
+
const rows = paginate(
|
|
1328
|
+
(query) => {
|
|
1329
|
+
this.stats.grantPages += 1;
|
|
1330
|
+
return this.client.request("GET", path, {
|
|
1331
|
+
...this.readOptions,
|
|
1332
|
+
query
|
|
1333
|
+
});
|
|
1334
|
+
},
|
|
1335
|
+
{ page: 1, take: PAGE_SIZE },
|
|
1336
|
+
{ maxPages: MAX_PAGES }
|
|
1285
1337
|
);
|
|
1286
1338
|
const roots = /* @__PURE__ */ new Map();
|
|
1287
|
-
for (const row of
|
|
1288
|
-
roots.
|
|
1339
|
+
for await (const row of rows) {
|
|
1340
|
+
const nodes = roots.get(row.permission);
|
|
1341
|
+
if (nodes) {
|
|
1342
|
+
nodes.add(row.node_id);
|
|
1343
|
+
} else {
|
|
1344
|
+
roots.set(row.permission, /* @__PURE__ */ new Set([row.node_id]));
|
|
1345
|
+
}
|
|
1289
1346
|
}
|
|
1290
1347
|
this.grants.set(identityId, {
|
|
1291
1348
|
roots,
|
|
@@ -1310,43 +1367,104 @@ var LocalAuthorizer = class {
|
|
|
1310
1367
|
* Revalidate rather than re-read. The hierarchy is the expensive half and
|
|
1311
1368
|
* the one that changes least, so the common case is a `304` and no transfer
|
|
1312
1369
|
* at all — the tree stays in memory and only its expiry moves.
|
|
1370
|
+
*
|
|
1371
|
+
* The edges come a page at a time, so only the first page is conditional:
|
|
1372
|
+
* its validator covers the whole hierarchy, and a `304` to it means no page
|
|
1373
|
+
* has changed. On a `200` the remaining pages are read plainly and the map
|
|
1374
|
+
* is rebuilt from every page.
|
|
1375
|
+
*
|
|
1376
|
+
* A walk over several pages is not one snapshot. A node added or removed
|
|
1377
|
+
* between two page reads shifts the rows under the offset, so a row can be
|
|
1378
|
+
* skipped, which would leave a node out of the map until the next refresh.
|
|
1379
|
+
* So the walk is confirmed by asking for the first page again with the
|
|
1380
|
+
* validator it came with: a `304` proves nothing changed while the pages
|
|
1381
|
+
* were read. A `200` is a fresh first page, and the walk starts again from
|
|
1382
|
+
* it, up to {@link MAX_TREE_WALKS} times.
|
|
1313
1383
|
*/
|
|
1314
1384
|
async fetchTree() {
|
|
1315
1385
|
this.stats.treeRequests += 1;
|
|
1316
1386
|
const held = this.tree;
|
|
1317
|
-
|
|
1387
|
+
let first = await this.parentsFirstPage(held?.etag ?? void 0);
|
|
1388
|
+
if (!first.modified) {
|
|
1389
|
+
if (held) {
|
|
1390
|
+
this.stats.treeNotModified += 1;
|
|
1391
|
+
this.tree = { ...held, expiresAt: this.now() + this.ttlMs };
|
|
1392
|
+
return held.parents;
|
|
1393
|
+
}
|
|
1394
|
+
return this.keepTree([], null);
|
|
1395
|
+
}
|
|
1396
|
+
for (let walk = 1; ; walk++) {
|
|
1397
|
+
const edges = await this.parentsRemainingPages(first.data);
|
|
1398
|
+
if (!spansPages(first.data) || first.etag === null) {
|
|
1399
|
+
return this.keepTree(edges, first.etag);
|
|
1400
|
+
}
|
|
1401
|
+
const confirm = await this.parentsFirstPage(first.etag);
|
|
1402
|
+
if (!confirm.modified) {
|
|
1403
|
+
return this.keepTree(edges, first.etag);
|
|
1404
|
+
}
|
|
1405
|
+
if (walk >= MAX_TREE_WALKS) {
|
|
1406
|
+
return this.keepTree(edges, null);
|
|
1407
|
+
}
|
|
1408
|
+
first = confirm;
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
parentsFirstPage(etag) {
|
|
1412
|
+
this.stats.treePages += 1;
|
|
1413
|
+
return this.client.requestConditional(
|
|
1318
1414
|
"GET",
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
this.readOptions
|
|
1415
|
+
PARENTS_PATH,
|
|
1416
|
+
etag,
|
|
1417
|
+
{ ...this.readOptions, query: { page: 1, take: PAGE_SIZE } }
|
|
1418
|
+
);
|
|
1419
|
+
}
|
|
1420
|
+
/** The first page's edges followed by every later page's. */
|
|
1421
|
+
async parentsRemainingPages(first) {
|
|
1422
|
+
const edges = [...first.items];
|
|
1423
|
+
if (!spansPages(first)) {
|
|
1424
|
+
return edges;
|
|
1425
|
+
}
|
|
1426
|
+
const rest = paginate(
|
|
1427
|
+
(query) => {
|
|
1428
|
+
this.stats.treePages += 1;
|
|
1429
|
+
return this.client.request("GET", PARENTS_PATH, {
|
|
1430
|
+
...this.readOptions,
|
|
1431
|
+
query
|
|
1432
|
+
});
|
|
1433
|
+
},
|
|
1434
|
+
{ page: first.pagination.page + 1, take: PAGE_SIZE },
|
|
1435
|
+
{ maxPages: MAX_PAGES - 1 }
|
|
1322
1436
|
);
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1437
|
+
for await (const page of rest.pages()) {
|
|
1438
|
+
for (const edge of page.items) {
|
|
1439
|
+
edges.push(edge);
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
return edges;
|
|
1443
|
+
}
|
|
1444
|
+
keepTree(edges, etag) {
|
|
1445
|
+
const ids = /* @__PURE__ */ new Map();
|
|
1446
|
+
for (const edge of edges) {
|
|
1447
|
+
ids.set(edge.id, edge.id);
|
|
1327
1448
|
}
|
|
1328
1449
|
const parents = /* @__PURE__ */ new Map();
|
|
1329
|
-
|
|
1330
|
-
const
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
const parent = edge.parent_node_id ?? null;
|
|
1336
|
-
parents.set(
|
|
1337
|
-
edge.id,
|
|
1338
|
-
parent === null ? null : ids.get(parent) ?? parent
|
|
1339
|
-
);
|
|
1340
|
-
}
|
|
1450
|
+
for (const edge of edges) {
|
|
1451
|
+
const parent = edge.parent_node_id ?? null;
|
|
1452
|
+
parents.set(
|
|
1453
|
+
edge.id,
|
|
1454
|
+
parent === null ? null : ids.get(parent) ?? parent
|
|
1455
|
+
);
|
|
1341
1456
|
}
|
|
1342
1457
|
this.tree = {
|
|
1343
1458
|
parents,
|
|
1344
|
-
etag
|
|
1459
|
+
etag,
|
|
1345
1460
|
expiresAt: this.now() + this.ttlMs
|
|
1346
1461
|
};
|
|
1347
1462
|
return parents;
|
|
1348
1463
|
}
|
|
1349
1464
|
};
|
|
1465
|
+
function spansPages(first) {
|
|
1466
|
+
return first.pagination.has_next_page && first.items.length > 0;
|
|
1467
|
+
}
|
|
1350
1468
|
|
|
1351
1469
|
// src/status.ts
|
|
1352
1470
|
function tokenErrorStatus(error) {
|