-
-
Notifications
You must be signed in to change notification settings - Fork 358
/
acm.go
96 lines (81 loc) · 2.54 KB
/
acm.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
package resources
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/service/acm"
"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/go-commons/errors"
)
// Returns a list of strings of ACM ARNs
func (a *ACM) getAll(c context.Context, configObj config.Config) ([]*string, error) {
params := &acm.ListCertificatesInput{}
acmArns := []*string{}
err := a.Client.ListCertificatesPages(params,
func(page *acm.ListCertificatesOutput, lastPage bool) bool {
for i := range page.CertificateSummaryList {
logging.Debug(fmt.Sprintf("Found ACM %s with domain name %s",
*page.CertificateSummaryList[i].CertificateArn, *page.CertificateSummaryList[i].DomainName))
if a.shouldInclude(page.CertificateSummaryList[i], configObj) {
logging.Debug(fmt.Sprintf(
"Including ACM %s", *page.CertificateSummaryList[i].CertificateArn))
acmArns = append(acmArns, page.CertificateSummaryList[i].CertificateArn)
} else {
logging.Debug(fmt.Sprintf(
"Skipping ACM %s", *page.CertificateSummaryList[i].CertificateArn))
}
}
return !lastPage
},
)
if err != nil {
return nil, errors.WithStackTrace(err)
}
return acmArns, nil
}
func (a *ACM) shouldInclude(acm *acm.CertificateSummary, configObj config.Config) bool {
if acm == nil {
return false
}
if acm.InUse != nil && *acm.InUse {
logging.Debug(fmt.Sprintf("ACM %s is in use", *acm.CertificateArn))
return false
}
shouldInclude := configObj.ACM.ShouldInclude(config.ResourceValue{
Name: acm.DomainName,
Time: acm.CreatedAt,
})
logging.Debug(fmt.Sprintf("shouldInclude result for ACM: %s w/ domain name: %s, time: %s, and config: %+v",
*acm.CertificateArn, *acm.DomainName, acm.CreatedAt, configObj.ACM))
return shouldInclude
}
// Deletes all ACMs
func (a *ACM) nukeAll(arns []*string) error {
if len(arns) == 0 {
logging.Debugf("No ACMs to nuke in region %s", a.Region)
return nil
}
logging.Debugf("Deleting all ACMs in region %s", a.Region)
deletedCount := 0
for _, acmArn := range arns {
params := &acm.DeleteCertificateInput{
CertificateArn: acmArn,
}
_, err := a.Client.DeleteCertificate(params)
if err != nil {
logging.Debugf("[Failed] %s", err)
} else {
deletedCount++
logging.Debugf("Deleted ACM: %s", *acmArn)
}
e := report.Entry{
Identifier: *acmArn,
ResourceType: "ACM",
Error: err,
}
report.Record(e)
}
logging.Debugf("[OK] %d ACM(s) terminated in %s", deletedCount, a.Region)
return nil
}