Debian Codegraph @

cmd/atomdns/handlers/sign/sign.go

raw
1 // Package sign implements a zone signer as a hander.
2 package sign
3
4 import (
5 "context"
6 "log/slog"
7 "path/filepath"
8 "time"
9
10 "codeberg.org/miekg/dns"
11 "codeberg.org/miekg/dns/cmd/atomdns/handlers/dbfile/zone"
12 "codeberg.org/miekg/dns/cmd/atomdns/internal/dnszone"
13 "codeberg.org/miekg/dns/pkg/pool"
14 )
15
16 type Sign struct {
17 Path string
18 Directory string
19 KeyPairs []KeyPair
20 Zonemd bool
21 pool *pool.Pool
22
23 Zones map[string]*zone.Zone
24 ttl uint32 // default ttl on all records
25
26 ctx context.Context
27 cancel context.CancelFunc
28 }
29
30 func (s *Sign) HandlerFunc(next dns.HandlerFunc) dns.HandlerFunc { return nil }
31
32 // Various duration constants for signing of the zones.
33 const (
34 expireDays = 9
35
36 signatureExpire = 32 * Day // sign for 32 days
37 signatureInception = -3 * time.Hour // -(2+1) hours, be sure to catch daylight saving time and such, jitter is subtracted
38
39 inceptionJitter = -18 * time.Hour // default max jitter for the inception
40 expirationJitter = 100 * time.Hour // default max jitter for the expiration
41 )
42
43 const Day = 24 * time.Hour
44
45 const Interval = 5 * time.Hour // Interval is the resign wake up interval.
46
47 // Resign launches a resign routine that listens for _write_ events to the origin zone files and resigns them.
48 func (s *Sign) Resign() error {
49 fn := func() {
50 for _, z := range s.Zones {
51 alog := log().With(slog.String("zone", z.Origin()), slog.String("file", filepath.Base(s.Path)))
52 zs, err := s.Sign(z.Origin())
53 if err != nil {
54 alog.Error("Failed to resign", Err(err))
55 return
56 }
57 if err := s.Write(zs); err != nil {
58 alog.Error("Failed to resign", Err(err))
59 break
60 }
61 alog.With(slog.Uint64("serial", uint64(dnszone.Serial(zs)))).Info("Successful resign")
62 }
63 }
64
65 go func() {
66 ticker := time.NewTicker(Interval)
67 defer ticker.Stop()
68 for range ticker.C {
69 fn()
70 }
71 }()
72
73 return dnszone.Watch(s.ctx, s.Path, fn)
74 }