-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.go
1037 lines (871 loc) · 31.4 KB
/
web.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"context"
cryptorand "crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"io"
"log/slog"
"maps"
"net"
"net/http"
"slices"
"sort"
"strings"
"time"
"github.com/libdns/libdns"
"github.com/miekg/dns"
"github.com/mjl-/bstore"
"github.com/mjl-/sherpa"
"github.com/mjl-/sherpadoc"
)
// API is the webapi used by the admin frontend.
type API struct{}
var apiDoc sherpadoc.Section
func httpBasicAuth(fn func(w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if !ok || username != "admin" || password != adminpassword {
w.Header().Set("WWW-Authenticate", `Basic realm="dnsclay"`)
http.Error(w, "401 - unauthorized", http.StatusUnauthorized)
return
}
fn(w, r)
}
}
// write a copy of the database from within a readonly transaction, for a consistent view.
func exportDatabase(w http.ResponseWriter, r *http.Request) {
log := cidlog(r.Context())
h := w.Header()
h.Set("Content-Type", "application/octet-stream")
h.Set("Cache-Control", "no-cache, max-age=0")
err := database.Read(r.Context(), func(tx *bstore.Tx) error {
_, err := tx.WriteTo(w)
return err
})
if err != nil && !isClosed(err) {
log.Error("exporting database", "err", err)
}
}
// NOTE: Functions starting with an underscore can panic with a *sherpa.Error. They
// are are recognized by the sherpa handler and turned into regular error
// conditions.
func _zone(tx *bstore.Tx, zone string) (z Zone) {
z = Zone{Name: zone}
err := tx.Get(&z)
_checkf(err, "get zone")
return
}
func _checkType(typ Type) {
switch uint16(typ) {
case
dns.TypeReserved,
dns.TypeNone,
dns.TypeNXNAME,
dns.TypeOPT,
dns.TypeUINFO,
dns.TypeUID,
dns.TypeGID,
dns.TypeUNSPEC,
dns.TypeTKEY,
dns.TypeTSIG,
dns.TypeIXFR,
dns.TypeAXFR,
dns.TypeMAILA,
dns.TypeMAILB,
dns.TypeANY:
_checkuserf(fmt.Errorf("invalid meta type %d", typ), "checking type")
}
if _, ok := dns.TypeToString[uint16(typ)]; !ok {
_checkuserf(fmt.Errorf("invalid type %d", typ), "checking type")
}
}
func _recordType(t Type) string {
_checkType(t)
return dns.TypeToString[uint16(t)]
}
// Zones returns all zones.
func (x API) Zones(ctx context.Context) []Zone {
zones, err := bstore.QueryDB[Zone](ctx, database).List()
_checkf(err, "listing zones")
return zones
}
// Zone returns details about a single zone, the provider config, dns notify
// destinations, credentials with access to the zone, and record sets. The returned
// record sets includes those no long active (i.e. deleted). The
// history/propagation state fo the record sets only includes those that may still
// be in caches. Use ZoneRecordSetHistory for the full history for a single record
// set.
func (x API) Zone(ctx context.Context, zone string) (z Zone, pc ProviderConfig, notifies []ZoneNotify, credentials []Credential, sets []RecordSet) {
var records []Record
_dbread(ctx, func(tx *bstore.Tx) {
z = _zone(tx, zone)
pc = ProviderConfig{Name: z.ProviderConfigName}
err := tx.Get(&pc)
_checkf(err, "get provider config")
notifies, err = bstore.QueryTx[ZoneNotify](tx).FilterNonzero(ZoneNotify{Zone: zone}).List()
_checkf(err, "listing notify addresses")
err = bstore.QueryTx[ZoneCredential](tx).FilterNonzero(ZoneCredential{Zone: zone}).ForEach(func(zc ZoneCredential) error {
c := Credential{ID: zc.ID}
err := tx.Get(&c)
_checkf(err, "get credential for zone")
credentials = append(credentials, c)
return nil
})
_checkf(err, "listing zone credentials")
records, err = bstore.QueryTx[Record](tx).FilterNonzero(Record{Zone: zone}).List()
_checkf(err, "list records")
})
sets = _propagationStates(records)
return
}
// ZoneRecords returns all records for a zone, including historic records, without
// grouping them into record sets.
func (x API) ZoneRecords(ctx context.Context, zone string) (records []Record) {
_dbread(ctx, func(tx *bstore.Tx) {
z := _zone(tx, zone)
var err error
records, err = bstore.QueryTx[Record](tx).FilterNonzero(Record{Zone: z.Name}).List()
_checkf(err, "list records")
})
return
}
// ZoneRefresh starts a sync of the records from the provider into the local
// database, sending dns notify if needed. ZoneRefresh returns all records
// (included deleted) from after the synchronization.
func (x API) ZoneRefresh(ctx context.Context, zone string) (z Zone, sets []RecordSet) {
log := cidlog(ctx)
var provider Provider
_dbread(ctx, func(tx *bstore.Tx) {
var err error
z, provider, err = zoneProvider(tx, zone)
_checkf(err, "get zone and provider")
})
unlock := lockZone(z.Name)
defer unlock()
var cancel func()
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
defer cancel()
latest, err := getRecords(ctx, log, provider, zone)
_checkf(err, "getting latest records through provider")
var notify bool
defer possiblyZoneNotify(log, zone, ¬ify)
_dbwrite(ctx, func(tx *bstore.Tx) {
z = _zone(tx, zone) // Again.
notify, _, _, _, err = syncRecords(log, tx, z, latest)
_checkf(err, "storing latest records in database")
records, err := bstore.QueryTx[Record](tx).FilterNonzero(Record{Zone: zone}).List()
_checkf(err, "list records")
sets = _propagationStates(records)
})
return
}
// ZonePurgeHistory removes historic records from the database, those marked "deleted".
func (x API) ZonePurgeHistory(ctx context.Context, zone string) (z Zone, sets []RecordSet) {
_dbwrite(ctx, func(tx *bstore.Tx) {
z = _zone(tx, zone) // Again.
q := bstore.QueryTx[Record](tx)
q.FilterNonzero(Record{Zone: z.Name})
q.FilterFn(func(r Record) bool { return r.Deleted != nil })
_, err := q.Delete()
_checkf(err, "removing record history")
records, err := bstore.QueryTx[Record](tx).FilterNonzero(Record{Zone: z.Name}).List()
_checkf(err, "listing records ")
sets = _propagationStates(records)
})
return
}
// ZoneAdd adds a new zone to the database. A TSIG credential is created
// automatically. Records are fetched returning the new zone, in the background.
//
// If pc.ProviderName is non-empty, a new ProviderConfig is added.
func (x API) ZoneAdd(ctx context.Context, z Zone, notifies []ZoneNotify) (nzone Zone) {
log := cidlog(ctx)
var provider Provider
_dbwrite(ctx, func(tx *bstore.Tx) {
now := time.Now()
z.Name = _cleanAbsName(strings.TrimSuffix(z.Name, ".") + ".")
z.SyncInterval = 24 * time.Hour
z.RefreshInterval = time.Hour
z.NextSync = now.Add(z.SyncInterval)
z.NextRefresh = now.Add(z.RefreshInterval / (5 * 10))
err := tx.Insert(&z)
_checkf(err, "adding zone")
_, provider, err = zoneProvider(tx, z.Name)
_checkf(err, "get zone and provider")
for _, n := range notifies {
n.Zone = z.Name
switch n.Protocol {
case "tcp", "udp":
default:
_checkuserf(fmt.Errorf("unknown protocol %q", n.Protocol), "checking notify")
}
_, _, err := net.SplitHostPort(n.Address)
_checkuserf(err, "checking notify address")
err = tx.Insert(&n)
_checkf(err, "inserting notify")
}
tsigbuf := make([]byte, 32)
_, err = io.ReadFull(cryptorand.Reader, tsigbuf)
_checkf(err, "read random")
cred := Credential{
Name: "zone-default-tsig-" + strings.TrimSuffix(z.Name, "."),
Type: "tsig",
TSIGSecret: base64.StdEncoding.EncodeToString(tsigbuf),
}
err = tx.Insert(&cred)
_checkf(err, "inserting tsig credential")
zonecred := ZoneCredential{
Zone: z.Name,
CredentialID: cred.ID,
}
err = tx.Insert(&zonecred)
_checkf(err, "inserting tsig zone credential")
})
go func() {
defer recoverPanic(log, "fetching records for new zone")
unlock := lockZone(z.Name)
defer unlock()
var cancel func()
ctx, cancel = context.WithTimeout(shutdownCtx, 30*time.Second)
defer cancel()
latest, err := getRecords(ctx, log, provider, z.Name)
if err != nil {
log.Debug("getting latest records through provider", "err", err)
return
}
var changed bool
defer possiblyZoneNotify(log, z.Name, &changed)
err = database.Write(ctx, func(tx *bstore.Tx) error {
_zone(tx, z.Name) // Again.
changed, _, _, _, err = syncRecords(log, tx, z, latest)
return err
})
if err != nil {
log.Debug("updating records in database", "err", err)
}
}()
return z
}
// ZoneDelete removes a zone and all its records, credentials and dns notify addresses, from the database.
func (x API) ZoneDelete(ctx context.Context, zone string) {
_dbwrite(ctx, func(tx *bstore.Tx) {
z := _zone(tx, zone)
_, err := bstore.QueryTx[ZoneNotify](tx).FilterNonzero(ZoneNotify{Zone: z.Name}).Delete()
_checkf(err, "deleting notify addresses for zone")
zonecreds, err := bstore.QueryTx[ZoneCredential](tx).FilterNonzero(ZoneCredential{Zone: z.Name}).List()
_checkf(err, "listing zone credentials")
for _, zc := range zonecreds {
err = tx.Delete(&zc)
_checkf(err, "deleting zone credential")
err := tx.Delete(&Credential{ID: zc.CredentialID})
_checkf(err, "deleting credential")
}
_, err = bstore.QueryTx[Record](tx).FilterNonzero(Record{Zone: z.Name}).Delete()
_checkf(err, "deleting records for zone")
err = tx.Delete(&z)
_checkf(err, "deleting zone")
exists, err := bstore.QueryTx[Zone](tx).FilterNonzero(Zone{ProviderConfigName: z.ProviderConfigName}).Exists()
_checkf(err, "checking if references to provider config still exists")
if !exists {
pc := ProviderConfig{Name: z.ProviderConfigName}
err := tx.Delete(&pc)
_checkf(err, "deleting provider config")
}
})
}
// ZoneUpdate updates the provider config and refresh & sync interval for a zone.
func (x API) ZoneUpdate(ctx context.Context, z Zone) (nz Zone) {
_dbwrite(ctx, func(tx *bstore.Tx) {
oz := _zone(tx, z.Name)
oz.ProviderConfigName = z.ProviderConfigName
oz.RefreshInterval = z.RefreshInterval
oz.SyncInterval = z.SyncInterval
err := tx.Update(&oz)
_checkf(err, "update zone")
nz = oz
})
return
}
// ZoneNotify send a DNS notify message to an address.
func (x API) ZoneNotify(ctx context.Context, zoneNotifyID int64) {
log := cidlog(ctx)
zn := ZoneNotify{ID: zoneNotifyID}
var soa dns.SOA
_dbread(ctx, func(tx *bstore.Tx) {
err := tx.Get(&zn)
_checkf(err, "get zone notify details")
q := bstore.QueryTx[Record](tx)
q.FilterNonzero(Record{Type: Type(dns.TypeSOA), Zone: zn.Zone})
q.FilterFn(func(r Record) bool { return r.Deleted == nil })
r, err := q.Get()
_checkf(err, "get soa from db")
soarr, err := r.RR()
_checkf(err, "get rr for db soa record")
soa = *soarr.(*dns.SOA)
})
err := dnsNotify(log, zn, soa)
_checkf(err, "notifying")
}
// ZoneNotifyAdd adds a new DNS NOTIFY destination to a zone.
func (x API) ZoneNotifyAdd(ctx context.Context, zn ZoneNotify) (nzn ZoneNotify) {
_dbwrite(ctx, func(tx *bstore.Tx) {
zn.Created = time.Time{}
err := tx.Insert(&zn)
_checkf(err, "inserting zone notify")
nzn = zn
})
return
}
// ZoneNotifyDelete removes a DNS NOTIFY destination from a zone.
func (x API) ZoneNotifyDelete(ctx context.Context, zoneNotifyID int64) {
_dbwrite(ctx, func(tx *bstore.Tx) {
zn := ZoneNotify{ID: zoneNotifyID}
err := tx.Delete(&zn)
_checkf(err, "deleting zone notify")
})
}
// ZoneCredentialAdd adds a new TSIG or TLS public key credential to a zone.
func (x API) ZoneCredentialAdd(ctx context.Context, zone string, c Credential) (nc Credential) {
_dbwrite(ctx, func(tx *bstore.Tx) {
_zone(tx, zone)
// Name must be valid for use in DNS, we store it without trailing dot.
name := _cleanAbsName(strings.TrimSuffix(c.Name, ".") + ".")
c.Name = strings.TrimSuffix(name, ".")
c.Created = time.Time{}
switch c.Type {
case "tsig":
if c.TSIGSecret == "" {
randbuf := make([]byte, 32)
_, err := io.ReadFull(cryptorand.Reader, randbuf)
_checkf(err, "reading random bytes")
c.TSIGSecret = base64.StdEncoding.EncodeToString(randbuf)
} else {
_, err := base64.StdEncoding.DecodeString(c.TSIGSecret)
_checkuserf(err, "parsing tsig secret %q", c.TSIGSecret)
}
c.TLSPublicKey = ""
case "tlspubkey":
if c.TLSPublicKey == "" {
_checkuserf(errors.New("must not be empty"), "checking tls public key")
}
buf, err := base64.RawURLEncoding.DecodeString(c.TLSPublicKey)
if len(buf) != sha256.Size {
err = fmt.Errorf("got %d bytes, need %d", len(buf), sha256.Size)
}
_checkuserf(err, "parsing tls public key")
c.TSIGSecret = ""
q := bstore.QueryTx[Credential](tx)
q.FilterNonzero(Credential{TLSPublicKey: c.TLSPublicKey, Type: "tlspubkey"})
ok, err := q.Exists()
if err == nil && ok {
err = errors.New("public key already present")
}
_checkf(err, "checking tlspubkey")
default:
_checkuserf(fmt.Errorf("unknown value %q", c.Type), "checking type")
}
err := tx.Insert(&c)
_checkf(err, "inserting credential")
zc := ZoneCredential{0, zone, c.ID}
err = tx.Insert(&zc)
_checkf(err, "inserting zone credential")
nc = c
})
return
}
// ZoneCredentialDelete removes a TSIG/TLS public key credential from a zone.
func (x API) ZoneCredentialDelete(ctx context.Context, credentialID int64) {
_dbwrite(ctx, func(tx *bstore.Tx) {
c := Credential{ID: credentialID}
err := tx.Get(&c)
_checkf(err, "get credential")
n, err := bstore.QueryTx[ZoneCredential](tx).FilterNonzero(ZoneCredential{CredentialID: c.ID}).Delete()
if err == nil && n != 1 {
err = fmt.Errorf("deleted %d records, expected 1", n)
}
_checkf(err, "deleting zone credential")
err = tx.Delete(&c)
_checkf(err, "delete credential")
})
}
// ZoneImportRecords parses records in zonefile, assuming standard zone file syntax,
// and adds the records via the provider and syncs the newly added records to the
// local database. The latest records, included historic/deleted records after the
// sync are returned.
func (x API) ZoneImportRecords(ctx context.Context, zone, zonefile string) []Record {
log := cidlog(ctx)
var z Zone
var provider Provider
_dbread(ctx, func(tx *bstore.Tx) {
var err error
z, provider, err = zoneProvider(tx, zone)
_checkf(err, "get zone and provider")
})
zp := dns.NewZoneParser(strings.NewReader(zonefile), z.Name, "")
zp.SetDefaultTTL(300)
var l []Record
for rr, ok := zp.Next(); ok; rr, ok = zp.Next() {
hex, value, err := recordData(rr)
_checkf(err, "parsing record value")
h := rr.Header()
if h.Class != dns.ClassINET {
_checkuserf(errors.New("only class INET supported"), "checking record")
}
h.Name = _cleanAbsName(h.Name)
_checkType(Type(h.Rrtype))
l = append(l, Record{0, z.Name, 0, 0, time.Time{}, nil, h.Name, Type(h.Rrtype), dns.ClassINET, TTL(h.Ttl), hex, value, ""})
}
err := zp.Err()
if err == nil && len(l) == 0 {
err = errors.New("no records found")
}
_checkuserf(err, "parsing zone file")
unlock := lockZone(z.Name)
defer unlock()
// Get latest.
latest, err := getRecords(ctx, log, provider, z.Name)
_checkf(err, "get latest records")
var notify bool
defer possiblyZoneNotify(log, z.Name, ¬ify)
var soa Record
_dbwrite(ctx, func(tx *bstore.Tx) {
notify, _, _, _, err = syncRecords(log, tx, z, latest)
_checkf(err, "updating records from latest before adding")
soa = zoneSOA(log, tx, z.Name)
})
var cancel func()
ctx, cancel = context.WithTimeout(ctx, time.Minute)
defer cancel()
ladded, err := appendRecords(ctx, log, provider, z.Name, libdnsRecords(l))
if err == nil && len(ladded) < len(l) {
err = fmt.Errorf("provider added %d records, expected %d (%v != %v)", len(ladded), len(l), ladded, l)
}
_checkf(err, "adding records via provider")
log.Debug("added record through provider", "records", l, "ladded", ladded)
var rkl []recordKey
for _, r := range l {
rkl = append(rkl, r.recordKey())
}
inserted, _, err := ensurePropagate(ctx, log, provider, z, rkl, nil, soa.SerialFirst)
_checkf(err, "ensuring record propagation")
return inserted
}
// RecordSetChange is a new or updated record set.
type RecordSetChange struct {
RelName string
TTL TTL
Type Type
Values []string
}
func _parseNewSet(zone string, rsc RecordSetChange) []Record {
absname := rsc.RelName
if absname != "" {
absname += "."
}
absname += zone
absname = _cleanAbsName(absname)
typ := _recordType(rsc.Type)
if rsc.TTL == 0 {
_checkuserf(errors.New("ttl must be > 0"), "checking ttl")
}
var records []Record
if len(rsc.Values) == 0 {
_checkuserf(errors.New("at least one value required"), "checking values")
}
for _, v := range rsc.Values {
text := fmt.Sprintf("%s %d %s %s", absname, rsc.TTL, typ, v)
rr, err := dns.NewRR(text)
_checkuserf(err, "parsing new record")
hex, value, err := recordData(rr)
_checkf(err, "parsing record value")
r := Record{0, zone, 0, 0, time.Time{}, nil, absname, rsc.Type, dns.ClassINET, rsc.TTL, hex, value, ""}
records = append(records, r)
}
return records
}
// RecordSetAdd adds a record set through the provider, then waits for it to
// synchronize back to the local database.
//
// The name and type must not already exist. Use RecordSetUpdate to add values to
// an existing record set.
//
// The inserted records are returned.
func (x API) RecordSetAdd(ctx context.Context, zone string, rsc RecordSetChange) []Record {
log := cidlog(ctx)
var z Zone
var provider Provider
var soa Record
_dbread(ctx, func(tx *bstore.Tx) {
var err error
z, provider, err = zoneProvider(tx, zone)
_checkf(err, "get zone and provider")
})
nset := _parseNewSet(zone, rsc)
unlock := lockZone(z.Name)
defer unlock()
// Get latest.
latest, err := getRecords(ctx, log, provider, zone)
_checkf(err, "get latest records")
var notify bool
defer possiblyZoneNotify(log, zone, ¬ify)
_dbwrite(ctx, func(tx *bstore.Tx) {
notify, _, _, _, err = syncRecords(log, tx, z, latest)
_checkf(err, "updating records from latest before looking record to delete")
soa = zoneSOA(log, tx, z.Name)
q := bstore.QueryTx[Record](tx)
q.FilterNonzero(Record{Zone: z.Name, AbsName: nset[0].AbsName, Type: nset[0].Type})
q.FilterFn(func(r Record) bool { return r.Deleted == nil })
oset, err := q.List()
_checkf(err, "listing current records for name and type")
if len(oset) != 0 {
_checkuserf(errors.New("already exists, edit the existing record set instead"), "checking for existing records with name and type")
}
})
var cancel func()
ctx, cancel = context.WithTimeout(ctx, time.Minute)
defer cancel()
ladded, err := appendRecords(ctx, log, provider, z.Name, libdnsRecords(nset))
if err == nil && len(ladded) != len(nset) {
err = fmt.Errorf("provider added %d records, expected %d", len(ladded), len(nset))
}
_checkf(err, "adding records via provider")
log.Debug("added record through provider", "records", nset, "ladded", ladded)
var expAdd []recordKey
for _, r := range nset {
expAdd = append(expAdd, r.recordKey())
}
inserted, _, err := ensurePropagate(ctx, log, provider, z, expAdd, nil, soa.SerialFirst)
_checkf(err, "ensuring record propagation")
return inserted
}
// RecordSetUpdate updates an existing record set, replacing its values with the
// new values. If the name has changed, the old records are deleted and new records
// with new name inserted.
//
// Before changing, prevRecordIDs are compared with the current records for the
// name and type, and must be the same.
//
// valueRecordIDs match Values from RecordNewSet (must have the same number of
// items). New values must have 0 as record ID.
//
// The records of the updated record set are returned.
func (x API) RecordSetUpdate(ctx context.Context, zone string, oldRelName string, rsc RecordSetChange, prevRecordIDs, valueRecordIDs []int64) []Record {
log := cidlog(ctx)
if len(rsc.Values) != len(valueRecordIDs) {
_checkuserf(errors.New("providerIDs must have same number of values"), "checking values")
}
if len(rsc.Values) == 0 {
_checkuserf(errors.New("must have at least one value"), "checking values")
}
var z Zone
var provider Provider
_dbread(ctx, func(tx *bstore.Tx) {
var err error
z, provider, err = zoneProvider(tx, zone)
_checkf(err, "get zone and provider")
})
nset := _parseNewSet(z.Name, rsc)
oldAbsName := libdns.AbsoluteName(oldRelName, z.Name)
unlock := lockZone(z.Name)
defer unlock()
// Get latest.
latest, err := getRecords(ctx, log, provider, zone)
_checkf(err, "get latest records")
var notify bool
defer possiblyZoneNotify(log, zone, ¬ify)
var soa Record
var oset []Record
_dbwrite(ctx, func(tx *bstore.Tx) {
notify, _, _, _, err = syncRecords(log, tx, z, latest)
_checkf(err, "updating records from latest before looking record to delete")
soa = zoneSOA(log, tx, z.Name)
var err error
q := bstore.QueryTx[Record](tx)
q.FilterNonzero(Record{Zone: z.Name, AbsName: oldAbsName, Type: nset[0].Type})
q.FilterFn(func(r Record) bool { return r.Deleted == nil })
oset, err = q.List()
_checkf(err, "get old set")
})
// Check user is operating on latest records.
var orecordIDs []int64
for _, or := range oset {
orecordIDs = append(orecordIDs, or.ID)
}
slices.Sort(orecordIDs)
slices.Sort(prevRecordIDs)
if !slices.Equal(orecordIDs, prevRecordIDs) {
_checkuserf(fmt.Errorf("user expects %v, current latest %v", prevRecordIDs, orecordIDs), "checking user is operating on latest records")
}
if oset[0].Type != rsc.Type {
_checkuserf(errors.New("type cannot be changed, remove old and create new records instead"), "checking type")
}
ormap := map[int64]Record{}
for _, or := range oset {
ormap[or.ID] = or
}
for _, id := range valueRecordIDs {
if _, ok := ormap[id]; id > 0 && !ok {
_checkuserf(fmt.Errorf("unknown record id %v", id), "checking record ids of updated records")
}
}
// We'll make changes by either "set" of records (changing them) or deleting &
// adding. If the name changed, we'll just deleted & add. Otherwise (the common
// case), we use SetRecords only for records that changed and have ProviderIDs
// (because we don't really know what "setting records" means otherwise if there is
// no reference), and use AppendRecords/DeleteRecords otherwise.
var adds []Record
var expAdds []recordKey
var dels []Record
var sets []Record
for _, or := range oset {
index := slices.Index(valueRecordIDs, or.ID)
if index >= 0 && or.recordKey() == nset[index].recordKey() {
continue // Unchanged.
} else if or.AbsName != oldAbsName || index < 0 || or.ProviderID == "" {
dels = append(dels, or)
}
// Otherwise, the record will be updated and we'll handle it below.
}
for i, nr := range nset {
if orID := valueRecordIDs[i]; orID > 0 && ormap[orID].recordKey() == nr.recordKey() {
continue // Unchanged.
} else if oset[0].AbsName != oldAbsName || orID <= 0 || ormap[orID].ProviderID == "" {
adds = append(adds, nr)
expAdds = append(expAdds, nr.recordKey())
} else {
nr.ProviderID = ormap[orID].ProviderID
sets = append(sets, nr)
expAdds = append(expAdds, nr.recordKey())
}
}
if len(dels) == 0 && len(adds) == 0 && len(sets) == 0 {
_checkuserf(errors.New("no added/removed/updated records"), "gathering changes")
}
log.Debug("updating record set", "oset", oset, "nset", nset, "dels", dels, "sets", sets, "dels", dels)
var cancel func()
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
defer cancel()
if len(dels) > 0 {
ldrdels := libdnsRecords(dels)
ldeleted, err := deleteRecords(ctx, log, provider, z.Name, ldrdels)
if err == nil && len(ldeleted) != len(ldrdels) {
slog.Info("provider reported deletes", "deleted", ldeleted, "requested", ldrdels)
err = fmt.Errorf("provider reports %d records were deleted, expected %d", len(ldeleted), len(ldrdels))
}
_checkf(err, "deleting old records through provider")
log.Debug("records deleted through provider", "deleted", ldeleted)
}
if len(sets) > 0 {
ldrsets := libdnsRecords(sets)
lupdated, err := setRecords(ctx, log, provider, z.Name, ldrsets)
if err == nil && len(lupdated) != len(ldrsets) {
slog.Info("provider reported updates", "updated", lupdated, "requested", ldrsets)
err = fmt.Errorf("provider reports %d records were updated, expected %d", len(lupdated), len(sets))
}
_checkf(err, "updating records through provider")
log.Debug("records updated through provider", "updated", lupdated)
}
if len(adds) > 0 {
ldradds := libdnsRecords(nset)
ladded, err := appendRecords(ctx, log, provider, z.Name, ldradds)
if err == nil && len(ladded) < len(ldradds) {
slog.Info("provider reported appends", "added", ladded, "requested", ldradds)
err = fmt.Errorf("provider reports %d records were added, expected %d", len(ladded), len(ldradds))
}
_checkf(err, "adding records through provider")
log.Debug("records added through provider", "added", ladded)
}
inserted, _, err := ensurePropagate(ctx, log, provider, z, expAdds, dels, soa.SerialFirst)
_checkf(err, "ensuring propagation")
return inserted
}
// RecordSetDelete removes a record set through the provider and waits for the
// change to be synced to the local database. The historic/deleted record is
// returned.
//
// recordIDs must be the current record ids the caller expects to invalidate.
//
// The updated records, now marked as deleted, are returned.
func (x API) RecordSetDelete(ctx context.Context, zone string, relName string, typ Type, recordIDs []int64) []Record {
log := cidlog(ctx)
_checkType(typ)
var z Zone
var provider Provider
var soa Record
_dbread(ctx, func(tx *bstore.Tx) {
var err error
z, provider, err = zoneProvider(tx, zone)
_checkf(err, "get zone and provider")
})
unlock := lockZone(z.Name)
defer unlock()
// Get latest.
latest, err := getRecords(ctx, log, provider, zone)
_checkf(err, "get latest records")
var notify bool
defer possiblyZoneNotify(log, zone, ¬ify)
var records []Record
// Sync and get record to delete.
_dbwrite(ctx, func(tx *bstore.Tx) {
notify, _, _, _, err = syncRecords(log, tx, z, latest)
_checkf(err, "updating records from latest before looking record to delete")
soa = zoneSOA(log, tx, z.Name)
q := bstore.QueryTx[Record](tx)
q.FilterNonzero(Record{Zone: z.Name, AbsName: libdns.AbsoluteName(relName, zone), Type: typ})
q.FilterFn(func(r Record) bool { return r.Deleted == nil })
records, err = q.List()
_checkf(err, "list records")
})
if len(records) == 0 {
_checkuserf(errors.New("no record found"), "fetching records to remove")
}
var gotIDs []int64
for _, r := range records {
gotIDs = append(gotIDs, r.ID)
}
slices.Sort(gotIDs)
slices.Sort(recordIDs)
if !slices.Equal(gotIDs, recordIDs) {
_checkuserf(fmt.Errorf("found %v, user expects %v", gotIDs, recordIDs), "comparing record ids")
}
var cancel func()
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
defer cancel()
removed, err := deleteRecords(ctx, log, provider, z.Name, libdnsRecords(records))
if err == nil && len(removed) != len(records) {
err = fmt.Errorf("provider reports %d records were removed, expected %d", len(removed), len(records))
}
_checkf(err, "deleting records through provider")
log.Debug("records removed", "records", removed)
_, dels, err := ensurePropagate(ctx, log, provider, z, nil, records, soa.SerialFirst)
_checkf(err, "ensuring propagation")
return dels
}
// Version returns the version of this build of the application.
func (x API) Version(ctx context.Context) string {
return version
}
// DNSTypeNames returns a mapping of DNS type numbers to strings.
func (x API) DNSTypeNames(ctx context.Context) map[uint16]string {
return dns.TypeToString
}
// KnownProviders is a dummy method whose sole purpose is to get an API description
// of all known providers in the API documentation, for use in TypeScript.
func (x API) KnownProviders(ctx context.Context) (KnownProviders, sherpadoc.Section) {
return KnownProviders{}, sherpadoc.Section{}
}
// Docs returns the API docs. The TypeScript code uses this documentation to build
// a UI for the fields in configurations for providers (as included through
// KnownProviders).
func (x API) Docs(ctx context.Context) sherpadoc.Section {
return apiDoc
}
// ProviderConfigTest tests the provider configuration for zone. Used before
// creating a zone with a new config or updating the config for an existing zone.
func (x API) ProviderConfigTest(ctx context.Context, zone string, provider string, providerConfigJSON string) (nrecords int) {
log := cidlog(ctx)
zone = strings.TrimSuffix(zone, ".") + "."
zone = _cleanAbsName(zone)
p, err := providerForConfig(provider, providerConfigJSON)
if err != nil && errors.Is(err, errProviderUserError) {
_checkuserf(err, "checking provider")
}
_checkf(err, "checking provider")
records, err := getRecords(ctx, log, p, zone)
_checkuserf(err, "fetching records for testing provider config")
return len(records)
}
// ProviderConfigs returns all provider configs.
func (x API) ProviderConfigs(ctx context.Context) (providerConfigs []ProviderConfig) {
_dbread(ctx, func(tx *bstore.Tx) {
var err error
providerConfigs, err = bstore.QueryTx[ProviderConfig](tx).List()
_checkf(err, "listing provider configs")
})
return
}
// ProviderConfigAdd adds a new provider config.
func (x API) ProviderConfigAdd(ctx context.Context, pc ProviderConfig) (npc ProviderConfig) {
_dbwrite(ctx, func(tx *bstore.Tx) {
_, err := providerForConfig(pc.ProviderName, pc.ProviderConfigJSON)
if err != nil && errors.Is(err, errProviderUserError) {
_checkuserf(err, "checking provider config")
}
_checkf(err, "checking provider config")
err = tx.Insert(&pc)
_checkf(err, "update providerconfig")
npc = pc
})
return
}
// ProviderConfigUpdate updates a provider config.
func (x API) ProviderConfigUpdate(ctx context.Context, pc ProviderConfig) (npc ProviderConfig) {
_dbwrite(ctx, func(tx *bstore.Tx) {
opc := ProviderConfig{Name: pc.Name}
err := tx.Get(&opc)
_checkf(err, "get provider config")
_, err = providerForConfig(pc.ProviderName, pc.ProviderConfigJSON)
if err != nil && errors.Is(err, errProviderUserError) {
_checkuserf(err, "checking provider config")
}
_checkf(err, "checking provider config")
err = tx.Update(&pc)
_checkf(err, "update providerconfig")
npc = pc
})
return
}
func _propagationStates(records []Record) (sets []RecordSet) {
m, err := propagationStates(time.Now(), records, "", -1, true)
_checkf(err, "get record sets and propagation states")
// Ensure we return sets sorted, for tests.
keys := slices.Collect(maps.Keys(m))
sort.Slice(keys, func(i, j int) bool {
a, b := keys[i], keys[j]
return a.AbsName < b.AbsName || a.AbsName == b.AbsName && a.Type < b.Type
})
for _, k := range keys {
versions := m[k]
sets = append(sets, versions[len(versions)-1])
}
return
}
// ZoneRecordSets returns the current record sets including propagation states that
// are not the latest version but that may still be in caches. For the full history
// of a record set, see ZoneRecordSetHistory.