UNPKG

@cloud-copilot/iam-collect

Version:

Collect IAM information from AWS Accounts

84 lines 3.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createStorageClient = createStorageClient; exports.createInMemoryStorageClient = createInMemoryStorageClient; exports.resourcePrefix = resourcePrefix; exports.resourceTypePrefix = resourceTypePrefix; exports.joinPathParts = joinPathParts; const iam_utils_1 = require("@cloud-copilot/iam-utils"); const path_1 = require("path"); const config_js_1 = require("../config/config.js"); const FileSystemAwsIamStore_js_1 = require("./file/FileSystemAwsIamStore.js"); const InMemoryPathBasedPersistenceAdapter_js_1 = require("./InMemoryPathBasedPersistenceAdapter.js"); const S3PathBasedPersistenceAdapter_js_1 = require("./s3/S3PathBasedPersistenceAdapter.js"); function createStorageClient(storageConfig, partition) { if (Array.isArray(storageConfig)) { const foundConfig = (0, config_js_1.getStorageConfig)(storageConfig); if (!foundConfig) { throw new Error('No storage configuration found. Cannot create storage client.'); } storageConfig = foundConfig; } if (storageConfig.type === 'file') { return new FileSystemAwsIamStore_js_1.FileSystemAwsIamStore(storageConfig.path, partition, path_1.sep); } else if (storageConfig.type === 's3') { const persistenceAdapter = new S3PathBasedPersistenceAdapter_js_1.S3PathBasedPersistenceAdapter(storageConfig); return new FileSystemAwsIamStore_js_1.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. */ function createInMemoryStorageClient() { return new FileSystemAwsIamStore_js_1.FileSystemAwsIamStore('mock', 'aws', '/', new InMemoryPathBasedPersistenceAdapter_js_1.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. */ function resourcePrefix(startingPath, resourceArn, separator) { const parts = (0, iam_utils_1.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. */ function resourceTypePrefix(startingPath, parts, separator) { return joinPathParts([ startingPath, parts.partition, parts.service, parts.region, parts.account === 'aws' ? parts.account : undefined, parts.resourceType ], separator); } 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