-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (63 loc) · 1.42 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
package main
import (
"flag"
"fmt"
"log"
"git.dcpri.me/some-fancy-tools/sg-audit/audit"
)
var (
profile = flag.String("profile", "", "AWS Profile to use")
region = flag.String("region", "", "AWS Region to use")
nocolor = flag.Bool("no-color", false, "No Colored output")
csv = flag.Bool("csv", false, "Output in CSV Format")
)
func main() {
flag.Parse()
aws, err := audit.NewAWS(*profile, *region)
if err != nil {
log.Fatal(err)
}
sgs, err := aws.DescribeSecurityGroups()
if err != nil {
log.Fatal(err)
}
ins, err := aws.DescribeInstances()
if err != nil {
log.Fatal(err)
}
sgmap := map[string]int{}
for _, in := range ins {
for _, sg := range in.SecurityGroups {
if _, ok := sgmap[*sg.GroupId]; !ok {
sgmap[*sg.GroupId] = 0
}
sgmap[*sg.GroupId]++
}
}
if *csv {
fmt.Println(audit.CSVHeader)
} else {
fmt.Printf("Got %d Security Groups, starting audit...\n", len(sgs))
}
for i, sg := range sgs {
// time.Sleep(time.Millisecond * 100)
if !*csv {
fmt.Printf("Audited %d Security Groups\r", i)
}
rs := audit.Audit(sg)
for _, r := range rs {
r.InstanceCount = sgmap[r.SecurityGroupID]
r.AddColor()
if *csv {
fmt.Print(r.String(audit.ResultFormatCSV))
} else if *nocolor {
fmt.Print(r.String(audit.ResultFormatLog))
} else {
fmt.Print(r.String(audit.ResultFormatLogColor))
}
}
}
if !*csv {
fmt.Printf("Audited %d Security Groups\n", len(sgs))
}
}