2019-03-11 16:56:48 +00:00
|
|
|
package resolver
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
2019-02-09 04:40:45 +00:00
|
|
|
"context"
|
2018-12-06 21:50:17 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2020-01-12 16:16:46 +00:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2019-07-30 19:19:32 +00:00
|
|
|
"github.com/go-acme/lego/v3/acme"
|
|
|
|
"github.com/go-acme/lego/v3/acme/api"
|
|
|
|
"github.com/go-acme/lego/v3/challenge"
|
|
|
|
"github.com/go-acme/lego/v3/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v3/challenge/http01"
|
|
|
|
"github.com/go-acme/lego/v3/challenge/tlsalpn01"
|
|
|
|
"github.com/go-acme/lego/v3/log"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type byType []acme.Challenge
|
|
|
|
|
|
|
|
func (a byType) Len() int { return len(a) }
|
|
|
|
func (a byType) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
2019-01-03 15:59:53 +00:00
|
|
|
func (a byType) Less(i, j int) bool { return a[i].Type > a[j].Type }
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
type SolverManager struct {
|
|
|
|
core *api.Core
|
|
|
|
solvers map[challenge.Type]solver
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSolversManager(core *api.Core) *SolverManager {
|
|
|
|
return &SolverManager{
|
2019-01-03 15:59:53 +00:00
|
|
|
solvers: map[challenge.Type]solver{},
|
2018-12-06 21:50:17 +00:00
|
|
|
core: core,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHTTP01Provider specifies a custom provider p that can solve the given HTTP-01 challenge.
|
|
|
|
func (c *SolverManager) SetHTTP01Provider(p challenge.Provider) error {
|
|
|
|
c.solvers[challenge.HTTP01] = http01.NewChallenge(c.core, validate, p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTLSALPN01Provider specifies a custom provider p that can solve the given TLS-ALPN-01 challenge.
|
|
|
|
func (c *SolverManager) SetTLSALPN01Provider(p challenge.Provider) error {
|
|
|
|
c.solvers[challenge.TLSALPN01] = tlsalpn01.NewChallenge(c.core, validate, p)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDNS01Provider specifies a custom provider p that can solve the given DNS-01 challenge.
|
|
|
|
func (c *SolverManager) SetDNS01Provider(p challenge.Provider, opts ...dns01.ChallengeOption) error {
|
|
|
|
c.solvers[challenge.DNS01] = dns01.NewChallenge(c.core, validate, p, opts...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-03 15:59:53 +00:00
|
|
|
// Remove Remove a challenge type from the available solvers.
|
|
|
|
func (c *SolverManager) Remove(chlgType challenge.Type) {
|
|
|
|
delete(c.solvers, chlgType)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks all challenges from the server in order and returns the first matching solver.
|
|
|
|
func (c *SolverManager) chooseSolver(authz acme.Authorization) solver {
|
|
|
|
// Allow to have a deterministic challenge order
|
2019-01-03 15:59:53 +00:00
|
|
|
sort.Sort(byType(authz.Challenges))
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
domain := challenge.GetTargetedDomain(authz)
|
|
|
|
for _, chlg := range authz.Challenges {
|
|
|
|
if solvr, ok := c.solvers[challenge.Type(chlg.Type)]; ok {
|
|
|
|
log.Infof("[%s] acme: use %s solver", domain, chlg.Type)
|
|
|
|
return solvr
|
|
|
|
}
|
|
|
|
log.Infof("[%s] acme: Could not find solver for: %s", domain, chlg.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validate(core *api.Core, domain string, chlg acme.Challenge) error {
|
|
|
|
chlng, err := core.Challenges.New(chlg.URL)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to initiate challenge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
valid, err := checkChallengeStatus(chlng)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if valid {
|
|
|
|
log.Infof("[%s] The server validated our request", domain)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-09 04:40:45 +00:00
|
|
|
ra, err := strconv.Atoi(chlng.RetryAfter)
|
|
|
|
if err != nil {
|
|
|
|
// The ACME server MUST return a Retry-After.
|
|
|
|
// If it doesn't, we'll just poll hard.
|
|
|
|
// Boulder does not implement the ability to retry challenges or the Retry-After header.
|
|
|
|
// https://github.com/letsencrypt/boulder/blob/master/docs/acme-divergences.md#section-82
|
|
|
|
ra = 5
|
|
|
|
}
|
|
|
|
initialInterval := time.Duration(ra) * time.Second
|
|
|
|
|
|
|
|
bo := backoff.NewExponentialBackOff()
|
|
|
|
bo.InitialInterval = initialInterval
|
|
|
|
bo.MaxInterval = 10 * initialInterval
|
|
|
|
bo.MaxElapsedTime = 100 * initialInterval
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
// After the path is sent, the ACME server will access our server.
|
|
|
|
// Repeatedly check the server for an updated status on our request.
|
2019-02-09 04:40:45 +00:00
|
|
|
operation := func() error {
|
2018-12-06 21:50:17 +00:00
|
|
|
authz, err := core.Authorizations.Get(chlng.AuthorizationURL)
|
|
|
|
if err != nil {
|
2019-02-09 04:40:45 +00:00
|
|
|
cancel()
|
2018-12-06 21:50:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
valid, err := checkAuthorizationStatus(authz)
|
|
|
|
if err != nil {
|
2019-02-09 04:40:45 +00:00
|
|
|
cancel()
|
2018-12-06 21:50:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if valid {
|
|
|
|
log.Infof("[%s] The server validated our request", domain)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-09 04:40:45 +00:00
|
|
|
return errors.New("the server didn't respond to our request")
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
2019-02-09 04:40:45 +00:00
|
|
|
|
|
|
|
return backoff.Retry(operation, backoff.WithContext(bo, ctx))
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkChallengeStatus(chlng acme.ExtendedChallenge) (bool, error) {
|
|
|
|
switch chlng.Status {
|
|
|
|
case acme.StatusValid:
|
|
|
|
return true, nil
|
|
|
|
case acme.StatusPending, acme.StatusProcessing:
|
|
|
|
return false, nil
|
|
|
|
case acme.StatusInvalid:
|
|
|
|
return false, chlng.Error
|
|
|
|
default:
|
|
|
|
return false, errors.New("the server returned an unexpected state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkAuthorizationStatus(authz acme.Authorization) (bool, error) {
|
|
|
|
switch authz.Status {
|
|
|
|
case acme.StatusValid:
|
|
|
|
return true, nil
|
|
|
|
case acme.StatusPending, acme.StatusProcessing:
|
|
|
|
return false, nil
|
|
|
|
case acme.StatusDeactivated, acme.StatusExpired, acme.StatusRevoked:
|
|
|
|
return false, fmt.Errorf("the authorization state %s", authz.Status)
|
|
|
|
case acme.StatusInvalid:
|
|
|
|
for _, chlg := range authz.Challenges {
|
|
|
|
if chlg.Status == acme.StatusInvalid && chlg.Error != nil {
|
|
|
|
return false, chlg.Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("the authorization state %s", authz.Status)
|
|
|
|
default:
|
|
|
|
return false, errors.New("the server returned an unexpected state")
|
|
|
|
}
|
|
|
|
}
|