forked from TrueCloudLab/lego
Check DNS entry for validity before hitting boulder
This commit is contained in:
parent
6863cc0c5b
commit
a3f134e3fb
2 changed files with 50 additions and 0 deletions
|
@ -9,8 +9,18 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type preCheckDNSFunc func() bool
|
||||||
|
|
||||||
|
var preCheckDNS = func() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var preCheckDNSFallbackCount = 5
|
||||||
|
|
||||||
// DNSProvider represents a service for creating dns records.
|
// DNSProvider represents a service for creating dns records.
|
||||||
type DNSProvider interface {
|
type DNSProvider interface {
|
||||||
// CreateTXT creates a TXT record
|
// CreateTXT creates a TXT record
|
||||||
|
@ -44,6 +54,43 @@ func (s *dnsChallenge) Solve(chlng challenge, domain string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if preCheckDNS() {
|
||||||
|
// check if the expected DNS entry was created. If not wait for some time and try again.
|
||||||
|
m := new(dns.Msg)
|
||||||
|
m.SetQuestion(domain+".", dns.TypeSOA)
|
||||||
|
c := new(dns.Client)
|
||||||
|
in, _, err := c.Exchange(m, "8.8.8.8:53")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var authorativeNS string
|
||||||
|
for _, answ := range in.Answer {
|
||||||
|
soa := answ.(*dns.SOA)
|
||||||
|
authorativeNS = soa.Ns
|
||||||
|
}
|
||||||
|
|
||||||
|
fallbackCnt := 0
|
||||||
|
for fallbackCnt < preCheckDNSFallbackCount {
|
||||||
|
m.SetQuestion(fqdn, dns.TypeTXT)
|
||||||
|
in, _, err = c.Exchange(m, authorativeNS+":53")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(in.Answer) > 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
fallbackCnt++
|
||||||
|
if fallbackCnt >= preCheckDNSFallbackCount {
|
||||||
|
return errors.New("Could not retrieve the value from DNS in a timely manner. Aborting.")
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(time.Second * time.Duration(fallbackCnt))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
jsonBytes, err := json.Marshal(challenge{Resource: "challenge", Type: chlng.Type, Token: chlng.Token, KeyAuthorization: keyAuth})
|
jsonBytes, err := json.Marshal(challenge{Resource: "challenge", Type: chlng.Type, Token: chlng.Token, KeyAuthorization: keyAuth})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Failed to marshal network message...")
|
return errors.New("Failed to marshal network message...")
|
||||||
|
|
|
@ -11,6 +11,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDNSValidServerResponse(t *testing.T) {
|
func TestDNSValidServerResponse(t *testing.T) {
|
||||||
|
preCheckDNS = func() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
privKey, _ := generatePrivateKey(rsakey, 512)
|
privKey, _ := generatePrivateKey(rsakey, 512)
|
||||||
|
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
Loading…
Reference in a new issue