Ready for CoreDNS testing

This commit is contained in:
Dessa Simpson 2025-12-01 01:13:27 -07:00
parent 772469d21b
commit b79df4bf1d
9 changed files with 290 additions and 124 deletions

View file

@ -26,26 +26,6 @@ type YamlPluginConfig struct {
var log = clog.NewWithPlugin("yaml")
func (y YamlPlugin) lookupRRs(qname string, qtype string) ([]dns.RR, bool) {
records, ok := y.Zone.LookupType(qname, qtype)
if !ok {
return nil, false
}
rrs := []dns.RR{}
for _, record := range records {
ttl := record.Ttl
if ttl == 0 {
ttl = y.Config.DefaultTtl
}
rr, err := dns.NewRR(fmt.Sprintf("%s %d %s %s", qname, ttl, record.Type, record.Value))
if err != nil {
return nil, false
}
rrs = append(rrs, rr)
}
return rrs, true
}
func (y YamlPlugin) Name() string { return "yaml" }
func (y YamlPlugin) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
@ -57,17 +37,16 @@ func (y YamlPlugin) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
reply.SetReply(r)
reply.Authoritative = true
rrs, ok := y.lookupRRs(qname, qtype)
if !ok {
return dns.RcodeNameError, nil
rcode, err := y.lookupRRs(qname, qtype, reply)
if err != nil {
return dns.RcodeServerFailure, fmt.Errorf("failed to lookup RRs for %s %s: %w", qname, qtype, err)
}
reply.Answer = rrs
w.WriteMsg(reply)
return dns.RcodeSuccess, nil
return rcode, nil
}
func setup(c *caddy.Controller) error {
c.Next() // yaml
c.Next() // skip "yaml"
filename := "zones.yaml"
if c.NextArg() {
filename = c.Val()
@ -97,3 +76,37 @@ func setup(c *caddy.Controller) error {
}
func init() { plugin.Register("yaml", setup) }
func (y YamlPlugin) lookupRRs(qname string, qtype string, reply *dns.Msg) (int, error) {
var rcode int
res, ok := y.Zone.LookupType(qname, qtype)
if !ok {
// NXDOMAIN
rcode = dns.RcodeNameError
}
if res.IsReferral {
reply.Authoritative = false
}
sections := []struct {
source []NamedRecord
dest *[]dns.RR
}{
{res.Answer, &reply.Answer},
{res.Ns, &reply.Ns},
{res.Extra, &reply.Extra},
}
for _, section := range sections {
for _, record := range section.source {
ttl := record.Record.Ttl
if ttl == 0 {
ttl = y.Config.DefaultTtl
}
rr, err := dns.NewRR(fmt.Sprintf("%s %d %s %s", record.Name, ttl, record.Record.Type, record.Record.Value))
if err != nil {
return rcode, fmt.Errorf("failed to generate RR for %s %s %s: %w", record.Name, record.Record.Type, record.Record.Value, err)
}
*section.dest = append(*section.dest, rr)
}
}
return rcode, nil
}