|
| 1 | +import { Config } from 'aws-sdk/lib/config' |
| 2 | +import SSM, { DescribeDocumentPermissionResponse } from 'aws-sdk/clients/ssm' |
| 3 | +import STS, { GetCallerIdentityResponse } from 'aws-sdk/clients/sts' |
| 4 | +import { PromiseResult } from 'aws-sdk/lib/request' |
| 5 | +import { AWSError } from 'aws-sdk/lib/error' |
| 6 | +import isEmpty from 'lodash/isEmpty' |
| 7 | +import groupBy from 'lodash/groupBy' |
| 8 | +import { convertAwsTagsToTagMap } from '../../utils/format' |
| 9 | +import { TagMap } from '../../types' |
| 10 | +import { initTestEndpoint } from '../../utils' |
| 11 | +import AwsErrorLog from '../../utils/errorLog' |
| 12 | + |
| 13 | +const serviceName = 'systemsManagerDocument' |
| 14 | +const endpoint = initTestEndpoint(serviceName) |
| 15 | +const errorLog = new AwsErrorLog(serviceName) |
| 16 | +export interface RawAwsSystemsManagerDocument |
| 17 | + extends Omit<SSM.DocumentIdentifier, 'Tags'> { |
| 18 | + region: string |
| 19 | + accountId: string |
| 20 | + permissions: { |
| 21 | + accountIds: string[] |
| 22 | + accountSharingInfoList: { |
| 23 | + AccountId?: string |
| 24 | + SharedDocumentVersion?: string |
| 25 | + }[] |
| 26 | + } |
| 27 | + Tags: TagMap |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * SystemsManagerDocument |
| 32 | + */ |
| 33 | + |
| 34 | +export default async ({ |
| 35 | + regions, |
| 36 | + config, |
| 37 | +}: { |
| 38 | + regions: string |
| 39 | + config: Config |
| 40 | +}): Promise<{ [region: string]: RawAwsSystemsManagerDocument[] }> => { |
| 41 | + const result: RawAwsSystemsManagerDocument[] = [] |
| 42 | + |
| 43 | + const activeRegions = regions.split(',') |
| 44 | + // We need the account in the raw data so we can connect to trags |
| 45 | + let account: PromiseResult<GetCallerIdentityResponse, AWSError> |
| 46 | + try { |
| 47 | + account = await new STS(config).getCallerIdentity().promise() |
| 48 | + } catch (err) { |
| 49 | + errorLog.generateAwsErrorLog({ functionName: 'getCallerIdentity', err }) |
| 50 | + } |
| 51 | + const accountId = account?.Account |
| 52 | + for (const region of activeRegions) { |
| 53 | + const client = new SSM({ ...config, region, endpoint }) |
| 54 | + const systemsManagerDocumentData: SSM.DocumentIdentifier[] = [] |
| 55 | + try { |
| 56 | + const filterParam = { Filters: [{ Key: 'Owner', Values: ['Self'] }] } |
| 57 | + const data = await client.listDocuments(filterParam).promise() |
| 58 | + systemsManagerDocumentData.push(...data.DocumentIdentifiers) |
| 59 | + let marker = data.NextToken |
| 60 | + while (marker) { |
| 61 | + const nextPage = await client |
| 62 | + .listDocuments({ ...filterParam, NextToken: marker }) |
| 63 | + .promise() |
| 64 | + if (!isEmpty(data.DocumentIdentifiers)) { |
| 65 | + systemsManagerDocumentData.push(...nextPage.DocumentIdentifiers) |
| 66 | + marker = nextPage.NextToken |
| 67 | + } |
| 68 | + } |
| 69 | + } catch (err) { |
| 70 | + errorLog.generateAwsErrorLog({ functionName: 'listDocuments', err }) |
| 71 | + } |
| 72 | + for (const doc of systemsManagerDocumentData) { |
| 73 | + let documentPermissions: PromiseResult< |
| 74 | + DescribeDocumentPermissionResponse, |
| 75 | + AWSError |
| 76 | + > |
| 77 | + try { |
| 78 | + documentPermissions = await client |
| 79 | + .describeDocumentPermission({ |
| 80 | + Name: doc.Name, |
| 81 | + PermissionType: 'Share', |
| 82 | + }) |
| 83 | + .promise() |
| 84 | + } catch (err) { |
| 85 | + errorLog.generateAwsErrorLog({ |
| 86 | + functionName: 'describeDocumentPermission', |
| 87 | + err, |
| 88 | + }) |
| 89 | + } |
| 90 | + result.push({ |
| 91 | + ...doc, |
| 92 | + accountId, |
| 93 | + permissions: { |
| 94 | + accountIds: documentPermissions?.AccountIds, |
| 95 | + accountSharingInfoList: documentPermissions?.AccountSharingInfoList, |
| 96 | + }, |
| 97 | + Tags: convertAwsTagsToTagMap(doc.Tags ?? []), |
| 98 | + region, |
| 99 | + }) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + errorLog.reset() |
| 104 | + return groupBy(result, 'region') |
| 105 | +} |
0 commit comments