-
Notifications
You must be signed in to change notification settings - Fork 0
/
alb.go
187 lines (145 loc) · 4.22 KB
/
alb.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package certutils
import (
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/olekukonko/tablewriter"
)
type ALB struct {
client *elbv2.ELBV2
}
type ALBDescription struct {
name string
dnsname string
certs []ALBCertificate
}
type ALBCertificate struct {
arn string
port int64
listenerArn string
}
func NewALB(sess *session.Session) *ALB {
return &ALB{
client: elbv2.New(sess),
}
}
func createALBDescribeListenersInput(lbArn string) *elbv2.DescribeListenersInput {
input := &elbv2.DescribeListenersInput{}
input.SetLoadBalancerArn(lbArn)
return input
}
func (alb *ALB) getLBs(certFilter string) ([]ALBDescription, error) {
input := &elbv2.DescribeLoadBalancersInput{}
out, err := alb.client.DescribeLoadBalancers(input)
lbs := make([]ALBDescription, 0, len(out.LoadBalancers))
for _, lb := range out.LoadBalancers {
albdesc := ALBDescription{}
albdesc.dnsname = *lb.DNSName
albdesc.name = *lb.LoadBalancerName
lout, err := alb.client.DescribeListeners(createALBDescribeListenersInput(*lb.LoadBalancerArn))
if err != nil {
return []ALBDescription{}, err
}
for _, l := range lout.Listeners {
for _, cert := range l.Certificates {
if certFilter != "" && certFilter != *cert.CertificateArn {
continue
}
albcert := ALBCertificate{
arn: *cert.CertificateArn,
port: *l.Port,
listenerArn: *l.ListenerArn,
}
albdesc.certs = append(albdesc.certs, albcert)
}
}
if len(albdesc.certs) < 1 {
continue
}
lbs = append(lbs, albdesc)
}
return lbs, err
}
func (alb *ALB) List(certFilter string) ([]ALBDescription, error) {
return alb.getLBs(certFilter)
}
func (alb *ALB) getListener(name string) (*elbv2.Listener, error) {
lbinput := &elbv2.DescribeLoadBalancersInput{}
names := []string{name}
lbinput.SetNames(aws.StringSlice(names))
lbout, err := alb.client.DescribeLoadBalancers(lbinput)
if err != nil {
return &elbv2.Listener{}, err
}
if len(lbout.LoadBalancers) < 1 {
return &elbv2.Listener{}, fmt.Errorf("Listener not found")
}
linput := &elbv2.DescribeListenersInput{}
linput.SetLoadBalancerArn(*lbout.LoadBalancers[0].LoadBalancerArn)
lout, err := alb.client.DescribeListeners(linput)
if err != nil {
return &elbv2.Listener{}, err
}
if len(lout.Listeners) < 1 {
return &elbv2.Listener{}, fmt.Errorf("Listener not found")
}
return lout.Listeners[0], nil
}
func createALBModifyListenerInput(listenerArn, certArn string) *elbv2.ModifyListenerInput {
input := &elbv2.ModifyListenerInput{}
cert := &elbv2.Certificate{}
cert.SetCertificateArn(certArn)
certs := []*elbv2.Certificate{cert}
input.SetCertificates(certs)
input.SetListenerArn(listenerArn)
return input
}
func (alb *ALB) Update(name string, certArn string) error {
l, err := alb.getListener(name)
if err != nil {
return err
}
_, err = alb.client.ModifyListener(createALBModifyListenerInput(*l.ListenerArn, certArn))
return err
}
func albUpdateMsg(name string, port int64, src, dest string) string {
return fmt.Sprintf("Updated %s:%d %s -> %s", name, port, src, dest)
}
func (alb *ALB) BulkUpdate(srcCertArn, destCertArn string, dryRun bool) ([]string, error) {
lbs, err := alb.getLBs(srcCertArn)
if err != nil {
return []string{}, err
}
updates := make([]string, 0)
if dryRun {
updates = append(updates, dryRunMsg()...)
}
for _, lb := range lbs {
for _, cert := range lb.certs {
if dryRun {
updates = append(updates, albUpdateMsg(lb.name, cert.port, srcCertArn, destCertArn))
} else {
_, err := alb.client.ModifyListener(createALBModifyListenerInput(cert.listenerArn, destCertArn))
if err != nil {
return []string{}, err
}
updates = append(updates, albUpdateMsg(lb.name, cert.port, srcCertArn, destCertArn))
}
}
}
return updates, nil
}
func (alb *ALB) ReadableList(descs []ALBDescription) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Port", "Listener SSL Certificate"})
table.SetAutoMergeCells(true)
table.SetRowLine(true)
for _, desc := range descs {
for _, cert := range desc.certs {
table.Append([]string{desc.name, fmt.Sprint(cert.port), cert.arn})
}
}
table.Render()
}