-
Notifications
You must be signed in to change notification settings - Fork 183
/
inventory.go
1754 lines (1556 loc) · 51.2 KB
/
inventory.go
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package aws
import (
"context"
"fmt"
"path/filepath"
"sort"
"strconv"
"sync"
"github.com/BishopFox/cloudfox/console"
"github.com/BishopFox/cloudfox/utils"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/apigateway"
"github.com/aws/aws-sdk-go-v2/service/apigatewayv2"
"github.com/aws/aws-sdk-go-v2/service/apprunner"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
"github.com/aws/aws-sdk-go-v2/service/cloudfront"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/eks"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
"github.com/aws/aws-sdk-go-v2/service/glue"
"github.com/aws/aws-sdk-go-v2/service/grafana"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/aws/aws-sdk-go-v2/service/lightsail"
"github.com/aws/aws-sdk-go-v2/service/mq"
"github.com/aws/aws-sdk-go-v2/service/opensearch"
"github.com/aws/aws-sdk-go-v2/service/rds"
"github.com/aws/aws-sdk-go-v2/service/redshift"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/secretsmanager"
"github.com/aws/aws-sdk-go-v2/service/sns"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/sirupsen/logrus"
)
type Inventory2Module struct {
// General configuration data
LambdaClient *lambda.Client
EC2Client *ec2.Client
ECSClient *ecs.Client
EKSClient *eks.Client
S3Client *s3.Client
CloudFormationClient *cloudformation.Client
SecretsManagerClient *secretsmanager.Client
SSMClient *ssm.Client
RDSClient *rds.Client
APIGatewayv2Client *apigatewayv2.Client
ELBv2Client *elasticloadbalancingv2.Client
ELBClient *elasticloadbalancing.Client
IAMClient *iam.Client
MQClient *mq.Client
OpenSearchClient *opensearch.Client
GrafanaClient *grafana.Client
APIGatewayClient *apigateway.Client
RedshiftClient *redshift.Client
CloudfrontClient *cloudfront.Client
AppRunnerClient *apprunner.Client
LightsailClient *lightsail.Client
GlueClient *glue.Client
SNSClient *sns.Client
SQSClient *sqs.Client
DynamoDBClient *dynamodb.Client
Caller sts.GetCallerIdentityOutput
AWSRegions []string
OutputFormat string
Goroutines int
AWSProfile string
// Main module data
RegionResourceCount int
CommandCounter console.CommandCounter
GlobalResourceCounts []GlobalResourceCount2
serviceMap map[string]map[string]int
services []string
totalRegionCounts map[string]int
mu sync.Mutex
// Used to store output data for pretty printing
output utils.OutputData2
globalOutput utils.OutputData2
modLog *logrus.Entry
}
type GlobalResourceCount2 struct {
resourceType string
count int
}
func (m *Inventory2Module) PrintInventoryPerRegion(outputFormat string, outputDirectory string, verbosity int) {
// These stuct values are used by the output module
m.output.Verbosity = verbosity
m.output.Directory = outputDirectory
m.output.CallingModule = "inventory"
m.modLog = utils.TxtLogger.WithFields(logrus.Fields{
"module": "inventory",
},
)
// def change this to build dynamically in the future.
m.services = []string{"total", "APIGateway RestAPIs", "APIGatewayv2 APIs", "AppRunner Services", "CloudFormation Stacks", "Cloudfront Distributions", "DynamoDB Tables", "EC2 Instances", "ECS Tasks", "EKS Clusters", "ELB Load Balancers", "ELBv2 Load Balancers", "Glue Dev Endpoints", "Glue Jobs", "Grafana Workspaces", "Lambda Functions", "Lightsail Instances/Containers", "MQ Brokers", "OpenSearch DomainNames", "RDS DB Instances", "SecretsManager Secrets", "SNS Topics", "SQS Queues", "SSM Parameters"}
m.serviceMap = map[string]map[string]int{}
m.totalRegionCounts = map[string]int{}
if m.AWSProfile == "" {
m.AWSProfile = utils.BuildAWSPath(m.Caller)
}
//initialize servicemap and total
for _, service := range m.services {
m.serviceMap[service] = map[string]int{}
for _, region := range m.AWSRegions {
m.serviceMap[service][region] = 0
m.totalRegionCounts[region] = 0
}
}
fmt.Printf("[%s] Enumerating selected services in all regions for account %s.\n", cyan(m.output.CallingModule), aws.ToString(m.Caller.Account))
fmt.Printf("[%s] Supported Services: ApiGateway, ApiGatewayv2, AppRunner, CloudFormation, Cloudfront, DynamoDB, EC2, ECS, EKS, \n", cyan(m.output.CallingModule))
fmt.Printf("[%s] \t\t\tELB, ELBv2, Glue, Grafana, IAM, Lambda, Lightsail, MQ, OpenSearch, RDS, S3, SecretsManager, SNS, SQS, SSM\n", cyan(m.output.CallingModule))
wg := new(sync.WaitGroup)
semaphore := make(chan struct{}, m.Goroutines)
// Create a channel to signal the spinner aka task status goroutine to finish
spinnerDone := make(chan bool)
//fire up the the task status spinner/updated
go console.SpinUntil(m.output.CallingModule, &m.CommandCounter, spinnerDone, "tasks")
//create a channel to receive the objects
dataReceiver := make(chan GlobalResourceCount2)
// Create a channel to signal to stop
receiverDone := make(chan bool)
go m.Receiver(dataReceiver, receiverDone)
for _, region := range m.AWSRegions {
m.CommandCounter.Total++
wg.Add(1)
m.CommandCounter.Pending++
go m.executeChecks(region, wg, semaphore, dataReceiver)
}
// for _, r := range []string{"us-east-1", "us-east-2", "ap-northeast-1", "eu-west-1", "us-west-2"} {
// m.CommandCounter.Total++
// wg.Add(1)
// go m.getAppRunnerServicesPerRegion(r, wg, semaphore)
// }
wg.Wait()
// Send a message to the spinner goroutine to close the channel and stop
spinnerDone <- true
<-spinnerDone
//duration := time.Since(start)
//fmt.Printf("\n\n[*] Total execution time %s\n", duration)
// This creates the header row (columns) dynamically - a region oly gets printed if it has at least one resource.
m.output.Headers = append(m.output.Headers, "Resource Type")
type kv struct {
Key string
Value int
}
var ss []kv
for k, v := range m.totalRegionCounts {
ss = append(ss, kv{k, v})
}
sort.Slice(ss, func(i, j int) bool {
return ss[i].Value > ss[j].Value
})
for _, region := range ss {
if region.Value != 0 {
m.output.Headers = append(m.output.Headers, region.Key)
}
}
//move total up here.
var totalRow []string
var temprow []string
temprow = append(temprow, "Total")
for _, region := range ss {
if region.Value != 0 {
if m.serviceMap["total"][region.Key] > 0 {
temprow = append(temprow, strconv.Itoa(m.serviceMap["total"][region.Key]))
} else {
temprow = append(temprow, "-")
}
}
}
for _, val := range temprow {
totalRow = append(totalRow, val)
}
m.output.Body = append(m.output.Body, totalRow)
// var sortedBody []kv
// for k, v := range m.serviceMap {
// sortedBody = append(sortedBody, kv{k, v})
// }
// sort.Slice(sortedBody, func(i, j int) bool {
// return sortedBody[i].Key > ss[j].Key
// })
// This is where we create the per service row with variable number of columns as well, using the same logic we used for the header
for _, service := range m.services {
if service != "total" {
var outputRow []string
var temprow []string
temprow = append(temprow, service)
for _, region := range ss {
if region.Value != 0 {
if m.serviceMap[service][region.Key] > 0 {
temprow = append(temprow, strconv.Itoa(m.serviceMap[service][region.Key]))
} else {
temprow = append(temprow, "-")
}
}
}
// Convert the slice of strings to a slice of interfaces??? not sure, but this was needed. I couldnt just pass temp row to the output.Body
for _, val := range temprow {
outputRow = append(outputRow, val)
}
// Finally write the row to the table
m.output.Body = append(m.output.Body, outputRow)
}
}
m.output.FilePath = filepath.Join(outputDirectory, "cloudfox-output", "aws", m.AWSProfile)
// if verbosity > 1 {
// fmt.Printf("\nAnalyzed Global Resources\n\n")
// }
if len(m.output.Body) > 0 {
//m.output.OutputSelector(outputFormat)
utils.OutputSelector(verbosity, outputFormat, m.output.Headers, m.output.Body, m.output.FilePath, m.output.CallingModule, m.output.CallingModule)
m.PrintGlobalResources(outputFormat, outputDirectory, verbosity, dataReceiver)
m.PrintTotalResources(outputFormat)
} else {
fmt.Printf("[%s] No resources identified, skipping the creation of an output file.\n", cyan(m.output.CallingModule))
}
// Send a message to the data receiver goroutine to close the channel and stop
receiverDone <- true
<-receiverDone
}
func (m *Inventory2Module) PrintGlobalResources(outputFormat string, outputDirectory string, verbosity int, dataReceiver chan GlobalResourceCount2) {
m.globalOutput.Verbosity = verbosity
m.globalOutput.CallingModule = "inventory"
m.globalOutput.FullFilename = "inventory-global"
m.getBuckets(verbosity, dataReceiver)
m.getIAMUsers(verbosity, dataReceiver)
m.getIAMRoles(verbosity, dataReceiver)
m.getCloudfrontDistros(verbosity, dataReceiver)
//m.globalOutput.CallingModule = fmt.Sprintf("%s-global", m.globalOutput.CallingModule)
m.globalOutput.FilePath = filepath.Join(outputDirectory, "cloudfox-output", "aws", m.AWSProfile)
m.globalOutput.Headers = []string{
"Resource Type",
"Total",
}
for i, GlobalResourceCount := range m.GlobalResourceCounts {
if m.GlobalResourceCounts[i].count != 0 {
m.globalOutput.Body = append(
m.globalOutput.Body,
[]string{
GlobalResourceCount.resourceType,
strconv.Itoa(GlobalResourceCount.count),
},
)
}
}
//m.globalOutput.FilePath = filepath.Join(path, m.globalOutput.CallingModule)
//m.globalOutput.OutputSelector(outputFormat)
utils.OutputSelector(verbosity, outputFormat, m.globalOutput.Headers, m.globalOutput.Body, m.globalOutput.FilePath, m.globalOutput.FullFilename, m.globalOutput.CallingModule)
}
func (m *Inventory2Module) Receiver(receiver chan GlobalResourceCount2, receiverDone chan bool) {
defer close(receiverDone)
for {
select {
case data := <-receiver:
m.GlobalResourceCounts = append(m.GlobalResourceCounts, data)
case <-receiverDone:
receiverDone <- true
return
}
}
}
func (m *Inventory2Module) executeChecks(r string, wg *sync.WaitGroup, semaphore chan struct{}, dataReceiver chan GlobalResourceCount2) {
defer wg.Done()
m.CommandCounter.Total++
wg.Add(1)
go m.getLambdaFunctionsPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getEc2InstancesPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getCloudFormationStacksPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getSecretsManagerSecretsPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getEksClustersPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getEcsTasksPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getRdsClustersPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getAPIGatewayvAPIsPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getAPIGatewayv2APIsPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getELBv2ListenersPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getELBListenersPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
m.getMqBrokersPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
m.getOpenSearchPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getGrafanaWorkspacesPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getAppRunnerServicesPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getLightsailInstancesAndContainersPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getSSMParametersPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getGlueDevEndpointsPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getGlueJobsPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getSNSTopicsPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getSQSQueuesPerRegion(r, wg, semaphore)
m.CommandCounter.Total++
wg.Add(1)
go m.getDynamoDBTablesPerRegion(r, wg, semaphore)
}
func (m *Inventory2Module) getLambdaFunctionsPerRegion(r string, wg *sync.WaitGroup, semaphore chan struct{}) {
defer func() {
wg.Done()
m.CommandCounter.Executing--
m.CommandCounter.Complete++
}()
semaphore <- struct{}{}
defer func() {
<-semaphore
}()
// m.CommandCounter.Total++
m.CommandCounter.Pending--
m.CommandCounter.Executing++
// "PaginationMarker" is a control variable used for output continuity, as AWS return the output in pages.
var PaginationMarker *string
var totalCountThisServiceThisRegion = 0
var service = "Lambda Functions"
// This for loop exits at the end depending on whether the output hits its last page (see pagination control block at the end of the loop).
for {
functions, err := m.LambdaClient.ListFunctions(
context.TODO(),
&lambda.ListFunctionsInput{
Marker: PaginationMarker,
},
func(o *lambda.Options) {
o.Region = r
},
)
if err != nil {
//modLog.Error(err.Error())
m.modLog.Error(err.Error())
m.CommandCounter.Error++
break
}
// Add this page of resources to the total count
totalCountThisServiceThisRegion = totalCountThisServiceThisRegion + len(functions.Functions)
// Pagination control. After the last page of output, the for loop exits.
if functions.NextMarker != nil {
PaginationMarker = functions.NextMarker
} else {
PaginationMarker = nil
// No more pages, update the module's service map
m.mu.Lock()
m.serviceMap[service][r] = totalCountThisServiceThisRegion
m.totalRegionCounts[r] = m.totalRegionCounts[r] + totalCountThisServiceThisRegion
m.serviceMap["total"][r] = m.serviceMap["total"][r] + totalCountThisServiceThisRegion
m.mu.Unlock()
break
}
}
}
func (m *Inventory2Module) getEc2InstancesPerRegion(r string, wg *sync.WaitGroup, semaphore chan struct{}) {
defer func() {
wg.Done()
m.CommandCounter.Executing--
m.CommandCounter.Complete++
}()
semaphore <- struct{}{}
defer func() {
<-semaphore
}()
// m.CommandCounter.Total++
m.CommandCounter.Pending--
m.CommandCounter.Executing++
// "PaginationMarker" is a control variable used for output continuity, as AWS return the output in pages.
var PaginationControl *string
var totalCountThisServiceThisRegion = 0
var service = "EC2 Instances"
for {
DescribeInstances, err := m.EC2Client.DescribeInstances(
context.TODO(),
&(ec2.DescribeInstancesInput{
NextToken: PaginationControl,
}),
func(o *ec2.Options) {
o.Region = r
},
)
if err != nil {
m.modLog.Error(err.Error())
m.CommandCounter.Error++
break
}
// Add this page of resources to the total count
totalCountThisServiceThisRegion = totalCountThisServiceThisRegion + len(DescribeInstances.Reservations)
// The "NextToken" value is nil when there's no more data to return.
if DescribeInstances.NextToken != nil {
PaginationControl = DescribeInstances.NextToken
} else {
PaginationControl = nil
// No more pages, update the module's service map
m.mu.Lock()
m.serviceMap[service][r] = totalCountThisServiceThisRegion
m.totalRegionCounts[r] = m.totalRegionCounts[r] + totalCountThisServiceThisRegion
m.serviceMap["total"][r] = m.serviceMap["total"][r] + totalCountThisServiceThisRegion
m.mu.Unlock()
break
}
}
}
func (m *Inventory2Module) getEksClustersPerRegion(r string, wg *sync.WaitGroup, semaphore chan struct{}) {
defer func() {
wg.Done()
m.CommandCounter.Executing--
m.CommandCounter.Complete++
}()
semaphore <- struct{}{}
defer func() {
<-semaphore
}()
// m.CommandCounter.Total++
m.CommandCounter.Pending--
m.CommandCounter.Executing++
// "PaginationMarker" is a control variable used for output continuity, as AWS return the output in pages.
var PaginationControl *string
var totalCountThisServiceThisRegion = 0
var service = "EKS Clusters"
for {
ListClusters, err := m.EKSClient.ListClusters(
context.TODO(),
&(eks.ListClustersInput{
NextToken: PaginationControl,
}),
func(o *eks.Options) {
o.Region = r
},
)
if err != nil {
m.modLog.Error(err.Error())
m.CommandCounter.Error++
break
}
// Add this page of resources to the total count
totalCountThisServiceThisRegion = totalCountThisServiceThisRegion + len(ListClusters.Clusters)
// The "NextToken" value is nil when there's no more data to return.
if ListClusters.NextToken != nil {
PaginationControl = ListClusters.NextToken
} else {
PaginationControl = nil
// No more pages, update the module's service map
m.mu.Lock()
m.serviceMap[service][r] = totalCountThisServiceThisRegion
m.totalRegionCounts[r] = m.totalRegionCounts[r] + totalCountThisServiceThisRegion
m.serviceMap["total"][r] = m.serviceMap["total"][r] + totalCountThisServiceThisRegion
m.mu.Unlock()
break
}
}
}
func (m *Inventory2Module) getCloudFormationStacksPerRegion(r string, wg *sync.WaitGroup, semaphore chan struct{}) {
defer func() {
wg.Done()
m.CommandCounter.Executing--
m.CommandCounter.Complete++
}()
semaphore <- struct{}{}
defer func() {
<-semaphore
}()
// m.CommandCounter.Total++
m.CommandCounter.Pending--
m.CommandCounter.Executing++
// "PaginationMarker" is a control variable used for output continuity, as AWS return the output in pages.
var PaginationControl *string
var totalCountThisServiceThisRegion = 0
var service = "CloudFormation Stacks"
for {
ListStacks, err := m.CloudFormationClient.ListStacks(
context.TODO(),
&(cloudformation.ListStacksInput{
NextToken: PaginationControl,
}),
func(o *cloudformation.Options) {
o.Region = r
},
)
if err != nil {
m.modLog.Error(err.Error())
m.CommandCounter.Error++
break
}
// Add this page of resources to the total count
// Currently this counts both active and deleted stacks as they technically still exist. Might
// change this to only count active ones in the future.
totalCountThisServiceThisRegion = totalCountThisServiceThisRegion + len(ListStacks.StackSummaries)
// The "NextToken" value is nil when there's no more data to return.
if ListStacks.NextToken != nil {
PaginationControl = ListStacks.NextToken
} else {
PaginationControl = nil
// No more pages, update the module's service map
m.mu.Lock()
m.serviceMap[service][r] = totalCountThisServiceThisRegion
m.totalRegionCounts[r] = m.totalRegionCounts[r] + totalCountThisServiceThisRegion
m.serviceMap["total"][r] = m.serviceMap["total"][r] + totalCountThisServiceThisRegion
m.mu.Unlock()
break
}
}
}
func (m *Inventory2Module) getSecretsManagerSecretsPerRegion(r string, wg *sync.WaitGroup, semaphore chan struct{}) {
defer func() {
wg.Done()
m.CommandCounter.Executing--
m.CommandCounter.Complete++
}()
semaphore <- struct{}{}
defer func() {
<-semaphore
}()
// m.CommandCounter.Total++
m.CommandCounter.Pending--
m.CommandCounter.Executing++
// "PaginationMarker" is a control variable used for output continuity, as AWS return the output in pages.
var PaginationControl *string
var totalCountThisServiceThisRegion = 0
var service = "SecretsManager Secrets"
for {
ListSecrets, err := m.SecretsManagerClient.ListSecrets(
context.TODO(),
&(secretsmanager.ListSecretsInput{
NextToken: PaginationControl,
}),
func(o *secretsmanager.Options) {
o.Region = r
},
)
if err != nil {
m.modLog.Error(err.Error())
m.CommandCounter.Error++
break
}
// Add this page of results to the total count
totalCountThisServiceThisRegion = totalCountThisServiceThisRegion + len(ListSecrets.SecretList)
// The "NextToken" value is nil when there's no more data to return.
if ListSecrets.NextToken != nil {
PaginationControl = ListSecrets.NextToken
} else {
PaginationControl = nil
// No more pages, update the module's service map
m.mu.Lock()
m.serviceMap[service][r] = totalCountThisServiceThisRegion
m.totalRegionCounts[r] = m.totalRegionCounts[r] + totalCountThisServiceThisRegion
m.serviceMap["total"][r] = m.serviceMap["total"][r] + totalCountThisServiceThisRegion
m.mu.Unlock()
break
}
}
}
func (m *Inventory2Module) getRdsClustersPerRegion(r string, wg *sync.WaitGroup, semaphore chan struct{}) {
defer func() {
wg.Done()
m.CommandCounter.Executing--
m.CommandCounter.Complete++
}()
semaphore <- struct{}{}
defer func() {
<-semaphore
}()
// m.CommandCounter.Total++
m.CommandCounter.Pending--
m.CommandCounter.Executing++
// "PaginationMarker" is a control variable used for output continuity, as AWS return the output in pages.
var PaginationControl *string
var totalCountThisServiceThisRegion = 0
var service = "RDS DB Instances"
for {
DescribeDBInstances, err := m.RDSClient.DescribeDBInstances(
context.TODO(),
&(rds.DescribeDBInstancesInput{
Marker: PaginationControl,
}),
func(o *rds.Options) {
o.Region = r
},
)
if err != nil {
m.modLog.Error(err.Error())
m.CommandCounter.Error++
break
}
// Add this page of resources to the total count
totalCountThisServiceThisRegion = totalCountThisServiceThisRegion + len(DescribeDBInstances.DBInstances)
// The "NextToken" value is nil when there's no more data to return.
if DescribeDBInstances.Marker != nil {
PaginationControl = DescribeDBInstances.Marker
} else {
PaginationControl = nil
m.mu.Lock()
m.serviceMap[service][r] = totalCountThisServiceThisRegion
m.totalRegionCounts[r] = m.totalRegionCounts[r] + totalCountThisServiceThisRegion
m.serviceMap["total"][r] = m.serviceMap["total"][r] + totalCountThisServiceThisRegion
m.mu.Unlock()
break
}
}
}
func (m *Inventory2Module) getAPIGatewayvAPIsPerRegion(r string, wg *sync.WaitGroup, semaphore chan struct{}) {
defer func() {
wg.Done()
m.CommandCounter.Executing--
m.CommandCounter.Complete++
}()
semaphore <- struct{}{}
defer func() {
<-semaphore
}()
// m.CommandCounter.Total++
m.CommandCounter.Pending--
m.CommandCounter.Executing++
// "PaginationMarker" is a control variable used for output continuity, as AWS return the output in pages.
var PaginationControl *string
var totalCountThisServiceThisRegion = 0
var service = "APIGateway RestAPIs"
// This for loop exits at the end depending on whether the output hits its last page (see pagination control block at the end of the loop).
for {
GetRestApis, err := m.APIGatewayClient.GetRestApis(
context.TODO(),
&apigateway.GetRestApisInput{
Position: PaginationControl,
},
func(o *apigateway.Options) {
o.Region = r
},
)
if err != nil {
m.modLog.Error(err.Error())
m.CommandCounter.Error++
break
}
// Add this page of resources to the total count
totalCountThisServiceThisRegion = totalCountThisServiceThisRegion + len(GetRestApis.Items)
// Pagination control. After the last page of output, the for loop exits.
if GetRestApis.Position != nil {
PaginationControl = GetRestApis.Position
} else {
PaginationControl = nil
m.mu.Lock()
m.serviceMap[service][r] = totalCountThisServiceThisRegion
m.totalRegionCounts[r] = m.totalRegionCounts[r] + totalCountThisServiceThisRegion
m.serviceMap["total"][r] = m.serviceMap["total"][r] + totalCountThisServiceThisRegion
m.mu.Unlock()
break
}
}
}
func (m *Inventory2Module) getAPIGatewayv2APIsPerRegion(r string, wg *sync.WaitGroup, semaphore chan struct{}) {
defer func() {
wg.Done()
m.CommandCounter.Executing--
m.CommandCounter.Complete++
}()
semaphore <- struct{}{}
defer func() {
<-semaphore
}()
// m.CommandCounter.Total++
m.CommandCounter.Pending--
m.CommandCounter.Executing++
// "PaginationMarker" is a control variable used for output continuity, as AWS return the output in pages.
var PaginationControl *string
var totalCountThisServiceThisRegion = 0
var service = "APIGatewayv2 APIs"
// This for loop exits at the end depending on whether the output hits its last page (see pagination control block at the end of the loop).
for {
GetApis, err := m.APIGatewayv2Client.GetApis(
context.TODO(),
&apigatewayv2.GetApisInput{
NextToken: PaginationControl,
},
func(o *apigatewayv2.Options) {
o.Region = r
},
)
if err != nil {
m.modLog.Error(err.Error())
m.CommandCounter.Error++
break
}
// Add this page of resources to the total count
totalCountThisServiceThisRegion = totalCountThisServiceThisRegion + len(GetApis.Items)
// Pagination control. After the last page of output, the for loop exits.
if GetApis.NextToken != nil {
PaginationControl = GetApis.NextToken
} else {
PaginationControl = nil
m.mu.Lock()
m.serviceMap[service][r] = totalCountThisServiceThisRegion
m.totalRegionCounts[r] = m.totalRegionCounts[r] + totalCountThisServiceThisRegion
m.serviceMap["total"][r] = m.serviceMap["total"][r] + totalCountThisServiceThisRegion
m.mu.Unlock()
break
}
}
}
func (m *Inventory2Module) getELBv2ListenersPerRegion(r string, wg *sync.WaitGroup, semaphore chan struct{}) {
defer func() {
wg.Done()
m.CommandCounter.Executing--
m.CommandCounter.Complete++
}()
semaphore <- struct{}{}
defer func() {
<-semaphore
}()
// m.CommandCounter.Total++
m.CommandCounter.Pending--
m.CommandCounter.Executing++
// "PaginationMarker" is a control variable used for output continuity, as AWS return the output in pages.
var PaginationControl *string
var totalCountThisServiceThisRegion = 0
var service = "ELBv2 Load Balancers"
// This for loop exits at the end depending on whether the output hits its last page (see pagination control block at the end of the loop).
for {
DescribeLoadBalancers, err := m.ELBv2Client.DescribeLoadBalancers(
context.TODO(),
&elasticloadbalancingv2.DescribeLoadBalancersInput{
Marker: PaginationControl,
},
func(o *elasticloadbalancingv2.Options) {
o.Region = r
},
)
if err != nil {
m.modLog.Error(err.Error())
m.CommandCounter.Error++
break
}
// Add this page of resources to the total count
totalCountThisServiceThisRegion = totalCountThisServiceThisRegion + len(DescribeLoadBalancers.LoadBalancers)
// Pagination control. After the last page of output, the for loop exits.
if DescribeLoadBalancers.NextMarker != nil {
PaginationControl = DescribeLoadBalancers.NextMarker
} else {
PaginationControl = nil
// No more pages, update the module's service map
m.mu.Lock()
m.serviceMap[service][r] = totalCountThisServiceThisRegion
m.totalRegionCounts[r] = m.totalRegionCounts[r] + totalCountThisServiceThisRegion
m.serviceMap["total"][r] = m.serviceMap["total"][r] + totalCountThisServiceThisRegion
m.mu.Unlock()
break
}
}
}
func (m *Inventory2Module) getELBListenersPerRegion(r string, wg *sync.WaitGroup, semaphore chan struct{}) {
defer func() {
wg.Done()
m.CommandCounter.Executing--
m.CommandCounter.Complete++
}()
semaphore <- struct{}{}
defer func() {
<-semaphore
}()
// m.CommandCounter.Total++
m.CommandCounter.Pending--
m.CommandCounter.Executing++
// "PaginationMarker" is a control variable used for output continuity, as AWS return the output in pages.
var PaginationControl *string
var totalCountThisServiceThisRegion = 0
var service = "ELB Load Balancers"
// This for loop exits at the end depending on whether the output hits its last page (see pagination control block at the end of the loop).
for {
DescribeLoadBalancers, err := m.ELBClient.DescribeLoadBalancers(
context.TODO(),
&elasticloadbalancing.DescribeLoadBalancersInput{
Marker: PaginationControl,
},
func(o *elasticloadbalancing.Options) {
o.Region = r
},
)
if err != nil {
m.modLog.Error(err.Error())
m.CommandCounter.Error++
break
}
// Add this page of resources to the total count
totalCountThisServiceThisRegion = totalCountThisServiceThisRegion + len(DescribeLoadBalancers.LoadBalancerDescriptions)
// Pagination control. After the last page of output, the for loop exits.
if DescribeLoadBalancers.NextMarker != nil {
PaginationControl = DescribeLoadBalancers.NextMarker
} else {
PaginationControl = nil
// No more pages, update the module's service map
m.mu.Lock()
m.serviceMap[service][r] = totalCountThisServiceThisRegion
m.totalRegionCounts[r] = m.totalRegionCounts[r] + totalCountThisServiceThisRegion
m.serviceMap["total"][r] = m.serviceMap["total"][r] + totalCountThisServiceThisRegion
m.mu.Unlock()
break
}
}
}
func (m *Inventory2Module) getMqBrokersPerRegion(r string, wg *sync.WaitGroup, semaphore chan struct{}) {
defer func() {
wg.Done()
m.CommandCounter.Executing--
m.CommandCounter.Complete++
}()
semaphore <- struct{}{}
defer func() {
<-semaphore
}()
// m.CommandCounter.Total++
m.CommandCounter.Pending--
m.CommandCounter.Executing++
// "PaginationMarker" is a control variable used for output continuity, as AWS return the output in pages.
var PaginationControl *string
var totalCountThisServiceThisRegion = 0
var service = "MQ Brokers"
// This for loop exits at the end depending on whether the output hits its last page (see pagination control block at the end of the loop).
for {
ListBrokers, err := m.MQClient.ListBrokers(
context.TODO(),
&mq.ListBrokersInput{
NextToken: PaginationControl,
},
func(o *mq.Options) {
o.Region = r
},
)
if err != nil {
m.modLog.Error(err.Error())
m.CommandCounter.Error++
break
}
// Add this page of resources to the total count
totalCountThisServiceThisRegion = totalCountThisServiceThisRegion + len(ListBrokers.BrokerSummaries)
// Pagination control. After the last page of output, the for loop exits.
if ListBrokers.NextToken != nil {
PaginationControl = ListBrokers.NextToken
} else {
PaginationControl = nil
m.mu.Lock()
m.serviceMap[service][r] = totalCountThisServiceThisRegion
m.totalRegionCounts[r] = m.totalRegionCounts[r] + totalCountThisServiceThisRegion
m.serviceMap["total"][r] = m.serviceMap["total"][r] + totalCountThisServiceThisRegion
m.mu.Unlock()
break
}
}