/
main.go
540 lines (470 loc) · 15 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
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
package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"log"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"gopkg.in/yaml.v2"
)
var logger *log.Logger
const configFile string = "awsnukedocgen.yaml"
const iamTemplate string = `
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DoNotModifySelf",
"Effect": "Deny",
"NotAction": [
"iam:GetPolicy",
"iam:GetPolicyVersion",
"iam:GetRole",
"iam:GetRolePolicy",
"iam:ListRoles",
"iam:ListRolePolicies",
"iam:ListAttachedRolePolicies",
"iam:ListRoleTags",
"iam:ListPoliciesGrantingServiceAccess",
"iam:ListEntitiesForPolicy",
"iam:ListPolicyVersions",
"iam:GenerateServiceLastAccessedDetails"
],
"Resource": [
"arn:aws:iam::123456789012:policy/DCEPrincipalDefaultPolicy",
"arn:aws:iam::123456789012:role/DCEPrincipal",
"arn:aws:iam::123456789012:role/OrganizationAccountAccessRole"
]
},
{
"Sid": "DenyTaggedResourcesAWS",
"Effect": "Deny",
"Action": "*",
"Resource": "*",
"Condition": {
"StringEquals": {
"aws:ResourceTag/AppName": [
"DCE"
]
}
}
},
{
"Sid": "DenyIAM",
"Effect": "Deny",
"Action": [
"iam:DeactivateMFADevice",
"iam:CreateSAMLProvider",
"iam:UpdateAccountPasswordPolicy",
"iam:DeleteVirtualMFADevice",
"iam:EnableMFADevice",
"iam:CreateAccountAlias",
"iam:DeleteAccountAlias",
"iam:UpdateSAMLProvider",
"iam:DeleteSAMLProvider"
],
"Resource": "*"
},
{
"Sid": "AllowedServices",
"Effect": "Allow",
"Action": [
{{- range .ServiceShortNames}}
"{{.}}:*",
{{- end}}
],
"Resource": "*",
"Condition": {
"StringEquals": {
"aws:RequestedRegion": [
"us-east-1",
"us-west-1"
]
}
}
}
]
}
`
const markdownTemplate string = `# Account Cleanup with AWS Nuke
DCE uses a [fork of AWS Nuke](https://github.com/Optum/aws-nuke)
to facilitate account cleanup. Shown here is a list of services that are
supported and those that are not supported for account cleanup.
## Supported Services
{{range .SupportedServices -}}
* {{.}}
{{end}}
## Unsupported Services
{{range .UnsupportedServices -}}
* {{.}}
{{end}}
`
// ToolConfig stories the configuration for the services
type ToolConfig struct {
AdditionalServices []string `yaml:"additionalServices"`
ServiceAliases map[string]string `yaml:"serviceAliases"`
}
var configuration *ToolConfig
// init initializes the configuration by reading it from the
// yaml file.
func init() {
logger = log.New(os.Stderr, "", 0)
configuration = &ToolConfig{}
yamlStr, err := os.ReadFile(configFile)
if err != nil {
logger.Fatalln("error loading configuration", err)
}
err = yaml.Unmarshal(yamlStr, configuration)
if err != nil {
logger.Fatalln("error while trying to parse configuration", err)
}
}
// GetUniqueValues returns a list of unique strings from the
// given values
func GetUniqueValues(from []string) []string {
keys := make(map[string]bool)
result := []string{}
for _, entry := range from {
if _, val := keys[entry]; !val {
keys[entry] = true
result = append(result, entry)
}
}
return result
}
// exitWithErr exits the program with an error
func exitWithErr(err error) {
logger.Fatalf("error: %v", err)
}
// fileExists returns true if the file with the given name exists.“
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func dirExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}
// NukeParser parses the /resources folder in the AWS Nuke source to pull
// out the .\Delete methods.
type NukeParser struct {
nukeSourceDir string
deleteMethodExpression *regexp.Regexp
serviceReferenceExpression *regexp.Regexp
}
// GetDeleteMethods returns a list of all of the delete methods used
// in the /resources folder of AWS Nukes
func (np *NukeParser) GetDeleteMethods() ([]string, error) {
return np.scanForSupportedDeleteMethods(
np.serviceReferenceExpression,
np.deleteMethodExpression,
)
}
// Close closes the resource because file.Close() returns an error,
// which is flagged by gosec.
func (np *NukeParser) Close(f *os.File) {
err := f.Close()
if err != nil {
logger.Printf("could not close file: %s", f.Name())
}
}
// scanForExpression scans for the given expression in the directory
func (np *NukeParser) scanForSupportedDeleteMethods(
refExpr *regexp.Regexp,
methodExpr *regexp.Regexp,
) ([]string, error) {
results := make([]string, 0)
dir := path.Join(np.nukeSourceDir, "resources")
files, err := os.ReadDir(dir)
if err != nil {
return results, fmt.Errorf("error while trying to get files: %v", err)
}
for _, f := range files {
logger.Printf("scanning file: %s", f.Name())
methods := make([]string, 0)
services := make([]string, 0)
file, err := os.Open(filepath.Clean(path.Join(dir, f.Name())))
if err != nil {
logger.Printf("error opening file \"%s\": %v", f.Name(), err)
continue
}
defer np.Close(file)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
refGroup := refExpr.FindAllStringSubmatch(scanner.Text(), -1)
if len(refGroup) > 0 {
val := refGroup[0][1]
// look up the name of the service in the map, and if it's
// found in the overrides, add in the overridden value...
override, ok := configuration.ServiceAliases[val]
if ok {
services = append(services, override)
} else {
services = append(services, val)
}
}
methodGroup := methodExpr.FindAllStringSubmatch(scanner.Text(), -1)
if len(methodGroup) == 0 {
continue
}
val := methodGroup[0][1]
methods = append(methods, val)
}
for _, svc := range services {
for _, method := range methods {
results = append(results, fmt.Sprintf("%s:%s", svc, method))
}
}
}
return results, nil
}
// NewNukeParser creates a new instance of the NukeParser
func NewNukeParser(sourceDir string) *NukeParser {
return &NukeParser{
nukeSourceDir: sourceDir,
deleteMethodExpression: regexp.MustCompile(`^.*\.(Delete[^(]+|Terminate[^(]+)\(.*`),
serviceReferenceExpression: regexp.MustCompile(`^.*aws-sdk-go\/service\/([^\/]+)\"\s*$`),
}
}
// SupportedDelete contains the short name and the supported delete
// method that was found in AWS Nuke.
type SupportedDelete struct {
ServiceName string
ServicePrefix string
MethodName string
}
// ServiceInfo contains the short name and available actions for the
// service.
type ServiceInfo struct {
ServiceName *string `json:"StringPrefix"`
Actions []*string `json:"Actions"`
}
// PoliciesConfig contains the policy configuration
type PoliciesConfig struct {
ServiceMap map[string]ServiceInfo `json:"serviceMap"`
}
// PoliciesParser parses the policies file to get the name of the service
// and the actions supported by the services
// The JSON looks like this:
//
// "serviceMap": {
// "Amazon Comprehend": {
// "StringPrefix": "comprehend",
// "Actions": [
//
// ...
type PoliciesParser struct {
policiesJSFile string
deleteMethods []string
supportedDeletes []*SupportedDelete
supportedServices []string
unsupportedServices []string
}
// SupportedServices returns a list of supported services with "nice"
// descriptions.
func (pp *PoliciesParser) SupportedServices() []string {
return pp.supportedServices
}
// UnsupportedServices returns a list of unsupported services with "nice"
// descriptions
func (pp *PoliciesParser) UnsupportedServices() []string {
return pp.unsupportedServices
}
// SupportedDeleteMethods returns a list of supported delete operations
func (pp *PoliciesParser) SupportedDeleteMethods() []*SupportedDelete {
return pp.supportedDeletes
}
// Parse parses the policies file and returns a list of supported
// services
func (pp *PoliciesParser) Parse() error {
deletes := make([]*SupportedDelete, 0)
supportedServices := make([]string, 0)
unsupportedServices := make([]string, 0)
if !fileExists(pp.policiesJSFile) {
return fmt.Errorf("file %s does not exist", pp.policiesJSFile)
}
policies := &PoliciesConfig{}
file, err := os.Open(pp.policiesJSFile)
if err != nil {
log.Fatalf("error opening file: %s", err.Error())
}
bytes, _ := io.ReadAll(file)
err = json.Unmarshal(bytes, policies)
if err != nil {
log.Fatalf("error while unmarshaling JSON file: %s", err.Error())
}
// now iterate through the service maps, and for each of those
// iterate through the actions. If the action is a supported method,
// we'll
for svc, info := range policies.ServiceMap {
isFound := false
for _, action := range info.Actions {
if pp.isSupported(fmt.Sprintf("%s:%s", *info.ServiceName, *action)) {
deletes = append(deletes, &SupportedDelete{
ServiceName: svc,
ServicePrefix: *info.ServiceName,
MethodName: *action,
})
isFound = true
supportedServices = append(supportedServices, svc)
}
}
if !isFound {
unsupportedServices = append(unsupportedServices, svc)
}
}
// now, as a double check, we'll get the list of all services that are
// referenced by the source code. If it's a service that's not referenced
// at all we'll remove it from the list.
pp.supportedDeletes = deletes
pp.supportedServices = GetUniqueValues(supportedServices)
pp.unsupportedServices = GetUniqueValues(unsupportedServices)
return nil
}
func (pp *PoliciesParser) isSupported(method string) bool {
for _, m := range pp.deleteMethods {
if m == method {
return true
}
}
return false
}
// NewPoliciesParser creates a news instance of *PoliciesParser
func NewPoliciesParser(policiesJS string, methods []string) *PoliciesParser {
return &PoliciesParser{
policiesJSFile: policiesJS,
deleteMethods: methods,
supportedDeletes: make([]*SupportedDelete, 0),
supportedServices: make([]string, 0),
unsupportedServices: make([]string, 0),
}
}
// MarkdownGenerator generates Markdown documentation that identifies
// which services are supported by AWS Nuke and which service are NOT
// supported by AWS Nuke
type MarkdownGenerator struct {
SupportedServices []string
UnsupportedServices []string
IAMExample string
}
// Generate returns a string of text in Markdown format that
// documents what is supported and what AWS services are not supported
// for use in DCE
func (mg *MarkdownGenerator) Generate() (string, error) {
sort.Strings(mg.SupportedServices)
sort.Strings(mg.UnsupportedServices)
buf := new(bytes.Buffer)
tplate := template.Must(template.New("md").Parse(markdownTemplate))
err := tplate.Execute(buf, mg)
return buf.String(), err
}
// NewMarkdownGenerator creates a new instance of the MarkdownGenerator
func NewMarkdownGenerator(supported []string, unsupported []string, ipg *IAMPolicyGenerator) *MarkdownGenerator {
policy, err := ipg.GeneratePolicy()
if err != nil {
policy = ""
}
return &MarkdownGenerator{
SupportedServices: supported,
UnsupportedServices: unsupported,
IAMExample: policy,
}
}
// IAMPolicyGenerator combines the functions used by AWS nuke and all
// of those supported by AWS to create a policy.
type IAMPolicyGenerator struct {
usePermissive bool
SupportedDeletes []*SupportedDelete
ServiceShortNames []string
}
// GeneratePolicy generates the IAM policy
func (ipg *IAMPolicyGenerator) GeneratePolicy() (string, error) {
buf := new(bytes.Buffer)
tplate := template.Must(template.New("pol").Parse(iamTemplate))
err := tplate.Execute(buf, ipg)
return buf.String(), err
}
// NewIAMPolicyGenerator creates a new instance of *IAMPolicyGenerator
func NewIAMPolicyGenerator(permissive bool, methods []*SupportedDelete, overrides []string) *IAMPolicyGenerator {
services := make([]string, 0)
for _, sd := range methods {
services = append(services, sd.ServicePrefix)
}
// Add in the overrides for additional services that should be included but
// are parsed incorrectly or not at all.
services = append(services, overrides...)
services = GetUniqueValues(services)
sort.Strings(services)
return &IAMPolicyGenerator{
usePermissive: permissive,
SupportedDeletes: methods,
ServiceShortNames: services,
}
}
// main method for the iampolgen tool
func main() {
logger.Println("Starting now...")
policiesJSFile := flag.String("policies-js-file", "./policies.js", "name of the file containing IAM policy configuration.")
generateMarkdown := flag.Bool("generate-markdown", false, "if true, tells the generator to generate a Markdown file documenting the gaps")
nukeSourceDir := flag.String("nuke-source-dir", "", "location of the AWS nuke source")
iamUsePermissive := flag.Bool("iam-use-permissive", true, "if set to true, will generate permissive polcies using wildcards")
flag.Parse()
// Do some basic checking to make sure the args supplied are good.
if !fileExists(*policiesJSFile) {
exitWithErr(fmt.Errorf("specified policy file \"%s\" does not exist", *policiesJSFile))
}
if len(*nukeSourceDir) == 0 {
exitWithErr(fmt.Errorf("must specify a valid AWS nuke directory"))
}
if !dirExists(*nukeSourceDir) {
exitWithErr(fmt.Errorf("specified AWS nuke source directory \"%s\" does not exist", *nukeSourceDir))
}
// get the list of delete commands that are used by AWS Nuke
nuke := NewNukeParser(*nukeSourceDir)
methods, err := nuke.GetDeleteMethods()
if err != nil {
exitWithErr(fmt.Errorf("error whilst trying to get methods: %v", err))
}
// get the list of the AWS services and their delete permissions
policy := NewPoliciesParser(*policiesJSFile, methods)
err = policy.Parse()
if err != nil {
exitWithErr(fmt.Errorf("error parsing policies file: %v", err))
}
// now hand these off to the generator, which will compare the two
// and generate an IAM policy using awesomeness.
polgen := NewIAMPolicyGenerator(*iamUsePermissive, policy.SupportedDeleteMethods(), configuration.AdditionalServices)
if *generateMarkdown {
logger.Printf("generating Markdown file now...")
markdown := NewMarkdownGenerator(
policy.SupportedServices(),
policy.UnsupportedServices(),
polgen,
)
md, err := markdown.Generate()
if err != nil {
exitWithErr(fmt.Errorf("error while generating markdown: %v", err))
}
fmt.Println(md)
return
}
samplePolicy, err := polgen.GeneratePolicy()
if err != nil {
exitWithErr(fmt.Errorf("error while creating sample policy: %v", err))
}
fmt.Println(samplePolicy)
}