|
| 1 | +import CloudGraph from '@cloudgraph/sdk' |
| 2 | +import groupBy from 'lodash/groupBy' |
| 3 | +import isEmpty from 'lodash/isEmpty' |
| 4 | +import IAM, { |
| 5 | + InstanceProfile, |
| 6 | + ListInstanceProfilesRequest, |
| 7 | + ListInstanceProfilesResponse, |
| 8 | + ListInstanceProfileTagsResponse, |
| 9 | +} from 'aws-sdk/clients/iam' |
| 10 | + |
| 11 | +import { Config } from 'aws-sdk/lib/config' |
| 12 | +import { AWSError } from 'aws-sdk/lib/error' |
| 13 | +import { convertAwsTagsToTagMap } from '../../utils/format' |
| 14 | +import { AwsTag, TagMap } from '../../types' |
| 15 | + |
| 16 | +import awsLoggerText from '../../properties/logger' |
| 17 | +import { initTestEndpoint, setAwsRetryOptions } from '../../utils' |
| 18 | +import AwsErrorLog from '../../utils/errorLog' |
| 19 | +import { globalRegionName } from '../../enums/regions' |
| 20 | + |
| 21 | +import { |
| 22 | + IAM_CUSTOM_DELAY, |
| 23 | + MAX_FAILED_AWS_REQUEST_RETRIES, |
| 24 | +} from '../../config/constants' |
| 25 | + |
| 26 | +const lt = { ...awsLoggerText } |
| 27 | +const { logger } = CloudGraph |
| 28 | +const serviceName = 'IAM Instace Profile' |
| 29 | +const errorLog = new AwsErrorLog(serviceName) |
| 30 | +const endpoint = initTestEndpoint(serviceName) |
| 31 | +const customRetrySettings = setAwsRetryOptions({ |
| 32 | + maxRetries: MAX_FAILED_AWS_REQUEST_RETRIES, |
| 33 | + baseDelay: IAM_CUSTOM_DELAY, |
| 34 | +}) |
| 35 | + |
| 36 | +export const listInstancesProfiles = async ( |
| 37 | + iam: IAM |
| 38 | +): Promise<InstanceProfile[]> => |
| 39 | + new Promise(resolve => { |
| 40 | + const instanceProfileList: InstanceProfile[] = [] |
| 41 | + let args: ListInstanceProfilesRequest = {} |
| 42 | + const listAllInstanceProfiles = (marker?: string) => { |
| 43 | + if (marker) { |
| 44 | + args = { ...args, Marker: marker } |
| 45 | + } |
| 46 | + try { |
| 47 | + iam.listInstanceProfiles( |
| 48 | + args, |
| 49 | + async (err: AWSError, data: ListInstanceProfilesResponse) => { |
| 50 | + if (err) { |
| 51 | + errorLog.generateAwsErrorLog({ |
| 52 | + functionName: 'iam:listInstanceProfiles', |
| 53 | + err, |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + const { InstanceProfiles = [], IsTruncated, Marker } = data |
| 58 | + |
| 59 | + instanceProfileList.push(...InstanceProfiles) |
| 60 | + |
| 61 | + if (IsTruncated) { |
| 62 | + listAllInstanceProfiles(Marker) |
| 63 | + } |
| 64 | + |
| 65 | + resolve(instanceProfileList) |
| 66 | + } |
| 67 | + ) |
| 68 | + } catch (error) { |
| 69 | + resolve([]) |
| 70 | + } |
| 71 | + } |
| 72 | + listAllInstanceProfiles() |
| 73 | + }) |
| 74 | + |
| 75 | +export const getTags = async ({ |
| 76 | + iam, |
| 77 | + name, |
| 78 | +}: { |
| 79 | + iam: IAM |
| 80 | + name: string |
| 81 | +}): Promise<TagMap> => |
| 82 | + new Promise(resolve => { |
| 83 | + try { |
| 84 | + iam.listInstanceProfileTags( |
| 85 | + { InstanceProfileName: name }, |
| 86 | + (err: AWSError, data: ListInstanceProfileTagsResponse) => { |
| 87 | + if (err) { |
| 88 | + errorLog.generateAwsErrorLog({ |
| 89 | + functionName: 'iam:listInstanceProfileTags', |
| 90 | + err, |
| 91 | + }) |
| 92 | + resolve({}) |
| 93 | + } |
| 94 | + const { Tags = [] } = data || {} |
| 95 | + resolve(convertAwsTagsToTagMap(Tags as AwsTag[])) |
| 96 | + } |
| 97 | + ) |
| 98 | + } catch (error) { |
| 99 | + resolve({}) |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | +/** |
| 104 | + * IAM Instance Profile |
| 105 | + */ |
| 106 | + |
| 107 | +export interface RawAwsInstanceProfile extends Omit<InstanceProfile, 'Tags'> { |
| 108 | + region: string |
| 109 | + Tags?: TagMap |
| 110 | +} |
| 111 | + |
| 112 | +export default async ({ |
| 113 | + config, |
| 114 | +}: { |
| 115 | + regions: string |
| 116 | + config: Config |
| 117 | +}): Promise<{ |
| 118 | + [region: string]: RawAwsInstanceProfile[] |
| 119 | +}> => |
| 120 | + new Promise(async resolve => { |
| 121 | + const instancesProfilesResult: RawAwsInstanceProfile[] = [] |
| 122 | + const tagsPromises = [] |
| 123 | + |
| 124 | + const client = new IAM({ |
| 125 | + ...config, |
| 126 | + region: globalRegionName, |
| 127 | + endpoint, |
| 128 | + ...customRetrySettings, |
| 129 | + }) |
| 130 | + |
| 131 | + const instancesProfiles = await listInstancesProfiles(client) |
| 132 | + |
| 133 | + if (!isEmpty(instancesProfiles)) { |
| 134 | + instancesProfiles.map( |
| 135 | + ({ Tags, InstanceProfileName, ...instancesProfile }, idx) => { |
| 136 | + instancesProfilesResult.push({ |
| 137 | + InstanceProfileName, |
| 138 | + ...instancesProfile, |
| 139 | + region: globalRegionName, |
| 140 | + }) |
| 141 | + |
| 142 | + const tagsPromise = new Promise<void>(async resolveTags => { |
| 143 | + instancesProfilesResult[idx].Tags = await getTags({ |
| 144 | + iam: client, |
| 145 | + name: InstanceProfileName, |
| 146 | + }) |
| 147 | + resolveTags() |
| 148 | + }) |
| 149 | + tagsPromises.push(tagsPromise) |
| 150 | + } |
| 151 | + ) |
| 152 | + } |
| 153 | + |
| 154 | + logger.debug(lt.foundInstanceProfiles(instancesProfiles.length)) |
| 155 | + await Promise.all(tagsPromises) |
| 156 | + errorLog.reset() |
| 157 | + |
| 158 | + resolve(groupBy(instancesProfilesResult, 'region')) |
| 159 | + }) |
0 commit comments