-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
318 lines (274 loc) · 9.26 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
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
package main
import (
"context"
"encoding/gob"
"os"
"sync"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/retry"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"github.com/padok-team/yatas-aws/aws/acm"
"github.com/padok-team/yatas-aws/aws/apigateway"
"github.com/padok-team/yatas-aws/aws/autoscaling"
"github.com/padok-team/yatas-aws/aws/cloudfront"
"github.com/padok-team/yatas-aws/aws/cloudtrail"
"github.com/padok-team/yatas-aws/aws/cognito"
"github.com/padok-team/yatas-aws/aws/configservice"
"github.com/padok-team/yatas-aws/aws/dynamodb"
"github.com/padok-team/yatas-aws/aws/ec2"
"github.com/padok-team/yatas-aws/aws/ecr"
"github.com/padok-team/yatas-aws/aws/eks"
"github.com/padok-team/yatas-aws/aws/guardduty"
"github.com/padok-team/yatas-aws/aws/iam"
"github.com/padok-team/yatas-aws/aws/lambda"
"github.com/padok-team/yatas-aws/aws/loadbalancers"
"github.com/padok-team/yatas-aws/aws/rds"
"github.com/padok-team/yatas-aws/aws/s3"
"github.com/padok-team/yatas-aws/aws/volumes"
"github.com/padok-team/yatas-aws/aws/vpc"
"github.com/padok-team/yatas-aws/internal"
"github.com/padok-team/yatas-aws/logger"
"github.com/padok-team/yatas/plugins/commons"
)
// Create a new session that the SDK will use to load
// credentials from. With either SSO or credentials
func initAuth(a internal.AWS_Account) aws.Config {
logger.Logger.Debug("Init auth")
s := initSession(a)
return s
}
// Create a new session that the SDK will use to load
// credentials from credentials
func createSessionWithCredentials(c internal.AWS_Account) aws.Config {
var s aws.Config
var err error
if c.Profile == "" {
s, err = config.LoadDefaultConfig(context.TODO(),
config.WithRegion(c.Region),
config.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), 10)
}),
config.WithRetryMode(aws.RetryMode(aws.RetryModeAdaptive)),
)
} else {
s, err = config.LoadDefaultConfig(context.TODO(),
config.WithRegion(c.Region),
config.WithSharedConfigProfile(c.Profile),
config.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), 10)
}),
config.WithRetryMode(aws.RetryMode(aws.RetryModeAdaptive)),
)
}
if err != nil {
logger.Logger.Error(err.Error())
}
return s
}
// Create a new session that the SDK will use to load
// credentials from the shared credentials file.
// Usefull for SSO
func createSessionWithSSO(c internal.AWS_Account) aws.Config {
if c.Profile == "" {
s, err := config.LoadDefaultConfig(context.Background(),
config.WithRegion(c.Region),
config.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), 10)
}),
config.WithRetryMode(aws.RetryMode(aws.RetryModeAdaptive)),
)
if err != nil {
logger.Logger.Error(err.Error())
}
return s
} else {
s, err := config.LoadDefaultConfig(context.Background(),
config.WithRegion(c.Region),
config.WithSharedConfigProfile(c.Profile),
config.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), 10)
}),
config.WithRetryMode(aws.RetryMode(aws.RetryModeAdaptive)),
)
if err != nil {
logger.Logger.Error(err.Error())
}
return s
}
}
// Create a new session that the SDK will use to load
// credentials from. With either SSO or credentials
func initSession(c internal.AWS_Account) aws.Config {
if c.SSO {
logger.Logger.Debug("Init SSO")
return createSessionWithSSO(c)
} else {
logger.Logger.Debug("Init Credentials")
return createSessionWithCredentials(c)
}
}
// Public Functin used to run the AWS tests
func Run(c *commons.Config, accounts []internal.AWS_Account) ([]commons.Tests, error) {
logger.Logger.Debug("Run tests")
var wg sync.WaitGroup
var queue = make(chan commons.Tests, 10)
var checks []commons.Tests
wg.Add(len(accounts))
for _, account := range accounts {
go runTestsForAccount(account, c, queue)
}
go func() {
for t := range queue {
checks = append(checks, t)
wg.Done()
}
}()
wg.Wait()
return checks, nil
}
// For each account we run the tests. We use a queue to store the results and a waitgroup to wait for all the tests to be done. This allows to run all tests asynchronously.
func runTestsForAccount(account internal.AWS_Account, c *commons.Config, queue chan commons.Tests) {
s := initAuth(account)
checks := initTest(s, c, account)
queue <- checks
}
// Main function that launched all the test for a given account. If a new category is added, it needs to be added here.
func initTest(s aws.Config, c *commons.Config, a internal.AWS_Account) commons.Tests {
var checks commons.Tests
checks.Account = a.Name
var wg sync.WaitGroup
queue := make(chan []commons.Check, 100)
go commons.CheckMacroTest(&wg, c, cognito.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, acm.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, s3.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, volumes.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, rds.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, vpc.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, cloudtrail.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, ecr.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, lambda.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, dynamodb.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, ec2.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, cloudfront.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, apigateway.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, autoscaling.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, loadbalancers.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, guardduty.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, iam.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, eks.RunChecks)(&wg, s, c, queue)
go commons.CheckMacroTest(&wg, c, configservice.RunChecks)(&wg, s, c, queue)
go func() {
for t := range queue {
checks.Checks = append(checks.Checks, t...)
wg.Done()
}
}()
wg.Wait()
return checks
}
type YatasPlugin struct {
logger hclog.Logger
}
func UnmarshalAWS(g *YatasPlugin, c *commons.Config) ([]internal.AWS_Account, error) {
var accounts []internal.AWS_Account
for _, r := range c.PluginConfig {
var tmpAccounts []internal.AWS_Account
awsFound := false
for key, value := range r {
switch key {
case "pluginName":
if value == "aws" {
awsFound = true
}
case "accounts":
for _, v := range value.([]interface{}) {
var account internal.AWS_Account
logger.Logger.Debug("🔎")
logger.Logger.Debug("%v", v)
for keyaccounts, valueaccounts := range v.(map[string]interface{}) {
switch keyaccounts {
case "name":
account.Name = valueaccounts.(string)
case "profile":
account.Profile = valueaccounts.(string)
case "region":
account.Region = valueaccounts.(string)
case "sso":
account.SSO = valueaccounts.(bool)
}
}
tmpAccounts = append(tmpAccounts, account)
}
}
}
if awsFound {
accounts = tmpAccounts
}
}
logger.Logger.Debug("✅")
logger.Logger.Debug("%v", accounts)
logger.Logger.Debug("Length of accounts: %d", len(accounts))
if len(accounts) == 0 {
logger.Logger.Error("No AWS accounts found in config file")
}
return accounts, nil
}
func (g *YatasPlugin) Run(c *commons.Config) []commons.Tests {
logger.Logger = g.logger
logger.Logger.Debug("message from YatasPlugin.Run")
var err error
var accounts []internal.AWS_Account
accounts, err = UnmarshalAWS(g, c)
if err != nil {
logger.Logger.Error("Error unmarshaling AWS accounts", "error", err)
return nil
}
var checksAll []commons.Tests
checks, err := runPlugins(c, "aws", accounts)
if err != nil {
logger.Logger.Error("Error running plugins", "error", err)
}
checksAll = append(checksAll, checks...)
return checksAll
}
// handshakeConfigs are used to just do a basic handshake between
// a plugin and host. If the handshake fails, a user friendly error is shown.
// This prevents users from executing bad plugins or executing a plugin
// directory. It is a UX feature, not a security feature.
var handshakeConfig = plugin.HandshakeConfig{
ProtocolVersion: 2,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "hello",
}
func main() {
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})
logger := hclog.New(&hclog.LoggerOptions{
Level: hclog.Debug,
Output: os.Stderr,
JSONFormat: true,
})
yatasPlugin := &YatasPlugin{
logger: logger,
}
// pluginMap is the map of plugins we can dispense.
var pluginMap = map[string]plugin.Plugin{
"aws": &commons.YatasPlugin{Impl: yatasPlugin},
}
logger.Debug("message from plugin", "foo", "bar")
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: handshakeConfig,
Plugins: pluginMap,
})
}
// Run the plugins that are enabled in the config with a switch based on the name of the plugin
func runPlugins(c *commons.Config, _ string, accounts []internal.AWS_Account) ([]commons.Tests, error) {
var checksAll []commons.Tests
checksAll, err := Run(c, accounts)
if err != nil {
return nil, err
}
return checksAll, nil
}