Repository navigation
Expand file tree
/
Copy pathprovider.go
More file actions
371 lines (339 loc) · 10.1 KB
/
Copy pathprovider.go
File metadata and controls
371 lines (339 loc) · 10.1 KB
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
// Package connbyte implements a DNS record management client compatible
// with the libdns interfaces for ConnByte (https://connbyte.com/).
//
// It uses the ConnByte DNS API (https://api.connbyte.com/) with a bearer token.
//
// Supported record types are the ones accepted by the ConnByte API:
// A, AAAA, CNAME, MX, NS, SRV and TXT.
//
// The ConnByte API has no batch operations, so SetRecords and DeleteRecords
// are not atomic. If an error occurs part way, some changes may already be
// applied.
package connbyte
import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/libdns/libdns"
)
// Provider facilitates DNS record manipulation with ConnByte.
type Provider struct {
// APIToken is the ConnByte API token (required).
APIToken string `json:"api_token,omitempty"`
// BaseURL overrides the API endpoint. Optional, defaults to https://api.connbyte.com.
BaseURL string `json:"base_url,omitempty"`
// HTTPClient is used for API calls. Optional.
HTTPClient *http.Client `json:"-"`
mu sync.Mutex
}
// supportedTypes are the record types the ConnByte API accepts.
var supportedTypes = map[string]bool{"A": true, "AAAA": true, "CNAME": true, "MX": true, "NS": true, "SRV": true, "TXT": true}
// findZone returns the ConnByte zone ID for the given zone name.
func (p *Provider) findZone(ctx context.Context, zone string) (string, error) {
var zones []struct {
Zone apiZone `json:"zone"`
}
if err := call(ctx, p, "/ns/zone/list", struct{}{}, &zones); err != nil {
return "", err
}
want := normalizeZone(zone)
for _, z := range zones {
if normalizeZone(z.Zone.Zone) == want {
return z.Zone.ID, nil
}
}
return "", fmt.Errorf("connbyte: zone %q not found in account", want)
}
func (p *Provider) listRecords(ctx context.Context, zoneID string) ([]apiRecord, error) {
var items []struct {
Record apiRecord `json:"record"`
}
if err := call(ctx, p, "/ns/record/list", map[string]string{"zoneId": zoneID}, &items); err != nil {
return nil, err
}
out := make([]apiRecord, 0, len(items))
for _, it := range items {
out = append(out, it.Record)
}
return out, nil
}
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
p.mu.Lock()
defer p.mu.Unlock()
zoneID, err := p.findZone(ctx, zone)
if err != nil {
return nil, err
}
recs, err := p.listRecords(ctx, zoneID)
if err != nil {
return nil, err
}
out := make([]libdns.Record, 0, len(recs))
for _, r := range recs {
lr, err := toLibdns(r, zone)
if err != nil {
return nil, err
}
out = append(out, lr)
}
return out, nil
}
// AppendRecords adds records to the zone. It returns the records that were added.
func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.mu.Lock()
defer p.mu.Unlock()
zoneID, err := p.findZone(ctx, zone)
if err != nil {
return nil, err
}
return p.appendRecords(ctx, zoneID, zone, records)
}
func (p *Provider) appendRecords(ctx context.Context, zoneID, zone string, records []libdns.Record) ([]libdns.Record, error) {
created := make([]libdns.Record, 0, len(records))
for _, rec := range records {
in, err := fromLibdns(rec, zoneID)
if err != nil {
return created, err
}
var res recordCreated
if err := call(ctx, p, "/ns/record/create", in, &res); err != nil {
return created, err
}
lr, err := toLibdns(apiRecord{
ID: res.ID,
Name: libdns.AbsoluteName(in.Name, normalizeZone(zone)),
Type: in.Type,
Content: in.Content,
TTL: in.TTL,
Priority: in.Priority,
}, zone)
if err != nil {
return created, err
}
created = append(created, lr)
}
return created, nil
}
// SetRecords sets the records in the zone, either by updating existing records or creating new ones.
// For every (name, type) pair in the input, existing records that are not in the input are deleted.
// It returns the records that were set (kept or created). This operation is not atomic.
func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.mu.Lock()
defer p.mu.Unlock()
zoneID, err := p.findZone(ctx, zone)
if err != nil {
return nil, err
}
existing, err := p.listRecords(ctx, zoneID)
if err != nil {
return nil, err
}
type key struct{ name, typ string }
wanted := make(map[key][]libdns.RR)
for _, rec := range records {
rr := rec.RR()
k := key{strings.ToLower(libdns.RelativeName(libdns.AbsoluteName(rr.Name, zone), zone)), rr.Type}
wanted[k] = append(wanted[k], rr)
}
var toCreate []libdns.Record
var set []libdns.Record
kept := make(map[int]bool)
for k, rrs := range wanted {
for _, want := range rrs {
found := false
for i, ex := range existing {
if kept[i] {
continue
}
exl, err := toLibdns(ex, zone)
if err != nil {
return nil, err
}
exrr := exl.RR()
if strings.ToLower(exrr.Name) == k.name && exrr.Type == k.typ && exrr.Data == want.Data && exrr.TTL == effectiveTTL(want.TTL) {
kept[i] = true
found = true
set = append(set, exl)
break
}
}
if !found {
toCreate = append(toCreate, want)
}
}
}
for i, ex := range existing {
if kept[i] {
continue
}
exl, err := toLibdns(ex, zone)
if err != nil {
return nil, err
}
exrr := exl.RR()
if _, ok := wanted[key{strings.ToLower(exrr.Name), exrr.Type}]; !ok {
continue
}
if err := call[struct{}](ctx, p, "/ns/record/delete", map[string]any{"zoneId": zoneID, "recordId": ex.ID}, nil); err != nil {
return nil, err
}
}
created, err := p.appendRecords(ctx, zoneID, zone, toCreate)
return append(set, created...), err
}
// DeleteRecords deletes the specified records from the zone. It returns the records that were deleted.
// Type, TTL and value may be left empty to match any value. Records that do not exist are ignored.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.mu.Lock()
defer p.mu.Unlock()
zoneID, err := p.findZone(ctx, zone)
if err != nil {
return nil, err
}
existing, err := p.listRecords(ctx, zoneID)
if err != nil {
return nil, err
}
var deleted []libdns.Record
done := make(map[int64]bool)
for _, rec := range records {
want := rec.RR()
wantName := strings.ToLower(libdns.RelativeName(libdns.AbsoluteName(want.Name, zone), zone))
for _, ex := range existing {
if done[ex.ID] {
continue
}
exl, err := toLibdns(ex, zone)
if err != nil {
return deleted, err
}
exrr := exl.RR()
if strings.ToLower(exrr.Name) != wantName ||
(want.Type != "" && want.Type != exrr.Type) ||
(want.TTL != 0 && want.TTL != exrr.TTL) ||
(want.Data != "" && want.Data != exrr.Data) {
continue
}
if err := call[struct{}](ctx, p, "/ns/record/delete", map[string]any{"zoneId": zoneID, "recordId": ex.ID}, nil); err != nil {
return deleted, err
}
done[ex.ID] = true
deleted = append(deleted, exl)
}
}
return deleted, nil
}
// ListZones returns the zones of the account.
func (p *Provider) ListZones(ctx context.Context) ([]libdns.Zone, error) {
p.mu.Lock()
defer p.mu.Unlock()
var zones []struct {
Zone apiZone `json:"zone"`
}
if err := call(ctx, p, "/ns/zone/list", struct{}{}, &zones); err != nil {
return nil, err
}
out := make([]libdns.Zone, 0, len(zones))
for _, z := range zones {
out = append(out, libdns.Zone{Name: normalizeZone(z.Zone.Zone) + "."})
}
return out, nil
}
const (
minTTL = 60 * time.Second
maxTTL = 86400 * time.Second
defaultTTL = 3600 * time.Second
)
// effectiveTTL returns the TTL the API will store for the requested TTL.
func effectiveTTL(ttl time.Duration) time.Duration {
switch {
case ttl == 0:
return defaultTTL
case ttl < minTTL:
return minTTL
case ttl > maxTTL:
return maxTTL
}
return ttl.Truncate(time.Second)
}
func normalizeZone(zone string) string {
return strings.ToLower(strings.TrimSuffix(zone, "."))
}
// fromLibdns converts a libdns record to a ConnByte API create payload.
func fromLibdns(rec libdns.Record, zoneID string) (recordInput, error) {
rr := rec.RR()
if !supportedTypes[rr.Type] {
return recordInput{}, fmt.Errorf("connbyte: record type %q is not supported", rr.Type)
}
name := rr.Name
if name == "" {
// The API rejects an empty name and uses "@" for the zone apex.
name = "@"
}
in := recordInput{
ZoneID: zoneID,
Name: name,
Type: rr.Type,
Content: rr.Data,
TTL: int(effectiveTTL(rr.TTL) / time.Second),
}
switch rr.Type {
case "MX", "SRV":
// The API stores the MX preference / SRV priority in a separate field.
prio, rest, ok := strings.Cut(strings.TrimSpace(rr.Data), " ")
if !ok {
return recordInput{}, fmt.Errorf("connbyte: invalid %s data %q", rr.Type, rr.Data)
}
n, err := strconv.Atoi(prio)
if err != nil {
return recordInput{}, fmt.Errorf("connbyte: invalid %s priority %q", rr.Type, prio)
}
in.Priority = n
in.Content = strings.TrimSpace(rest)
}
return in, nil
}
// toLibdns converts a ConnByte API record into a typed libdns record relative to zone.
func toLibdns(r apiRecord, zone string) (libdns.Record, error) {
data := r.Content
switch r.Type {
case "TXT":
data = unquoteTXT(data)
case "MX", "SRV":
data = strconv.Itoa(r.Priority) + " " + data
}
rr := libdns.RR{
Name: libdns.RelativeName(strings.TrimSuffix(r.Name, ".")+".", normalizeZone(zone)+"."),
TTL: time.Duration(r.TTL) * time.Second,
Type: r.Type,
Data: data,
}
parsed, err := rr.Parse()
if err != nil {
return nil, fmt.Errorf("connbyte: record %d: %w", r.ID, err)
}
return parsed, nil
}
// unquoteTXT removes the surrounding quotes some DNS backends add to TXT content.
func unquoteTXT(s string) string {
if len(s) >= 2 && strings.HasPrefix(s, `"`) && strings.HasSuffix(s, `"`) && !strings.Contains(s[1:len(s)-1], `" "`) {
if u, err := strconv.Unquote(s); err == nil {
return u
}
return s[1 : len(s)-1]
}
return s
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
_ libdns.ZoneLister = (*Provider)(nil)
)