2019-08-09 12:40:28 +05:30
|
|
|
package azure
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/coredns/coredns/plugin"
|
|
|
|
|
"github.com/coredns/coredns/plugin/file"
|
|
|
|
|
"github.com/coredns/coredns/plugin/pkg/fall"
|
|
|
|
|
"github.com/coredns/coredns/plugin/pkg/upstream"
|
|
|
|
|
"github.com/coredns/coredns/request"
|
|
|
|
|
|
2020-03-11 00:52:23 +05:30
|
|
|
publicdns "github.com/Azure/azure-sdk-for-go/profiles/latest/dns/mgmt/dns"
|
|
|
|
|
privatedns "github.com/Azure/azure-sdk-for-go/profiles/latest/privatedns/mgmt/privatedns"
|
2019-08-09 12:40:28 +05:30
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type zone struct {
|
2020-03-11 00:52:23 +05:30
|
|
|
id string
|
|
|
|
|
z *file.Zone
|
|
|
|
|
zone string
|
|
|
|
|
private bool
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type zones map[string][]*zone
|
|
|
|
|
|
|
|
|
|
// Azure is the core struct of the azure plugin.
|
|
|
|
|
type Azure struct {
|
2020-03-11 00:52:23 +05:30
|
|
|
zoneNames []string
|
|
|
|
|
publicClient publicdns.RecordSetsClient
|
|
|
|
|
privateClient privatedns.RecordSetsClient
|
|
|
|
|
upstream *upstream.Upstream
|
plugin/azure: add refresh directive (#8614)
Brings azure to parity with route53, which already exposes a refresh
directive to control how often zones are re-synced. azure currently
hardcodes this to 1 minute (Run calls h.run(ctx, time.Minute)) with no
way to change it, even though the internal run() method already takes
an arbitrary interval parameter - the plumbing exists, it just wasn't
exposed as a Corefile option.
Adds a refresh field to the Azure struct, threads it through New(), and
has Run() use h.refresh instead of the hardcoded value. Parses the
refresh directive in parse() mirroring route53's exact validation
(accepts a plain number as seconds or a duration string, must be
greater than 0).
Extends TestSetup with refresh coverage (valid duration, valid plain
number, missing arg, non-numeric, zero, negative). Verified it fails
without the setup.go change (unknown property error) and passes with
it.
Signed-off-by: stackedbyaradhya <aradhyaj736@gmail.com>
2026-10-09 07:18:10 +05:30
|
|
|
refresh time.Duration
|
2020-03-11 00:52:23 +05:30
|
|
|
zMu sync.RWMutex
|
|
|
|
|
zones zones
|
2026-09-11 11:54:12 +08:00
|
|
|
updates sync.WaitGroup
|
2019-08-09 16:10:26 +01:00
|
|
|
|
|
|
|
|
Next plugin.Handler
|
|
|
|
|
Fall fall.F
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
|
|
|
|
|
2026-09-11 11:54:12 +08:00
|
|
|
// New initializes the configured DNS zones without contacting Azure.
|
plugin/azure: add refresh directive (#8614)
Brings azure to parity with route53, which already exposes a refresh
directive to control how often zones are re-synced. azure currently
hardcodes this to 1 minute (Run calls h.run(ctx, time.Minute)) with no
way to change it, even though the internal run() method already takes
an arbitrary interval parameter - the plumbing exists, it just wasn't
exposed as a Corefile option.
Adds a refresh field to the Azure struct, threads it through New(), and
has Run() use h.refresh instead of the hardcoded value. Parses the
refresh directive in parse() mirroring route53's exact validation
(accepts a plain number as seconds or a duration string, must be
greater than 0).
Extends TestSetup with refresh coverage (valid duration, valid plain
number, missing arg, non-numeric, zero, negative). Verified it fails
without the setup.go change (unknown property error) and passes with
it.
Signed-off-by: stackedbyaradhya <aradhyaj736@gmail.com>
2026-10-09 07:18:10 +05:30
|
|
|
// refresh sets how often the zones are refreshed from Azure DNS.
|
|
|
|
|
func New(_ctx context.Context, publicClient publicdns.RecordSetsClient, privateClient privatedns.RecordSetsClient, keys map[string][]string, accessMap map[string]string, refresh time.Duration) (*Azure, error) {
|
2019-08-09 12:40:28 +05:30
|
|
|
zones := make(map[string][]*zone, len(keys))
|
2025-12-10 10:17:07 +08:00
|
|
|
names := make([]string, 0, len(keys))
|
2019-08-09 16:10:26 +01:00
|
|
|
for resourceGroup, znames := range keys {
|
|
|
|
|
for _, name := range znames {
|
|
|
|
|
fqdn := dns.Fqdn(name)
|
|
|
|
|
if _, ok := zones[fqdn]; !ok {
|
|
|
|
|
names = append(names, fqdn)
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
2026-09-11 11:54:12 +08:00
|
|
|
zones[fqdn] = append(zones[fqdn], &zone{
|
|
|
|
|
id: resourceGroup, zone: name, private: accessMap[resourceGroup+name] == "private",
|
|
|
|
|
z: file.NewZone(fqdn, ""),
|
|
|
|
|
})
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
|
|
|
|
}
|
2020-03-11 00:52:23 +05:30
|
|
|
|
2019-08-09 12:40:28 +05:30
|
|
|
return &Azure{
|
2020-03-11 00:52:23 +05:30
|
|
|
publicClient: publicClient,
|
|
|
|
|
privateClient: privateClient,
|
|
|
|
|
zones: zones,
|
|
|
|
|
zoneNames: names,
|
|
|
|
|
upstream: upstream.New(),
|
plugin/azure: add refresh directive (#8614)
Brings azure to parity with route53, which already exposes a refresh
directive to control how often zones are re-synced. azure currently
hardcodes this to 1 minute (Run calls h.run(ctx, time.Minute)) with no
way to change it, even though the internal run() method already takes
an arbitrary interval parameter - the plumbing exists, it just wasn't
exposed as a Corefile option.
Adds a refresh field to the Azure struct, threads it through New(), and
has Run() use h.refresh instead of the hardcoded value. Parses the
refresh directive in parse() mirroring route53's exact validation
(accepts a plain number as seconds or a duration string, must be
greater than 0).
Extends TestSetup with refresh coverage (valid duration, valid plain
number, missing arg, non-numeric, zero, negative). Verified it fails
without the setup.go change (unknown property error) and passes with
it.
Signed-off-by: stackedbyaradhya <aradhyaj736@gmail.com>
2026-10-09 07:18:10 +05:30
|
|
|
refresh: refresh,
|
2019-08-09 12:40:28 +05:30
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 11:54:12 +08:00
|
|
|
// Run starts initial and periodic zone synchronization in the background.
|
2019-08-09 12:40:28 +05:30
|
|
|
func (h *Azure) Run(ctx context.Context) error {
|
2026-09-11 11:54:12 +08:00
|
|
|
h.updates.Go(func() {
|
plugin/azure: add refresh directive (#8614)
Brings azure to parity with route53, which already exposes a refresh
directive to control how often zones are re-synced. azure currently
hardcodes this to 1 minute (Run calls h.run(ctx, time.Minute)) with no
way to change it, even though the internal run() method already takes
an arbitrary interval parameter - the plumbing exists, it just wasn't
exposed as a Corefile option.
Adds a refresh field to the Azure struct, threads it through New(), and
has Run() use h.refresh instead of the hardcoded value. Parses the
refresh directive in parse() mirroring route53's exact validation
(accepts a plain number as seconds or a duration string, must be
greater than 0).
Extends TestSetup with refresh coverage (valid duration, valid plain
number, missing arg, non-numeric, zero, negative). Verified it fails
without the setup.go change (unknown property error) and passes with
it.
Signed-off-by: stackedbyaradhya <aradhyaj736@gmail.com>
2026-10-09 07:18:10 +05:30
|
|
|
h.run(ctx, h.refresh)
|
2026-09-11 11:54:12 +08:00
|
|
|
})
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Azure) run(ctx context.Context, interval time.Duration) {
|
|
|
|
|
timer := time.NewTimer(0)
|
|
|
|
|
defer timer.Stop()
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
case <-timer.C:
|
|
|
|
|
if ctx.Err() != nil {
|
2019-08-09 12:40:28 +05:30
|
|
|
return
|
|
|
|
|
}
|
2026-09-11 11:54:12 +08:00
|
|
|
if err := h.updateZones(ctx); err != nil && ctx.Err() == nil {
|
|
|
|
|
log.Errorf("Failed to update zones %v: %v", h.zoneNames, err)
|
|
|
|
|
}
|
|
|
|
|
timer.Reset(interval)
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
2026-09-11 11:54:12 +08:00
|
|
|
}
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Azure) updateZones(ctx context.Context) error {
|
|
|
|
|
errs := make([]string, 0)
|
|
|
|
|
for zName, z := range h.zones {
|
2026-09-11 11:54:12 +08:00
|
|
|
for _, hostedZone := range z {
|
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
|
return ctx.Err()
|
2020-03-11 00:52:23 +05:30
|
|
|
}
|
2026-09-11 11:54:12 +08:00
|
|
|
if err := h.updateZone(ctx, zName, hostedZone); err != nil {
|
|
|
|
|
errs = append(errs, fmt.Sprintf("failed to update %s:%s from azure: %v", hostedZone.id, hostedZone.zone, err))
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(errs) != 0 {
|
|
|
|
|
return fmt.Errorf("errors updating zones: %v", errs)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 11:54:12 +08:00
|
|
|
func (h *Azure) updateZone(ctx context.Context, name string, hostedZone *zone) error {
|
|
|
|
|
// Bound the entire listing, including SDK retries and all pages, so a
|
|
|
|
|
// failing zone cannot indefinitely prevent other zones from updating.
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Minute)
|
|
|
|
|
defer cancel()
|
|
|
|
|
newZ := file.NewZone(name, "")
|
|
|
|
|
if hostedZone.private {
|
|
|
|
|
page, err := h.privateClient.List(ctx, hostedZone.id, hostedZone.zone, nil, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for page.NotDone() {
|
|
|
|
|
updateZoneFromPrivateResourceSet(page, newZ)
|
|
|
|
|
if err := page.NextWithContext(ctx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
page, err := h.publicClient.ListByDNSZone(ctx, hostedZone.id, hostedZone.zone, nil, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for page.NotDone() {
|
|
|
|
|
updateZoneFromPublicResourceSet(page, newZ)
|
|
|
|
|
if err := page.NextWithContext(ctx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
|
return ctx.Err()
|
|
|
|
|
}
|
|
|
|
|
if newZ.SOA == nil {
|
|
|
|
|
return fmt.Errorf("zone has no SOA record")
|
|
|
|
|
}
|
|
|
|
|
newZ.Upstream = h.upstream
|
|
|
|
|
h.zMu.Lock()
|
|
|
|
|
hostedZone.z = newZ
|
|
|
|
|
h.zMu.Unlock()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 11:12:47 +01:00
|
|
|
func updateZoneFromPublicResourceSet(recordSet publicdns.RecordSetListResultPage, newZ *file.Zone) {
|
2019-08-09 12:40:28 +05:30
|
|
|
for _, result := range *(recordSet.Response().Value) {
|
2025-04-04 20:27:39 +02:00
|
|
|
resultFqdn := *(result.Fqdn)
|
2026-01-01 13:50:29 +05:30
|
|
|
resultTTL := uint32(*(result.TTL)) // #nosec G115 -- Azure API guarantees TTL fits in uint32
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.ARecords != nil {
|
|
|
|
|
for _, A := range *(result.ARecords) {
|
2019-08-09 16:10:26 +01:00
|
|
|
a := &dns.A{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: resultTTL},
|
2019-08-09 12:40:28 +05:30
|
|
|
A: net.ParseIP(*(A.Ipv4Address))}
|
2019-08-09 16:10:26 +01:00
|
|
|
newZ.Insert(a)
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.AaaaRecords != nil {
|
|
|
|
|
for _, AAAA := range *(result.AaaaRecords) {
|
2019-08-09 16:10:26 +01:00
|
|
|
aaaa := &dns.AAAA{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: resultTTL},
|
2019-08-09 12:40:28 +05:30
|
|
|
AAAA: net.ParseIP(*(AAAA.Ipv6Address))}
|
2019-08-09 16:10:26 +01:00
|
|
|
newZ.Insert(aaaa)
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.MxRecords != nil {
|
|
|
|
|
for _, MX := range *(result.MxRecords) {
|
2019-08-09 16:10:26 +01:00
|
|
|
mx := &dns.MX{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeMX, Class: dns.ClassINET, Ttl: resultTTL},
|
2026-01-01 13:50:29 +05:30
|
|
|
Preference: uint16(*(MX.Preference)), // #nosec G115 -- MX preference fits in uint16
|
2019-08-09 12:40:28 +05:30
|
|
|
Mx: dns.Fqdn(*(MX.Exchange))}
|
2019-08-09 16:10:26 +01:00
|
|
|
newZ.Insert(mx)
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.PtrRecords != nil {
|
|
|
|
|
for _, PTR := range *(result.PtrRecords) {
|
2019-08-09 16:10:26 +01:00
|
|
|
ptr := &dns.PTR{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: resultTTL},
|
2019-08-09 12:40:28 +05:30
|
|
|
Ptr: dns.Fqdn(*(PTR.Ptrdname))}
|
2019-08-09 16:10:26 +01:00
|
|
|
newZ.Insert(ptr)
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.SrvRecords != nil {
|
|
|
|
|
for _, SRV := range *(result.SrvRecords) {
|
2019-08-09 16:10:26 +01:00
|
|
|
srv := &dns.SRV{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeSRV, Class: dns.ClassINET, Ttl: resultTTL},
|
2026-01-01 13:50:29 +05:30
|
|
|
Priority: uint16(*(SRV.Priority)), // #nosec G115 -- SRV priority fits in uint16
|
|
|
|
|
Weight: uint16(*(SRV.Weight)), // #nosec G115 -- SRV weight fits in uint16
|
|
|
|
|
Port: uint16(*(SRV.Port)), // #nosec G115 -- Port fits in uint16
|
2019-08-09 12:40:28 +05:30
|
|
|
Target: dns.Fqdn(*(SRV.Target))}
|
2019-08-09 16:10:26 +01:00
|
|
|
newZ.Insert(srv)
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.TxtRecords != nil {
|
|
|
|
|
for _, TXT := range *(result.TxtRecords) {
|
2019-08-09 16:10:26 +01:00
|
|
|
txt := &dns.TXT{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: resultTTL},
|
2019-08-09 12:40:28 +05:30
|
|
|
Txt: *(TXT.Value)}
|
2019-08-09 16:10:26 +01:00
|
|
|
newZ.Insert(txt)
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.NsRecords != nil {
|
|
|
|
|
for _, NS := range *(result.NsRecords) {
|
2019-08-09 16:10:26 +01:00
|
|
|
ns := &dns.NS{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeNS, Class: dns.ClassINET, Ttl: resultTTL},
|
2019-08-09 12:40:28 +05:30
|
|
|
Ns: *(NS.Nsdname)}
|
2019-08-09 16:10:26 +01:00
|
|
|
newZ.Insert(ns)
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.SoaRecord != nil {
|
|
|
|
|
SOA := result.SoaRecord
|
2019-08-09 16:10:26 +01:00
|
|
|
soa := &dns.SOA{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: resultTTL},
|
2026-01-01 13:50:29 +05:30
|
|
|
Minttl: uint32(*(SOA.MinimumTTL)), // #nosec G115 -- DNS protocol mandates uint32 for SOA
|
|
|
|
|
Expire: uint32(*(SOA.ExpireTime)), // #nosec G115 -- DNS protocol mandates uint32 for SOA
|
|
|
|
|
Retry: uint32(*(SOA.RetryTime)), // #nosec G115 -- DNS protocol mandates uint32 for SOA
|
|
|
|
|
Refresh: uint32(*(SOA.RefreshTime)), // #nosec G115 -- DNS protocol mandates uint32 for SOA
|
|
|
|
|
Serial: uint32(*(SOA.SerialNumber)), // #nosec G115 -- DNS protocol mandates uint32 for SOA
|
2019-08-09 12:40:28 +05:30
|
|
|
Mbox: dns.Fqdn(*(SOA.Email)),
|
|
|
|
|
Ns: *(SOA.Host)}
|
2019-08-09 16:10:26 +01:00
|
|
|
newZ.Insert(soa)
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
|
|
|
|
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.CnameRecord != nil {
|
|
|
|
|
CNAME := result.CnameRecord.Cname
|
2019-08-09 16:10:26 +01:00
|
|
|
cname := &dns.CNAME{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: resultTTL},
|
2019-08-09 12:40:28 +05:30
|
|
|
Target: dns.Fqdn(*CNAME)}
|
2019-08-09 16:10:26 +01:00
|
|
|
newZ.Insert(cname)
|
2019-08-09 12:40:28 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 11:12:47 +01:00
|
|
|
func updateZoneFromPrivateResourceSet(recordSet privatedns.RecordSetListResultPage, newZ *file.Zone) {
|
2020-03-11 00:52:23 +05:30
|
|
|
for _, result := range *(recordSet.Response().Value) {
|
2025-04-04 20:27:39 +02:00
|
|
|
resultFqdn := *(result.Fqdn)
|
2026-01-01 13:50:29 +05:30
|
|
|
resultTTL := uint32(*(result.TTL)) // #nosec G115 -- Azure API guarantees TTL fits in uint32
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.ARecords != nil {
|
|
|
|
|
for _, A := range *(result.ARecords) {
|
2020-03-11 00:52:23 +05:30
|
|
|
a := &dns.A{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: resultTTL},
|
|
|
|
|
A: net.ParseIP(*(A.Ipv4Address))}
|
|
|
|
|
newZ.Insert(a)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.AaaaRecords != nil {
|
|
|
|
|
for _, AAAA := range *(result.AaaaRecords) {
|
2020-03-11 00:52:23 +05:30
|
|
|
aaaa := &dns.AAAA{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: resultTTL},
|
|
|
|
|
AAAA: net.ParseIP(*(AAAA.Ipv6Address))}
|
|
|
|
|
newZ.Insert(aaaa)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.MxRecords != nil {
|
|
|
|
|
for _, MX := range *(result.MxRecords) {
|
2020-03-11 00:52:23 +05:30
|
|
|
mx := &dns.MX{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeMX, Class: dns.ClassINET, Ttl: resultTTL},
|
2026-01-01 13:50:29 +05:30
|
|
|
Preference: uint16(*(MX.Preference)), // #nosec G115 -- MX preference fits in uint16
|
2020-03-11 00:52:23 +05:30
|
|
|
Mx: dns.Fqdn(*(MX.Exchange))}
|
|
|
|
|
newZ.Insert(mx)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.PtrRecords != nil {
|
|
|
|
|
for _, PTR := range *(result.PtrRecords) {
|
2020-03-11 00:52:23 +05:30
|
|
|
ptr := &dns.PTR{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: resultTTL},
|
|
|
|
|
Ptr: dns.Fqdn(*(PTR.Ptrdname))}
|
|
|
|
|
newZ.Insert(ptr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.SrvRecords != nil {
|
|
|
|
|
for _, SRV := range *(result.SrvRecords) {
|
2020-03-11 00:52:23 +05:30
|
|
|
srv := &dns.SRV{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeSRV, Class: dns.ClassINET, Ttl: resultTTL},
|
2026-01-01 13:50:29 +05:30
|
|
|
Priority: uint16(*(SRV.Priority)), // #nosec G115 -- SRV priority fits in uint16
|
|
|
|
|
Weight: uint16(*(SRV.Weight)), // #nosec G115 -- SRV weight fits in uint16
|
|
|
|
|
Port: uint16(*(SRV.Port)), // #nosec G115 -- Port fits in uint16
|
2020-03-11 00:52:23 +05:30
|
|
|
Target: dns.Fqdn(*(SRV.Target))}
|
|
|
|
|
newZ.Insert(srv)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.TxtRecords != nil {
|
|
|
|
|
for _, TXT := range *(result.TxtRecords) {
|
2020-03-11 00:52:23 +05:30
|
|
|
txt := &dns.TXT{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: resultTTL},
|
|
|
|
|
Txt: *(TXT.Value)}
|
|
|
|
|
newZ.Insert(txt)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.SoaRecord != nil {
|
|
|
|
|
SOA := result.SoaRecord
|
2020-03-11 00:52:23 +05:30
|
|
|
soa := &dns.SOA{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: resultTTL},
|
2026-01-01 13:50:29 +05:30
|
|
|
Minttl: uint32(*(SOA.MinimumTTL)), // #nosec G115 -- DNS protocol mandates uint32 for SOA
|
|
|
|
|
Expire: uint32(*(SOA.ExpireTime)), // #nosec G115 -- DNS protocol mandates uint32 for SOA
|
|
|
|
|
Retry: uint32(*(SOA.RetryTime)), // #nosec G115 -- DNS protocol mandates uint32 for SOA
|
|
|
|
|
Refresh: uint32(*(SOA.RefreshTime)), // #nosec G115 -- DNS protocol mandates uint32 for SOA
|
|
|
|
|
Serial: uint32(*(SOA.SerialNumber)), // #nosec G115 -- DNS protocol mandates uint32 for SOA
|
2020-03-11 00:52:23 +05:30
|
|
|
Mbox: dns.Fqdn(*(SOA.Email)),
|
2020-11-12 14:48:03 +00:00
|
|
|
Ns: dns.Fqdn(*(SOA.Host))}
|
2020-03-11 00:52:23 +05:30
|
|
|
newZ.Insert(soa)
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-04 20:27:39 +02:00
|
|
|
if result.CnameRecord != nil {
|
|
|
|
|
CNAME := result.CnameRecord.Cname
|
2020-03-11 00:52:23 +05:30
|
|
|
cname := &dns.CNAME{Hdr: dns.RR_Header{Name: resultFqdn, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: resultTTL},
|
|
|
|
|
Target: dns.Fqdn(*CNAME)}
|
|
|
|
|
newZ.Insert(cname)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-09 12:40:28 +05:30
|
|
|
// ServeDNS implements the plugin.Handler interface.
|
|
|
|
|
func (h *Azure) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
|
|
|
|
state := request.Request{W: w, Req: r}
|
|
|
|
|
qname := state.Name()
|
|
|
|
|
|
2019-08-09 16:10:26 +01:00
|
|
|
zone := plugin.Zones(h.zoneNames).Matches(qname)
|
|
|
|
|
if zone == "" {
|
2019-08-09 12:40:28 +05:30
|
|
|
return plugin.NextOrFailure(h.Name(), h.Next, ctx, w, r)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 16:08:55 -04:00
|
|
|
zones, ok := h.zones[zone] // ok true if we are authoritative for the zone.
|
2019-08-09 16:10:26 +01:00
|
|
|
if !ok || zones == nil {
|
2019-08-09 12:40:28 +05:30
|
|
|
return dns.RcodeServerFailure, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m := new(dns.Msg)
|
|
|
|
|
m.SetReply(r)
|
|
|
|
|
m.Authoritative = true
|
|
|
|
|
var result file.Result
|
2019-08-09 16:10:26 +01:00
|
|
|
for _, z := range zones {
|
2026-08-17 05:11:16 -07:00
|
|
|
// Only the zone pointer itself needs to be guarded against a
|
|
|
|
|
// concurrent swap in updateZones; Lookup can run unlocked since it
|
|
|
|
|
// may block for a while resolving external names via upstream.
|
2019-08-09 12:40:28 +05:30
|
|
|
h.zMu.RLock()
|
2026-08-17 05:11:16 -07:00
|
|
|
zz := z.z
|
2019-08-09 12:40:28 +05:30
|
|
|
h.zMu.RUnlock()
|
|
|
|
|
|
2026-08-17 05:11:16 -07:00
|
|
|
m.Answer, m.Ns, m.Extra, result = zz.Lookup(ctx, state, qname)
|
|
|
|
|
|
2019-08-09 12:40:28 +05:30
|
|
|
// record type exists for this name (NODATA).
|
|
|
|
|
if len(m.Answer) != 0 || result == file.NoData {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(m.Answer) == 0 && result != file.NoData && h.Fall.Through(qname) {
|
|
|
|
|
return plugin.NextOrFailure(h.Name(), h.Next, ctx, w, r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
|
case file.Success:
|
|
|
|
|
case file.NoData:
|
|
|
|
|
case file.NameError:
|
|
|
|
|
m.Rcode = dns.RcodeNameError
|
|
|
|
|
case file.Delegation:
|
|
|
|
|
m.Authoritative = false
|
|
|
|
|
case file.ServerFailure:
|
|
|
|
|
return dns.RcodeServerFailure, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.WriteMsg(m)
|
|
|
|
|
return dns.RcodeSuccess, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Name implements plugin.Handler.Name.
|
|
|
|
|
func (h *Azure) Name() string { return "azure" }
|