-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.ts
More file actions
869 lines (833 loc) · 28.8 KB
/
index.ts
File metadata and controls
869 lines (833 loc) · 28.8 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
import CloudGraph, {
Service,
Opts,
ProviderData,
sortResourcesDependencies,
} from '@cloudgraph/sdk'
import { loadFilesSync } from '@graphql-tools/load-files'
import { mergeTypeDefs } from '@graphql-tools/merge'
import AWS, { Config } from 'aws-sdk'
import chalk from 'chalk'
import { DocumentNode } from 'graphql'
import STS from 'aws-sdk/clients/sts'
import { isEmpty, merge } from 'lodash'
import path from 'path'
import regions from '../enums/regions'
import resources from '../enums/resources'
import services from '../enums/services'
import serviceMap from '../enums/serviceMap'
import schemasMap from '../enums/schemasMap'
import relations from '../enums/relations'
import { Credentials } from '../types'
import { obfuscateSensitiveString } from '../utils/format'
import { checkAndMergeConnections } from '../utils'
import { Account, rawDataInterface } from './base'
import enhancers, { EnhancerConfig } from './base/enhancers'
const DEFAULT_REGION = 'us-east-1'
const DEFAULT_RESOURCES = Object.values(services).join(',')
const ENV_VAR_CREDS_LOG = 'Using ENV variable credentials'
export const enums = {
services,
regions,
resources,
schemasMap,
}
export default class Provider extends CloudGraph.Client {
constructor(config: any) {
super(config)
this.properties = enums
}
private credentials: Credentials | undefined
private profile: string | undefined
private role: string | undefined
private properties: {
services: { [key: string]: string }
regions: string[]
resources: { [key: string]: string }
}
logSelectedAccessRegionsAndResources(
profilesOrRolesToLog: string[],
regionsToLog: string,
resourcesToLog: string
): void {
this.logger.info(
`Profiles and role ARNs configured: ${chalk.green(
profilesOrRolesToLog.join(', ')
)}`
)
this.logger.info(
`Regions configured: ${chalk.green(regionsToLog.replace(/,/g, ', '))}`
)
this.logger.info(
`Resources configured: ${chalk.green(resourcesToLog.replace(/,/g, ', '))}`
)
}
// TODO: update to also support ignorePrompts config
async configure(): Promise<{ [key: string]: any }> {
const { flags = {}, cloudGraphConfig, ...providerSettings } = this.config
const result: { [key: string]: any } = { ...providerSettings }
let profiles
try {
profiles = this.getProfilesFromSharedConfig()
} catch (error: any) {
this.logger.warn('No AWS profiles found')
}
const accounts = []
/**
* Multi account setup flow. We loop through the questions and allow them to answer yes to add another account
* If we find profiles, we show that list of profiles and allow them to select one
* They can then add a role ARN and externalId to that profile for it to assume other roles
* If we find no profiles, they can input just a role ARN and (if needed) an externalId to authenticate that way
* If they want to just use default creds of the system (such as in ec2), they can just answer no to the role ARN ?
*/
while (true) {
if (accounts.length > 0) {
const { addAccount } = await this.interface.prompt([
{
type: 'confirm',
message: 'Configure another AWS account?',
name: 'addAccount',
default: true,
},
])
if (!addAccount) {
break
}
}
let profile = ''
let role = ''
let externalId = ''
if (!flags['use-roles'] && profiles && profiles.length) {
const { profile: profileAnswer } = await this.interface.prompt([
{
type: 'list',
message: 'Please select AWS identity',
name: 'profile',
loop: false,
choices: profiles.map((profile: string) => ({
name: profile,
})),
},
])
profile = profileAnswer
} else {
this.logger.info(
'** NOTE: if you want to use the "built in" (metadata) credentials for ec2/ecs, leave the roleArn blank for that account.'
)
}
const { addRoleArn } = await this.interface.prompt([
{
type: 'confirm',
message:
'Do you want to provide a role ARN for this identity to assume?',
name: 'addRoleArn',
default: false,
},
])
if (addRoleArn) {
const {
role: roleAnswer,
externalId: externalIdAnswer,
}: { role: string; externalId: string } = await this.interface.prompt([
{
type: 'input',
message: 'Enter role ARN for identity to assume',
name: 'role',
},
{
type: 'input',
message: 'Enter ExternalID for role OR press ENTER for none',
name: 'externalId',
},
])
role = roleAnswer
externalId = externalIdAnswer
}
accounts.push({ profile, roleArn: role, externalId })
}
if (!accounts.length) {
accounts.push({ profile: '', roleArn: '', externalId: '' })
}
result.accounts = accounts
const { regions: regionsAnswer } = await this.interface.prompt([
{
type: 'checkbox',
message: 'Select regions to scan',
loop: false,
name: 'regions',
choices: regions.map((region: string) => ({
name: region,
})),
},
])
this.logger.debug(`Regions selected: ${regionsAnswer}`)
if (!regionsAnswer.length) {
this.logger.info(
`No Regions selected, using default region: ${chalk.green(
DEFAULT_REGION
)}`
)
result.regions = DEFAULT_REGION
} else {
result.regions = regionsAnswer.join(',')
}
// Prompt for resources if flag set
if (flags.resources) {
const { resources: resourcesAnswer } = await this.interface.prompt([
{
type: 'checkbox',
message: 'Select services to scan',
loop: false,
name: 'resources',
choices: Object.values(services as { [key: string]: string }).map(
(service: string) => ({
name: service,
})
),
},
])
this.logger.debug(resourcesAnswer)
if (resourcesAnswer.length > 0) {
result.resources = resourcesAnswer.join(',')
} else {
result.resources = DEFAULT_RESOURCES
}
} else {
result.resources = DEFAULT_RESOURCES
}
const confettiBall = String.fromCodePoint(0x1f38a) // confetti ball emoji
this.logger.success(
`${confettiBall} ${chalk.green(
'AWS'
)} configuration successfully completed ${confettiBall}`
)
this.logSelectedAccessRegionsAndResources(
result.accounts.map(acct => acct.roleArn ?? acct.profile),
result.regions,
result.resources
)
return result
}
async getIdentity(account: Account): Promise<{ accountId: string }> {
try {
const config = await this.getAwsConfig(account)
return new Promise((resolve, reject) =>
new STS(config).getCallerIdentity((err, data) => {
if (err) {
return reject(err)
}
return resolve({ accountId: data.Account })
})
)
} catch (e) {
this.logger.error('There was an error in function getIdentity')
this.logger.debug(e)
return { accountId: '' }
}
}
private unsetAwsCredentials(): void {
this.credentials = undefined
}
private getAwsConfig({
profile,
roleArn: role,
externalId,
accessKeyId: configuredAccessKey,
secretAccessKey: configuredSecretKey,
}: Account): Promise<Config> {
const {
cloudGraphConfig: { ignorePrompts, ignoreEnvVariables } = {
ignorePrompts: false,
ignoreEnvVariables: false,
},
} = this.config
let configCopy
return new Promise(async (resolveConfig, rejectConfig) => {
// If we have keys set in the config file, just use them
if (configuredAccessKey && configuredSecretKey) {
const creds = {
accessKeyId: configuredAccessKey,
secretAccessKey: configuredSecretKey,
}
if (!this.credentials) {
this.logger.warn(
'Using hard coded accessKeyId and secretAccessKey, it is not advised to save these in config'
)
this.logger.success(
`accessKeyId: ${chalk.underline.green(
obfuscateSensitiveString(configuredAccessKey)
)}`
)
this.logger.success(
`secretAccessKey: ${chalk.underline.green(
obfuscateSensitiveString(configuredSecretKey)
)}`
)
}
this.credentials = creds
configCopy = { ...AWS.config, credentials: this.credentials }
return resolveConfig(configCopy)
}
// If the client instance has creds set, weve gone through this function before.. just reuse them
if (
this.credentials &&
(this.profile === profile || this.role === role)
) {
configCopy = { ...AWS.config, credentials: this.credentials }
return resolveConfig(configCopy)
}
/**
* Tries to find creds in priority order
* 1. if they have configured a roleArn and profile assume that role using STS
* 2. if they have configured a profile, assume that profile from ~/.aws/credentials
* 3. if they have not configured either of the above, assume profile = default from ~/.aws/credentials
*/
this.logger.info('Searching for AWS credentials...')
switch (true) {
case role && role !== '': {
let sts = new AWS.STS()
await new Promise<void>(resolve => {
if (profile && profile !== 'default') {
const creds = this.getSharedIniFileCredentials(profile)
if (creds) {
sts = new AWS.STS({ credentials: creds })
}
}
const options = {
RoleSessionName: 'CloudGraph',
RoleArn: role,
...(externalId && { ExternalId: externalId }),
}
sts.assumeRole(options, (err, data) => {
if (err) {
this.logger.error(
`No valid credentials found for roleARN: ${role}`
)
this.logger.debug(err)
resolve()
} else {
// successful response
const {
AccessKeyId: accessKeyId,
SecretAccessKey: secretAccessKey,
SessionToken: sessionToken,
} = data.Credentials
const creds = {
accessKeyId,
secretAccessKey,
sessionToken,
}
this.credentials = creds
configCopy = { ...AWS.config, credentials: creds }
this.profile = profile
resolve()
}
})
})
break
}
case profile && profile !== 'default': {
try {
const credentials = this.getSharedIniFileCredentials(profile)
if (credentials) {
this.credentials = credentials
configCopy = { ...AWS.config, credentials }
this.profile = profile
}
break
} catch (error: any) {
break
}
}
default: {
// unset credentials before getting them for multi account scenarios
this.unsetAwsCredentials()
await new Promise<void>(resolve =>
AWS.config.getCredentials((err: any) => {
if (err) {
resolve()
} else {
this.credentials = AWS.config.credentials
configCopy = { ...AWS.config, credentials: this.credentials }
this.profile = profile
resolve()
}
})
)
}
}
if (!this.credentials && !ignorePrompts) {
this.logger.info(
'No AWS Credentials found for scan, please enter them manually'
)
// when pausing the ora spinner the position of this call must come after any logger output
const msg = this.logger.stopSpinner()
const answers = await this.interface.prompt([
{
type: 'input',
message: 'Please input a valid accessKeyId',
name: 'accessKeyId',
},
{
type: 'input',
message: 'Please input a valid secretAccessKey',
name: 'secretAccessKey',
},
])
if (answers?.accessKeyId && answers?.secretAccessKey) {
this.credentials = answers
this.profile = profile
configCopy = { ...AWS.config, credentials: this.credentials }
} else {
const errText = 'Cannot scan AWS without credentials'
this.logger.error(errText)
return rejectConfig(new Error(errText))
}
this.logger.startSpinner(msg)
}
const profileName = profile || 'default'
const usingEnvCreds =
!!process.env.AWS_ACCESS_KEY_ID && !ignoreEnvVariables
if (!this.credentials) {
return rejectConfig(new Error('No Credentials found for AWS'))
}
if (usingEnvCreds) {
this.logger.success('Using credentials set by ENV variables')
} else {
this.logger.success('Found and using the following AWS credentials')
this.logger.success(
`${role ? 'roleARN' : 'profile'}: ${chalk.underline.green(
role || profileName
)}`
)
}
this.logger.success(
`accessKeyId: ${chalk.underline.green(
obfuscateSensitiveString(this.credentials.accessKeyId)
)}`
)
this.logger.success(
`secretAccessKey: ${chalk.underline.green(
obfuscateSensitiveString(this.credentials.secretAccessKey)
)}`
)
resolveConfig(configCopy)
})
}
/**
* getSchema is used to get the schema for provider
* @returns A string of graphql sub schemas
*/
getSchema(): DocumentNode {
const typesArray = loadFilesSync(path.join(__dirname), {
recursive: true,
extensions: ['graphql'],
})
return mergeTypeDefs(typesArray)
}
/**
* Factory function to return AWS service classes based on input service
* @param service an AWS service that is listed within the service map (current supported services)
* @returns Instance of an AWS service class to interact with that AWS service
*/
private getService(service: string): Service {
if (serviceMap[service]) {
return new serviceMap[service](this)
}
}
private getSharedIniFileCredentials(
profile: string
): AWS.SharedIniFileCredentials {
let credentials
try {
// TODO: how to catch the error from SharedIniFileCredentials when profile doent exist
credentials = new AWS.SharedIniFileCredentials({
profile,
callback: (err: any) => {
if (err) {
this.logger.error(`No credentials found for profile ${profile}`)
}
},
})
} catch (error: any) {
this.logger.debug(error)
}
return credentials
}
private getProfilesFromSharedConfig(): string[] {
let profiles
try {
profiles = Object.keys(
AWS['util'].getProfilesFromSharedConfig(AWS['util'].iniLoader)
)
} catch (error: any) {
this.logger.warn('Unable to read AWS shared credential file')
this.logger.debug(error)
}
return profiles || []
}
private mergeRawData(
oldData: rawDataInterface[],
newData: rawDataInterface[]
): rawDataInterface[] {
if (isEmpty(oldData)) {
return newData
}
const result: rawDataInterface[] = []
for (const entity of oldData) {
try {
const { className, name, data } = entity
const newDataForEntity =
newData.find(({ name: serviceName }) => name === serviceName).data ||
{}
if (newDataForEntity) {
let mergedData = {}
// if there is no old data for this service but there is new data, use the new data
if (isEmpty(data)) {
mergedData = newDataForEntity
} else {
// if we have data for an entity (like vpc) in both data sets, merge their data
for (const region in data) {
if (newDataForEntity[region]) {
this.logger.debug(
`Found additional data for ${name} in ${region}, merging`
)
mergedData[region] = [
...(data[region] ?? []),
...newDataForEntity[region],
]
} else {
mergedData[region] = data[region]
}
}
}
result.push({
className,
name,
data: mergedData,
})
// if not, just use the old data
} else {
result.push({
className,
name,
data,
})
}
} catch (error: any) {
this.logger.debug(error)
this.logger.error('There was an error merging raw data for AWS')
}
}
return result
}
private async getRawData(
account: Account,
opts?: Opts
): Promise<rawDataInterface[]> {
let { regions: configuredRegions, resources: configuredResources } =
this.config
const result: rawDataInterface[] = []
if (!configuredRegions) {
configuredRegions = this.properties.regions.join(',')
} else {
configuredRegions = [...new Set(configuredRegions.split(','))].join(',')
}
if (!configuredResources) {
configuredResources = Object.values(this.properties.services).join(',')
}
const resourceNames: string[] = sortResourcesDependencies(relations, [
...new Set<string>(configuredResources.split(',')),
])
const config = await this.getAwsConfig(account)
const { accountId } = await this.getIdentity(account)
for (const resource of resourceNames) {
const serviceClass = this.getService(resource)
if (serviceClass && serviceClass.getData) {
try {
const data = await serviceClass.getData({
regions: configuredRegions,
config,
opts,
account: accountId,
rawData: result,
})
result.push({
className: serviceClass.constructor.name,
name: resource,
accountId,
data,
})
this.logger.success(`${resource} scan completed`)
} catch (error: any) {
this.logger.error(
`There was an error scanning AWS sdk data for ${resource} resource`
)
this.logger.debug(error)
}
} else {
this.logger.warn(
`Skipping service ${resource} as there was an issue getting data for it. Is it currently supported?`
)
}
}
this.logger.success(`Account: ${accountId} scan completed`)
return result
}
private enhanceData({ data, ...config }: EnhancerConfig): ProviderData {
let enhanceData = {
entities: data.entities,
connections: data.connections,
}
for (const { name, enhancer } of enhancers) {
try {
enhanceData = enhancer({ ...config, data: enhanceData })
} catch (error: any) {
this.logger.error(
`There was an error enriching AWS data with ${name} data`
)
this.logger.debug(error)
return enhanceData
}
}
return enhanceData
}
/**
* getData is used to fetch all provider data specified in the config for the provider
* @param opts: A set of optional values to configure how getData works
* @returns Promise<any> All provider data
*/
async getData({ opts }: { opts: Opts }): Promise<ProviderData> {
const result: ProviderData = {
entities: [],
connections: {},
}
let { regions: configuredRegions, resources: configuredResources } =
this.config
const {
accounts: configuredAccounts,
cloudGraphConfig: { ignoreEnvVariables } = { ignoreEnvVariables: false },
}: {
accounts: Account[]
cloudGraphConfig: { ignoreEnvVariables: boolean }
} = this.config
if (!configuredRegions) {
configuredRegions = this.properties.regions.join(',')
} else {
configuredRegions = [...new Set(configuredRegions.split(','))].join(',')
}
if (!configuredResources) {
configuredResources = Object.values(this.properties.services).join(',')
}
const usingEnvCreds = !!process.env.AWS_ACCESS_KEY_ID && !ignoreEnvVariables
this.logSelectedAccessRegionsAndResources(
usingEnvCreds
? [ENV_VAR_CREDS_LOG]
: configuredAccounts.map(acct => {
return acct.roleArn || acct.profile
}),
configuredRegions,
configuredResources
)
// Leaving this here in case we need to test another service or to inject a logging function
// setAwsRetryOptions({ global: true, configObj: this.config })
let rawData: rawDataInterface[] = []
// We need to keep a merged copy of raw data so we can handle connections but keep a separate raw
// data so we can pass along accountId
// TODO: find a better way to handle this
let mergedRawData: rawDataInterface[] = []
const globalRegion = 'aws-global'
const tags = { className: 'Tag', name: 'tag', data: { [globalRegion]: [] } }
const accounts = {
className: 'AwsAccount',
name: 'account',
data: { [globalRegion]: [] },
}
// If the user has passed aws creds as env variables, dont use profile list
if (usingEnvCreds) {
rawData = await this.getRawData(
{ profile: 'default', roleArn: undefined, externalId: undefined },
opts
)
} else {
const crawledAccounts = []
for (const account of configuredAccounts) {
const { profile, roleArn: role } = account
// verify that profile exists in the shared credential file
if (profile) {
const profiles = this.getProfilesFromSharedConfig()
if (!profiles.includes(profile)) {
this.logger.warn(
`Profile: ${profile} not found in shared credentials file. Skipping...`
)
// eslint-disable-next-line no-continue
continue
}
}
const { accountId } = await this.getIdentity(account)
accounts.data[globalRegion].push({
id: accountId,
regions: configuredRegions.split(','),
})
if (!crawledAccounts.find(val => val === accountId)) {
crawledAccounts.push(accountId)
const newRawData = await this.getRawData(account, opts)
mergedRawData = this.mergeRawData(mergedRawData, newRawData)
rawData = [...rawData, ...newRawData]
} else {
this.logger.warn(
// eslint-disable-next-line max-len
`${profile ? 'profile' : 'roleARN'}: ${
profile ?? role
} returned accountId ${accountId} which has already been crawled, skipping...`
)
}
this.unsetAwsCredentials()
}
}
// Handle global tag entities
try {
for (const { data: entityData } of rawData) {
for (const region of Object.keys(entityData)) {
const dataAtRegion = entityData[region] ?? []
dataAtRegion.forEach(singleEntity => {
if (!isEmpty(singleEntity.Tags)) {
for (const [key, value] of Object.entries(singleEntity.Tags)) {
if (
!tags.data[globalRegion].find(
({ id }) => id === `${key}:${value}`
)
) {
tags.data[globalRegion].push({
id: `${key}:${value}`,
key,
value,
})
}
}
}
})
}
}
rawData.push(accounts)
const existingTagsIdx = rawData.findIndex(({ name }) => {
return name === 'tag'
})
if (existingTagsIdx > -1) {
rawData[existingTagsIdx] = tags
} else {
rawData.push(tags)
}
} catch (error: any) {
this.logger.error('There was an error aggregating AWS tags')
this.logger.debug(error)
}
for (const serviceData of rawData) {
try {
const serviceClass = this.getService(serviceData.name)
const entities: any[] = []
const rawEntities: any[] = []
for (const region of Object.keys(serviceData.data)) {
await new Promise(resolve => setTimeout(resolve, 10)) // free the main nodejs thread to process other requests
const data = serviceData.data[region]
if (!isEmpty(data)) {
data.forEach((service: any) => {
const formattedData = serviceClass.format({
service,
region,
account: serviceData.accountId,
})
entities.push(formattedData)
rawEntities.push(service)
if (typeof serviceClass.getConnections === 'function') {
// We need to loop through all configured regions here because services can be connected to things in another region
let serviceConnections = {}
for (const connectionRegion of configuredRegions.split(',')) {
const connections = serviceClass.getConnections({
service,
region: connectionRegion,
account: serviceData.accountId,
data: mergedRawData,
})
serviceConnections = checkAndMergeConnections(
serviceConnections,
connections
)
}
Object.assign(result.connections, serviceConnections)
}
})
}
}
/**
* we have 2 things to check here, both dealing with multi-account senarios
* 1. Do we already have an entity by this name in the result (i.e. both accounts have vpcs)
* 2. Do we already have the data for an entity that lives in multiple accounts
* (i.e. a cloudtrail that appears in a master and sandbox account).
* If so, we need to merge the data. We use lodash merge to recursively merge arrays as there are
* cases where acct A gets more data for service obj X than acct B does.
* (i.e. Acct A cannot access the cloudtrail's tags but acct B can because the cloudtrail's arn points to acct B)
*/
const existingServiceIdx = result.entities.findIndex(({ name }) => {
return name === serviceData.name
})
if (existingServiceIdx > -1) {
const existingData = result.entities[existingServiceIdx].data
for (const currentEntity of entities) {
const existingEntityIdx = existingData.findIndex(
({ id }) => id === currentEntity.id
)
if (existingEntityIdx > -1) {
const entityToDelete = existingData[existingEntityIdx]
existingData.splice(existingEntityIdx, 1)
const entityToMergeIdx = entities.findIndex(
({ id }) => id === currentEntity.id
)
entities[entityToMergeIdx] = merge(entityToDelete, currentEntity)
}
}
const existingRawData = result.entities[existingServiceIdx].rawData
for (const currentRawEntity of rawEntities) {
const existingEntityIdx = existingData.findIndex(
({ id }) => id === currentRawEntity.id
)
if (existingEntityIdx > -1) {
const rawEntityToDelete = existingRawData[existingEntityIdx]
existingRawData.splice(existingEntityIdx, 1)
const entityToMergeIdx = rawEntities.findIndex(
({ id }) => id === currentRawEntity.id
)
rawEntities[entityToMergeIdx] = merge(rawEntityToDelete, currentRawEntity)
}
}
result.entities[existingServiceIdx] = {
className: serviceClass.constructor.name,
name: serviceData.name,
mutation: serviceClass.mutation,
data: [...existingData, ...entities],
rawData: [...existingRawData, ...rawEntities],
}
} else {
result.entities.push({
className: serviceClass.constructor.name,
name: serviceData.name,
mutation: serviceClass.mutation,
data: entities,
rawData: rawEntities,
})
}
} catch (error: any) {
this.logger.error(
`There was an error formatting/connecting service ${serviceData.name} `
)
this.logger.debug(error)
}
}
return this.enhanceData({
accounts: accounts.data[globalRegion],
configuredRegions,
rawData: mergedRawData,
data: result,
})
}
}