Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Checks for Host header when issuing selftest #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/acme/acme.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func New(kubeLego kubelego.KubeLego) *Acme {
kubelego: kubeLego,
challengesHostToToken: map[string]string{},
challengesTokenToKey: map[string]string{},
domainsToVerify: map[string]struct{}{},
id: utils.RandomToken(16),
}
if kubeLego != nil {
Expand Down Expand Up @@ -57,6 +58,12 @@ func (a *Acme) Mux() *http.ServeMux {
mux.HandleFunc(kubelego.AcmeHttpChallengePath+"/", a.handleChallenge)

mux.HandleFunc(kubelego.AcmeHttpSelfTest, func(w http.ResponseWriter, r *http.Request) {
host := strings.Split(r.Host, ":")[0]
if !a.domainToVerifyExists(host) {
w.WriteHeader(http.StatusNotFound)
return
}

w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, a.id)
Expand Down
21 changes: 21 additions & 0 deletions pkg/acme/cert_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,28 @@ func (a *Acme) testReachablilty(domain string) error {
return nil
}

func (a *Acme) enableDomainVerification(domain string) {
a.domainsMutex.Lock()
a.domainsToVerify[domain] = struct{}{}
a.domainsMutex.Unlock()
}

func (a *Acme) disableDomainVerification(domain string) {
a.domainsMutex.Lock()
delete(a.domainsToVerify, domain)
a.domainsMutex.Unlock()
}

func (a *Acme) domainToVerifyExists(domain string) bool {
a.domainsMutex.RLock()
_, ok := a.domainsToVerify[domain]
a.domainsMutex.RUnlock()
return ok
}

func (a *Acme) verifyDomain(domain string) (auth *acme.Authorization, err error) {
a.enableDomainVerification(domain)
defer a.disableDomainVerification(domain)
err = a.testReachablilty(domain)
if err != nil {
return nil, fmt.Errorf("reachability test failed: %s", err)
Expand Down
3 changes: 3 additions & 0 deletions pkg/acme/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type Acme struct {
challengesHostToToken map[string]string
challengesTokenToKey map[string]string

domainsMutex sync.RWMutex
domainsToVerify map[string]struct{}

notFound string // string displayed for 404 messages
id string // identification (random string)

Expand Down