@cloud-copilot/iam-lens 0.1.108 → 0.1.110

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.
Files changed (63) hide show
  1. package/dist/cjs/index.d.ts +2 -0
  2. package/dist/cjs/index.d.ts.map +1 -1
  3. package/dist/cjs/index.js +3 -1
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/utils/bitset.js +3 -3
  6. package/dist/cjs/utils/bitset.js.map +1 -1
  7. package/dist/cjs/whoCan/WhoCanMainThreadWorker.d.ts +65 -3
  8. package/dist/cjs/whoCan/WhoCanMainThreadWorker.d.ts.map +1 -1
  9. package/dist/cjs/whoCan/WhoCanMainThreadWorker.js +52 -31
  10. package/dist/cjs/whoCan/WhoCanMainThreadWorker.js.map +1 -1
  11. package/dist/cjs/whoCan/WhoCanProcessor.d.ts +371 -0
  12. package/dist/cjs/whoCan/WhoCanProcessor.d.ts.map +1 -0
  13. package/dist/cjs/whoCan/WhoCanProcessor.js +980 -0
  14. package/dist/cjs/whoCan/WhoCanProcessor.js.map +1 -0
  15. package/dist/cjs/whoCan/WhoCanWorker.d.ts +2 -0
  16. package/dist/cjs/whoCan/WhoCanWorker.d.ts.map +1 -1
  17. package/dist/cjs/whoCan/WhoCanWorker.js.map +1 -1
  18. package/dist/cjs/whoCan/WhoCanWorkerThreadWorker.js +99 -80
  19. package/dist/cjs/whoCan/WhoCanWorkerThreadWorker.js.map +1 -1
  20. package/dist/cjs/whoCan/principalArnFilter.d.ts +84 -0
  21. package/dist/cjs/whoCan/principalArnFilter.d.ts.map +1 -0
  22. package/dist/cjs/whoCan/principalArnFilter.js +256 -0
  23. package/dist/cjs/whoCan/principalArnFilter.js.map +1 -0
  24. package/dist/cjs/whoCan/untrustingActions.d.ts +7 -0
  25. package/dist/cjs/whoCan/untrustingActions.d.ts.map +1 -0
  26. package/dist/cjs/whoCan/untrustingActions.js +30 -0
  27. package/dist/cjs/whoCan/untrustingActions.js.map +1 -0
  28. package/dist/cjs/whoCan/whoCan.d.ts +35 -2
  29. package/dist/cjs/whoCan/whoCan.d.ts.map +1 -1
  30. package/dist/cjs/whoCan/whoCan.js +277 -233
  31. package/dist/cjs/whoCan/whoCan.js.map +1 -1
  32. package/dist/esm/index.d.ts +2 -0
  33. package/dist/esm/index.d.ts.map +1 -1
  34. package/dist/esm/index.js +2 -0
  35. package/dist/esm/index.js.map +1 -1
  36. package/dist/esm/utils/bitset.js +3 -3
  37. package/dist/esm/utils/bitset.js.map +1 -1
  38. package/dist/esm/whoCan/WhoCanMainThreadWorker.d.ts +65 -3
  39. package/dist/esm/whoCan/WhoCanMainThreadWorker.d.ts.map +1 -1
  40. package/dist/esm/whoCan/WhoCanMainThreadWorker.js +53 -34
  41. package/dist/esm/whoCan/WhoCanMainThreadWorker.js.map +1 -1
  42. package/dist/esm/whoCan/WhoCanProcessor.d.ts +371 -0
  43. package/dist/esm/whoCan/WhoCanProcessor.d.ts.map +1 -0
  44. package/dist/esm/whoCan/WhoCanProcessor.js +970 -0
  45. package/dist/esm/whoCan/WhoCanProcessor.js.map +1 -0
  46. package/dist/esm/whoCan/WhoCanWorker.d.ts +2 -0
  47. package/dist/esm/whoCan/WhoCanWorker.d.ts.map +1 -1
  48. package/dist/esm/whoCan/WhoCanWorker.js.map +1 -1
  49. package/dist/esm/whoCan/WhoCanWorkerThreadWorker.js +102 -81
  50. package/dist/esm/whoCan/WhoCanWorkerThreadWorker.js.map +1 -1
  51. package/dist/esm/whoCan/principalArnFilter.d.ts +84 -0
  52. package/dist/esm/whoCan/principalArnFilter.d.ts.map +1 -0
  53. package/dist/esm/whoCan/principalArnFilter.js +251 -0
  54. package/dist/esm/whoCan/principalArnFilter.js.map +1 -0
  55. package/dist/esm/whoCan/untrustingActions.d.ts +7 -0
  56. package/dist/esm/whoCan/untrustingActions.d.ts.map +1 -0
  57. package/dist/esm/whoCan/untrustingActions.js +27 -0
  58. package/dist/esm/whoCan/untrustingActions.js.map +1 -0
  59. package/dist/esm/whoCan/whoCan.d.ts +35 -2
  60. package/dist/esm/whoCan/whoCan.d.ts.map +1 -1
  61. package/dist/esm/whoCan/whoCan.js +278 -237
  62. package/dist/esm/whoCan/whoCan.js.map +1 -1
  63. package/package.json +3 -3
