/
zones.go
290 lines (258 loc) · 6.92 KB
/
zones.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
package netbox2dns
import (
"fmt"
"net/netip"
"sort"
"strings"
log "github.com/golang/glog"
"github.com/scottlaird/netboxlib/netbox"
)
// ByLength is a wrapper for []string for sorting the string
// slice by length, from longest to shortest.
type ByLength []*Zone
func (a ByLength) Len() int {
return len(a)
}
func (a ByLength) Less(i, j int) bool {
return len(a[i].Name) > len(a[j].Name)
}
func (a ByLength) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
// Zones represents the set of all DNS zones known to netbox2dns.
type Zones struct {
Zones map[string]*Zone
sortedZones []*Zone
}
// NewZones creates a new Zones structure and initializes it.
func NewZones() *Zones {
return &Zones{
Zones: make(map[string]*Zone),
}
}
// AddRecord adds a record to the appropriate zone. It finds the
// longest suffix match among all known zones and adds the new record
// there. If no zones match, then an error is returned.
func (z *Zones) AddRecord(r *Record) error {
for _, zone := range z.sortedZones {
if strings.HasSuffix(r.Name, zone.Name+".") {
zone.AddRecord(r)
return nil
}
}
return fmt.Errorf("Can't find zone matching record %q in %v", r.Name, z.sortedZones)
}
// AddZone adds a new Zone to Zones.
func (z *Zones) AddZone(zone *Zone) {
z.Zones[zone.Name] = zone
z.sortZones()
}
// NewZone creates a new Zone in Zones using the settings in the
// provided ConfigZone. The resulting Zone is added to Zones
// automatically.
func (z *Zones) NewZone(cz *ConfigZone) {
zone := Zone{
Name: cz.Name,
ZoneName: cz.ZoneName,
Project: cz.Project,
Filename: cz.Filename,
DeleteEntries: cz.DeleteEntries,
TTL: cz.TTL,
Records: make(map[string][]*Record),
}
z.AddZone(&zone)
}
// sortZones sorts zones from longest to shortest and populates `sortedZones`.
func (z *Zones) sortZones() {
zones := make([]*Zone, len(z.Zones))
i := 0
for _, zone := range z.Zones {
zones[i] = zone
i++
}
sort.Sort(ByLength(zones))
z.sortedZones = zones
}
// Compare compares two Zones structures and returns a slice of
// ZoneDeltas showing what has changed.
func (z *Zones) Compare(newer *Zones) []*ZoneDelta {
zones := make(map[string]bool)
deltas := []*ZoneDelta{}
// Create union of zones in z and newer
for k := range z.Zones {
zones[k] = true
}
for k := range newer.Zones {
zones[k] = true
}
for k := range zones {
if z.Zones[k] == nil {
// Only in 'newer'
fmt.Printf("*** Added Zone %q\n", k)
} else if newer.Zones[k] == nil {
// Only in 'z'
fmt.Printf("*** Removed Zone %q\n", k)
} else {
zd := z.Zones[k].NewZoneDelta()
z.Zones[k].Compare(newer.Zones[k], zd)
deltas = append(deltas, zd)
}
}
return deltas
}
// Zone represents a single DNS zone on a single provider (Google
// Cloud DNS, fixed zone files, etc).
type Zone struct {
Name string
ZoneName string
Project string
Filename string
DeleteEntries bool
TTL int64
Records map[string][]*Record
}
// AddRecord adds a single record to this zone. It does not check
// that this is the correct zone for the record.
func (z *Zone) AddRecord(r *Record) {
if r.TTL == 0 {
r.TTL = z.TTL
}
z.Records[r.Name] = append(z.Records[r.Name], r)
}
// Compare compares two Zone structures and updates a ZoneDelta with
// changes.
func (z *Zone) Compare(newer *Zone, zd *ZoneDelta) {
records := make(map[string]bool)
// Create union of zones in z and newer
for k := range z.Records {
records[k] = true
}
for k := range newer.Records {
records[k] = true
}
for k := range records {
if z.Records[k] == nil {
// Only in 'newer'
zd.AddRecords[k] = newer.Records[k]
} else if newer.Records[k] == nil {
// Only in 'z'
if z.DeleteEntries {
zd.RemoveRecords[k] = z.Records[k]
}
} else {
CompareRecordSets(z.Records[k], newer.Records[k], zd)
}
}
}
// NewZoneDelta creates a new ZoneDelta. This is used to track
// changes between versions of a DNS zone.
func (z *Zone) NewZoneDelta() *ZoneDelta {
zd := &ZoneDelta{
Name: z.Name,
ZoneName: z.ZoneName,
Project: z.Project,
Filename: z.Filename,
AddRecords: make(map[string][]*Record),
RemoveRecords: make(map[string][]*Record),
}
return zd
}
// ZoneDelta describes the difference between two versions of the same
// zone. It shows added and removed records.
type ZoneDelta struct {
Name string
ZoneName string
Project string
Filename string
AddRecords map[string][]*Record
RemoveRecords map[string][]*Record
}
// CompareRecordSets compares sets of records and updates a ZoneDelta
// with results.
func CompareRecordSets(older []*Record, newer []*Record, zd *ZoneDelta) {
// So, let's start by looking for identical Records.
o := make([]string, len(older))
n := make([]string, len(newer))
for i, r := range older {
o[i] = fmt.Sprintf("%+v", r)
}
for i, r := range newer {
n[i] = fmt.Sprintf("%+v", r)
}
// Now, let's start by removing duplicates. These sets should
// be small, so O(N^2) is fine.
for i, r := range o {
for j, s := range n {
if r == s {
// Duplicate! Remove from each set.
o[i] = ""
n[j] = ""
}
}
}
// At this point, any non-"" entries in o or n are actual deltas.
for i, r := range o {
if r != "" {
name := older[i].Name
zd.RemoveRecords[name] = append(zd.RemoveRecords[name], older[i])
}
}
for i, r := range n {
if r != "" {
name := newer[i].Name
zd.AddRecords[name] = append(zd.AddRecords[name], newer[i])
}
}
}
// ReverseName takes an IP address and returns the correct reverse DNS
// name for that IP. It maps IPv4 addresses into `in-addr.arpa` and
// IPv6 addresses into `ip6.arpa`.
func ReverseName(addr netip.Addr) string {
if addr.Is4() {
return reverseName4(addr)
}
return reverseName6(addr)
}
func reverseName4(addr netip.Addr) string {
b := addr.As4()
return fmt.Sprintf("%d.%d.%d.%d.in-addr.arpa.", b[3], b[2], b[1], b[0])
}
func reverseName6(addr netip.Addr) string {
ret := ""
b := addr.As16()
for i := 15; i >= 0; i-- {
ret += fmt.Sprintf("%x.%x.", b[i]&0xf, (b[i]&0xf0)>>4)
}
return ret + "ip6.arpa."
}
// AddAddrs adds multiple addresses to a set of Zones. This creates
// both forward and reverse DNS entries.
func (z *Zones) AddAddrs(addrs netbox.IPAddrs) error {
for _, addr := range addrs {
if addr.DNSName != "" && addr.Status == "active" {
forward := Record{
Name: addr.DNSName + ".",
Rrdatas: []string{addr.Address.Addr().String()},
}
reverse := Record{
Name: ReverseName(addr.Address.Addr()),
Type: "PTR",
Rrdatas: []string{addr.DNSName + "."},
}
if addr.Address.Addr().Is4() {
forward.Type = "A"
} else {
forward.Type = "AAAA"
}
err := z.AddRecord(&forward)
if err != nil {
return fmt.Errorf("Unable to add forward record for %q: %v", addr.DNSName, err)
}
err = z.AddRecord(&reverse)
if err != nil {
log.Warningf("Unable to add reverse record: %v", err)
}
}
}
return nil
}