-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoute53.go
337 lines (273 loc) · 8.67 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
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
package main
import (
// #include <stdbool.h>
// #include "cgo_functions.h"
// #include "dns.h"
// #include "fdw.h"
"C"
"fmt"
"strings"
"github.com/aws/aws-sdk-go/service/route53"
)
//export r53dbGoBeginScan
func r53dbGoBeginScan(scanState *C.char, hosted_zone_id_c *C.char) {
hosted_zone_id := C.GoString(hosted_zone_id_c)
StoreDNSResults(scanState, hosted_zone_id)
}
//export r53dbGoEndScan
func r53dbGoEndScan() {
// NOP
}
//export r53dbGoIterateScan
func r53dbGoIterateScan() {
// NOP, handled in C
}
func rrSetFromRow(row *C.r53dbDNSRR) *route53.ResourceRecordSet {
if row == nil {
return nil
}
r53rr := route53.ResourceRecordSet{
Name: GoCharStringPtr(row.name),
Type: GoStringPtr(C.GoString(row._type)),
}
if row.at_dns_name == nil {
r53rr.TTL = GoInt64Ptr(row.ttl)
r53rr.ResourceRecords = []*route53.ResourceRecord{
&route53.ResourceRecord{
Value: GoCharStringPtr(row.data),
},
}
} else {
r53rr.AliasTarget = &route53.AliasTarget{
EvaluateTargetHealth: GoBoolPtr(bool(row.at_evaluate_target_health)),
DNSName: GoCharStringPtr(row.at_dns_name),
HostedZoneId: GoCharStringPtr(row.at_hosted_zone_id),
}
}
return &r53rr
}
func rowsFromRRSet(rrset *route53.ResourceRecordSet) []*C.r53dbDNSRR {
var rows []*C.r53dbDNSRR
if rrset.AliasTarget != nil {
row := C.r53dbNewDNSRR()
row.name = C.CString(*rrset.Name)
row._type = C.CString(string(*rrset.Type))
row.at_dns_name = C.CString(*(*rrset.AliasTarget).DNSName)
row.at_hosted_zone_id = C.CString(*(*rrset.AliasTarget).HostedZoneId)
row.at_evaluate_target_health = (C.bool) (*(*rrset.AliasTarget).EvaluateTargetHealth)
rows = append(rows, row)
return rows
}
for _, rr := range rrset.ResourceRecords {
row := C.r53dbNewDNSRR()
row.name = C.CString(*rrset.Name)
row._type = C.CString(string(*rrset.Type))
row.ttl = (C.uint32_t) (int32(*rrset.TTL))
row.data = C.CString(*rr.Value)
rows = append(rows, row)
}
return rows
}
func getExistingRRSet(rname string, rtype string, hosted_zone_id string) *route53.ResourceRecordSet {
if !strings.HasSuffix(rname, ".") {
rname += "."
}
lhzInput := route53.ListResourceRecordSetsInput{
HostedZoneId: &hosted_zone_id,
StartRecordName: &rname,
StartRecordType: &rtype,
MaxItems: GoStringPtr("1"),
}
lhzReq, lhzResp := r53.ListResourceRecordSetsRequest(&lhzInput)
if err := lhzReq.Send(); err != nil {
error("ListResourceRecordSets: " + err.Error())
return nil;
}
if len(lhzResp.ResourceRecordSets) == 0 {
return nil;
}
// Route53 might return entries that are *after* what we're looking
// for. That means there was no match.
if *lhzResp.ResourceRecordSets[0].Name != rname {
return nil
}
if *lhzResp.ResourceRecordSets[0].Type != rtype {
return nil
}
return lhzResp.ResourceRecordSets[0]
}
func removeRRbyIndex(r []route53.ResourceRecord, index int) []route53.ResourceRecord {
// wtf... and also: thank you,
// https://stackoverflow.com/questions/37334119/how-to-delete-an-element-from-a-slice-in-golang
r[index] = r[len(r) - 1]
return r[:len(r) - 1]
}
func mergeWithExistingRRSet(
existingRRSet *route53.ResourceRecordSet,
newRow *route53.ResourceRecordSet,
oldRow *route53.ResourceRecordSet,
hosted_zone_id string,
op C.enum_r53dbDMLOp,
) *route53.ResourceRecordSet {
// Easy path: If there's no existingRRSet, then there's nothing to merge.
if op == C.DML_INSERT {
if existingRRSet == nil {
return newRow
}
}
// First, some sanity checks (none of those statements will 'return' successfully)
if op == C.DML_INSERT {
if existingRRSet.AliasTarget != nil {
error("Found existing RRSet with AliasTarget -- did you mean to UPDATE?")
return nil
}
}
if op == C.DML_UPDATE {
if *newRow.Name != *oldRow.Name {
error("r53db: RRSet Name cannot be changed in UPDATE")
return nil
}
if *newRow.Type != *oldRow.Type {
error("r53db: RRSet Type cannot be changed in UPDATE")
return nil
}
}
if op == C.DML_INSERT || op == C.DML_UPDATE {
if (existingRRSet.AliasTarget == nil) != (newRow.AliasTarget == nil) {
error("Cannot modify RRSet: Inconsistent AliasTarget use in new/existing data")
return nil
}
if newRow.AliasTarget == nil {
if *existingRRSet.TTL == *newRow.TTL {
if oldRow != nil {
*oldRow.TTL = *newRow.TTL
}
} else {
notice(fmt.Sprintf("Rows added to an existing RRSet cannot have a different TTL; " +
"using the RRSet's TTL (%d) instead", *existingRRSet.TTL))
}
}
}
// Sanity checks ok; perform operation...
if op == C.DML_INSERT {
existingRRSet.ResourceRecords = append(existingRRSet.ResourceRecords, newRow.ResourceRecords[0])
return existingRRSet
}
if op == C.DML_UPDATE && existingRRSet.AliasTarget != nil {
existingRRSet.AliasTarget.DNSName = newRow.AliasTarget.DNSName
existingRRSet.AliasTarget.HostedZoneId = newRow.AliasTarget.HostedZoneId
existingRRSet.AliasTarget.EvaluateTargetHealth = newRow.AliasTarget.EvaluateTargetHealth
return existingRRSet
}
if op == C.DML_DELETE && existingRRSet.AliasTarget != nil {
oldRow.AliasTarget = nil
return oldRow
}
// If control arrives here, we're either UPDATE or DELETE, without an Alias target.
// We check all existing RRs, removing and/or adding the RRs from
// UPDATE/DELETE as appropriate.
dataToRemove := oldRow.ResourceRecords[0].Value
replacedRRs := []*route53.ResourceRecord{}
for _, r := range existingRRSet.ResourceRecords {
if *r.Value != *dataToRemove {
replacedRRs = append(replacedRRs, r)
}
}
if newRow != nil {
replacedRRs = append(replacedRRs, newRow.ResourceRecords[0])
}
oldRow.ResourceRecords = replacedRRs
return oldRow
}
//export r53dbGoModifyDNSRR
func r53dbGoModifyDNSRR(cid *C.char, cNewRR *C.r53dbDNSRR, cOldRR *C.r53dbDNSRR, op C.enum_r53dbDMLOp) bool {
hosted_zone_id := C.GoString(cid)
newRow := rrSetFromRow(cNewRR)
oldRow := rrSetFromRow(cOldRR)
var rrsName string
var rrsType string
if newRow != nil {
rrsName, rrsType = *newRow.Name, *newRow.Type
} else {
rrsName, rrsType = *oldRow.Name, *oldRow.Type
}
existingRRSet := getExistingRRSet(rrsName, rrsType, hosted_zone_id)
mergedRRSet := mergeWithExistingRRSet(existingRRSet, newRow, oldRow, hosted_zone_id, op)
params := route53.ChangeResourceRecordSetsInput{
HostedZoneId: &hosted_zone_id,
ChangeBatch: &route53.ChangeBatch{
Changes: []*route53.Change{
&route53.Change{
Action: GoStringPtr("UPSERT"),
ResourceRecordSet: mergedRRSet,
},
},
},
}
debug(fmt.Sprintf("Merged RRSet for Modify operation: %v", mergedRRSet))
// Special case: if the RRSet is empty afterwards, Action will be DELETE;
// but we need to provide the original RRSet, because the Route53 API checks
// all ResourceRecords (it won't allow an empty ResourceRecords list).
// As a hack, we use the same logic to DELETE an AliasTarget entry.
if op == C.DML_DELETE && len(mergedRRSet.ResourceRecords) == 0 && mergedRRSet.AliasTarget == nil {
debug(fmt.Sprintf("RRSet %s is empty -- DELETEing the whole RRSet", *mergedRRSet.Name))
params.ChangeBatch.Changes[0].Action = GoStringPtr("DELETE")
params.ChangeBatch.Changes[0].ResourceRecordSet = existingRRSet
}
req, _ := r53.ChangeResourceRecordSetsRequest(¶ms)
if err := req.Send(); err != nil {
error("ChangeResourceRecordSets: " + err.Error())
return false
}
return true
}
//export r53dbGoGetZones
func r53dbGoGetZones(zoneList *C.char) *C.char {
var marker *string = nil
var truncated = true
for truncated {
lhzReq, lhzResp := r53.ListHostedZonesRequest(&route53.ListHostedZonesInput{
Marker: marker,
})
if err := lhzReq.Send(); err != nil {
error("ListHostedZones: " + err.Error())
}
for _, zone := range lhzResp.HostedZones {
czone := C.r53dbNewZone()
czone.id = C.CString(*zone.Id)
czone.name = C.CString(*zone.Name)
zoneList = C.r53dbStoreZone(zoneList, czone)
}
truncated = *lhzResp.IsTruncated
marker = lhzResp.NextMarker
}
return zoneList
}
func StoreDNSResults(scanState *C.char, hosted_zone_id string) {
var nextRName *string
var nextRType *string
var nextRIdent *string
var truncated = true
for truncated {
rrReq, rrResp := r53.ListResourceRecordSetsRequest(&route53.ListResourceRecordSetsInput{
HostedZoneId: &hosted_zone_id,
StartRecordName: nextRName,
StartRecordType: nextRType,
StartRecordIdentifier: nextRIdent,
MaxItems: GoStringPtr("2"),
})
if err := rrReq.Send(); err != nil {
error("r53db: StoreDNSResults(), ListResourceRecordSets: " + err.Error())
return
}
for _, rrset := range rrResp.ResourceRecordSets {
for _, row := range rowsFromRRSet(rrset) {
C.r53dbStoreResult(scanState, row)
}
}
truncated = *rrResp.IsTruncated
nextRName = rrResp.NextRecordName
nextRType = rrResp.NextRecordType
nextRIdent = rrResp.NextRecordIdentifier
}
}