-
Notifications
You must be signed in to change notification settings - Fork 4
/
acm.go
133 lines (105 loc) · 2.81 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
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
package awsu
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/acm"
"github.com/isan-rivkin/surf/lib/common"
log "github.com/sirupsen/logrus"
)
type _acmAsyncRes struct {
C *acm.CertificateDetail
Err error
}
type ACMFilter = func(c *acm.CertificateDetail) bool
type ACMResult struct {
Certificates []*acm.CertificateDetail
}
type AcmAPI interface {
ListAndFilter(parallel int, describe bool, filter ACMFilter) (*ACMResult, error)
}
type AcmClient struct {
c *acm.ACM
}
func NewAcmClient(c *acm.ACM) AcmAPI {
return &AcmClient{
c: c,
}
}
func (a *AcmClient) client() *acm.ACM {
return a.c
}
func (a *AcmClient) GetAll() ([]*acm.CertificateSummary, error) {
result := []*acm.CertificateSummary{}
input := &acm.ListCertificatesInput{}
err := a.client().ListCertificatesPages(input,
func(page *acm.ListCertificatesOutput, lastPage bool) bool {
result = append(result, page.CertificateSummaryList...)
return !lastPage
})
return result, err
}
func (a *AcmClient) ListAndFilter(parallel int, describe bool, filter ACMFilter) (*ACMResult, error) {
result := &ACMResult{
Certificates: []*acm.CertificateDetail{},
}
filteredResult := &ACMResult{
Certificates: []*acm.CertificateDetail{},
}
certsSummary, err := a.GetAll()
if err != nil {
return nil, err
}
pool := common.NewWorkerPool(parallel)
asyncResults := make(chan *_acmAsyncRes, len(certsSummary))
for _, cs := range certsSummary {
certArn := aws.StringValue(cs.CertificateArn)
if describe {
pool.Submit(func() {
reqInput := &acm.DescribeCertificateInput{
CertificateArn: aws.String(certArn),
}
req, out := a.client().DescribeCertificateRequest(reqInput)
var cert *acm.CertificateDetail
err := req.Send()
if err != nil || out == nil {
log.WithField("arn", certArn).
WithError(err).
Error("failed describing cert")
} else {
cert = out.Certificate
}
asyncResults <- &_acmAsyncRes{C: cert, Err: err}
})
} else {
cert := &acm.CertificateDetail{CertificateArn: aws.String(certArn)}
result.Certificates = append(result.Certificates, cert)
}
}
if describe {
pool.RunAll()
size := len(certsSummary)
counter := 1
for r := range asyncResults {
if counter >= size {
break
}
counter++
if r.Err != nil {
continue
}
if isMatch := filter(r.C); isMatch {
filteredResult.Certificates = append(filteredResult.Certificates, r.C)
}
}
} else {
for _, cert := range result.Certificates {
if isMatch := filter(cert); isMatch {
filteredResult.Certificates = append(filteredResult.Certificates, cert)
}
}
}
return filteredResult, err
}
func GenerateACMWebURL(region, certId string) string {
return fmt.Sprintf("https://console.aws.amazon.com/acm/home?region=%s#/certificates/%s", region, certId)
}