@cloud-copilot/iam-lens
Version:
Visibility in IAM in and across AWS accounts
51 lines • 2.58 kB
JavaScript
import { createStorageClient, loadConfigFiles } from '@cloud-copilot/iam-collect';
import { createRequire } from 'module';
import { IamCollectClient } from './client.js';
import { dynamicImport } from '../utils/dynamicImport.js';
/**
* Load IAM collect configs from the specified paths.
*
* @param configPaths the paths to the config files
* @returns the top-level configs
*/
export async function loadCollectConfigs(configPaths) {
return loadConfigFiles(configPaths);
}
/**
* Get a collect client for the specified partition using the provided configs.
*
* If a `clientFactoryPlugin` is provided, the factory function is called with
* `(store, clientOptions, data)` — i.e. the raw `AwsIamStore` and
* `IamCollectClientOptions` rather than a pre-built client — so the factory
* can construct whatever client subclass it needs without discarding an
* intermediate instance.
*
* @param configs - The top-level configs to use for storage.
* @param partition - Which partition to use (aws, aws-cn, aws-us-gov).
* @param options - Optional client options including a `clientFactoryPlugin`.
* @returns The iam-collect client to use for retrieving IAM resources.
*/
export async function getCollectClient(configs, partition, options) {
const { clientFactoryPlugin, ...clientOptions } = options ?? {};
const store = createStorageClient(configs, partition, true);
if (!clientFactoryPlugin)
return new IamCollectClient(store, clientOptions);
const mod = await dynamicImport(clientFactoryPlugin.module);
let factory = mod[clientFactoryPlugin.factoryExport];
// Some environments (e.g. vitest SSR) wrap import() results in a Proxy where
// Object.keys() returns the correct keys but property access returns undefined.
// When this happens, fall back to createRequire which bypasses the proxy.
if (!factory && Object.keys(mod).includes(clientFactoryPlugin.factoryExport)) {
const _require = createRequire(process.cwd() + '/');
const fallback = _require(clientFactoryPlugin.module);
factory = fallback[clientFactoryPlugin.factoryExport];
}
if (!factory) {
throw new Error(`Factory export '${clientFactoryPlugin.factoryExport}' not found in module '${clientFactoryPlugin.module}'`);
}
else if (typeof factory !== 'function') {
throw new Error(`Factory export '${clientFactoryPlugin.factoryExport}' in module '${clientFactoryPlugin.module}' is not a function`);
}
return factory(store, clientOptions, clientFactoryPlugin.data);
}
//# sourceMappingURL=collect.js.map