canopycms-cdk 0.5.0 → 0.6.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.
@@ -268,6 +268,19 @@ export interface CanopyCmsServiceProps {
268
268
  * not relieve you of supplying those two separately.
269
269
  */
270
270
  clerkSecretKeySecretJsonField?: string;
271
+ /**
272
+ * Whether the worker maps Clerk Organizations onto Canopy groups (default
273
+ * true). Stamped into the GitHub gateway's
274
+ * `CANOPYCMS_CLERK_ORGANIZATIONS_AS_GROUPS` only when set.
275
+ *
276
+ * In a deployment this, not `createClerkAuthPlugin`'s
277
+ * `useOrganizationsAsGroups`, decides what groups the editor sees: the
278
+ * Lambda serves auth from the cache the worker writes. Set it to match the
279
+ * plugin option. Set it `false` when Organizations is disabled on the Clerk
280
+ * instance; otherwise every refresh logs a warning and writes no groups. A
281
+ * worker bundle from before this prop existed ignores it.
282
+ */
283
+ clerkOrganizationsAsGroups?: boolean;
271
284
  /**
272
285
  * The branch editing branches fork from (default: unset, so the worker
273
286
  * detects it: the branch remote.git's HEAD names, or before remote.git exists
@@ -1198,6 +1198,13 @@ export class CanopyCmsService extends Construct {
1198
1198
  'gateway',
1199
1199
  ]);
1200
1200
  }
1201
+ if (props.clerkOrganizationsAsGroups !== undefined) {
1202
+ envEntries.push([
1203
+ 'CANOPYCMS_CLERK_ORGANIZATIONS_AS_GROUPS',
1204
+ String(props.clerkOrganizationsAsGroups),
1205
+ 'gateway',
1206
+ ]);
1207
+ }
1201
1208
  if (workerCapacity.type === 'spot') {
1202
1209
  // The termination watch is the gateway's.
1203
1210
  envEntries.push([WORKER_CAPACITY_ENV, 'spot', 'gateway']);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canopycms-cdk",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "AWS CDK constructs and EC2 worker for CanopyCMS deployment",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -34,7 +34,7 @@
34
34
  "peerDependencies": {
35
35
  "aws-cdk-lib": "^2.260.0",
36
36
  "constructs": "^10.0.0",
37
- "canopycms": "0.5.0"
37
+ "canopycms": "0.6.0"
38
38
  },
39
39
  "peerDependenciesMeta": {
40
40
  "aws-cdk-lib": {
@@ -60,8 +60,8 @@
60
60
  "tsx": "^4.19.0",
61
61
  "typescript": "^5.6.3",
62
62
  "vitest": "^4.1.0",
63
- "canopycms": "0.5.0",
64
- "canopycms-auth-clerk": "0.5.0"
63
+ "canopycms": "0.6.0",
64
+ "canopycms-auth-clerk": "0.6.0"
65
65
  },
66
66
  "scripts": {
67
67
  "build": "rm -rf dist && tsc && node ../../scripts/add-js-extensions.mjs dist",
@@ -81753,8 +81753,8 @@ var LocalGitHubGateway = class {
81753
81753
  }
81754
81754
  /**
81755
81755
  * One fetch serves every caller: concurrent callers join it, and its result is answered again
81756
- * for {@link AUTH_SNAPSHOT_REUSE_MS}. A fetch is a request per user, so a worker asking in a loop
81757
- * would otherwise hold the provider's API at its rate limit. The fetch runs apart from every
81756
+ * for {@link AUTH_SNAPSHOT_REUSE_MS}. A fetch pages the provider's whole user and membership lists,
81757
+ * so a worker asking in a loop would otherwise hold the provider's API at its rate limit. The fetch runs apart from every
81758
81758
  * caller's signal, which ends only that caller's wait, so a retry after a timed-out wait joins it.
81759
81759
  */
81760
81760
  async authSnapshot(signal) {
@@ -82491,7 +82491,7 @@ init_error();
82491
82491
  init_worker_status();
82492
82492
 
82493
82493
  // ../canopycms/src/version.ts
82494
- var CANOPYCMS_VERSION = "0.5.0";
82494
+ var CANOPYCMS_VERSION = "0.6.0";
82495
82495
 
82496
82496
  // ../canopycms/src/worker/schema-gate.ts
82497
82497
  import fs14 from "node:fs/promises";
@@ -97747,6 +97747,20 @@ function toUserSearchResult(user) {
97747
97747
  function unwrapClerkResponse(response) {
97748
97748
  return Array.isArray(response) ? response : response.data;
97749
97749
  }
97750
+ function clerkErrorStatus(err) {
97751
+ if (typeof err !== "object" || err === null || !("status" in err)) return void 0;
97752
+ const status = err.status;
97753
+ return typeof status === "number" ? status : void 0;
97754
+ }
97755
+ function clerkErrorCode(err) {
97756
+ if (typeof err !== "object" || err === null || !("errors" in err)) return void 0;
97757
+ const { errors } = err;
97758
+ if (!Array.isArray(errors)) return void 0;
97759
+ const first2 = errors[0];
97760
+ if (typeof first2 !== "object" || first2 === null || !("code" in first2)) return void 0;
97761
+ const { code } = first2;
97762
+ return typeof code === "string" ? code : void 0;
97763
+ }
97750
97764
  async function fetchClerkSnapshot(options) {
97751
97765
  const {
97752
97766
  secretKey,
@@ -97762,50 +97776,62 @@ async function fetchClerkSnapshot(options) {
97762
97776
  limit: pageSize,
97763
97777
  offset
97764
97778
  });
97765
- const page = unwrapClerkResponse(usersResponse);
97766
- clerkUsers.push(...page);
97767
- if (page.length < pageSize) break;
97779
+ const page2 = unwrapClerkResponse(usersResponse);
97780
+ clerkUsers.push(...page2);
97781
+ if (page2.length < pageSize) break;
97768
97782
  offset += pageSize;
97769
97783
  }
97770
97784
  const users = clerkUsers.map(toUserSearchResult);
97771
- let groups = [];
97772
- const memberships = {};
97773
- if (useOrganizationsAsGroups) {
97774
- const clerkOrgs = [];
97775
- let orgOffset = 0;
97776
- const orgPageSize = 100;
97777
- while (true) {
97778
- const orgsResponse = await clerkClient.organizations.getOrganizationList({
97779
- limit: orgPageSize,
97780
- offset: orgOffset
97781
- });
97782
- const page = unwrapClerkResponse(orgsResponse);
97783
- clerkOrgs.push(...page);
97784
- if (page.length < orgPageSize) break;
97785
- orgOffset += orgPageSize;
97786
- }
97787
- groups = clerkOrgs.map((o8) => ({
97788
- id: o8.id,
97789
- name: o8.name,
97790
- memberCount: o8.membersCount ?? o8.members_count
97791
- }));
97792
- for (const user of clerkUsers) {
97793
- try {
97794
- const membershipResponse = await clerkClient.users.getOrganizationMembershipList({
97795
- userId: user.id
97796
- });
97797
- const userMemberships = unwrapClerkResponse(membershipResponse);
97798
- if (userMemberships.length > 0) {
97799
- memberships[user.id] = userMemberships.map((m7) => m7.organization.id);
97800
- }
97801
- } catch (err) {
97802
- warn(
97803
- `Failed to fetch memberships for user ${user.id}:`,
97804
- err instanceof Error ? err.message : err
97805
- );
97806
- }
97807
- }
97785
+ if (!useOrganizationsAsGroups) return { users, groups: [], memberships: {} };
97786
+ const fetchOrgPage = async (orgOffset) => unwrapClerkResponse(
97787
+ await clerkClient.organizations.getOrganizationList({
97788
+ limit: pageSize,
97789
+ offset: orgOffset
97790
+ })
97791
+ );
97792
+ let page;
97793
+ try {
97794
+ page = await fetchOrgPage(0);
97795
+ } catch (err) {
97796
+ if (clerkErrorStatus(err) !== 403) throw err;
97797
+ const code = clerkErrorCode(err);
97798
+ warn(
97799
+ `Clerk refused the Organizations API (403${code ? ` ${code}` : ""}): Organizations is probably disabled on this Clerk instance. Writing users with no groups; set clerkOrganizationsAsGroups: false (CANOPYCMS_CLERK_ORGANIZATIONS_AS_GROUPS=false) to stop asking.`
97800
+ );
97801
+ return { users, groups: [], memberships: {} };
97802
+ }
97803
+ const clerkOrgs = [...page];
97804
+ for (let orgOffset = pageSize; page.length === pageSize; orgOffset += pageSize) {
97805
+ page = await fetchOrgPage(orgOffset);
97806
+ clerkOrgs.push(...page);
97807
+ }
97808
+ const memberSets = /* @__PURE__ */ new Map();
97809
+ let membershipOffset = 0;
97810
+ while (true) {
97811
+ const membershipResponse = await clerkClient.organizations.getInstanceOrganizationMembershipList({
97812
+ limit: pageSize,
97813
+ offset: membershipOffset,
97814
+ orderBy: "+created_at"
97815
+ });
97816
+ const membershipPage = unwrapClerkResponse(membershipResponse);
97817
+ for (const membership of membershipPage) {
97818
+ const userId = membership.publicUserData?.userId;
97819
+ const orgId = membership.organization?.id;
97820
+ if (!userId || !orgId) continue;
97821
+ const orgIds = memberSets.get(userId) ?? /* @__PURE__ */ new Set();
97822
+ orgIds.add(orgId);
97823
+ memberSets.set(userId, orgIds);
97824
+ }
97825
+ if (membershipPage.length < pageSize) break;
97826
+ membershipOffset += pageSize;
97808
97827
  }
97828
+ const memberships = {};
97829
+ for (const [userId, orgIds] of memberSets) memberships[userId] = [...orgIds];
97830
+ const groups = clerkOrgs.map((o8) => ({
97831
+ id: o8.id,
97832
+ name: o8.name,
97833
+ memberCount: o8.membersCount ?? o8.members_count
97834
+ }));
97809
97835
  return { users, groups, memberships };
97810
97836
  }
97811
97837
  async function refreshClerkCache(options) {
@@ -97849,16 +97875,25 @@ async function withKeyReread(secret, key, log6, attempt) {
97849
97875
  return await attempt(rotated);
97850
97876
  }
97851
97877
  }
97878
+ var CLERK_ORGANIZATIONS_AS_GROUPS_ENV = "CANOPYCMS_CLERK_ORGANIZATIONS_AS_GROUPS";
97879
+ function parseClerkOrganizationsAsGroups(env2) {
97880
+ const value = env2[CLERK_ORGANIZATIONS_AS_GROUPS_ENV];
97881
+ if (value === void 0 || value === "" || value === "true") return true;
97882
+ if (value === "false") return false;
97883
+ throw new Error(
97884
+ `${CLERK_ORGANIZATIONS_AS_GROUPS_ENV} is ${JSON.stringify(value)}, but it must be true or false`
97885
+ );
97886
+ }
97852
97887
  function createClerkAuthCacheRefresher(options) {
97853
- const { secret, cachePath } = options;
97888
+ const { secret, cachePath, useOrganizationsAsGroups } = options;
97854
97889
  if (!secret.current()) return void 0;
97855
97890
  const refreshWith = async (secretKey) => {
97856
97891
  const result = await refreshClerkCache({
97857
97892
  secretKey,
97858
97893
  cachePath,
97859
- useOrganizationsAsGroups: true,
97894
+ useOrganizationsAsGroups,
97860
97895
  // Injected rather than left to default `console.warn`: this runs in the
97861
- // worker, so its per-user membership-fetch warning needs the ISO-8601
97896
+ // worker, so its Organizations-refused warning needs the ISO-8601
97862
97897
  // prefix like everything else here. canopycms is only a peer dependency
97863
97898
  // of canopycms-auth-clerk, so the join happens at this entrypoint, which
97864
97899
  // already imports both.
@@ -97873,10 +97908,10 @@ function createClerkAuthCacheRefresher(options) {
97873
97908
  };
97874
97909
  }
97875
97910
  function createClerkSnapshotFetcher(options) {
97876
- const { secret } = options;
97911
+ const { secret, useOrganizationsAsGroups } = options;
97877
97912
  const log6 = options.log ?? WORKER_LOG;
97878
97913
  if (!secret.current()) return void 0;
97879
- const fetchWith = (secretKey) => fetchClerkSnapshot({ secretKey, useOrganizationsAsGroups: true, warn: log6.warn });
97914
+ const fetchWith = (secretKey) => fetchClerkSnapshot({ secretKey, useOrganizationsAsGroups, warn: log6.warn });
97880
97915
  return async () => {
97881
97916
  const key = secret.current();
97882
97917
  if (!key) throw new Error("No Clerk secret key is configured");
@@ -98023,10 +98058,12 @@ async function buildSingleProcessWorker(deps, workspacePath) {
98023
98058
  env2,
98024
98059
  WORKER_STATE_DIRECTORY_MISSING
98025
98060
  );
98061
+ const useOrganizationsAsGroups = parseClerkOrganizationsAsGroups(env2);
98026
98062
  const { githubToken, githubTokenSecret, githubAppAuth, clerkSecret } = await readWorkerCredentials(env2, deps.getSecret);
98027
98063
  const refreshAuthCache = createClerkAuthCacheRefresher({
98028
98064
  secret: clerkSecret,
98029
- cachePath: path33.join(workspacePath, ".cache")
98065
+ cachePath: path33.join(workspacePath, ".cache"),
98066
+ useOrganizationsAsGroups
98030
98067
  });
98031
98068
  const worker = deps.createWorker({
98032
98069
  workspacePath,
@@ -98063,6 +98100,11 @@ async function buildSplitWorker(deps, workspacePath, socketPath, onGatewayWait)
98063
98100
  `${PROTECTED_BRANCHES_ENV} is set on the worker unit, but beside the GitHub gateway it is the gateway's setting: move it to the gateway's environment (gateway.env)`
98064
98101
  );
98065
98102
  }
98103
+ if (env2[CLERK_ORGANIZATIONS_AS_GROUPS_ENV]) {
98104
+ throw new Error(
98105
+ `${CLERK_ORGANIZATIONS_AS_GROUPS_ENV} is set on the worker unit, but beside the GitHub gateway it is the gateway's setting: move it to the gateway's environment (gateway.env)`
98106
+ );
98107
+ }
98066
98108
  const gateway = deps.connectGateway(socketPath, { fetchInactivityMs: DEFAULT_TASK_TIMEOUT });
98067
98109
  let health;
98068
98110
  onGatewayWait(Date.now());
@@ -98236,6 +98278,7 @@ async function buildGateway(deps, listen) {
98236
98278
  env2,
98237
98279
  GATEWAY_STATE_DIRECTORY_MISSING
98238
98280
  );
98281
+ const useOrganizationsAsGroups = parseClerkOrganizationsAsGroups(env2);
98239
98282
  const { githubToken, githubTokenSecret, githubAppAuth, clerkSecret } = await readWorkerCredentials(env2, deps.getSecret, {
98240
98283
  onAppPrivateKey: (privateKey) => {
98241
98284
  registerPrivateKey(privateKey);
@@ -98264,6 +98307,7 @@ async function buildGateway(deps, listen) {
98264
98307
  protectedBranches: parseProtectedBranches(env2),
98265
98308
  authSnapshot: createClerkSnapshotFetcher({
98266
98309
  secret: clerkSecret,
98310
+ useOrganizationsAsGroups,
98267
98311
  log: { warn: gatewayLogWarn, error: gatewayLogError }
98268
98312
  }),
98269
98313
  ...instanceOptions(deps)
@@ -1 +1 @@
1
- 7c85791493bd41c35652f7f7bd69aa5f6d9b7a229529870ba5095cdd468f3f60 index.js
1
+ 79d771f61a54f9a9cfc254d2392361792f9f914d38d4764ab394f44668d64827 index.js
@@ -19,6 +19,8 @@ CANOPYCMS_GITHUB_TOKEN_SECRET_ARN=arn:aws:secretsmanager:us-east-1:123456789012:
19
19
  #CANOPYCMS_GITHUB_APP_PRIVATE_KEY_SECRET_JSON_FIELD=privateKey
20
20
  CLERK_SECRET_KEY_SECRET_ARN=arn:aws:secretsmanager:us-east-1:123456789012:secret:clerk-secret-key
21
21
  #CLERK_SECRET_KEY_SECRET_JSON_FIELD=secretKey
22
+ # Map Clerk Organizations onto Canopy groups (default true); false on an instance without Organizations.
23
+ #CANOPYCMS_CLERK_ORGANIZATIONS_AS_GROUPS=false
22
24
  # Branches the gateway never pushes, deletes or opens a pull request from: names and `*` globs.
23
25
  #CANOPYCMS_PROTECTED_BRANCHES=release/*,production
24
26
  # On spot capacity, so the gateway watches for an interruption notice too.