-
Notifications
You must be signed in to change notification settings - Fork 14
/
iamreport.go
100 lines (78 loc) · 1.88 KB
/
iamreport.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 report
import (
"fmt"
"github.com/Appliscale/cloud-security-audit/configuration"
"github.com/Appliscale/cloud-security-audit/resource"
"github.com/aws/aws-sdk-go/service/iam"
"strings"
)
type IAMReport struct {
UserName string
Groups string
InlinePolicies int
}
func NewIAMReport(u iam.UserDetail) *IAMReport {
return &IAMReport{
getUserName(u),
getUserGroups(u),
getNoOfUserInlinePolicies(u),
}
}
type IAMReports []*IAMReport
type IAMReportRequiredResources struct {
IAMInfo *resource.IAMInfo
}
func (i *IAMReports) GetHeaders() []string {
return []string{"User name", "Groups", "# of Inline\npolicies"}
}
func (i *IAMReports) FormatDataToTable() [][]string {
data := [][]string{}
for _, iamReport := range *i {
row := []string{
iamReport.UserName,
iamReport.Groups,
fmt.Sprintf("%d", iamReport.InlinePolicies),
}
data = append(data, row)
}
return data
}
func (i *IAMReports) GenerateReport(r *IAMReportRequiredResources) {
for _, user := range (*r.IAMInfo).GetUsers() {
iamReport := NewIAMReport(*user)
*i = append(*i, iamReport)
}
}
func (i *IAMReports) GetResources(config *configuration.Config) (*IAMReportRequiredResources, error) {
resources := &IAMReportRequiredResources{IAMInfo: &resource.IAMInfo{}}
for _, region := range *config.Regions {
err := resource.LoadResources(
config,
region,
resources.IAMInfo,
)
if err != nil {
return nil, err
}
}
return resources, nil
}
func getUserName(u iam.UserDetail) string {
return *u.UserName
}
func getUserGroups(u iam.UserDetail) string {
var grouplist []string
if len(u.GroupList) != 0 {
for _, group := range u.GroupList {
grouplist = append(grouplist, *group)
}
}
groups := "None"
if len(grouplist) > 0 {
groups = strings.Join(grouplist, ",")
}
return groups
}
func getNoOfUserInlinePolicies(u iam.UserDetail) int {
return len(u.UserPolicyList)
}