2016-03-21 10:10:18 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"sync"
|
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/rsa"
|
2016-03-21 10:10:18 +00:00
|
|
|
"crypto/x509"
|
2016-08-18 12:20:11 +00:00
|
|
|
"encoding/gob"
|
|
|
|
"github.com/containous/traefik/cluster"
|
|
|
|
"github.com/containous/traefik/log"
|
2016-03-21 10:10:18 +00:00
|
|
|
"github.com/xenolf/lego/acme"
|
2016-08-18 12:20:11 +00:00
|
|
|
"time"
|
2016-03-21 10:10:18 +00:00
|
|
|
)
|
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
func init() {
|
|
|
|
gob.Register(rsa.PrivateKey{})
|
|
|
|
gob.Register(rsa.PublicKey{})
|
|
|
|
}
|
|
|
|
|
|
|
|
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 newMemoryChallengeProvider(store cluster.Store) *challengeProvider {
|
|
|
|
return &challengeProvider{
|
|
|
|
store: store,
|
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-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
|
|
|
|
}
|
|
|
|
if certBinary, ok := account.ChallengeCerts[domain]; ok {
|
|
|
|
cert := &tls.Certificate{}
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
buffer.Write(certBinary)
|
|
|
|
dec := gob.NewDecoder(&buffer)
|
|
|
|
err := dec.Decode(cert)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error unmarshaling challenge cert %s", err.Error())
|
|
|
|
return nil, false
|
|
|
|
}
|
2016-03-21 10:10:18 +00:00
|
|
|
return cert, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2016-08-18 12:20:11 +00:00
|
|
|
func (c *challengeProvider) Present(domain, token, keyAuth string) error {
|
|
|
|
log.Debugf("Challenge Present %s", domain)
|
2016-06-18 12:51:52 +00:00
|
|
|
cert, _, err := acme.TLSSNI01ChallengeCert(keyAuth)
|
2016-03-21 10:10:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
|
|
|
|
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 {
|
|
|
|
account.ChallengeCerts = map[string][]byte{}
|
|
|
|
}
|
2016-03-21 10:10:18 +00:00
|
|
|
for i := range cert.Leaf.DNSNames {
|
2016-08-18 12:20:11 +00:00
|
|
|
var buffer bytes.Buffer
|
|
|
|
enc := gob.NewEncoder(&buffer)
|
|
|
|
err := enc.Encode(cert)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
account.ChallengeCerts[cert.Leaf.DNSNames[i]] = buffer.Bytes()
|
|
|
|
log.Debugf("Challenge Present cert: %s", cert.Leaf.DNSNames[i])
|
2016-03-21 10:10:18 +00:00
|
|
|
}
|
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
|
|
|
}
|