|
| 1 | +import CloudGraph from '@cloudgraph/sdk' |
| 2 | +import groupBy from 'lodash/groupBy' |
| 3 | +import isEmpty from 'lodash/isEmpty' |
| 4 | +import AccessAnalyzer, { |
| 5 | + ListAnalyzersRequest, |
| 6 | + ListAnalyzersResponse, |
| 7 | + AnalyzerSummary, |
| 8 | + AnalyzersList, |
| 9 | +} from 'aws-sdk/clients/accessanalyzer' |
| 10 | + |
| 11 | +import { Config } from 'aws-sdk/lib/config' |
| 12 | +import { AWSError } from 'aws-sdk/lib/error' |
| 13 | +import awsLoggerText from '../../properties/logger' |
| 14 | +import { initTestEndpoint } from '../../utils' |
| 15 | +import AwsErrorLog from '../../utils/errorLog' |
| 16 | +import { TagMap } from '../../types' |
| 17 | + |
| 18 | +const lt = { ...awsLoggerText } |
| 19 | +const { logger } = CloudGraph |
| 20 | +const serviceName = 'IAM Access Analyzer' |
| 21 | +const errorLog = new AwsErrorLog(serviceName) |
| 22 | +const endpoint = initTestEndpoint(serviceName) |
| 23 | + |
| 24 | +const listAnalyzersData = async ({ |
| 25 | + accessAnalyzer, |
| 26 | + region, |
| 27 | + nextToken: NextToken = '', |
| 28 | +}: { |
| 29 | + accessAnalyzer: AccessAnalyzer |
| 30 | + region: string |
| 31 | + nextToken?: string |
| 32 | +}): Promise<(AnalyzerSummary & { region: string })[]> => |
| 33 | + new Promise<(AnalyzerSummary & { region: string })[]>(resolve => { |
| 34 | + let analyzerSummarytData: (AnalyzerSummary & { |
| 35 | + region: string |
| 36 | + })[] = [] |
| 37 | + |
| 38 | + const analyzersList: AnalyzersList = [] |
| 39 | + let args: ListAnalyzersRequest = {} |
| 40 | + |
| 41 | + if (NextToken) { |
| 42 | + args = { ...args, nextToken: NextToken } |
| 43 | + } |
| 44 | + |
| 45 | + accessAnalyzer.listAnalyzers( |
| 46 | + args, |
| 47 | + (err: AWSError, data: ListAnalyzersResponse) => { |
| 48 | + if (err) { |
| 49 | + errorLog.generateAwsErrorLog({ |
| 50 | + functionName: 'accessAnalyzer:listAnalyzers', |
| 51 | + err, |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + if (!isEmpty(data)) { |
| 56 | + const { nextToken, analyzers: analyzersData = [] } = data |
| 57 | + |
| 58 | + analyzersList.push(...analyzersData) |
| 59 | + |
| 60 | + logger.debug(lt.fetchedaccessAnalyzers(analyzersList.length)) |
| 61 | + |
| 62 | + if (nextToken) { |
| 63 | + listAnalyzersData({ accessAnalyzer, region, nextToken }) |
| 64 | + } |
| 65 | + |
| 66 | + analyzerSummarytData = analyzersList.map(analyzer => ({ |
| 67 | + ...analyzer, |
| 68 | + region, |
| 69 | + })) |
| 70 | + } |
| 71 | + |
| 72 | + resolve(analyzerSummarytData) |
| 73 | + } |
| 74 | + ) |
| 75 | + }) |
| 76 | + |
| 77 | +/** |
| 78 | + * IAM Access Analyzer |
| 79 | + */ |
| 80 | + |
| 81 | +export interface RawAwsAnalyzerSummary extends Omit<AnalyzerSummary, 'Tags'> { |
| 82 | + region: string |
| 83 | + Tags?: TagMap |
| 84 | +} |
| 85 | + |
| 86 | +export default async ({ |
| 87 | + regions, |
| 88 | + config, |
| 89 | +}: { |
| 90 | + regions: string |
| 91 | + config: Config |
| 92 | +}): Promise<{ |
| 93 | + [region: string]: RawAwsAnalyzerSummary[] |
| 94 | +}> => |
| 95 | + new Promise(async resolve => { |
| 96 | + const analyzerSummaryResult: RawAwsAnalyzerSummary[] = [] |
| 97 | + |
| 98 | + const regionPromises = regions.split(',').map(region => { |
| 99 | + const accessAnalyzer = new AccessAnalyzer({ ...config, region, endpoint }) |
| 100 | + |
| 101 | + return new Promise<void>(async resolveAnalyzerSummaryData => { |
| 102 | + const analyzerSummaryData = await listAnalyzersData({ |
| 103 | + accessAnalyzer, |
| 104 | + region, |
| 105 | + }) |
| 106 | + |
| 107 | + if (!isEmpty(analyzerSummaryData)) { |
| 108 | + for (const analyzer of analyzerSummaryData) { |
| 109 | + analyzerSummaryResult.push({ |
| 110 | + ...analyzer, |
| 111 | + region, |
| 112 | + Tags: analyzer.tags || {} |
| 113 | + }) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + resolveAnalyzerSummaryData() |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + await Promise.all(regionPromises) |
| 122 | + errorLog.reset() |
| 123 | + |
| 124 | + resolve(groupBy(analyzerSummaryResult, 'region')) |
| 125 | + }) |
0 commit comments