-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (114 loc) · 3.8 KB
/
main.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
package main
import (
"aws-resource-discovery/pkg/cloudtrail"
"aws-resource-discovery/pkg/config"
"aws-resource-discovery/pkg/interfaces"
"aws-resource-discovery/pkg/logger"
"aws-resource-discovery/pkg/managers"
"aws-resource-discovery/pkg/scanner"
"context"
"flag"
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
aws_config "github.com/aws/aws-sdk-go-v2/config"
aws_trail "github.com/aws/aws-sdk-go-v2/service/cloudtrail"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/organizations"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/sts"
)
func main() {
ctx := context.Background()
userConfig := parseFlags()
// Setup CSV Logger
csvLogger, err := logger.NewCSVLogger("aws-resource-discovery.csv")
if err != nil {
log.Fatalf("Failed to initialize CSV logger: %v", err)
}
defer csvLogger.Close()
// Load the AWS SDK configuration
cfg, err := aws_config.LoadDefaultConfig(ctx)
if err != nil {
log.Fatalf("Failed to load AWS configuration: %v", err)
}
// Create STS Client
stsClient := sts.NewFromConfig(cfg)
credsManager := managers.NewCredentialsManager(userConfig.RoleName, stsClient)
sessionManager := managers.NewSessionManager(stsClient)
// Create EC2 Client
ec2Client := ec2.NewFromConfig(cfg)
// Create RegionsManager with EC2 Client
regionsManager := managers.NewRegionManager(ec2Client)
// Create Organizations Client
orgClient := organizations.NewFromConfig(cfg)
orgDetector := scanner.NewOrgDetector(orgClient, csvLogger)
// Create Scanner service with factory function for ResourceScanner
scanService := scanner.NewScanner(
stsClient,
sessionManager,
regionsManager,
credsManager,
orgDetector,
func(cfg aws.Config) interfaces.OrganizationsClient {
return organizations.NewFromConfig(cfg)
},
csvLogger,
)
startTime := time.Now()
// Determine the flow based on the parsed configuration
var scanResult scanner.ScanResult
if userConfig.AccountId != "" {
fmt.Println("Single account scan selected.")
scanResult, err = scanService.ScanSingleAccount(ctx, userConfig)
} else {
fmt.Println("Organization scan selected.")
scanResult, err = scanService.ScanOrganization(ctx, userConfig)
}
if err != nil {
log.Fatalf("Failed to perform scan: %v", err)
}
endTime := time.Now()
duration := endTime.Sub(startTime)
minutes := int(duration.Minutes())
seconds := int(duration.Seconds()) % 60
if minutes > 0 {
fmt.Printf("\nScan completed in %d minutes %d seconds.\n", minutes, seconds)
} else {
fmt.Printf("\nScan completed in %d seconds.\n", seconds)
}
// Perform CloudTrail check based on the scan result
if userConfig.Trail {
ctClient := aws_trail.NewFromConfig(cfg)
s3Client := s3.NewFromConfig(cfg)
ctChecker := &cloudtrail.CloudTrailChecker{
CTClient: ctClient,
S3Client: s3Client,
AWSConfig: cfg,
}
trailInfos, err := ctChecker.CheckCloudTrail(ctx, scanResult)
if err != nil {
log.Fatalf("Failed to check CloudTrail: %v", err)
}
cloudtrail.PrintTable(ctx, trailInfos)
}
}
func parseFlags() config.Config {
var config config.Config
var excludeAccounts string
flag.StringVar(&config.RoleArn, "AWS_ROLE_ARN", "", "AWS Role ARN to assume")
flag.StringVar(&config.AccountId, "AWS_ACCOUNT_ID", "", "AWS Account ID")
flag.StringVar(&config.Region, "AWS_REGION", "", "AWS Region")
flag.StringVar(&config.RoleName, "AWS_ROLE_NAME", "", "AWS Role Name")
flag.BoolVar(&config.Trail, "AWS_TRAIL", false, "Set to true to print CloudTrail information")
flag.StringVar(&excludeAccounts, "EXCLUDE", "", "Comma-separated list of AWS account numbers to exclude")
// Parse flags
flag.Parse()
// Split the EXCLUDE_ACCOUNT flag value into a slice of strings
if excludeAccounts != "" {
config.ExcludeAccounts = strings.Split(excludeAccounts, ",")
}
return config
}