@@ -0,0 +1,970 @@
1
+ import {} from '@cloud-copilot/iam-collect';
2
+ import { numberOfCpus, StreamingJobQueue } from '@cloud-copilot/job';
3
+ import { Worker } from 'worker_threads';
4
+ import { getCollectClient } from '../collect/collect.js';
5
+ import { IamCollectClient } from '../collect/client.js';
6
+ import { getAccountIdForResource, getResourcePolicyForResource } from '../resources.js';
7
+ import { Arn } from '../utils/arn.js';
8
+ import {} from '../utils/s3Abac.js';
9
+ import { getWorkerScriptPath } from '../utils/workerScript.js';
10
+ import { SharedArrayBufferMainCache } from '../workers/SharedArrayBufferMainCache.js';
11
+ import { StreamingWorkQueue } from '../workers/StreamingWorkQueue.js';
12
+ import { createMainThreadStreamingWorkQueue } from './WhoCanMainThreadWorker.js';
13
+ import {} from './WhoCanWorker.js';
14
+ import { intersectWithPrincipalScope, resolvePrincipalScope } from './principalScope.js';
15
+ import {} from './requestAnalysis.js';
16
+ import { actionsForWhoCan, accountsToCheckBasedOnResourcePolicy, uniqueAccountsToCheck, sortWhoCanResults } from './whoCan.js';
17
+ import { isAssumedRoleArn, isIamRoleArn, isIamUserArn, isServicePrincipal } from '@cloud-copilot/iam-utils';
18
+ import { buildPrincipalArnFilter, principalMatchesFilter } from './principalArnFilter.js';
19
+ // ──────────────────────────────────────────────────────────────────────────────
20
+ // Helpers
21
+ // ──────────────────────────────────────────────────────────────────────────────
22
+ let nextRequestId = 0;
23
+ /**
24
+ * Generates a unique request ID for a new request.
25
+ *
26
+ * @returns a unique string ID
27
+ */
28
+ function generateRequestId() {
29
+ return `req-${++nextRequestId}`;
30
+ }
31
+ /**
32
+ * Get the number of worker threads to use, defaulting to number of CPUs - 1.
33
+ *
34
+ * @param overrideValue the override value, if any
35
+ * @returns the override value if provided, otherwise number of CPUs - 1
36
+ */
37
+ function getNumberOfWorkers(overrideValue) {
38
+ if (typeof overrideValue === 'number' && overrideValue >= 0) {
39
+ return Math.floor(overrideValue);
40
+ }
41
+ return Math.max(0, numberOfCpus() - 1);
42
+ }
43
+ // ──────────────────────────────────────────────────────────────────────────────
44
+ // Processor
45
+ // ──────────────────────────────────────────────────────────────────────────────
46
+ /**
47
+ * A queue-first bulk processor that accepts many whoCan requests, expands
48
+ * scenarios on the main thread, and feeds a shared simulation scheduler used
49
+ * by worker threads and an optional main-thread runner.
50
+ *
51
+ * Results are delivered through the {@link WhoCanProcessorConfig.onRequestSettled}
52
+ * callback as each request completes — they are not stored inside the processor.
53
+ *
54
+ * Use {@link enqueueWhoCan} to submit requests, then {@link waitForIdle} to
55
+ * wait for all work to complete. Call {@link shutdown} when done to terminate
56
+ * worker threads.
57
+ */
58
+ export class WhoCanProcessor {
59
+ constructor(workers, collectClient, config, preparationQueue) {
60
+ this.isShutdown = false;
61
+ this.workersDead = false;
62
+ // Admission state
63
+ this.pendingRequests = [];
64
+ this.activeRequestOrder = [];
65
+ this.requestStates = new Map();
66
+ this.admissionPumpRunning = false;
67
+ this.draining = false;
68
+ // Idle / drain tracking
69
+ this.idleWaiters = [];
70
+ this.settledCallbackErrors = [];
71
+ this.workers = workers;
72
+ this.collectClient = collectClient;
73
+ this.config = config;
74
+ this.preparationQueue = preparationQueue;
75
+ }
76
+ /**
77
+ * Creates a new WhoCanProcessor with worker threads, a shared cache, and
78
+ * lifetime-scoped message routing. The processor is ready to accept requests
79
+ * immediately after creation.
80
+ *
81
+ * @param config - The configuration for the processor, including collect configs,
82
+ * partition, simulation options, tuning, and the onRequestSettled callback.
83
+ * @returns a new WhoCanProcessor instance
84
+ */
85
+ static async create(config) {
86
+ const numWorkers = getNumberOfWorkers(config.tuning?.workerThreads);
87
+ const perWorkerConcurrency = config.tuning?.perWorkerConcurrency ?? 50;
88
+ const workerPath = getWorkerScriptPath('whoCan/WhoCanWorkerThreadWorker.js');
89
+ const workers = !workerPath
90
+ ? []
91
+ : new Array(numWorkers).fill(undefined).map(() => {
92
+ return new Worker(workerPath, {
93
+ workerData: {
94
+ collectConfigs: config.collectConfigs,
95
+ partition: config.partition,
96
+ concurrency: perWorkerConcurrency,
97
+ s3AbacOverride: config.s3AbacOverride,
98
+ collectGrantDetails: config.collectGrantDetails,
99
+ clientFactoryPlugin: config.clientFactoryPlugin
100
+ }
101
+ });
102
+ });
103
+ const collectClient = await getCollectClient(config.collectConfigs, config.partition, {
104
+ cacheProvider: new SharedArrayBufferMainCache(workers),
105
+ clientFactoryPlugin: config.clientFactoryPlugin
106
+ });
107
+ const preparationConcurrency = config.tuning?.preparationConcurrency ?? Math.min(50, Math.max(1, numberOfCpus() * 2));
108
+ const preparationQueue = new StreamingJobQueue(preparationConcurrency, console, async () => { });
109
+ const processor = new WhoCanProcessor(workers, collectClient, config, preparationQueue);
110
+ processor.installLifetimeWorkerListeners();
111
+ processor.createMainThreadRunner();
112
+ return processor;
113
+ }
114
+ /**
115
+ * Enqueues a whoCan request for processing. Returns a unique request ID
116
+ * that will appear in the corresponding {@link WhoCanSettledEvent}.
117
+ *
118
+ * This method never activates a request directly — it appends to
119
+ * pendingRequests and signals the admission pump.
120
+ *
121
+ * @param request - The whoCan request parameters.
122
+ * @returns the unique request ID assigned to this request.
123
+ * @throws if the processor is shut down or draining via waitForIdle.
124
+ */
125
+ enqueueWhoCan(request) {
126
+ if (this.isShutdown) {
127
+ throw new Error('WhoCanProcessor has been shut down');
128
+ }
129
+ if (this.draining) {
130
+ throw new Error('Cannot enqueue while draining — waitForIdle() is in progress');
131
+ }
132
+ const requestId = generateRequestId();
133
+ this.pendingRequests.push({ requestId, request });
134
+ this.wakeAdmissionPump();
135
+ return requestId;
136
+ }
137
+ /**
138
+ * Returns a promise that resolves when all pending and active work has
139
+ * completed and all onRequestSettled callbacks have finished.
140
+ *
141
+ * While draining, new calls to {@link enqueueWhoCan} will throw. Once
142
+ * the drain completes, the processor re-opens for new enqueues.
143
+ *
144
+ * @returns a promise that resolves when idle, or rejects if a worker crashes
145
+ * or an onRequestSettled callback throws/rejects.
146
+ */
147
+ async waitForIdle() {
148
+ if (this.isShutdown) {
149
+ throw new Error('WhoCanProcessor has been shut down');
150
+ }
151
+ // If already idle, return immediately
152
+ if (this.isIdle()) {
153
+ this.rejectIfSettledCallbackErrors();
154
+ return;
155
+ }
156
+ this.draining = true;
157
+ try {
158
+ await new Promise((resolve, reject) => {
159
+ this.idleWaiters.push({ resolve, reject });
160
+ });
161
+ this.rejectIfSettledCallbackErrors();
162
+ }
163
+ finally {
164
+ // Only clear draining when the last waiter has been notified
165
+ if (this.idleWaiters.length === 0) {
166
+ this.draining = false;
167
+ }
168
+ }
169
+ }
170
+ /**
171
+ * Shuts down the processor by rejecting all pending requests, waiting for
172
+ * active requests to settle, and terminating all worker threads.
173
+ *
174
+ * This method is idempotent — calling it multiple times is safe.
175
+ */
176
+ async shutdown() {
177
+ // If already shutting down or shut down, return the existing promise
178
+ if (this.shutdownPromise) {
179
+ return this.shutdownPromise;
180
+ }
181
+ this.shutdownPromise = this.executeShutdown();
182
+ return this.shutdownPromise;
183
+ }
184
+ /**
185
+ * Internal shutdown implementation. Rejects pending requests, waits for
186
+ * active requests to drain, then terminates workers.
187
+ */
188
+ async executeShutdown() {
189
+ this.isShutdown = true;
190
+ // Reject all pending requests that haven't been admitted
191
+ while (this.pendingRequests.length > 0) {
192
+ const submitted = this.pendingRequests.shift();
193
+ try {
194
+ await this.config.onRequestSettled({
195
+ status: 'rejected',
196
+ requestId: submitted.requestId,
197
+ request: submitted.request,
198
+ error: new Error('WhoCanProcessor was shut down before this request was processed')
199
+ });
200
+ }
201
+ catch (err) {
202
+ this.settledCallbackErrors.push(err instanceof Error ? err : new Error(String(err)));
203
+ }
204
+ }
205
+ // Wait for active requests to finish naturally (includes draining in-flight work)
206
+ if (this.activeRequestOrder.length > 0) {
207
+ await new Promise((resolve) => {
208
+ if (this.activeRequestOrder.length === 0) {
209
+ resolve();
210
+ }
211
+ else {
212
+ this.idleWaiters.push({ resolve, reject: () => resolve() });
213
+ }
214
+ });
215
+ }
216
+ if (this.workersDead) {
217
+ return;
218
+ }
219
+ // Drain main thread worker
220
+ if (this.mainThreadWorker) {
221
+ await this.mainThreadWorker.finishAllWork();
222
+ this.mainThreadWorker = undefined;
223
+ }
224
+ // Gracefully shut down workers
225
+ const workerPromises = this.workers.map((worker) => {
226
+ return new Promise((resolve) => {
227
+ worker.on('message', (msg) => {
228
+ if (msg.type === 'finished') {
229
+ worker.terminate().then(() => resolve());
230
+ }
231
+ });
232
+ worker.on('error', () => {
233
+ worker
234
+ .terminate()
235
+ .then(() => resolve())
236
+ .catch(() => resolve());
237
+ });
238
+ worker.postMessage({ type: 'finishWork' });
239
+ });
240
+ });
241
+ await Promise.all(workerPromises);
242
+ this.workersDead = true;
243
+ }
244
+ // ──────────────────────────────────────────────────────────────────────────
245
+ // Lifetime worker listeners
246
+ // ──────────────────────────────────────────────────────────────────────────
247
+ /**
248
+ * Installs lifetime-scoped message, error, and exit listeners on all workers.
249
+ * Message listeners route simulation results and deny-detail checks to the
250
+ * correct request state using requestId. Error/exit listeners detect crashes
251
+ * and mark the processor as fatally failed.
252
+ */
253
+ installLifetimeWorkerListeners() {
254
+ for (const worker of this.workers) {
255
+ worker.on('message', (msg) => {
256
+ this.handleWorkerMessage(msg, worker);
257
+ });
258
+ worker.on('error', (err) => {
259
+ if (!this.isShutdown) {
260
+ this.handleWorkerFailure(new Error(`Worker error: ${err.message}`));
261
+ }
262
+ });
263
+ worker.on('exit', (code) => {
264
+ if (!this.isShutdown && code !== 0) {
265
+ this.handleWorkerFailure(new Error(`Worker exited unexpectedly with code ${code}`));
266
+ }
267
+ });
268
+ }
269
+ }
270
+ /**
271
+ * Routes a message from a worker thread to the appropriate handler based
272
+ * on message type and requestId.
273
+ *
274
+ * @param msg - The message received from the worker.
275
+ * @param worker - The worker that sent the message.
276
+ */
277
+ handleWorkerMessage(msg, worker) {
278
+ if (msg.type === 'requestTask') {
279
+ const task = this.dequeueNextScenario();
280
+ worker.postMessage({ type: 'task', workerId: msg.workerId, task });
281
+ }
282
+ else if (msg.type === 'result') {
283
+ this.handleSimulationResult(msg.requestId, msg.result, !!msg.denyDetailsCheckWillFollow);
284
+ }
285
+ else if (msg.type === 'checkDenyDetails') {
286
+ this.handleCheckDenyDetails(msg.requestId, msg.checkId, msg.lightAnalysis, worker);
287
+ }
288
+ else if (msg.type === 'denyDetailsResult') {
289
+ this.handleDenyDetailsResult(msg.requestId, msg.denyDetail);
290
+ }
291
+ }
292
+ /**
293
+ * Creates the main-thread simulation runner if mainThreadConcurrency > 0.
294
+ * The runner pulls from the FIFO scheduler and routes results by requestId.
295
+ */
296
+ createMainThreadRunner() {
297
+ const mainThreadConcurrency = this.config.tuning?.mainThreadConcurrency ?? 50;
298
+ if (mainThreadConcurrency <= 0) {
299
+ return;
300
+ }
301
+ const { collectGrantDetails, s3AbacOverride } = this.config;
302
+ this.mainThreadWorker = createMainThreadStreamingWorkQueue(() => this.dequeueNextScenario(), (requestId, result) => this.handleSimulationResult(requestId, result), (requestId, lightAnalysis) => {
303
+ const state = this.requestStates.get(requestId);
304
+ if (state && !state.settled) {
305
+ return state.denyDetailsCallback?.(lightAnalysis) ?? false;
306
+ }
307
+ return false;
308
+ }, (requestId, detail) => this.handleDenyDetailsResult(requestId, detail), this.collectClient, s3AbacOverride, collectGrantDetails ?? false, mainThreadConcurrency);
309
+ }
310
+ // ──────────────────────────────────────────────────────────────────────────
311
+ // FIFO queue-of-queues scheduler
312
+ // ──────────────────────────────────────────────────────────────────────────
313
+ /**
314
+ * Dequeues the next simulation scenario using FIFO request priority.
315
+ * Prefers the oldest active request that has ready scenarios. If the oldest
316
+ * is temporarily empty (still preparing), falls back to the next request
317
+ * with ready scenarios so cores do not idle.
318
+ *
319
+ * @returns the next work item, or undefined if no scenarios are ready.
320
+ */
321
+ dequeueNextScenario() {
322
+ for (const requestId of this.activeRequestOrder) {
323
+ const state = this.requestStates.get(requestId);
324
+ if (!state || state.settled)
325
+ continue;
326
+ const item = state.scenarios.dequeue();
327
+ if (item) {
328
+ return { ...item, requestId };
329
+ }
330
+ }
331
+ return undefined;
332
+ }
333
+ /**
334
+ * Notifies all simulation consumers (workers and main thread) that new
335
+ * work may be available in the scheduler.
336
+ */
337
+ notifySimulationConsumers() {
338
+ this.mainThreadWorker?.notifyWorkAvailable();
339
+ for (const worker of this.workers) {
340
+ worker.postMessage({ type: 'workAvailable' });
341
+ }
342
+ }
343
+ // ──────────────────────────────────────────────────────────────────────────
344
+ // Admission pump
345
+ // ──────────────────────────────────────────────────────────────────────────
346
+ /**
347
+ * Wakes the admission pump to process pending requests. If the pump is
348
+ * already running, this is a no-op — the running pump will pick up new
349
+ * pending requests on its next iteration.
350
+ */
351
+ wakeAdmissionPump() {
352
+ if (this.admissionPumpRunning)
353
+ return;
354
+ this.admissionPumpRunning = true;
355
+ // Run asynchronously so enqueueWhoCan returns immediately
356
+ void this.runAdmissionPump();
357
+ }
358
+ /**
359
+ * The admission pump loop. Drains pendingRequests into active processing
360
+ * up to maxRequestsInProgress. Only one instance of this loop runs at a time,
361
+ * guarded by admissionPumpRunning.
362
+ */
363
+ async runAdmissionPump() {
364
+ const maxActive = this.config.tuning?.maxRequestsInProgress ?? 30;
365
+ try {
366
+ while (this.pendingRequests.length > 0 && this.activeRequestOrder.length < maxActive) {
367
+ if (this.isShutdown)
368
+ break;
369
+ const submitted = this.pendingRequests.shift();
370
+ const state = this.createRequestState(submitted);
371
+ this.requestStates.set(submitted.requestId, state);
372
+ this.activeRequestOrder.push(submitted.requestId);
373
+ // Enqueue the root preparation job for this request
374
+ this.enqueueRootPreparation(state);
375
+ }
376
+ }
377
+ finally {
378
+ this.admissionPumpRunning = false;
379
+ }
380
+ // After admitting, check if we became idle
381
+ this.checkIdle();
382
+ }
383
+ /**
384
+ * Creates a fresh RequestState for an admitted request.
385
+ *
386
+ * @param submitted - The submitted request to create state for.
387
+ * @returns the new RequestState.
388
+ */
389
+ createRequestState(submitted) {
390
+ return {
391
+ requestId: submitted.requestId,
392
+ request: submitted.request,
393
+ allScenariosCreated: false,
394
+ scenarios: new StreamingWorkQueue(),
395
+ created: 0,
396
+ completed: 0,
397
+ pendingPreparationJobs: 0,
398
+ allowed: [],
399
+ principalsNotFound: [],
400
+ accountsNotFound: [],
401
+ organizationsNotFound: [],
402
+ organizationalUnitsNotFound: [],
403
+ allAccountsChecked: false,
404
+ denyDetails: [],
405
+ simulationCount: 0,
406
+ denyDetailsCallback: submitted.request.denyDetailsCallback,
407
+ pendingDenyDetailsChecks: 0,
408
+ settled: false,
409
+ callbackInvoked: false,
410
+ simulationErrors: []
411
+ };
412
+ }
413
+ // ──────────────────────────────────────────────────────────────────────────
414
+ // Preparation
415
+ // ──────────────────────────────────────────────────────────────────────────
416
+ /**
417
+ * Enqueues the root preparation job for a request. This job performs resource
418
+ * account resolution, resource policy lookup, action expansion, principal scope
419
+ * handling, and then enqueues follow-up preparation jobs to enumerate principals.
420
+ *
421
+ * @param state - The request state to prepare.
422
+ */
423
+ enqueueRootPreparation(state) {
424
+ state.pendingPreparationJobs++;
425
+ this.preparationQueue.enqueue({
426
+ properties: {},
427
+ execute: async () => {
428
+ try {
429
+ await this.executeRootPreparation(state);
430
+ }
431
+ catch (err) {
432
+ this.settleRequestAsError(state, err instanceof Error ? err : new Error(String(err)));
433
+ }
434
+ finally {
435
+ state.pendingPreparationJobs--;
436
+ this.checkRequestCompletion(state);
437
+ }
438
+ }
439
+ });
440
+ }
441
+ /**
442
+ * Executes the root preparation for a request: resolves the resource account,
443
+ * fetches the resource policy, expands actions, determines which accounts and
444
+ * principals to check, and enqueues follow-up preparation jobs.
445
+ *
446
+ * @param state - The request state to prepare.
447
+ */
448
+ async executeRootPreparation(state) {
449
+ if (state.settled)
450
+ return;
451
+ const { request } = state;
452
+ const { resource } = request;
453
+ const collectClient = this.collectClient;
454
+ if (!request.resourceAccount && !request.resource) {
455
+ throw new Error('Either resourceAccount or resource must be provided in the request.');
456
+ }
457
+ const resourceAccount = request.resourceAccount || (await getAccountIdForResource(collectClient, resource));
458
+ if (!resourceAccount) {
459
+ throw new Error(`Could not determine account ID for resource ${resource}. Please use a different ARN or specify resourceAccount.`);
460
+ }
461
+ const actions = await actionsForWhoCan({
462
+ actions: request.actions,
463
+ resource: request.resource
464
+ });
465
+ if (!actions || actions.length === 0) {
466
+ throw new Error('No valid actions provided or found for the resource.');
467
+ }
468
+ let resourcePolicy = undefined;
469
+ if (resource) {
470
+ resourcePolicy = await getResourcePolicyForResource(collectClient, resource, resourceAccount);
471
+ const resourceArn = new Arn(resource);
472
+ if ((resourceArn.matches({ service: 'iam', resourceType: 'role' }) ||
473
+ resourceArn.matches({ service: 'kms', resourceType: 'key' })) &&
474
+ !resourcePolicy) {
475
+ throw new Error(`Unable to find resource policy for ${resource}. Cannot determine who can access the resource.`);
476
+ }
477
+ }
478
+ const accountsToCheck = await accountsToCheckBasedOnResourcePolicy(resourcePolicy, resourceAccount);
479
+ const principalArnFilter = buildPrincipalArnFilter(resourcePolicy);
480
+ const uniqueAccounts = await uniqueAccountsToCheck(collectClient, accountsToCheck);
481
+ // Store not-found arrays on the request state
482
+ state.accountsNotFound = uniqueAccounts.accountsNotFound;
483
+ state.organizationsNotFound = uniqueAccounts.organizationsNotFound;
484
+ state.organizationalUnitsNotFound = uniqueAccounts.organizationalUnitsNotFound;
485
+ state.allAccountsChecked = request.principalScope ? false : accountsToCheck.allAccounts;
486
+ let accountsForSearch = uniqueAccounts.accounts;
487
+ let principalsForSearch = accountsToCheck.specificPrincipals;
488
+ let scopeIncludesResourceAccount = true;
489
+ if (request.principalScope) {
490
+ const resolved = await resolvePrincipalScope(collectClient, request.principalScope);
491
+ const intersection = intersectWithPrincipalScope(uniqueAccounts.accounts, accountsToCheck.specificPrincipals, accountsToCheck.allAccounts, resolved.accounts, resolved.principals);
492
+ accountsForSearch = intersection.accounts;
493
+ principalsForSearch = intersection.principals;
494
+ scopeIncludesResourceAccount = resolved.accounts.has(resourceAccount);
495
+ }
496
+ // Principals explicitly named in the resource policy are enqueued via the
497
+ // specific-principals path (which skips the PrincipalArn filter). Track them
498
+ // so the account-enumeration paths can skip duplicates without needing to
499
+ // store all enumerated principals in memory.
500
+ const specificPrincipalSet = new Set(principalsForSearch);
501
+ // Enqueue follow-up preparation jobs for account/principal enumeration
502
+ const principalIndexExists = !this.config.ignorePrincipalIndex && (await collectClient.principalIndexExists());
503
+ if (principalIndexExists) {
504
+ // Use the principal index to find relevant principals directly
505
+ state.pendingPreparationJobs++;
506
+ this.preparationQueue.enqueue({
507
+ properties: {},
508
+ execute: async () => {
509
+ try {
510
+ if (state.settled)
511
+ return;
512
+ const allFromAccount = scopeIncludesResourceAccount && accountsToCheck.checkAllForCurrentAccount
513
+ ? resourceAccount
514
+ : undefined;
515
+ for (const action of actions) {
516
+ const indexedPrincipals = await collectClient.getPrincipalsWithActionAllowed(allFromAccount, accountsForSearch, action);
517
+ for (const principal of indexedPrincipals || []) {
518
+ if (specificPrincipalSet.has(principal))
519
+ continue;
520
+ if (principalArnFilter &&
521
+ !isServicePrincipal(principal) &&
522
+ !principalMatchesFilter(principal, action, resourceAccount, principalArnFilter)) {
523
+ continue;
524
+ }
525
+ state.scenarios.enqueue({
526
+ resource,
527
+ action,
528
+ principal,
529
+ resourceAccount,
530
+ strictContextKeys: state.request.strictContextKeys,
531
+ collectDenyDetails: !!state.denyDetailsCallback
532
+ });
533
+ state.created++;
534
+ }
535
+ }
536
+ this.notifySimulationConsumers();
537
+ }
538
+ catch (err) {
539
+ this.settleRequestAsError(state, err instanceof Error ? err : new Error(String(err)));
540
+ }
541
+ finally {
542
+ state.pendingPreparationJobs--;
543
+ this.checkRequestCompletion(state);
544
+ }
545
+ }
546
+ });
547
+ }
548
+ else {
549
+ // No principal index — enumerate all principals per account
550
+ for (const account of accountsForSearch) {
551
+ state.pendingPreparationJobs++;
552
+ this.preparationQueue.enqueue({
553
+ properties: {},
554
+ execute: async () => {
555
+ try {
556
+ if (state.settled)
557
+ return;
558
+ const principals = await collectClient.getAllPrincipalsInAccount(account);
559
+ for (const principal of principals) {
560
+ if (specificPrincipalSet.has(principal))
561
+ continue;
562
+ const skipFilter = !principalArnFilter || isServicePrincipal(principal);
563
+ for (const action of actions) {
564
+ if (!skipFilter &&
565
+ !principalMatchesFilter(principal, action, resourceAccount, principalArnFilter)) {
566
+ continue;
567
+ }
568
+ state.scenarios.enqueue({
569
+ resource,
570
+ action,
571
+ principal,
572
+ resourceAccount,
573
+ strictContextKeys: state.request.strictContextKeys,
574
+ collectDenyDetails: !!state.denyDetailsCallback
575
+ });
576
+ state.created++;
577
+ }
578
+ }
579
+ this.notifySimulationConsumers();
580
+ }
581
+ catch (err) {
582
+ this.settleRequestAsError(state, err instanceof Error ? err : new Error(String(err)));
583
+ }
584
+ finally {
585
+ state.pendingPreparationJobs--;
586
+ this.checkRequestCompletion(state);
587
+ }
588
+ }
589
+ });
590
+ }
591
+ }
592
+ // Enqueue specific principals from resource policy (iterate the Set to
593
+ // deduplicate — the same principal can appear in the list more than once
594
+ // when multiple statements reference it, e.g. an explicit Principal element
595
+ // and a StringEquals aws:PrincipalArn condition).
596
+ for (const principal of specificPrincipalSet) {
597
+ state.pendingPreparationJobs++;
598
+ this.preparationQueue.enqueue({
599
+ properties: {},
600
+ execute: async () => {
601
+ try {
602
+ if (state.settled)
603
+ return;
604
+ if (isServicePrincipal(principal)) {
605
+ for (const action of actions) {
606
+ state.scenarios.enqueue({
607
+ resource,
608
+ action,
609
+ principal,
610
+ resourceAccount,
611
+ strictContextKeys: state.request.strictContextKeys,
612
+ collectDenyDetails: !!state.denyDetailsCallback
613
+ });
614
+ state.created++;
615
+ }
616
+ }
617
+ else if (isIamUserArn(principal) ||
618
+ isIamRoleArn(principal) ||
619
+ isAssumedRoleArn(principal)) {
620
+ const principalExists = await collectClient.principalExists(principal);
621
+ if (!principalExists) {
622
+ state.principalsNotFound.push(principal);
623
+ }
624
+ else {
625
+ for (const action of actions) {
626
+ state.scenarios.enqueue({
627
+ resource,
628
+ action,
629
+ principal,
630
+ resourceAccount,
631
+ strictContextKeys: state.request.strictContextKeys,
632
+ collectDenyDetails: !!state.denyDetailsCallback
633
+ });
634
+ state.created++;
635
+ }
636
+ }
637
+ }
638
+ else {
639
+ state.principalsNotFound.push(principal);
640
+ }
641
+ this.notifySimulationConsumers();
642
+ }
643
+ catch (err) {
644
+ this.settleRequestAsError(state, err instanceof Error ? err : new Error(String(err)));
645
+ }
646
+ finally {
647
+ state.pendingPreparationJobs--;
648
+ this.checkRequestCompletion(state);
649
+ }
650
+ }
651
+ });
652
+ }
653
+ // All follow-up prep jobs have been enqueued. Mark scenarios as fully specified
654
+ // once the root prep and all follow-ups complete (tracked by pendingPreparationJobs).
655
+ state.allScenariosCreated = true;
656
+ // Notify consumers that scenarios may be available
657
+ this.notifySimulationConsumers();
658
+ }
659
+ // ──────────────────────────────────────────────────────────────────────────
660
+ // Simulation result handling
661
+ // ──────────────────────────────────────────────────────────────────────────
662
+ /**
663
+ * Handles a simulation result from a worker or the main thread runner.
664
+ * Routes the result to the correct request state and checks for completion.
665
+ *
666
+ * @param requestId - The ID of the request this result belongs to.
667
+ * @param result - The simulation job result.
668
+ */
669
+ handleSimulationResult(requestId, result, denyDetailsCheckWillFollow = false) {
670
+ const state = this.requestStates.get(requestId);
671
+ if (!state)
672
+ return;
673
+ state.completed++;
674
+ if (denyDetailsCheckWillFollow) {
675
+ state.pendingDenyDetailsChecks++;
676
+ }
677
+ if (state.settled) {
678
+ // Request already settled (e.g., failed). Still count the result so
679
+ // the drain check can fire, but discard the actual data.
680
+ this.checkRequestCompletion(state);
681
+ return;
682
+ }
683
+ state.simulationCount++;
684
+ if (result.status === 'fulfilled' && result.value) {
685
+ state.allowed.push(result.value);
686
+ }
687
+ else if (result.status === 'rejected') {
688
+ console.error('Error running simulation:', result.reason);
689
+ state.simulationErrors.push(result);
690
+ }
691
+ this.checkRequestCompletion(state);
692
+ }
693
+ /**
694
+ * Handles a checkDenyDetails request from a worker thread. Looks up the
695
+ * request's denyDetailsCallback and responds.
696
+ *
697
+ * @param requestId - The ID of the request.
698
+ * @param checkId - The unique check ID for this deny-details round trip.
699
+ * @param lightAnalysis - The light analysis to pass to the callback.
700
+ * @param worker - The worker to respond to.
701
+ */
702
+ handleCheckDenyDetails(requestId, checkId, lightAnalysis, worker) {
703
+ const state = this.requestStates.get(requestId);
704
+ const shouldInclude = state && !state.settled ? (state.denyDetailsCallback?.(lightAnalysis) ?? false) : false;
705
+ if (!shouldInclude && state) {
706
+ // No denyDetailsResult message will follow — decrement the counter
707
+ state.pendingDenyDetailsChecks--;
708
+ this.checkRequestCompletion(state);
709
+ }
710
+ worker.postMessage({
711
+ type: 'denyDetailsCheckResult',
712
+ checkId,
713
+ shouldInclude
714
+ });
715
+ }
716
+ /**
717
+ * Handles a deny details result from a worker thread. Decrements the
718
+ * pending deny-details counter and checks for request completion.
719
+ *
720
+ * @param requestId - The ID of the request.
721
+ * @param denyDetail - The deny detail to store.
722
+ */
723
+ handleDenyDetailsResult(requestId, denyDetail) {
724
+ const state = this.requestStates.get(requestId);
725
+ if (!state)
726
+ return;
727
+ if (state.pendingDenyDetailsChecks > 0) {
728
+ state.pendingDenyDetailsChecks--;
729
+ }
730
+ if (!state.settled) {
731
+ state.denyDetails.push(denyDetail);
732
+ }
733
+ this.checkRequestCompletion(state);
734
+ }
735
+ // ──────────────────────────────────────────────────────────────────────────
736
+ // Request completion and settlement
737
+ // ──────────────────────────────────────────────────────────────────────────
738
+ /**
739
+ * Checks whether a request has completed all preparation and simulation work.
740
+ * If so, settles the request as successful.
741
+ *
742
+ * @param state - The request state to check.
743
+ */
744
+ checkRequestCompletion(state) {
745
+ if (state.settled) {
746
+ this.checkRequestDrain(state);
747
+ return;
748
+ }
749
+ if (!state.allScenariosCreated)
750
+ return;
751
+ if (state.pendingPreparationJobs > 0)
752
+ return;
753
+ if (state.created !== state.completed)
754
+ return;
755
+ if (state.pendingDenyDetailsChecks > 0)
756
+ return;
757
+ // All work done — settle as success
758
+ if (state.simulationErrors.length > 0) {
759
+ this.settleRequestAsError(state, new Error(`Completed with ${state.simulationErrors.length} simulation errors. See previous logs.`));
760
+ }
761
+ else {
762
+ this.settleRequestAsSuccess(state);
763
+ }
764
+ }
765
+ /**
766
+ * Settles a request as successful: builds the WhoCanResponse, awaits
767
+ * onRequestSettled, removes the request from active state, and wakes
768
+ * the admission pump.
769
+ *
770
+ * @param state - The request state to settle.
771
+ */
772
+ settleRequestAsSuccess(state) {
773
+ if (state.settled)
774
+ return;
775
+ state.settled = true;
776
+ const result = {
777
+ simulationCount: state.simulationCount,
778
+ allowed: state.allowed,
779
+ allAccountsChecked: state.allAccountsChecked,
780
+ accountsNotFound: state.accountsNotFound,
781
+ organizationsNotFound: state.organizationsNotFound,
782
+ organizationalUnitsNotFound: state.organizationalUnitsNotFound,
783
+ principalsNotFound: state.principalsNotFound,
784
+ denyDetails: state.denyDetailsCallback ? state.denyDetails : undefined
785
+ };
786
+ if (state.request.sort) {
787
+ sortWhoCanResults(result);
788
+ }
789
+ void this.invokeSettledCallbackAndCleanup(state, {
790
+ status: 'fulfilled',
791
+ requestId: state.requestId,
792
+ request: state.request,
793
+ result
794
+ });
795
+ }
796
+ /**
797
+ * Settles a request as failed: invokes onRequestSettled with the error
798
+ * immediately, but keeps the request in active state until all in-flight
799
+ * work drains (created === completed). Late results for settled requests
800
+ * are discarded but still counted so the drain completes.
801
+ *
802
+ * @param state - The request state to settle.
803
+ * @param error - The error that caused the failure.
804
+ */
805
+ settleRequestAsError(state, error) {
806
+ if (state.settled)
807
+ return;
808
+ state.settled = true;
809
+ // Await the callback (backpressure), then mark it done so checkRequestDrain
810
+ // can free the slot once all in-flight work also completes.
811
+ void (async () => {
812
+ await this.invokeSettledCallback({
813
+ status: 'rejected',
814
+ requestId: state.requestId,
815
+ request: state.request,
816
+ error
817
+ });
818
+ state.callbackInvoked = true;
819
+ this.checkRequestDrain(state);
820
+ })();
821
+ }
822
+ /**
823
+ * Invokes the onRequestSettled callback and accumulates any errors for
824
+ * later surfacing via waitForIdle.
825
+ *
826
+ * @param event - The settlement event to deliver.
827
+ */
828
+ async invokeSettledCallback(event) {
829
+ try {
830
+ await this.config.onRequestSettled(event);
831
+ }
832
+ catch (err) {
833
+ this.settledCallbackErrors.push(err instanceof Error ? err : new Error(String(err)));
834
+ }
835
+ }
836
+ /**
837
+ * Awaits the onRequestSettled callback, then removes the request from
838
+ * active state and wakes the admission pump. Used for successful settlements
839
+ * where all work is already complete.
840
+ *
841
+ * @param state - The request state being settled.
842
+ * @param event - The settlement event to deliver.
843
+ */
844
+ async invokeSettledCallbackAndCleanup(state, event) {
845
+ await this.invokeSettledCallback(event);
846
+ this.removeFromActiveState(state);
847
+ }
848
+ /**
849
+ * Checks whether a settled request has fully drained: the onRequestSettled
850
+ * callback has been awaited, all preparation jobs have finished, all
851
+ * simulation results have been received, and all deny-detail round trips
852
+ * have completed. Only then is the request removed from active state.
853
+ *
854
+ * @param state - The request state to check.
855
+ */
856
+ checkRequestDrain(state) {
857
+ if (!state.settled)
858
+ return;
859
+ if (!state.callbackInvoked)
860
+ return;
861
+ if (state.pendingPreparationJobs > 0)
862
+ return;
863
+ if (state.created !== state.completed)
864
+ return;
865
+ if (state.pendingDenyDetailsChecks > 0)
866
+ return;
867
+ this.removeFromActiveState(state);
868
+ }
869
+ /**
870
+ * Removes a request from active state, wakes the admission pump to fill
871
+ * the freed slot, and checks if the processor is now idle.
872
+ *
873
+ * @param state - The request state to remove.
874
+ */
875
+ removeFromActiveState(state) {
876
+ const idx = this.activeRequestOrder.indexOf(state.requestId);
877
+ if (idx !== -1) {
878
+ this.activeRequestOrder.splice(idx, 1);
879
+ }
880
+ this.requestStates.delete(state.requestId);
881
+ this.wakeAdmissionPump();
882
+ this.checkIdle();
883
+ }
884
+ // ──────────────────────────────────────────────────────────────────────────
885
+ // Idle checking
886
+ // ──────────────────────────────────────────────────────────────────────────
887
+ /**
888
+ * Returns true if the processor has no pending, active, or in-flight work.
889
+ *
890
+ * @returns true if fully idle.
891
+ */
892
+ isIdle() {
893
+ return this.pendingRequests.length === 0 && this.activeRequestOrder.length === 0;
894
+ }
895
+ /**
896
+ * Checks whether the processor has become idle and resolves or rejects the
897
+ * waitForIdle promise if so.
898
+ */
899
+ checkIdle() {
900
+ if (!this.isIdle())
901
+ return;
902
+ if (this.idleWaiters.length === 0)
903
+ return;
904
+ const waiters = this.idleWaiters.splice(0);
905
+ if (this.fatalError) {
906
+ for (const waiter of waiters) {
907
+ waiter.reject(this.fatalError);
908
+ }
909
+ }
910
+ else {
911
+ for (const waiter of waiters) {
912
+ waiter.resolve();
913
+ }
914
+ }
915
+ }
916
+ /**
917
+ * If any onRequestSettled callbacks threw, throws the first error.
918
+ * Called after waitForIdle resolves to surface callback errors.
919
+ */
920
+ rejectIfSettledCallbackErrors() {
921
+ if (this.settledCallbackErrors.length > 0) {
922
+ const error = this.settledCallbackErrors[0];
923
+ this.settledCallbackErrors = [];
924
+ throw error;
925
+ }
926
+ }
927
+ // ──────────────────────────────────────────────────────────────────────────
928
+ // Worker failure handling
929
+ // ──────────────────────────────────────────────────────────────────────────
930
+ /**
931
+ * Handles an unexpected worker failure by marking the processor as dead,
932
+ * terminating remaining workers, and rejecting all active and pending requests.
933
+ *
934
+ * @param error - The error that caused the worker failure.
935
+ */
936
+ handleWorkerFailure(error) {
937
+ this.workersDead = true;
938
+ this.isShutdown = true;
939
+ this.fatalError = error;
940
+ // Terminate remaining workers (fire-and-forget)
941
+ for (const worker of this.workers) {
942
+ worker.terminate().catch(() => { });
943
+ }
944
+ // Settle all active requests as failed
945
+ for (const requestId of [...this.activeRequestOrder]) {
946
+ const state = this.requestStates.get(requestId);
947
+ if (state && !state.settled) {
948
+ this.settleRequestAsError(state, error);
949
+ }
950
+ }
951
+ // Reject all pending requests
952
+ while (this.pendingRequests.length > 0) {
953
+ const submitted = this.pendingRequests.shift();
954
+ void this.config
955
+ .onRequestSettled({
956
+ status: 'rejected',
957
+ requestId: submitted.requestId,
958
+ request: submitted.request,
959
+ error
960
+ })
961
+ .catch(() => { });
962
+ }
963
+ // Reject all idle waiters
964
+ const waiters = this.idleWaiters.splice(0);
965
+ for (const waiter of waiters) {
966
+ waiter.reject(error);
967
+ }
968
+ }
969
+ }
970
+ //# sourceMappingURL=WhoCanProcessor.js.map