@cloud-copilot/iam-collect
Version:
Collect IAM information from AWS Accounts
85 lines • 3.88 kB
JavaScript
import { splitArnParts } from '@cloud-copilot/iam-utils';
import { sep } from 'path';
import { getStorageConfig } from '../config/config.js';
import { iamCollectVersion } from '../config/packageVersion.js';
import {} from './AwsIamStore.js';
import { FileSystemAdapter } from './file/FileSystemAdapter.js';
import { FileSystemAwsIamStore } from './file/FileSystemAwsIamStore.js';
import { InMemoryPathBasedPersistenceAdapter } from './InMemoryPathBasedPersistenceAdapter.js';
import { S3PathBasedPersistenceAdapter } from './s3/S3PathBasedPersistenceAdapter.js';
import { SqliteAwsIamStore } from './sqlite/SqliteAwsIamStore.js';
export function createStorageClient(storageConfig, partition, deleteData) {
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, new FileSystemAdapter(deleteData));
}
else if (storageConfig.type === 's3') {
const persistenceAdapter = new S3PathBasedPersistenceAdapter(storageConfig, deleteData);
return new FileSystemAwsIamStore(storageConfig.prefix || '', partition, '/', persistenceAdapter);
}
else if (storageConfig.type === 'sqlite') {
const version = iamCollectVersion();
return new SqliteAwsIamStore(storageConfig.path, partition, version);
}
throw new Error(`Unsupported storage type: ${storageConfig.type}. Supported types are: file, s3 and sqlite.`);
}
/**
* 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