@canopy-io/node 0.25.1 → 0.26.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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @canopy-io/node
2
2
 
3
+ ## 0.26.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 907bfbb: `LocalAuthorizer` revalidates a subject's grants instead of re-reading them. A refresh sends the `ETag` the held grants came with as `If-None-Match`, and Canopy answers an unchanged subject with `304` after reading only its access versions, so the grants stay in memory and only their expiry moves. Changed grants are read in full, as before. The cache time and what it promises are unchanged: a change still takes effect within `ttlMs`, and a grant still starts or ends at its window's edge. `snapshot()` adds `grantNotModified`. The generated types follow Canopy's current spec, which adds the grants `304` and two branding operations (`ApiBrandingController_uploadImage`, `ApiBrandingController_previewLink`), not wrapped yet. No caller changes are needed.
8
+
3
9
  ## 0.25.1
4
10
 
5
11
  ### Patch Changes
package/README.md CHANGED
@@ -410,13 +410,18 @@ everything beneath it_, so the roots are never expanded into their descendants.
410
410
 
411
411
  Both reads are paged, and the authorizer reads every page, 100 rows at a time:
412
412
  `GET /api/v1/identities/:id/grants` answers one row per permission and node
413
- it was granted at, and the hierarchy arrives 100 parent edges to a page. Only
414
- the hierarchy's first page is revalidated: its `ETag` covers the whole
415
- hierarchy, so a `304` keeps the copy held without reading any other page. When
416
- the hierarchy has changed, every page is read again and the copy rebuilt; a
417
- rebuild over several pages is then confirmed against the first page's `ETag`,
418
- and read again if the hierarchy changed while its pages were being read. At
419
- 50,000 nodes a rebuild is 501 requests, and a revalidation is one.
413
+ it was granted at, and the hierarchy arrives 100 parent edges to a page. Both
414
+ are revalidated rather than re-read: the first page is asked for with the
415
+ `ETag` the held copy came with, and its `ETag` covers every page, so a `304`
416
+ keeps the copy held without reading any other page. Canopy answers an unchanged
417
+ identity's grants with a `304` after reading only its access versions, so the
418
+ usual refresh is one small request on both sides. When the hierarchy has
419
+ changed, every page is read again and the copy rebuilt; a rebuild over several
420
+ pages is then confirmed against the first page's `ETag`, and read again if the
421
+ hierarchy changed while its pages were being read. At 50,000 nodes a rebuild is
422
+ 501 requests, and a revalidation is one. Changed grants are read in full, and
423
+ `snapshot()` counts the revalidations answered `304` as `grantNotModified` and
424
+ `treeNotModified`.
420
425
 
421
426
  Both are held for at most 60 seconds, which is the delay between an access
422
427
  change and it taking effect, including a moved node, unless a webhook or the
