UNPKG

@cloud-copilot/iam-collect

Version:

Collect IAM information from AWS Accounts

181 lines 8.09 kB
#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const cli_1 = require("@cloud-copilot/cli"); const analyze_js_1 = require("./analysis/analyze.js"); const configFile_js_1 = require("./config/configFile.js"); const createConfigFile_js_1 = require("./config/createConfigFile.js"); const defaultConfig_js_1 = require("./config/defaultConfig.js"); const packageVersion_js_1 = require("./config/packageVersion.js"); const download_js_1 = require("./download/download.js"); const index_js_1 = require("./index/index.js"); const mergeSqlite_js_1 = require("./mergeSqlite/mergeSqlite.js"); const log_1 = require("@cloud-copilot/log"); /** * For some reason the AWS SDK v3 looks for AWS_REGION and not AWS_DEFAULT_REGION * even though other SDKs and the CLI use AWS_DEFAULT_REGION. To make things easier * for users, if AWS_DEFAULT_REGION is set and AWS_REGION is not, copy it over. */ if (process.env.AWS_DEFAULT_REGION && !process.env.AWS_REGION) { process.env.AWS_REGION = process.env.AWS_DEFAULT_REGION; } const main = async () => { const cli = await (0, cli_1.parseCliArguments)('iam-collect', { init: { description: 'Initialize the iam-collect configuration file', arguments: { sqlite: (0, cli_1.booleanArgument)({ character: 's', description: 'Initialize configuration file with SQLite storage' }) } }, download: { description: 'Download IAM data and update indexes', arguments: { configFiles: (0, cli_1.stringArrayArgument)({ description: 'The configuration files to use', defaultValue: [] }), accounts: (0, cli_1.stringArrayArgument)({ description: 'The account IDs to download from', defaultValue: [] }), regions: (0, cli_1.stringArrayArgument)({ description: 'The regions to download from', defaultValue: [] }), services: (0, cli_1.stringArrayArgument)({ description: 'The services to download', defaultValue: [] }), concurrency: (0, cli_1.numberArgument)({ description: 'The maximum number of concurrent downloads to allow. Defaults based on your system CPUs' }), noIndex: (0, cli_1.booleanArgument)({ description: 'Skip refreshing the indexes after downloading', character: 'n' }), writeOnly: (0, cli_1.booleanArgument)({ description: 'Only write data for discovered resources and ignore any existing data. May improve performance if you know you have no existing data', character: 'w' }) } }, index: { description: 'Refresh the IAM data indexes', arguments: { configFiles: (0, cli_1.stringArrayArgument)({ description: 'The configuration files to use', defaultValue: [] }), partition: (0, cli_1.stringArgument)({ description: 'The partition to refresh index data for. Defaults to aws', defaultValue: 'aws' }), accounts: (0, cli_1.stringArrayArgument)({ description: 'The account IDs to refresh index data for', defaultValue: [] }), regions: (0, cli_1.stringArrayArgument)({ description: 'The regions to refresh index data for', defaultValue: [] }), services: (0, cli_1.stringArrayArgument)({ description: 'The services to refresh index data for', defaultValue: [] }), concurrency: (0, cli_1.numberArgument)({ description: 'The maximum number of concurrent indexers to run. Defaults based on your system CPUs' }) } }, 'merge-databases': { description: 'Merge multiple iam-collect SQLite databases into one', arguments: { targetDatabase: (0, cli_1.stringArgument)({ description: 'The target database to merge into. If it does not exist, it will be created. If it does exist, new data will be added to existing data' }), sourceDatabases: (0, cli_1.stringArrayArgument)({ description: 'The source databases to merge from' }) } }, 'analyze-logs': { description: 'Analyze iam-collect trace logs and summarize job execution times', arguments: { logFile: (0, cli_1.stringArgument)({ description: 'The path to the log file to analyze' }) } } }, { log: (0, cli_1.enumArgument)({ description: 'The log level to use', validValues: [...log_1.LogLevels] // Convert readonly to mutable array }) }, { envPrefix: 'IAM_COLLECT', showHelpIfNoArgs: true, requireSubcommand: true, expectOperands: false, version: { currentVersion: packageVersion_js_1.iamCollectVersion, checkForUpdates: '@cloud-copilot/iam-collect' } }); (0, log_1.setLogger)(new log_1.StandardLogger({ logLevel: cli.args.log ?? undefined })); if (cli.subcommand === 'init') { if ((0, defaultConfig_js_1.defaultConfigExists)()) { console.error('Configuration file already exists'); process.exit(1); } console.log('Initializing...'); await (0, createConfigFile_js_1.createDefaultConfiguration)({ type: cli.args.sqlite ? 'sqlite' : 'file' }); } else if (cli.subcommand === 'download') { const defaultConfig = './iam-collect.jsonc'; const configFiles = cli.args.configFiles.length > 0 ? cli.args.configFiles : [defaultConfig]; const configs = (0, configFile_js_1.loadConfigFiles)(configFiles); await (0, download_js_1.downloadData)(configs, cli.args.accounts, cli.args.regions, cli.args.services, cli.args.concurrency, cli.args.noIndex, cli.args.writeOnly); } else if (cli.subcommand === 'index') { const defaultConfig = './iam-collect.jsonc'; const configFiles = cli.args.configFiles.length > 0 ? cli.args.configFiles : [defaultConfig]; const configs = (0, configFile_js_1.loadConfigFiles)(configFiles); await (0, index_js_1.index)(configs, cli.args.partition || 'aws', cli.args.accounts, cli.args.regions, cli.args.services, cli.args.concurrency); } else if (cli.subcommand === 'merge-databases') { if (!cli.args.targetDatabase) { console.error('You must specify a target database using --target-database'); process.exit(1); } if (!cli.args.sourceDatabases) { console.error('You must specify at least one source database using --source-databases'); process.exit(1); } await (0, mergeSqlite_js_1.mergeSqliteDatabases)(cli.args.targetDatabase, cli.args.sourceDatabases); } else if (cli.subcommand === 'analyze-logs') { if (!cli.args.logFile) { console.error('You must specify a log file to analyze using --log-file'); process.exit(1); } const allComplete = await (0, analyze_js_1.conductLogAnalysis)(cli.args.logFile); if (!allComplete) { process.exit(1); } } }; main() .catch((e) => { console.error(e); process.exit(1); }) .then(() => { }) .finally(() => { }); //# sourceMappingURL=cli.js.map