UNPKG

@cloud-copilot/iam-collect

Version:

Collect IAM information from AWS Accounts

77 lines 3.4 kB
import { splitArnParts } from '@cloud-copilot/iam-utils'; import { sep } from 'path'; import { getStorageConfig } from '../config/config.js'; import { FileSystemAwsIamStore } from './file/FileSystemAwsIamStore.js'; import { InMemoryPathBasedPersistenceAdapter } from './InMemoryPathBasedPersistenceAdapter.js'; import { S3PathBasedPersistenceAdapter } from './s3/S3PathBasedPersistenceAdapter.js'; export function createStorageClient(storageConfig, partition) { if (Array.isArray(storageConfig)) { const foundConfig = getStorageConfig(storageConfig); if (!foundConfig) { throw new Error('No storage configuration found. Cannot create storage client.'); } storageConfig = foundConfig; } if (storageConfig.type === 'file') { return new FileSystemAwsIamStore(storageConfig.path, partition, sep); } else if (storageConfig.type === 's3') { const persistenceAdapter = new S3PathBasedPersistenceAdapter(storageConfig); return new FileSystemAwsIamStore(storageConfig.prefix || '', partition, '/', persistenceAdapter); } throw new Error(`Unsupported storage type: ${storageConfig.type}. Supported types are: file and s3.`); } /** * Create an in-memory storage client with the 'aws' partition. * * This is useful for testing. */ export function createInMemoryStorageClient() { return new FileSystemAwsIamStore('mock', 'aws', '/', new InMemoryPathBasedPersistenceAdapter()); } /** * Generate a resource prefix given a starting path, a resource ARN, and a separator. * The function uses splitArnParts to get the parts of the ARN and then joins each non-empty part * with the provided separator. The last segment (resourcePath) is URL encoded. * * @param startingPath - The starting path (e.g. a base folder) * @param resourceArn - The full resource ARN. * @param separator - The separator to use (e.g. '/' or '-'). * @returns A string that represents the resource prefix. */ export function resourcePrefix(startingPath, resourceArn, separator) { const parts = splitArnParts(resourceArn); return joinPathParts([ startingPath, parts.service, parts.region, parts.accountId === 'aws' ? parts.accountId : undefined, parts.resourceType, parts.resourcePath ? encodeURIComponent(parts.resourcePath.trim()) : undefined ], separator); } /** * Generate a resource type prefix based on the provided starting path and resource type parts. * * @param startingPath - The starting path (e.g. a base folder) * @param parts - An object containing the components of the resource type * @param separator - the separator to use for joining the parts. This could be '/' or any other string. * @returns A string that represents the resource type prefix. */ export function resourceTypePrefix(startingPath, parts, separator) { return joinPathParts([ startingPath, parts.partition, parts.service, parts.region, parts.account === 'aws' ? parts.account : undefined, parts.resourceType ], separator); } export function joinPathParts(parts, separator) { // Filter out undefined or empty strings const filteredParts = parts.filter((part) => part !== undefined && part.trim() !== ''); // Join the remaining parts with a '/' return filteredParts.join(separator); } //# sourceMappingURL=util.js.map