package/dist/index.cjs CHANGED
@@ -1354,6 +1354,19 @@ function understood(page, path) {
1354
1354
  var GRANTS_PATH = (type, id) => type === "service" ? `/api/v1/oauth-clients/${encodeURIComponent(id)}/grants` : `/api/v1/identities/${encodeURIComponent(id)}/grants`;
1355
1355
  var subjectKey = (type, id) => `${type}:${id}`;
1356
1356
  var PARENTS_PATH = "/api/v1/nodes/parents";
1357
+ function nextWindowEdge(holdings, now) {
1358
+ let edge = Number.POSITIVE_INFINITY;
1359
+ for (const windows of holdings.roots.values()) {
1360
+ for (const window of windows) {
1361
+ for (const at of [window.from, window.to]) {
1362
+ if (at !== null && at > now && at < edge) {
1363
+ edge = at;
1364
+ }
1365
+ }
1366
+ }
1367
+ }
1368
+ return edge;
1369
+ }
1357
1370
  function inEffect(window, now) {
1358
1371
  return (window.from === null || window.from <= now) && (window.to === null || window.to > now);
1359
1372
  }
@@ -1401,6 +1414,7 @@ var LocalAuthorizer = class {
1401
1414
  stats = {
1402
1415
  grantFetches: 0,
1403
1416
  grantPages: 0,
1417
+ grantNotModified: 0,
1404
1418
  treeRequests: 0,
1405
1419
  treePages: 0,
1406
1420
  treeNotModified: 0
@@ -2011,50 +2025,87 @@ var LocalAuthorizer = class {
2011
2025
  *
2012
2026
  * Every page carries the subject's deny roots whole, beside its rows; they
2013
2027
  * are gathered from each page, so a read is one snapshot of both.
2028
+ *
2029
+ * Revalidate rather than re-read. The first page is asked for with the
2030
+ * validator the held grants came with, and a `304` means nothing that
2031
+ * decides this subject's access has changed: the grants stay in memory and
2032
+ * only their expiry moves. Canopy answers it after reading the subject's
2033
+ * access versions alone, so the common refresh costs one small read on both
2034
+ * sides. Only the first page is conditional; its validator covers every
2035
+ * page.
2036
+ *
2037
+ * A read over several pages is not confirmed the way the hierarchy's is.
2038
+ * The validator kept is the first page's, taken before any later page was
2039
+ * read, so a change made during the walk moves the subject's version past
2040
+ * it, and the next refresh reads every page again.
2014
2041
  */
2015
2042
  async fetchGrantRoots(identityId, type, stillCurrent = () => true) {
2016
2043
  this.stats.grantFetches += 1;
2044
+ const key = subjectKey(type, identityId);
2045
+ const held = this.grants.get(key);
2017
2046
  const path = GRANTS_PATH(type, identityId);
2018
2047
  const denies = /* @__PURE__ */ new Map();
2019
- const rows = paginate(
2020
- (query) => {
2021
- this.stats.grantPages += 1;
2022
- return this.client.request("GET", path, {
2023
- ...this.readOptions,
2024
- query: { ...query, include: "windows" }
2025
- }).then((page) => {
2026
- const read = understood(
2027
- page,
2028
- path
2029
- ).denies;
2030
- for (const deny of read ?? []) {
2031
- const nodes = denies.get(deny.permission);
2032
- if (nodes) {
2033
- nodes.add(deny.node_id);
2034
- } else {
2035
- denies.set(deny.permission, /* @__PURE__ */ new Set([deny.node_id]));
2036
- }
2037
- }
2038
- return page;
2039
- });
2040
- },
2041
- { page: 1, take: PAGE_SIZE },
2042
- { maxPages: MAX_PAGES }
2048
+ const query = { take: PAGE_SIZE, include: "windows" };
2049
+ const gather = (page) => {
2050
+ const read = understood(
2051
+ page,
2052
+ path
2053
+ ).denies;
2054
+ for (const deny of read ?? []) {
2055
+ const nodes = denies.get(deny.permission);
2056
+ if (nodes) {
2057
+ nodes.add(deny.node_id);
2058
+ } else {
2059
+ denies.set(deny.permission, /* @__PURE__ */ new Set([deny.node_id]));
2060
+ }
2061
+ }
2062
+ return page;
2063
+ };
2064
+ this.stats.grantPages += 1;
2065
+ const first = await this.client.requestConditional(
2066
+ "GET",
2067
+ path,
2068
+ held?.etag ?? void 0,
2069
+ { ...this.readOptions, query: { ...query, page: 1 } }
2043
2070
  );
2071
+ if (!first.modified) {
2072
+ if (held) {
2073
+ this.stats.grantNotModified += 1;
2074
+ return this.keepGrants(key, held.holdings, held.etag, stillCurrent);
2075
+ }
2076
+ return this.keepGrants(
2077
+ key,
2078
+ { roots: /* @__PURE__ */ new Map(), denies },
2079
+ null,
2080
+ stillCurrent
2081
+ );
2082
+ }
2083
+ const rows = [...gather(first.data).items];
2084
+ if (spansPages(first.data)) {
2085
+ const rest = paginate(
2086
+ (pageQuery) => {
2087
+ this.stats.grantPages += 1;
2088
+ return this.client.request("GET", path, {
2089
+ ...this.readOptions,
2090
+ query: { ...pageQuery, include: query.include }
2091
+ }).then(gather);
2092
+ },
2093
+ { page: first.data.pagination.page + 1, take: PAGE_SIZE },
2094
+ { maxPages: MAX_PAGES - 1 }
2095
+ );
2096
+ for await (const page of rest.pages()) {
2097
+ for (const row of page.items) {
2098
+ rows.push(row);
2099
+ }
2100
+ }
2101
+ }
2044
2102
  const roots = /* @__PURE__ */ new Map();
2045
- const now = this.now();
2046
- let boundary = Number.POSITIVE_INFINITY;
2047
- for await (const row of rows) {
2103
+ for (const row of rows) {
2048
2104
  const window = {
2049
2105
  node: row.node_id,
2050
2106
  from: row.effective_from ? Date.parse(row.effective_from) : null,
2051
2107
  to: row.effective_to ? Date.parse(row.effective_to) : null
2052
2108
  };
2053
- for (const edge of [window.from, window.to]) {
2054
- if (edge !== null && edge > now && edge < boundary) {
2055
- boundary = edge;
2056
- }
2057
- }
2058
2109
  const windows = roots.get(row.permission);
2059
2110
  if (windows) {
2060
2111
  windows.push(window);
@@ -2062,11 +2113,20 @@ var LocalAuthorizer = class {
2062
2113
  roots.set(row.permission, [window]);
2063
2114
  }
2064
2115
  }
2065
- const holdings = { roots, denies };
2116
+ return this.keepGrants(key, { roots, denies }, first.etag, stillCurrent);
2117
+ }
2118
+ /**
2119
+ * Hold a subject's grants until the TTL, or until the nearest window edge
2120
+ * still ahead when that comes sooner, so a grant takes effect or ends on
2121
+ * time.
2122
+ */
2123
+ keepGrants(key, holdings, etag, stillCurrent) {
2124
+ const now = this.now();
2066
2125
  if (stillCurrent()) {
2067
- this.grants.set(subjectKey(type, identityId), {
2126
+ this.grants.set(key, {
2068
2127
  holdings,
2069
- expiresAt: Math.min(now + this.ttlMs, boundary)
2128
+ etag,
2129
+ expiresAt: Math.min(now + this.ttlMs, nextWindowEdge(holdings, now))
2070
2130
  });
2071
2131
  }
2072
2132
  return holdings;