forked from rebuy-de/aws-nuke
/
main.go
100 lines (83 loc) · 2.41 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
package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/BigDwarf/aws-nuke/v2/resources"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/fatih/color"
"github.com/rebuy-de/rebuy-go-sdk/v4/pkg/cmdutil"
"github.com/sirupsen/logrus"
)
type CFTypeSchema struct {
Handlers map[string]interface{} `json:"handlers"`
}
func main() {
ctx := cmdutil.SignalRootContext()
sess, err := session.NewSession(&aws.Config{
Region: aws.String(endpoints.UsEast1RegionID),
})
if err != nil {
logrus.Fatal(err)
}
cf := cloudformation.New(sess)
mapping := resources.GetCloudControlMapping()
in := &cloudformation.ListTypesInput{
Type: aws.String(cloudformation.RegistryTypeResource),
Visibility: aws.String(cloudformation.VisibilityPublic),
ProvisioningType: aws.String(cloudformation.ProvisioningTypeFullyMutable),
}
err = cf.ListTypesPagesWithContext(ctx, in, func(out *cloudformation.ListTypesOutput, _ bool) bool {
if out == nil {
return true
}
for _, summary := range out.TypeSummaries {
if summary == nil {
continue
}
typeName := aws.StringValue(summary.TypeName)
color.New(color.Bold).Printf("%-55s", typeName)
if !strings.HasPrefix(typeName, "AWS::") {
color.HiBlack("does not have a valid prefix")
continue
}
describe, err := cf.DescribeType(&cloudformation.DescribeTypeInput{
Type: aws.String(cloudformation.RegistryTypeResource),
TypeName: aws.String(typeName),
})
if err != nil {
color.New(color.FgRed).Println(err)
continue
}
var schema CFTypeSchema
err = json.Unmarshal([]byte(aws.StringValue(describe.Schema)), &schema)
if err != nil {
color.New(color.FgRed).Println(err)
continue
}
_, canList := schema.Handlers["list"]
if !canList {
color.New(color.FgHiBlack).Println("does not support list")
continue
}
resourceName, exists := mapping[typeName]
if exists && resourceName == typeName {
fmt.Print("is only covered by ")
color.New(color.FgGreen, color.Bold).Println(resourceName)
continue
} else if exists {
fmt.Print("is also covered by ")
color.New(color.FgBlue, color.Bold).Println(resourceName)
continue
}
color.New(color.FgYellow).Println("is not configured")
}
return true
})
if err != nil {
logrus.Fatal(err)
}
}