2016-03-21 10:10:18 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2016-12-30 08:21:13 +00:00
|
|
|
"fmt"
|
2016-09-23 16:27:01 +00:00
|
|
|
"strings"
|
2016-03-21 10:10:18 +00:00
|
|
|
"sync"
|
2016-12-30 08:21:13 +00:00
|
|
|
"time"
|
2016-03-21 10:10:18 +00:00
|
|
|
|
2016-09-23 16:27:01 +00:00
|
|
|
"github.com/cenk/backoff"
|
2016-08-18 12:20:11 +00:00
|
|
|
"github.com/containous/traefik/cluster"
|
|
|
|
"github.com/containous/traefik/log"
|
2016-12-08 12:32:12 +00:00
|
|
|
"github.com/containous/traefik/safe"
|
2016-03-21 10:10:18 +00:00
|
|
|
"github.com/xenolf/lego/acme"
|
|
|
|
)
|
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
var _ acme.ChallengeProviderTimeout = (*challengeProvider)(nil)
|
2016-08-16 17:13:18 +00:00
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
type challengeProvider struct {
|
|
|
|
store cluster.Store
|
|
|
|
lock sync.RWMutex
|
2016-03-21 10:10:18 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
func (c *challengeProvider) getCertificate(domain string) (cert *tls.Certificate, exists bool) {
|
|
|
|
log.Debugf("Challenge GetCertificate %s", domain)
|
2016-09-23 16:27:01 +00:00
|
|
|
if !strings.HasSuffix(domain, ".acme.invalid") {
|
|
|
|
return nil, false
|
|
|
|
}
|
2016-03-21 10:10:18 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2016-08-18 12:20:11 +00:00
|
|
|
account := c.store.Get().(*Account)
|
|
|
|
if account.ChallengeCerts == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2016-09-23 16:27:01 +00:00
|
|
|
account.Init()
|
|
|
|
var result *tls.Certificate
|
|
|
|
operation := func() error {
|
|
|
|
for _, cert := range account.ChallengeCerts {
|
|
|
|
for _, dns := range cert.certificate.Leaf.DNSNames {
|
|
|
|
if domain == dns {
|
|
|
|
result = cert.certificate
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2016-08-18 12:20:11 +00:00
|
|
|
}
|
2017-09-13 08:34:04 +00:00
|
|
|
return fmt.Errorf("cannot find challenge cert for domain %s", domain)
|
2016-03-21 10:10:18 +00:00
|
|
|
}
|
2016-09-23 16:27:01 +00:00
|
|
|
notify := func(err error, time time.Duration) {
|
|
|
|
log.Errorf("Error getting cert: %v, retrying in %s", err, time)
|
|
|
|
}
|
|
|
|
ebo := backoff.NewExponentialBackOff()
|
|
|
|
ebo.MaxElapsedTime = 60 * time.Second
|
2016-12-08 12:32:12 +00:00
|
|
|
err := backoff.RetryNotify(safe.OperationWithRecover(operation), ebo, notify)
|
2016-09-23 16:27:01 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error getting cert: %v", err)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return result, true
|
2016-03-21 10:10:18 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
func (c *challengeProvider) Present(domain, token, keyAuth string) error {
|
|
|
|
log.Debugf("Challenge Present %s", domain)
|
2016-09-23 16:27:01 +00:00
|
|
|
cert, _, err := TLSSNI01ChallengeCert(keyAuth)
|
2016-03-21 10:10:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2016-08-18 12:20:11 +00:00
|
|
|
transaction, object, err := c.store.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
account := object.(*Account)
|
|
|
|
if account.ChallengeCerts == nil {
|
2016-09-23 16:27:01 +00:00
|
|
|
account.ChallengeCerts = map[string]*ChallengeCert{}
|
2016-03-21 10:10:18 +00:00
|
|
|
}
|
2016-09-23 16:27:01 +00:00
|
|
|
account.ChallengeCerts[domain] = &cert
|
2016-08-18 12:20:11 +00:00
|
|
|
return transaction.Commit(account)
|
2016-03-21 10:10:18 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
func (c *challengeProvider) CleanUp(domain, token, keyAuth string) error {
|
|
|
|
log.Debugf("Challenge CleanUp %s", domain)
|
2016-03-21 10:10:18 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2016-08-18 12:20:11 +00:00
|
|
|
transaction, object, err := c.store.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
account := object.(*Account)
|
|
|
|
delete(account.ChallengeCerts, domain)
|
|
|
|
return transaction.Commit(account)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *challengeProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return 60 * time.Second, 5 * time.Second
|
2016-03-21 10:10:18 +00:00
|
|
|
}
|