2016-02-29 02:48:41 +00:00
|
|
|
package rfc2136
|
2015-12-03 20:01:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-02-28 20:09:05 +00:00
|
|
|
"fmt"
|
2015-12-03 20:01:46 +00:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2016-01-27 00:57:55 +00:00
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
2016-01-27 00:57:55 +00:00
|
|
|
"github.com/miekg/dns"
|
2018-06-11 15:32:50 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-12-03 20:01:46 +00:00
|
|
|
)
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
const (
|
2020-03-11 22:51:10 +00:00
|
|
|
fakeDomain = "123456789.www.example.com"
|
|
|
|
fakeKeyAuth = "123d=="
|
|
|
|
fakeValue = "Now36o-3BmlB623-0c1qCIUmgWVVmDJb88KGl24pqpo"
|
|
|
|
fakeFqdn = "_acme-challenge.123456789.www.example.com."
|
|
|
|
fakeZone = "example.com."
|
|
|
|
fakeTTL = 120
|
|
|
|
fakeTsigKey = "example.com."
|
|
|
|
fakeTsigSecret = "IwBTJx9wrDp4Y1RyC3H0gA=="
|
2015-12-03 20:01:46 +00:00
|
|
|
)
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestCanaryLocalTestServer(t *testing.T) {
|
2018-12-06 21:50:17 +00:00
|
|
|
dns01.ClearFqdnCache()
|
2015-12-03 20:01:46 +00:00
|
|
|
dns.HandleFunc("example.com.", serverHandlerHello)
|
|
|
|
defer dns.HandleRemove("example.com.")
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
server, addr, err := runLocalDNSTestServer(false)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "Failed to start test server")
|
2018-10-12 17:29:18 +00:00
|
|
|
defer func() { _ = server.Shutdown() }()
|
2015-12-03 20:01:46 +00:00
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
m := new(dns.Msg)
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
m.SetQuestion("example.com.", dns.TypeTXT)
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
r, _, err := c.Exchange(m, addr)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "Failed to communicate with test server")
|
|
|
|
assert.Len(t, r.Extra, 1, "Failed to communicate with test server")
|
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
txt := r.Extra[0].(*dns.TXT).Txt[0]
|
2018-06-11 15:32:50 +00:00
|
|
|
assert.Equal(t, "Hello world", txt)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestServerSuccess(t *testing.T) {
|
2018-12-06 21:50:17 +00:00
|
|
|
dns01.ClearFqdnCache()
|
2020-03-11 22:51:10 +00:00
|
|
|
dns.HandleFunc(fakeZone, serverHandlerReturnSuccess)
|
|
|
|
defer dns.HandleRemove(fakeZone)
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
server, addr, err := runLocalDNSTestServer(false)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "Failed to start test server")
|
2018-10-12 17:29:18 +00:00
|
|
|
defer func() { _ = server.Shutdown() }()
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2018-10-12 17:29:18 +00:00
|
|
|
config.Nameserver = addr
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
err = provider.Present(fakeDomain, "", fakeKeyAuth)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestServerError(t *testing.T) {
|
2018-12-06 21:50:17 +00:00
|
|
|
dns01.ClearFqdnCache()
|
2020-03-11 22:51:10 +00:00
|
|
|
dns.HandleFunc(fakeZone, serverHandlerReturnErr)
|
|
|
|
defer dns.HandleRemove(fakeZone)
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
server, addr, err := runLocalDNSTestServer(false)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "Failed to start test server")
|
2018-10-12 17:29:18 +00:00
|
|
|
defer func() { _ = server.Shutdown() }()
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2018-10-12 17:29:18 +00:00
|
|
|
config.Nameserver = addr
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
err = provider.Present(fakeDomain, "", fakeKeyAuth)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
if !strings.Contains(err.Error(), "NOTZONE") {
|
|
|
|
t.Errorf("Expected Present() to return an error with the 'NOTZONE' rcode string but it did not: %v", err)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestTsigClient(t *testing.T) {
|
2018-12-06 21:50:17 +00:00
|
|
|
dns01.ClearFqdnCache()
|
2020-03-11 22:51:10 +00:00
|
|
|
dns.HandleFunc(fakeZone, serverHandlerReturnSuccess)
|
|
|
|
defer dns.HandleRemove(fakeZone)
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
server, addr, err := runLocalDNSTestServer(true)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "Failed to start test server")
|
2018-10-12 17:29:18 +00:00
|
|
|
defer func() { _ = server.Shutdown() }()
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2018-10-12 17:29:18 +00:00
|
|
|
config.Nameserver = addr
|
2020-03-11 22:51:10 +00:00
|
|
|
config.TSIGKey = fakeTsigKey
|
|
|
|
config.TSIGSecret = fakeTsigSecret
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
err = provider.Present(fakeDomain, "", fakeKeyAuth)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestValidUpdatePacket(t *testing.T) {
|
2020-07-09 23:48:18 +00:00
|
|
|
reqChan := make(chan *dns.Msg, 10)
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
dns01.ClearFqdnCache()
|
2020-03-11 22:51:10 +00:00
|
|
|
dns.HandleFunc(fakeZone, serverHandlerPassBackRequest(reqChan))
|
|
|
|
defer dns.HandleRemove(fakeZone)
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
server, addr, err := runLocalDNSTestServer(false)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "Failed to start test server")
|
2018-10-12 17:29:18 +00:00
|
|
|
defer func() { _ = server.Shutdown() }()
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
txtRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN TXT %s", fakeFqdn, fakeTTL, fakeValue))
|
2016-02-28 20:09:05 +00:00
|
|
|
rrs := []dns.RR{txtRR}
|
2015-12-03 20:01:46 +00:00
|
|
|
m := new(dns.Msg)
|
2020-03-11 22:51:10 +00:00
|
|
|
m.SetUpdate(fakeZone)
|
2016-02-28 20:09:05 +00:00
|
|
|
m.RemoveRRset(rrs)
|
2015-12-03 20:01:46 +00:00
|
|
|
m.Insert(rrs)
|
2018-10-12 17:29:18 +00:00
|
|
|
expectStr := m.String()
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
expect, err := m.Pack()
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "error packing")
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2018-10-12 17:29:18 +00:00
|
|
|
config.Nameserver = addr
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
2016-01-15 04:06:25 +00:00
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
err = provider.Present(fakeDomain, "", "1234d==")
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
2015-12-03 20:01:46 +00:00
|
|
|
|
|
|
|
rcvMsg := <-reqChan
|
|
|
|
rcvMsg.Id = m.Id
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
actual, err := rcvMsg.Pack()
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "error packing")
|
2015-12-03 20:01:46 +00:00
|
|
|
|
|
|
|
if !bytes.Equal(actual, expect) {
|
|
|
|
tmp := new(dns.Msg)
|
|
|
|
if err := tmp.Unpack(actual); err != nil {
|
|
|
|
t.Fatalf("Error unpacking actual msg: %v", err)
|
|
|
|
}
|
2018-10-12 17:29:18 +00:00
|
|
|
t.Errorf("Expected msg:\n%s", expectStr)
|
2015-12-03 20:01:46 +00:00
|
|
|
t.Errorf("Actual msg:\n%v", tmp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
func runLocalDNSTestServer(tsig bool) (*dns.Server, string, error) {
|
|
|
|
pc, err := net.ListenPacket("udp", "127.0.0.1:0")
|
2015-12-03 20:01:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2018-10-12 17:29:18 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
server := &dns.Server{
|
|
|
|
PacketConn: pc,
|
|
|
|
ReadTimeout: time.Hour,
|
|
|
|
WriteTimeout: time.Hour,
|
|
|
|
MsgAcceptFunc: func(dh dns.Header) dns.MsgAcceptAction {
|
|
|
|
// bypass defaultMsgAcceptFunc to allow dynamic update (https://github.com/miekg/dns/pull/830)
|
|
|
|
return dns.MsgAccept
|
2020-07-09 23:48:18 +00:00
|
|
|
},
|
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
if tsig {
|
2020-03-11 22:51:10 +00:00
|
|
|
server.TsigSecret = map[string]string{fakeTsigKey: fakeTsigSecret}
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
waitLock := sync.Mutex{}
|
|
|
|
waitLock.Lock()
|
|
|
|
server.NotifyStartedFunc = waitLock.Unlock
|
|
|
|
|
|
|
|
go func() {
|
2018-09-24 19:07:20 +00:00
|
|
|
_ = server.ActivateAndServe()
|
2015-12-03 20:01:46 +00:00
|
|
|
pc.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
waitLock.Lock()
|
|
|
|
return server, pc.LocalAddr().String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func serverHandlerHello(w dns.ResponseWriter, req *dns.Msg) {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetReply(req)
|
|
|
|
m.Extra = make([]dns.RR, 1)
|
|
|
|
m.Extra[0] = &dns.TXT{
|
|
|
|
Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: 0},
|
|
|
|
Txt: []string{"Hello world"},
|
|
|
|
}
|
2018-09-24 19:07:20 +00:00
|
|
|
_ = w.WriteMsg(m)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func serverHandlerReturnSuccess(w dns.ResponseWriter, req *dns.Msg) {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetReply(req)
|
2016-02-28 20:09:05 +00:00
|
|
|
if req.Opcode == dns.OpcodeQuery && req.Question[0].Qtype == dns.TypeSOA && req.Question[0].Qclass == dns.ClassINET {
|
|
|
|
// Return SOA to appease findZoneByFqdn()
|
2020-03-11 22:51:10 +00:00
|
|
|
soaRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN SOA ns1.%s admin.%s 2016022801 28800 7200 2419200 1200", fakeZone, fakeTTL, fakeZone, fakeZone))
|
2016-02-28 20:09:05 +00:00
|
|
|
m.Answer = []dns.RR{soaRR}
|
|
|
|
}
|
2015-12-03 20:01:46 +00:00
|
|
|
|
|
|
|
if t := req.IsTsig(); t != nil {
|
|
|
|
if w.TsigStatus() == nil {
|
|
|
|
// Validated
|
2020-03-11 22:51:10 +00:00
|
|
|
m.SetTsig(fakeZone, dns.HmacMD5, 300, time.Now().Unix())
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
_ = w.WriteMsg(m)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func serverHandlerReturnErr(w dns.ResponseWriter, req *dns.Msg) {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetRcode(req, dns.RcodeNotZone)
|
2018-09-24 19:07:20 +00:00
|
|
|
_ = w.WriteMsg(m)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
func serverHandlerPassBackRequest(reqChan chan *dns.Msg) func(w dns.ResponseWriter, req *dns.Msg) {
|
|
|
|
return func(w dns.ResponseWriter, req *dns.Msg) {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetReply(req)
|
|
|
|
if req.Opcode == dns.OpcodeQuery && req.Question[0].Qtype == dns.TypeSOA && req.Question[0].Qclass == dns.ClassINET {
|
|
|
|
// Return SOA to appease findZoneByFqdn()
|
2020-03-11 22:51:10 +00:00
|
|
|
soaRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN SOA ns1.%s admin.%s 2016022801 28800 7200 2419200 1200", fakeZone, fakeTTL, fakeZone, fakeZone))
|
2018-12-06 21:50:17 +00:00
|
|
|
m.Answer = []dns.RR{soaRR}
|
|
|
|
}
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
if t := req.IsTsig(); t != nil {
|
|
|
|
if w.TsigStatus() == nil {
|
|
|
|
// Validated
|
2020-03-11 22:51:10 +00:00
|
|
|
m.SetTsig(fakeZone, dns.HmacMD5, 300, time.Now().Unix())
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
_ = w.WriteMsg(m)
|
|
|
|
if req.Opcode != dns.OpcodeQuery || req.Question[0].Qtype != dns.TypeSOA || req.Question[0].Qclass != dns.ClassINET {
|
|
|
|
// Only talk back when it is not the SOA RR.
|
|
|
|
reqChan <- req
|
|
|
|
}
|
2016-02-28 20:09:05 +00:00
|
|
|
}
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|