forked from barnybug/cli53
/
bind.go
400 lines (383 loc) · 10.1 KB
/
bind.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package cli53
import (
"fmt"
"net"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/miekg/dns"
)
func parseComment(rr dns.RR, comment string) dns.RR {
if strings.HasPrefix(comment, "; AWS ") {
kvs, err := ParseKeyValues(comment[6:])
if err == nil {
routing := kvs.GetString("routing")
if fn, ok := RoutingTypes[routing]; ok {
route := fn()
route.Parse(kvs)
rr = &AWSRR{
rr,
route,
kvs.GetOptString("healthCheckId"),
kvs.GetString("identifier"),
}
} else {
fmt.Printf("Warning: parse AWS extension - routing=\"%s\" not understood\n", routing)
}
} else {
fmt.Println("Warning: parse AWS extension: %s", err)
}
}
return rr
}
func parseBindFile(file, origin string) []dns.RR {
rdr, err := os.Open(file)
fatalIfErr(err)
tokensch := dns.ParseZone(rdr, origin, file)
records := []dns.RR{}
for token := range tokensch {
if token.Error != nil {
fatalIfErr(token.Error)
}
record := parseComment(token.RR, token.Comment)
records = append(records, record)
}
return records
}
// Convert a DNS record into a route53 ResourceRecord.
func ConvertBindToRR(record dns.RR) []*route53.ResourceRecord {
switch record := record.(type) {
case *dns.A:
rr := &route53.ResourceRecord{
Value: aws.String(record.A.String()),
}
return []*route53.ResourceRecord{rr}
case *dns.AAAA:
rr := &route53.ResourceRecord{
Value: aws.String(record.AAAA.String()),
}
return []*route53.ResourceRecord{rr}
case *dns.CNAME:
rr := &route53.ResourceRecord{
Value: aws.String(record.Target),
}
return []*route53.ResourceRecord{rr}
case *dns.MX:
value := fmt.Sprintf("%d %s", record.Preference, record.Mx)
rr := &route53.ResourceRecord{
Value: aws.String(value),
}
return []*route53.ResourceRecord{rr}
case *dns.NS:
rr := &route53.ResourceRecord{
Value: aws.String(record.Ns),
}
return []*route53.ResourceRecord{rr}
case *dns.PTR:
rr := &route53.ResourceRecord{
Value: aws.String(record.Ptr),
}
return []*route53.ResourceRecord{rr}
case *dns.SOA:
value := fmt.Sprintf("%s %s %d %d %d %d %d", record.Ns, record.Mbox, record.Serial, record.Refresh, record.Retry, record.Expire, record.Minttl)
rr := &route53.ResourceRecord{
Value: aws.String(value),
}
return []*route53.ResourceRecord{rr}
case *dns.SPF:
rrs := []*route53.ResourceRecord{}
for _, txt := range record.Txt {
rr := &route53.ResourceRecord{
Value: aws.String(`"` + txt + `"`),
}
rrs = append(rrs, rr)
}
return rrs
case *dns.SRV:
value := fmt.Sprintf("%d %d %d %s", record.Priority, record.Weight, record.Port, record.Target)
rr := &route53.ResourceRecord{
Value: aws.String(value),
}
return []*route53.ResourceRecord{rr}
case *dns.TXT:
rrs := []*route53.ResourceRecord{}
for _, txt := range record.Txt {
rr := &route53.ResourceRecord{
Value: aws.String(`"` + txt + `"`),
}
rrs = append(rrs, rr)
}
return rrs
default:
errorAndExit(fmt.Sprintf("Unsupported resource record: %s", record))
}
return []*route53.ResourceRecord{}
}
func ConvertAliasToRRSet(alias *dns.PrivateRR) *route53.ResourceRecordSet {
// AWS ALIAS extension record
hdr := alias.Header()
rdata := alias.Data.(*ALIAS)
return &route53.ResourceRecordSet{
Type: aws.String(rdata.Type),
Name: aws.String(hdr.Name),
AliasTarget: &route53.AliasTarget{
DNSName: aws.String(rdata.Target),
HostedZoneId: aws.String(rdata.ZoneId),
EvaluateTargetHealth: aws.Bool(rdata.EvaluateTargetHealth),
},
}
}
// Convert some DNS records into a route53 ResourceRecordSet. The records should have been
// previously grouped by matching name, type and (if applicable) identifier.
func ConvertBindToRRSet(records []dns.RR) *route53.ResourceRecordSet {
rrs := []*route53.ResourceRecord{}
for _, record := range records {
if rr, ok := record.(*dns.PrivateRR); ok {
return ConvertAliasToRRSet(rr)
} else if rec, ok := record.(*AWSRR); ok {
rrs = append(rrs, ConvertBindToRR(rec.RR)...)
} else {
rrs = append(rrs, ConvertBindToRR(record)...)
}
}
if len(rrs) > 0 {
hdr := records[0].Header()
rrset := &route53.ResourceRecordSet{
Type: aws.String(dns.Type(hdr.Rrtype).String()),
Name: aws.String(hdr.Name),
ResourceRecords: rrs,
TTL: aws.Int64(int64(hdr.Ttl)),
}
if awsrr, ok := records[0].(*AWSRR); ok {
switch route := awsrr.Route.(type) {
case *FailoverRoute:
rrset.Failover = aws.String(route.Failover)
case *GeoLocationRoute:
rrset.GeoLocation = &route53.GeoLocation{
CountryCode: route.CountryCode,
ContinentCode: route.ContinentCode,
SubdivisionCode: route.SubdivisionCode,
}
case *LatencyRoute:
rrset.Region = aws.String(route.Region)
case *WeightedRoute:
rrset.Weight = aws.Int64(route.Weight)
}
if awsrr.HealthCheckId != nil {
rrset.HealthCheckId = awsrr.HealthCheckId
}
rrset.SetIdentifier = aws.String(awsrr.Identifier)
}
return rrset
}
return nil
}
func ConvertRRSetToBind(rrset *route53.ResourceRecordSet) []dns.RR {
ret := []dns.RR{}
// A record either has resource records or is an alias.
// Optionally a routing policy can apply which will can be:
// - failover
// - geolocation
// - latency
// - weighted
name := unescaper.Replace(*rrset.Name)
// Only resource records without routing can be represented in vanilla bind.
if rrset.AliasTarget != nil {
alias := rrset.AliasTarget
dnsrr := &dns.PrivateRR{
Hdr: dns.RR_Header{
Name: name,
Rrtype: TypeALIAS,
Class: ClassAWS,
Ttl: 86400,
},
Data: &ALIAS{
*rrset.Type,
*alias.DNSName,
*alias.HostedZoneId,
*alias.EvaluateTargetHealth,
},
}
ret = append(ret, dnsrr)
} else {
switch *rrset.Type {
case "A":
for _, rr := range rrset.ResourceRecords {
dnsrr := &dns.A{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: uint32(*rrset.TTL),
},
A: net.ParseIP(*rr.Value),
}
ret = append(ret, dnsrr)
}
case "AAAA":
for _, rr := range rrset.ResourceRecords {
dnsrr := &dns.AAAA{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: uint32(*rrset.TTL),
},
AAAA: net.ParseIP(*rr.Value),
}
ret = append(ret, dnsrr)
}
case "CNAME":
for _, rr := range rrset.ResourceRecords {
dnsrr := &dns.CNAME{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeCNAME,
Class: dns.ClassINET,
Ttl: uint32(*rrset.TTL),
},
Target: *rr.Value,
}
ret = append(ret, dnsrr)
}
case "MX":
// parse value
for _, rr := range rrset.ResourceRecords {
var preference uint16
var value string
fmt.Sscanf(*rr.Value, "%d %s", &preference, &value)
dnsrr := &dns.MX{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeMX,
Class: dns.ClassINET,
Ttl: uint32(*rrset.TTL),
},
Mx: value,
Preference: preference,
}
ret = append(ret, dnsrr)
}
case "NS":
for _, rr := range rrset.ResourceRecords {
dnsrr := &dns.NS{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: uint32(*rrset.TTL),
},
Ns: *rr.Value,
}
ret = append(ret, dnsrr)
}
case "PTR":
for _, rr := range rrset.ResourceRecords {
dnsrr := &dns.PTR{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypePTR,
Class: dns.ClassINET,
Ttl: uint32(*rrset.TTL),
},
Ptr: *rr.Value,
}
ret = append(ret, dnsrr)
}
case "SOA":
for _, rr := range rrset.ResourceRecords {
// parse value
var ns, mbox string
var serial, refresh, retry, expire, minttl uint32
fmt.Sscanf(*rr.Value, "%s %s %d %d %d %d %d", &ns, &mbox, &serial, &refresh, &retry, &expire, &minttl)
dnsrr := &dns.SOA{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeSOA,
Class: dns.ClassINET,
Ttl: uint32(*rrset.TTL),
},
Ns: ns,
Mbox: mbox,
Serial: serial,
Refresh: refresh,
Retry: retry,
Expire: expire,
Minttl: minttl,
}
ret = append(ret, dnsrr)
}
case "SPF":
// SPF records are unusual in that multiple values are stored as a single bind record.
txt := []string{}
for _, rr := range rrset.ResourceRecords {
txt = append(txt, unquote(*rr.Value))
}
dnsrr := &dns.SPF{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeSPF,
Class: dns.ClassINET,
Ttl: uint32(*rrset.TTL),
},
Txt: txt,
}
ret = append(ret, dnsrr)
case "SRV":
for _, rr := range rrset.ResourceRecords {
// parse value
var priority, weight, port uint16
var target string
fmt.Sscanf(*rr.Value, "%d %d %d %s", &priority, &weight, &port, &target)
dnsrr := &dns.SRV{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeSRV,
Class: dns.ClassINET,
Ttl: uint32(*rrset.TTL),
},
Priority: priority,
Weight: weight,
Port: port,
Target: target,
}
ret = append(ret, dnsrr)
}
case "TXT":
// TXT records are unusual in that multiple values are stored as a single bind record.
txt := []string{}
for _, rr := range rrset.ResourceRecords {
txt = append(txt, unquote(*rr.Value))
}
dnsrr := &dns.TXT{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeTXT,
Class: dns.ClassINET,
Ttl: uint32(*rrset.TTL),
},
Txt: txt,
}
ret = append(ret, dnsrr)
}
}
var route AWSRoute
if rrset.Failover != nil {
route = &FailoverRoute{*rrset.Failover}
} else if rrset.Weight != nil {
route = &WeightedRoute{*rrset.Weight}
} else if rrset.Region != nil {
route = &LatencyRoute{*rrset.Region}
} else if rrset.GeoLocation != nil {
route = &GeoLocationRoute{rrset.GeoLocation.CountryCode, rrset.GeoLocation.ContinentCode, rrset.GeoLocation.SubdivisionCode}
}
if route != nil {
for i, rr := range ret {
// convert any records with AWS extensions into an AWSRR record
awsrr := &AWSRR{rr, route, rrset.HealthCheckId, *rrset.SetIdentifier}
ret[i] = awsrr
}
}
return ret
}