-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
security_hub.go
148 lines (127 loc) · 4.77 KB
/
security_hub.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
package resources
import (
"context"
"github.com/aws/aws-sdk-go/aws"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/securityhub"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
"github.com/gruntwork-io/cloud-nuke/util"
"github.com/gruntwork-io/go-commons/errors"
"strings"
)
func (sh *SecurityHub) getAll(c context.Context, configObj config.Config) ([]*string, error) {
var securityHubArns []*string
output, err := sh.Client.DescribeHub(&securityhub.DescribeHubInput{})
if err != nil {
// If Security Hub is not enabled when we call DescribeHub, we get back an error
// so we should ignore the error if it's just telling us the account/region is not
// subscribed and return nil to indicate there are no resources to nuke
if strings.Contains(err.Error(), "is not subscribed to AWS Security Hub") {
return nil, nil
}
return nil, errors.WithStackTrace(err)
}
if shouldIncludeHub(output, configObj) {
securityHubArns = append(securityHubArns, output.HubArn)
}
return securityHubArns, nil
}
func shouldIncludeHub(hub *securityhub.DescribeHubOutput, configObj config.Config) bool {
subscribedAt, err := util.ParseTimestamp(hub.SubscribedAt)
if err != nil {
logging.Debugf(
"Could not parse subscribedAt timestamp (%s) of security hub. Excluding from delete.", *hub.SubscribedAt)
return false
}
return configObj.SecurityHub.ShouldInclude(config.ResourceValue{Time: subscribedAt})
}
func (sh *SecurityHub) getAllSecurityHubMembers() ([]*string, error) {
var hubMemberAccountIds []*string
// OnlyAssociated=false input parameter includes "pending" invite members
members, err := sh.Client.ListMembers(&securityhub.ListMembersInput{OnlyAssociated: aws.Bool(false)})
if err != nil {
return nil, errors.WithStackTrace(err)
}
for _, member := range members.Members {
hubMemberAccountIds = append(hubMemberAccountIds, member.AccountId)
}
for awsgo.StringValue(members.NextToken) != "" {
members, err = sh.Client.ListMembers(&securityhub.ListMembersInput{NextToken: members.NextToken})
if err != nil {
return nil, errors.WithStackTrace(err)
}
for _, member := range members.Members {
hubMemberAccountIds = append(hubMemberAccountIds, member.AccountId)
}
}
logging.Debugf("Found %d member accounts attached to security hub", len(hubMemberAccountIds))
return hubMemberAccountIds, nil
}
func (sh *SecurityHub) removeMembersFromHub(accountIds []*string) error {
// Member accounts must first be disassociated
_, err := sh.Client.DisassociateMembers(&securityhub.DisassociateMembersInput{AccountIds: accountIds})
if err != nil {
return err
}
logging.Debugf("%d member accounts disassociated", len(accountIds))
// Once disassociated, member accounts can be deleted
_, err = sh.Client.DeleteMembers(&securityhub.DeleteMembersInput{AccountIds: accountIds})
if err != nil {
return err
}
logging.Debugf("%d member accounts deleted", len(accountIds))
return nil
}
func (sh *SecurityHub) nukeAll(securityHubArns []string) error {
if len(securityHubArns) == 0 {
logging.Debugf("No security hub resources to nuke in region %s", sh.Region)
return nil
}
// Check for any member accounts in security hub
// Security Hub cannot be disabled with active member accounts
memberAccountIds, err := sh.getAllSecurityHubMembers()
if err != nil {
return err
}
// Remove any member accounts if they exist
if err == nil && len(memberAccountIds) > 0 {
err = sh.removeMembersFromHub(memberAccountIds)
if err != nil {
logging.Errorf("[Failed] Failed to disassociate members from security hub")
}
}
// Check for an administrator account
// Security hub cannot be disabled with an active administrator account
adminAccount, err := sh.Client.GetAdministratorAccount(&securityhub.GetAdministratorAccountInput{})
if err != nil {
logging.Errorf("[Failed] Failed to check for administrator account")
}
// Disassociate administrator account if it exists
if adminAccount.Administrator != nil {
_, err := sh.Client.DisassociateFromAdministratorAccount(&securityhub.DisassociateFromAdministratorAccountInput{})
if err != nil {
logging.Errorf("[Failed] Failed to disassociate from administrator account")
}
}
// Disable security hub
_, err = sh.Client.DisableSecurityHub(&securityhub.DisableSecurityHubInput{})
if err != nil {
logging.Errorf("[Failed] Failed to disable security hub.")
e := report.Entry{
Identifier: aws.StringValue(&securityHubArns[0]),
ResourceType: "Security Hub",
Error: err,
}
report.Record(e)
} else {
logging.Debugf("[OK] Security Hub %s disabled", securityHubArns[0])
e := report.Entry{
Identifier: aws.StringValue(&securityHubArns[0]),
ResourceType: "Security Hub",
}
report.Record(e)
}
return nil
}