-
Notifications
You must be signed in to change notification settings - Fork 88
/
index.js
232 lines (204 loc) · 9.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
const R = require("ramda");
const {PromisePool} = require("@supercharge/promise-pool");
const {profileAsync, createArn} = require('../utils');
const logger = require("../logger");
const {parse: parseArn} = require("@aws-sdk/util-arn-parser");
const {
ACCESS_DENIED,
IAM,
ROLE,
DISCOVERY_ROLE_NAME
} = require("../constants");
function getDbResourcesMap(appSync) {
const {createPaginator, getResources} = appSync;
const getResourcesPaginator = createPaginator(getResources, 1000);
return async () => {
const resourcesMap = new Map();
for await (const resources of getResourcesPaginator({})) {
resources.forEach(r => resourcesMap.set(r.id, {
id: r.id,
label: r.label,
md5Hash: r.md5Hash,
// gql will return `null` for missing properties which will break the hashing
// comparison for sdk discovered resources
properties: R.reject(R.isNil, r.properties)
}));
}
return resourcesMap;
};
}
function getDbRelationshipsMap(appSync) {
const pageSize = 2500;
function getDbRelationships(pagination, relationshipsMap= new Map()) {
return appSync.getRelationships({pagination})
.then(relationships => {
if(R.isEmpty(relationships)) return relationshipsMap;
relationships.forEach(rel => {
const {id: source} = rel.source;
const {id: target} = rel.target;
const {label, id} = rel;
relationshipsMap.set(`${source}_${label}_${target}`, {
source, target, id, label
})
});
const {start, end} = pagination;
return getDbRelationships({start: start + pageSize, end: end + pageSize}, relationshipsMap);
})
}
return async () => getDbRelationships({start: 0, end: pageSize});
}
function process(processor) {
return ({concurrency, batchSize}, resources) => PromisePool
.withConcurrency(concurrency)
.for(R.splitEvery(batchSize, resources))
.process(processor);
}
function updateCrawledAccounts(appSync) {
return async accounts => {
const {errors, results} = await PromisePool
.withConcurrency(10) // the reserved concurrency of the settings lambda is 10
.for(accounts)
.process(async ({accountId, name, isIamRoleDeployed, lastCrawled, resourcesRegionMetadata}) => {
return appSync.updateAccount(
accountId,
name,
isIamRoleDeployed, isIamRoleDeployed ? new Date().toISOString() : lastCrawled,
resourcesRegionMetadata
);
});
logger.error(`There were ${errors.length} errors when updating last crawled time for accounts.`);
logger.debug('Errors: ', {errors});
return {errors, results};
}
}
function addCrawledAccounts(appSync) {
return async accounts => {
return Promise.resolve(accounts)
// we must ensure we do not persist any temporary credentials to the db
.then(R.map(R.omit(['credentials', 'toDelete'])))
.then(R.map(({regions, isIamRoleDeployed, lastCrawled, ...props}) => {
return {
...props,
isIamRoleDeployed,
regions: regions.map(name => ({name})),
lastCrawled: isIamRoleDeployed ? new Date().toISOString() : lastCrawled
}
}))
.then(appSync.addAccounts);
}
}
async function getOrgAccounts(
{ec2Client, organizationsClient, configClient}, appSyncClient, {configAggregator, organizationUnitId}
) {
const [dbAccounts, orgAccounts, {OrganizationAggregationSource}, regions] = await Promise.all([
appSyncClient.getAccounts(),
organizationsClient.getAllActiveAccountsFromParent(organizationUnitId),
configClient.getConfigAggregator(configAggregator),
ec2Client.getAllRegions()
]);
logger.info(`Organization source info.`, {OrganizationAggregationSource});
const dbAccountsMap = new Map(dbAccounts.map(x => [x.accountId, x]));
logger.info('Accounts from db.', {dbAccounts});
const orgAccountsMap = new Map(orgAccounts.map(x => [x.Id, x]));
const deletedAccounts = dbAccounts.reduce((acc, account) => {
const {accountId} = account;
if(dbAccountsMap.has(accountId) && !orgAccountsMap.has(accountId)) {
acc.push({...account, toDelete: true});
}
return acc;
}, []);
return orgAccounts
.map(({Id, isManagementAccount, Name: name, Arn}) => {
const [, organizationId] = parseArn(Arn).resource.split('/');
const lastCrawled = dbAccountsMap.get(Id)?.lastCrawled;
return {
accountId: Id,
organizationId,
name,
...(isManagementAccount ? {isManagementAccount} : {}),
...(lastCrawled != null ? {lastCrawled} : {}),
regions: OrganizationAggregationSource.AllAwsRegions
? regions.map(x => x.name) : OrganizationAggregationSource.AwsRegions,
toDelete: dbAccountsMap.has(Id) && !orgAccountsMap.has(Id)
};
})
.concat(deletedAccounts);
}
function createDiscoveryRoleArn(accountId, rootAccountId) {
return createArn({service: IAM, accountId, resource: `${ROLE}/${DISCOVERY_ROLE_NAME}-${rootAccountId}`});
}
const addAccountCredentials = R.curry(async ({stsClient}, rootAccountId, accounts) => {
const {errors, results} = await PromisePool
.withConcurrency(30)
.for(accounts)
.process(async ({accountId, organizationId, ...props}) => {
const roleArn = createDiscoveryRoleArn(accountId, rootAccountId);
const credentials = await stsClient.getCredentials(roleArn);
return {
...props,
accountId,
isIamRoleDeployed: true,
...(organizationId != null ? {organizationId} : {}),
credentials
};
});
errors.forEach(({message, raw: error, item: {accountId, isManagementAccount}}) => {
const roleArn = createDiscoveryRoleArn(accountId, rootAccountId);
if (error.Code === ACCESS_DENIED) {
const errorMessage = `Access denied assuming role: ${roleArn}.`;
if(isManagementAccount) {
logger.error(`${errorMessage} This is the management account, ensure the global resources template has been deployed to the account.`);
} else {
logger.error(`${errorMessage} Ensure the global resources template has been deployed to account: ${accountId}. The discovery for this account will be skipped.`);
}
} else {
logger.error(`Error assuming role: ${roleArn}: ${message}`);
}
});
return [
...errors.filter(({raw}) => raw.Code === ACCESS_DENIED).map(({item}) => ({...item, isIamRoleDeployed: false})),
...results,
];
});
module.exports = {
createApiClient(awsClient, appSync, config) {
const ec2Client = awsClient.createEc2Client();
const organizationsClient = awsClient.createOrganizationsClient();
const configClient = awsClient.createConfigServiceClient();
const stsClient = awsClient.createStsClient();
return {
getDbResourcesMap: profileAsync('Time to download resources from Neptune', getDbResourcesMap(appSync)),
getDbRelationshipsMap: profileAsync('Time to download relationships from Neptune', getDbRelationshipsMap(appSync)),
getAccounts: profileAsync('Time to get accounts', () => {
const accountsP = config.isUsingOrganizations
? getOrgAccounts({ec2Client, organizationsClient, configClient}, appSync, config)
: appSync.getAccounts().then(R.map(R.evolve({regions: R.map(x => x.name)})));
return accountsP
.then(addAccountCredentials({stsClient}, config.rootAccountId))
}),
addCrawledAccounts: addCrawledAccounts(appSync),
deleteAccounts: appSync.deleteAccounts,
storeResources: process(async resources => {
await appSync.indexResources(resources);
await appSync.addResources(resources);
}),
deleteResources: process(async ids => {
await appSync.deleteIndexedResources(ids);
await appSync.deleteResources(ids);
}),
updateResources: process(async resources => {
await appSync.updateIndexedResources(resources);
await appSync.updateResources(resources);
}),
deleteRelationships: process(async ids => {
return appSync.deleteRelationships(ids);
}),
storeRelationships: process(async relationships => {
return appSync.addRelationships(relationships);
}),
updateCrawledAccounts: updateCrawledAccounts(appSync)
};
}
}