|
| 1 | +import CloudGraph from '@cloudgraph/sdk' |
| 2 | +import ACM, { CertificateSummary, ListCertificatesRequest, ListCertificatesResponse, ListTagsForCertificateRequest, ListTagsForCertificateResponse, Tag } from 'aws-sdk/clients/acm' |
| 3 | +import { AWSError } from 'aws-sdk/lib/error' |
| 4 | +import { Config } from 'aws-sdk/lib/config' |
| 5 | +import isEmpty from 'lodash/isEmpty' |
| 6 | +import groupBy from 'lodash/groupBy' |
| 7 | +import awsLoggerText from '../../properties/logger' |
| 8 | +import { initTestEndpoint, setAwsRetryOptions } from '../../utils' |
| 9 | +import AwsErrorLog from '../../utils/errorLog' |
| 10 | +import { API_GATEWAY_CUSTOM_DELAY } from '../../config/constants' |
| 11 | +import { TagMap } from '../../types' |
| 12 | + |
| 13 | +const lt = { ...awsLoggerText } |
| 14 | +const { logger } = CloudGraph |
| 15 | +const MAX_CERTIFICATES = 500 |
| 16 | +const serviceName = 'ACM' |
| 17 | +const errorLog = new AwsErrorLog(serviceName) |
| 18 | +const endpoint = initTestEndpoint(serviceName) |
| 19 | +const customRetrySettings = setAwsRetryOptions({ |
| 20 | + baseDelay: API_GATEWAY_CUSTOM_DELAY, |
| 21 | +}) |
| 22 | + |
| 23 | +export const getCertificatesForRegion = async ( |
| 24 | + acm: ACM |
| 25 | +): Promise<CertificateSummary[]> => |
| 26 | + new Promise(async resolve => { |
| 27 | + const certificateSummaryList: CertificateSummary[] = [] |
| 28 | + const listCertificatesOpts: ListCertificatesRequest = {} |
| 29 | + const listAllCertificates = (token?: string): void => { |
| 30 | + listCertificatesOpts.MaxItems = MAX_CERTIFICATES |
| 31 | + if (token) { |
| 32 | + listCertificatesOpts.NextToken = token |
| 33 | + } |
| 34 | + try { |
| 35 | + acm.listCertificates( |
| 36 | + listCertificatesOpts, |
| 37 | + (err: AWSError, data: ListCertificatesResponse) => { |
| 38 | + if (err) { |
| 39 | + errorLog.generateAwsErrorLog({ |
| 40 | + functionName: 'acm:listCertificates', |
| 41 | + err, |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + if (isEmpty(data)) { |
| 46 | + return resolve([]) |
| 47 | + } |
| 48 | + |
| 49 | + const { NextToken: nextToken, CertificateSummaryList: items = [] } = data || {} |
| 50 | + |
| 51 | + if (isEmpty(items)) { |
| 52 | + return resolve([]) |
| 53 | + } |
| 54 | + |
| 55 | + logger.debug(lt.fetchedAcmCertificates(items.length)) |
| 56 | + |
| 57 | + certificateSummaryList.push(...items) |
| 58 | + |
| 59 | + if (nextToken) { |
| 60 | + listAllCertificates(nextToken) |
| 61 | + } else { |
| 62 | + resolve(certificateSummaryList) |
| 63 | + } |
| 64 | + } |
| 65 | + ) |
| 66 | + } catch (error) { |
| 67 | + resolve([]) |
| 68 | + } |
| 69 | + } |
| 70 | + listAllCertificates() |
| 71 | + }) |
| 72 | + |
| 73 | +const getTagsForCertificate = ( |
| 74 | + acm: ACM, |
| 75 | + certificateArn: string |
| 76 | +): Promise<{ certificateArn: string; tags: Tag[] }> => |
| 77 | + new Promise<{ certificateArn: string; tags: Tag[] }>(resolve => { |
| 78 | + const args: ListTagsForCertificateRequest = { CertificateArn: certificateArn } |
| 79 | + const listTags = (): void => { |
| 80 | + try { |
| 81 | + acm.listTagsForCertificate( |
| 82 | + args, |
| 83 | + (err: AWSError, data: ListTagsForCertificateResponse) => { |
| 84 | + if (err) { |
| 85 | + errorLog.generateAwsErrorLog({ |
| 86 | + functionName: 'acm:listTagsForCertificate', |
| 87 | + err, |
| 88 | + }) |
| 89 | + } |
| 90 | + if (isEmpty(data)) { |
| 91 | + return resolve({ |
| 92 | + certificateArn, |
| 93 | + tags: [], |
| 94 | + }) |
| 95 | + } |
| 96 | + const { Tags: tags = [] } = data || {} |
| 97 | + |
| 98 | + resolve({ certificateArn, tags }) |
| 99 | + } |
| 100 | + ); |
| 101 | + |
| 102 | + } catch (error) { |
| 103 | + resolve({ |
| 104 | + certificateArn, |
| 105 | + tags: [], |
| 106 | + }) |
| 107 | + } |
| 108 | + } |
| 109 | + listTags(); |
| 110 | + }) |
| 111 | + |
| 112 | +export interface RawAwsAcm extends CertificateSummary { |
| 113 | + region: string |
| 114 | + Tags: TagMap |
| 115 | + account |
| 116 | +} |
| 117 | + |
| 118 | +export default async ({ |
| 119 | + regions, |
| 120 | + config, |
| 121 | + account, |
| 122 | +}: { |
| 123 | + account: string |
| 124 | + regions: string |
| 125 | + config: Config |
| 126 | +}): Promise<{ |
| 127 | + [region: string]: RawAwsAcm[] |
| 128 | +}> => |
| 129 | + new Promise(async resolve => { |
| 130 | + const acmResult: RawAwsAcm[] = [] |
| 131 | + |
| 132 | + const regionPromises = regions.split(',').map(region => { |
| 133 | + const acm = new ACM({ |
| 134 | + ...config, |
| 135 | + region, |
| 136 | + endpoint, |
| 137 | + ...customRetrySettings, |
| 138 | + }) |
| 139 | + |
| 140 | + return new Promise<void>(async resolveAcmData => { |
| 141 | + // Get ACM certificate summaries |
| 142 | + const certificates = await getCertificatesForRegion(acm) |
| 143 | + |
| 144 | + const tagsPromises = certificates.map( |
| 145 | + ({ CertificateArn: certificateArn }) => getTagsForCertificate(acm, certificateArn) |
| 146 | + ) |
| 147 | + |
| 148 | + const tagsData = await Promise.all(tagsPromises) |
| 149 | + |
| 150 | + if (!isEmpty(certificates)) { |
| 151 | + for (const certificate of certificates) { |
| 152 | + acmResult.push({ |
| 153 | + ...certificate, |
| 154 | + Tags: tagsData?.find(t => t.certificateArn === certificate.CertificateArn) |
| 155 | + ?.tags.reduce((tagMap, {Key, Value}) => { |
| 156 | + tagMap[Key] = Value; |
| 157 | + return tagMap; |
| 158 | + }, {}), |
| 159 | + region, |
| 160 | + account, |
| 161 | + }) |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + resolveAcmData() |
| 166 | + }) |
| 167 | + }) |
| 168 | + |
| 169 | + await Promise.all(regionPromises) |
| 170 | + errorLog.reset() |
| 171 | + |
| 172 | + resolve(groupBy(acmResult, 'region')) |
| 173 | + }) |
0 commit comments