2016-02-29 04:48:41 +02:00
|
|
|
package rfc2136
|
2015-12-03 22:01:46 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-02-28 22:09:05 +02:00
|
|
|
"fmt"
|
2015-12-03 22:01:46 +02:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2016-01-27 02:57:55 +02:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2018-06-11 17:32:50 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-12-06 23:50:17 +02:00
|
|
|
"github.com/xenolf/lego/challenge/dns01"
|
2015-12-03 22:01:46 +02:00
|
|
|
)
|
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
const (
|
2018-10-12 19:29:18 +02:00
|
|
|
envTestDomain = "123456789.www.example.com"
|
|
|
|
envTestKeyAuth = "123d=="
|
|
|
|
envTestValue = "Now36o-3BmlB623-0c1qCIUmgWVVmDJb88KGl24pqpo"
|
|
|
|
envTestFqdn = "_acme-challenge.123456789.www.example.com."
|
|
|
|
envTestZone = "example.com."
|
|
|
|
envTestTTL = 120
|
|
|
|
envTestTsigKey = "example.com."
|
|
|
|
envTestTsigSecret = "IwBTJx9wrDp4Y1RyC3H0gA=="
|
2015-12-03 22:01:46 +02:00
|
|
|
)
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestCanaryLocalTestServer(t *testing.T) {
|
2018-12-06 23:50:17 +02:00
|
|
|
dns01.ClearFqdnCache()
|
2015-12-03 22:01:46 +02:00
|
|
|
dns.HandleFunc("example.com.", serverHandlerHello)
|
|
|
|
defer dns.HandleRemove("example.com.")
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
server, addr, err := runLocalDNSTestServer(false)
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err, "Failed to start test server")
|
2018-10-12 19:29:18 +02:00
|
|
|
defer func() { _ = server.Shutdown() }()
|
2015-12-03 22:01:46 +02:00
|
|
|
|
|
|
|
c := new(dns.Client)
|
|
|
|
m := new(dns.Msg)
|
2018-06-11 17:32:50 +02:00
|
|
|
|
2015-12-03 22:01:46 +02:00
|
|
|
m.SetQuestion("example.com.", dns.TypeTXT)
|
2018-06-11 17:32:50 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
r, _, err := c.Exchange(m, addr)
|
2018-06-11 17:32:50 +02: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 22:01:46 +02:00
|
|
|
txt := r.Extra[0].(*dns.TXT).Txt[0]
|
2018-06-11 17:32:50 +02:00
|
|
|
assert.Equal(t, "Hello world", txt)
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestServerSuccess(t *testing.T) {
|
2018-12-06 23:50:17 +02:00
|
|
|
dns01.ClearFqdnCache()
|
2018-10-12 19:29:18 +02:00
|
|
|
dns.HandleFunc(envTestZone, serverHandlerReturnSuccess)
|
|
|
|
defer dns.HandleRemove(envTestZone)
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
server, addr, err := runLocalDNSTestServer(false)
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err, "Failed to start test server")
|
2018-10-12 19:29:18 +02:00
|
|
|
defer func() { _ = server.Shutdown() }()
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
2018-10-12 19:29:18 +02:00
|
|
|
config.Nameserver = addr
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
err = provider.Present(envTestDomain, "", envTestKeyAuth)
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err)
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestServerError(t *testing.T) {
|
2018-12-06 23:50:17 +02:00
|
|
|
dns01.ClearFqdnCache()
|
2018-10-12 19:29:18 +02:00
|
|
|
dns.HandleFunc(envTestZone, serverHandlerReturnErr)
|
|
|
|
defer dns.HandleRemove(envTestZone)
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
server, addr, err := runLocalDNSTestServer(false)
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err, "Failed to start test server")
|
2018-10-12 19:29:18 +02:00
|
|
|
defer func() { _ = server.Shutdown() }()
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
2018-10-12 19:29:18 +02:00
|
|
|
config.Nameserver = addr
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
err = provider.Present(envTestDomain, "", envTestKeyAuth)
|
2018-06-11 17:32:50 +02: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 22:01:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestTsigClient(t *testing.T) {
|
2018-12-06 23:50:17 +02:00
|
|
|
dns01.ClearFqdnCache()
|
2018-10-12 19:29:18 +02:00
|
|
|
dns.HandleFunc(envTestZone, serverHandlerReturnSuccess)
|
|
|
|
defer dns.HandleRemove(envTestZone)
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
server, addr, err := runLocalDNSTestServer(true)
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err, "Failed to start test server")
|
2018-10-12 19:29:18 +02:00
|
|
|
defer func() { _ = server.Shutdown() }()
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
2018-10-12 19:29:18 +02:00
|
|
|
config.Nameserver = addr
|
|
|
|
config.TSIGKey = envTestTsigKey
|
|
|
|
config.TSIGSecret = envTestTsigSecret
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
err = provider.Present(envTestDomain, "", envTestKeyAuth)
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err)
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestValidUpdatePacket(t *testing.T) {
|
2018-12-06 23:50:17 +02:00
|
|
|
var reqChan = make(chan *dns.Msg, 10)
|
|
|
|
|
|
|
|
dns01.ClearFqdnCache()
|
|
|
|
dns.HandleFunc(envTestZone, serverHandlerPassBackRequest(reqChan))
|
2018-10-12 19:29:18 +02:00
|
|
|
defer dns.HandleRemove(envTestZone)
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
server, addr, err := runLocalDNSTestServer(false)
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err, "Failed to start test server")
|
2018-10-12 19:29:18 +02:00
|
|
|
defer func() { _ = server.Shutdown() }()
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
txtRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN TXT %s", envTestFqdn, envTestTTL, envTestValue))
|
2016-02-28 22:09:05 +02:00
|
|
|
rrs := []dns.RR{txtRR}
|
2015-12-03 22:01:46 +02:00
|
|
|
m := new(dns.Msg)
|
2018-10-12 19:29:18 +02:00
|
|
|
m.SetUpdate(envTestZone)
|
2016-02-28 22:09:05 +02:00
|
|
|
m.RemoveRRset(rrs)
|
2015-12-03 22:01:46 +02:00
|
|
|
m.Insert(rrs)
|
2018-10-12 19:29:18 +02:00
|
|
|
expectStr := m.String()
|
2018-06-11 17:32:50 +02:00
|
|
|
|
2015-12-03 22:01:46 +02:00
|
|
|
expect, err := m.Pack()
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err, "error packing")
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
2018-10-12 19:29:18 +02:00
|
|
|
config.Nameserver = addr
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err)
|
2016-01-15 06:06:25 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
err = provider.Present(envTestDomain, "", "1234d==")
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err)
|
2015-12-03 22:01:46 +02:00
|
|
|
|
|
|
|
rcvMsg := <-reqChan
|
|
|
|
rcvMsg.Id = m.Id
|
2018-06-11 17:32:50 +02:00
|
|
|
|
2015-12-03 22:01:46 +02:00
|
|
|
actual, err := rcvMsg.Pack()
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err, "error packing")
|
2015-12-03 22:01:46 +02: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 19:29:18 +02:00
|
|
|
t.Errorf("Expected msg:\n%s", expectStr)
|
2015-12-03 22:01:46 +02:00
|
|
|
t.Errorf("Actual msg:\n%v", tmp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 21:07:20 +02:00
|
|
|
func runLocalDNSTestServer(tsig bool) (*dns.Server, string, error) {
|
|
|
|
pc, err := net.ListenPacket("udp", "127.0.0.1:0")
|
2015-12-03 22:01:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2018-10-12 19:29:18 +02:00
|
|
|
|
2018-12-06 23:50:17 +02: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
|
|
|
|
}}
|
|
|
|
|
2015-12-03 22:01:46 +02:00
|
|
|
if tsig {
|
2018-10-12 19:29:18 +02:00
|
|
|
server.TsigSecret = map[string]string{envTestTsigKey: envTestTsigSecret}
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
waitLock := sync.Mutex{}
|
|
|
|
waitLock.Lock()
|
|
|
|
server.NotifyStartedFunc = waitLock.Unlock
|
|
|
|
|
|
|
|
go func() {
|
2018-09-24 21:07:20 +02:00
|
|
|
_ = server.ActivateAndServe()
|
2015-12-03 22:01:46 +02: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 21:07:20 +02:00
|
|
|
_ = w.WriteMsg(m)
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func serverHandlerReturnSuccess(w dns.ResponseWriter, req *dns.Msg) {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetReply(req)
|
2016-02-28 22:09:05 +02:00
|
|
|
if req.Opcode == dns.OpcodeQuery && req.Question[0].Qtype == dns.TypeSOA && req.Question[0].Qclass == dns.ClassINET {
|
|
|
|
// Return SOA to appease findZoneByFqdn()
|
2018-10-12 19:29:18 +02:00
|
|
|
soaRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN SOA ns1.%s admin.%s 2016022801 28800 7200 2419200 1200", envTestZone, envTestTTL, envTestZone, envTestZone))
|
2016-02-28 22:09:05 +02:00
|
|
|
m.Answer = []dns.RR{soaRR}
|
|
|
|
}
|
2015-12-03 22:01:46 +02:00
|
|
|
|
|
|
|
if t := req.IsTsig(); t != nil {
|
|
|
|
if w.TsigStatus() == nil {
|
|
|
|
// Validated
|
2018-10-12 19:29:18 +02:00
|
|
|
m.SetTsig(envTestZone, dns.HmacMD5, 300, time.Now().Unix())
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 21:07:20 +02:00
|
|
|
_ = w.WriteMsg(m)
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func serverHandlerReturnErr(w dns.ResponseWriter, req *dns.Msg) {
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetRcode(req, dns.RcodeNotZone)
|
2018-09-24 21:07:20 +02:00
|
|
|
_ = w.WriteMsg(m)
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 23:50:17 +02: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()
|
|
|
|
soaRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN SOA ns1.%s admin.%s 2016022801 28800 7200 2419200 1200", envTestZone, envTestTTL, envTestZone, envTestZone))
|
|
|
|
m.Answer = []dns.RR{soaRR}
|
|
|
|
}
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
if t := req.IsTsig(); t != nil {
|
|
|
|
if w.TsigStatus() == nil {
|
|
|
|
// Validated
|
|
|
|
m.SetTsig(envTestZone, dns.HmacMD5, 300, time.Now().Unix())
|
|
|
|
}
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 23:50:17 +02: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 22:09:05 +02:00
|
|
|
}
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|