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
|
|
|
|
2019-03-11 16:56:48 +00:00
|
|
|
"github.com/go-acme/lego/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 (
|
2018-10-12 17:29:18 +00: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 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()
|
2018-10-12 17:29:18 +00:00
|
|
|
dns.HandleFunc(envTestZone, serverHandlerReturnSuccess)
|
|
|
|
defer dns.HandleRemove(envTestZone)
|
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)
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err = provider.Present(envTestDomain, "", envTestKeyAuth)
|
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()
|
2018-10-12 17:29:18 +00:00
|
|
|
dns.HandleFunc(envTestZone, serverHandlerReturnErr)
|
|
|
|
defer dns.HandleRemove(envTestZone)
|
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)
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err = provider.Present(envTestDomain, "", envTestKeyAuth)
|
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()
|
2018-10-12 17:29:18 +00:00
|
|
|
dns.HandleFunc(envTestZone, serverHandlerReturnSuccess)
|
|
|
|
defer dns.HandleRemove(envTestZone)
|
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
|
|
|
|
config.TSIGKey = envTestTsigKey
|
|
|
|
config.TSIGSecret = envTestTsigSecret
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err = provider.Present(envTestDomain, "", envTestKeyAuth)
|
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) {
|
2018-12-06 21:50:17 +00:00
|
|
|
var reqChan = make(chan *dns.Msg, 10)
|
|
|
|
|
|
|
|
dns01.ClearFqdnCache()
|
|
|
|
dns.HandleFunc(envTestZone, serverHandlerPassBackRequest(reqChan))
|
2018-10-12 17:29:18 +00:00
|
|
|
defer dns.HandleRemove(envTestZone)
|
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-10-12 17:29:18 +00:00
|
|
|
txtRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN TXT %s", envTestFqdn, envTestTTL, envTestValue))
|
2016-02-28 20:09:05 +00:00
|
|
|
rrs := []dns.RR{txtRR}
|
2015-12-03 20:01:46 +00:00
|
|
|
m := new(dns.Msg)
|
2018-10-12 17:29:18 +00:00
|
|
|
m.SetUpdate(envTestZone)
|
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
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err = provider.Present(envTestDomain, "", "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
|
|
|
|
}}
|
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
if tsig {
|
2018-10-12 17:29:18 +00:00
|
|
|
server.TsigSecret = map[string]string{envTestTsigKey: envTestTsigSecret}
|
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()
|
2018-10-12 17:29:18 +00: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 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
|
2018-10-12 17:29:18 +00:00
|
|
|
m.SetTsig(envTestZone, 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()
|
|
|
|
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 20:01:46 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
if t := req.IsTsig(); t != nil {
|
|
|
|
if w.TsigStatus() == nil {
|
|
|
|
// Validated
|
|
|
|
m.SetTsig(envTestZone, dns.HmacMD5, 300, time.Now().Unix())
|
|
|
|
}
|
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
|
|
|
}
|