forked from rancher/external-dns
/
route53.go
224 lines (191 loc) · 5.91 KB
/
route53.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package route53
import (
"fmt"
"os"
"strings"
"github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
awsRoute53 "github.com/aws/aws-sdk-go/service/route53"
"github.com/juju/ratelimit"
"github.com/rancher/external-dns/providers"
"github.com/rancher/external-dns/utils"
)
var (
route53MaxRetries int = 3
)
type Route53Provider struct {
client *awsRoute53.Route53
hostedZoneId string
limiter *ratelimit.Bucket
}
func init() {
providers.RegisterProvider("route53", &Route53Provider{})
}
// Init creates a Route53 client with credentials from one of these
// two locations in that priority order:
// 1) Environment variables: AWS_ACCESS_KEY, AWS_SECRET_KEY
// 2) EC2 IAM role
func (r *Route53Provider) Init(rootDomainName string) error {
// Comply with the API's 5 req/s rate limit. If there are other
// clients using the same account the AWS SDK will throttle the
// requests automatically if the global rate limit is exhausted.
r.limiter = ratelimit.NewBucketWithRate(5.0, 1)
creds := credentials.NewChainCredentials(
[]credentials.Provider{
&credentials.EnvProvider{},
&ec2rolecreds.EC2RoleProvider{
Client: ec2metadata.New(session.Must(session.NewSession())),
},
})
config := aws.NewConfig().WithMaxRetries(route53MaxRetries).
WithCredentials(creds)
sess, err := session.NewSession(config)
if err != nil {
return fmt.Errorf("Failed to create Route53 session: %v", err)
}
r.client = awsRoute53.New(sess)
if err := r.setHostedZone(rootDomainName); err != nil {
return fmt.Errorf("Failed to configure hosted zone: %v", err)
}
logrus.Infof("Configured %s with hosted zone %s",
r.GetName(), rootDomainName)
return nil
}
func (r *Route53Provider) setHostedZone(rootDomainName string) error {
if envVal := os.Getenv("ROUTE53_ZONE_ID"); envVal != "" {
r.hostedZoneId = strings.TrimSpace(envVal)
if err := r.validateHostedZoneId(rootDomainName); err != nil {
return err
}
return nil
}
r.limiter.Wait(1)
params := &awsRoute53.ListHostedZonesByNameInput{
DNSName: aws.String(utils.UnFqdn(rootDomainName)),
MaxItems: aws.String("1"),
}
resp, err := r.client.ListHostedZonesByName(params)
if err != nil {
return fmt.Errorf("Could not list hosted zones: %v", err)
}
if len(resp.HostedZones) == 0 || *resp.HostedZones[0].Name != rootDomainName {
return fmt.Errorf("Hosted zone for '%s' not found", rootDomainName)
}
zoneId := *resp.HostedZones[0].Id
if strings.HasPrefix(zoneId, "/hostedzone/") {
zoneId = strings.TrimPrefix(zoneId, "/hostedzone/")
}
r.hostedZoneId = zoneId
return nil
}
func (r *Route53Provider) validateHostedZoneId(rootDomainName string) error {
r.limiter.Wait(1)
params := &awsRoute53.GetHostedZoneInput{
Id: aws.String(r.hostedZoneId),
}
resp, err := r.client.GetHostedZone(params)
if err != nil {
return fmt.Errorf("Could not look up hosted zone ID %s: %v",
r.hostedZoneId, err)
}
if *resp.HostedZone.Name != rootDomainName {
return fmt.Errorf("Hosted zone ID '%s' does not match name '%s'",
r.hostedZoneId, rootDomainName)
}
return nil
}
func (*Route53Provider) GetName() string {
return "Route 53"
}
func (r *Route53Provider) HealthCheck() error {
var params *awsRoute53.GetHostedZoneCountInput
_, err := r.client.GetHostedZoneCount(params)
return err
}
func (r *Route53Provider) AddRecord(record utils.DnsRecord) error {
return r.changeRecord(record, "UPSERT")
}
func (r *Route53Provider) UpdateRecord(record utils.DnsRecord) error {
return r.changeRecord(record, "UPSERT")
}
func (r *Route53Provider) RemoveRecord(record utils.DnsRecord) error {
return r.changeRecord(record, "DELETE")
}
func (r *Route53Provider) changeRecord(record utils.DnsRecord, action string) error {
r.limiter.Wait(1)
records := make([]*awsRoute53.ResourceRecord, len(record.Records))
for idx, value := range record.Records {
if record.Type == "TXT" {
value = `"` + value + `"`
}
records[idx] = &awsRoute53.ResourceRecord{
Value: aws.String(value),
}
}
params := &awsRoute53.ChangeResourceRecordSetsInput{
HostedZoneId: aws.String(r.hostedZoneId),
ChangeBatch: &awsRoute53.ChangeBatch{
Comment: aws.String("Managed by Rancher"),
Changes: []*awsRoute53.Change{
{
Action: aws.String(action),
ResourceRecordSet: &awsRoute53.ResourceRecordSet{
Name: aws.String(record.Fqdn),
Type: aws.String(record.Type),
TTL: aws.Int64(int64(record.TTL)),
ResourceRecords: records,
},
},
},
},
}
_, err := r.client.ChangeResourceRecordSets(params)
return err
}
func (r *Route53Provider) GetRecords() ([]utils.DnsRecord, error) {
r.limiter.Wait(1)
dnsRecords := []utils.DnsRecord{}
rrSets := []*awsRoute53.ResourceRecordSet{}
params := &awsRoute53.ListResourceRecordSetsInput{
HostedZoneId: aws.String(r.hostedZoneId),
MaxItems: aws.String("100"),
}
err := r.client.ListResourceRecordSetsPages(params,
func(page *awsRoute53.ListResourceRecordSetsOutput, lastPage bool) bool {
rrSets = append(rrSets, page.ResourceRecordSets...)
if !lastPage {
r.limiter.Wait(1)
}
return !lastPage
})
if err != nil {
return dnsRecords, fmt.Errorf("Route 53 API call has failed: %v", err)
}
for _, rrSet := range rrSets {
// skip proprietary Route 53 alias resource record sets
if rrSet.AliasTarget != nil {
logrus.Debug("Skipped Route53 alias RRset")
continue
}
records := []string{}
for _, rr := range rrSet.ResourceRecords {
value := *rr.Value
if *rrSet.Type == "TXT" {
value = strings.Trim(value, `"`)
}
records = append(records, value)
}
dnsRecord := utils.DnsRecord{
Fqdn: *rrSet.Name,
Records: records,
Type: *rrSet.Type,
TTL: int(*rrSet.TTL),
}
dnsRecords = append(dnsRecords, dnsRecord)
}
return dnsRecords, nil
}