2018-03-05 19:54:04 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-07-09 21:28:03 +00:00
|
|
|
"fmt"
|
2018-03-05 19:54:04 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-07-03 10:44:04 +00:00
|
|
|
"sync"
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/safe"
|
2018-03-05 19:54:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ Store = (*LocalStore)(nil)
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// LocalStore Stores implementation for local file.
|
2018-03-05 19:54:04 +00:00
|
|
|
type LocalStore struct {
|
2019-07-19 09:52:04 +00:00
|
|
|
saveDataChan chan map[string]*StoredData
|
2018-03-05 19:54:04 +00:00
|
|
|
filename string
|
2019-07-19 09:52:04 +00:00
|
|
|
|
|
|
|
lock sync.RWMutex
|
|
|
|
storedData map[string]*StoredData
|
2018-03-05 19:54:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// NewLocalStore initializes a new LocalStore with a file name.
|
2018-07-03 10:44:04 +00:00
|
|
|
func NewLocalStore(filename string) *LocalStore {
|
2019-07-19 09:52:04 +00:00
|
|
|
store := &LocalStore{filename: filename, saveDataChan: make(chan map[string]*StoredData)}
|
2018-03-05 19:54:04 +00:00
|
|
|
store.listenSaveAction()
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
2019-07-19 09:52:04 +00:00
|
|
|
func (s *LocalStore) save(resolverName string, storedData *StoredData) {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
s.storedData[resolverName] = storedData
|
2020-09-30 10:04:04 +00:00
|
|
|
|
|
|
|
// we cannot pass s.storedData directly, map is reference type and as result
|
|
|
|
// we can face with race condition, so we need to work with objects copy
|
|
|
|
s.saveDataChan <- s.unSafeCopyOfStoredData()
|
2019-07-19 09:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LocalStore) get(resolverName string) (*StoredData, error) {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
2018-03-05 19:54:04 +00:00
|
|
|
if s.storedData == nil {
|
2019-07-19 09:52:04 +00:00
|
|
|
s.storedData = map[string]*StoredData{}
|
2018-03-05 19:54:04 +00:00
|
|
|
|
2018-04-10 08:52:04 +00:00
|
|
|
hasData, err := CheckFile(s.filename)
|
2018-03-05 19:54:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-10 08:52:04 +00:00
|
|
|
if hasData {
|
2018-11-14 09:18:03 +00:00
|
|
|
logger := log.WithoutContext().WithField(log.ProviderName, "acme")
|
|
|
|
|
2018-04-10 08:52:04 +00:00
|
|
|
f, err := os.Open(s.filename)
|
|
|
|
if err != nil {
|
2018-03-05 19:54:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-10 08:52:04 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
file, err := ioutil.ReadAll(f)
|
2018-03-26 12:12:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-10 08:52:04 +00:00
|
|
|
|
|
|
|
if len(file) > 0 {
|
2019-07-19 09:52:04 +00:00
|
|
|
if err := json.Unmarshal(file, &s.storedData); err != nil {
|
2018-04-10 08:52:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-04-16 17:34:04 +00:00
|
|
|
|
|
|
|
// Delete all certificates with no value
|
2019-07-19 09:52:04 +00:00
|
|
|
var certificates []*CertAndStore
|
|
|
|
for _, storedData := range s.storedData {
|
|
|
|
for _, certificate := range storedData.Certificates {
|
|
|
|
if len(certificate.Certificate.Certificate) == 0 || len(certificate.Key) == 0 {
|
|
|
|
logger.Debugf("Deleting empty certificate %v for %v", certificate, certificate.Domain.ToStrArray())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
certificates = append(certificates, certificate)
|
|
|
|
}
|
|
|
|
if len(certificates) < len(storedData.Certificates) {
|
|
|
|
storedData.Certificates = certificates
|
2020-09-30 10:04:04 +00:00
|
|
|
|
|
|
|
// we cannot pass s.storedData directly, map is reference type and as result
|
|
|
|
// we can face with race condition, so we need to work with objects copy
|
|
|
|
s.saveDataChan <- s.unSafeCopyOfStoredData()
|
2018-04-16 17:34:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-26 12:12:03 +00:00
|
|
|
}
|
2018-03-05 19:54:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 09:52:04 +00:00
|
|
|
if s.storedData[resolverName] == nil {
|
|
|
|
s.storedData[resolverName] = &StoredData{}
|
|
|
|
}
|
|
|
|
return s.storedData[resolverName], nil
|
2018-03-05 19:54:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// listenSaveAction listens to a chan to store ACME data in json format into `LocalStore.filename`.
|
2018-03-05 19:54:04 +00:00
|
|
|
func (s *LocalStore) listenSaveAction() {
|
|
|
|
safe.Go(func() {
|
2018-11-14 09:18:03 +00:00
|
|
|
logger := log.WithoutContext().WithField(log.ProviderName, "acme")
|
2019-07-19 09:52:04 +00:00
|
|
|
for object := range s.saveDataChan {
|
2018-03-05 19:54:04 +00:00
|
|
|
data, err := json.MarshalIndent(object, "", " ")
|
|
|
|
if err != nil {
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Error(err)
|
2018-03-05 19:54:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 12:42:03 +00:00
|
|
|
err = ioutil.WriteFile(s.filename, data, 0o600)
|
2018-03-05 19:54:04 +00:00
|
|
|
if err != nil {
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Error(err)
|
2018-03-05 19:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:04:04 +00:00
|
|
|
// unSafeCopyOfStoredData creates maps copy of storedData. Is not thread safe, you should use `s.lock`.
|
|
|
|
func (s *LocalStore) unSafeCopyOfStoredData() map[string]*StoredData {
|
|
|
|
result := map[string]*StoredData{}
|
|
|
|
for k, v := range s.storedData {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// GetAccount returns ACME Account.
|
2019-07-19 09:52:04 +00:00
|
|
|
func (s *LocalStore) GetAccount(resolverName string) (*Account, error) {
|
|
|
|
storedData, err := s.get(resolverName)
|
2018-03-05 19:54:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return storedData.Account, nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// SaveAccount stores ACME Account.
|
2019-07-19 09:52:04 +00:00
|
|
|
func (s *LocalStore) SaveAccount(resolverName string, account *Account) error {
|
|
|
|
storedData, err := s.get(resolverName)
|
2018-03-05 19:54:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
storedData.Account = account
|
2019-07-19 09:52:04 +00:00
|
|
|
s.save(resolverName, storedData)
|
2018-03-05 19:54:04 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// GetCertificates returns ACME Certificates list.
|
2019-07-19 09:52:04 +00:00
|
|
|
func (s *LocalStore) GetCertificates(resolverName string) ([]*CertAndStore, error) {
|
|
|
|
storedData, err := s.get(resolverName)
|
2018-03-05 19:54:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return storedData.Certificates, nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// SaveCertificates stores ACME Certificates list.
|
2019-07-19 09:52:04 +00:00
|
|
|
func (s *LocalStore) SaveCertificates(resolverName string, certificates []*CertAndStore) error {
|
|
|
|
storedData, err := s.get(resolverName)
|
2018-03-05 19:54:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
storedData.Certificates = certificates
|
2019-07-19 09:52:04 +00:00
|
|
|
s.save(resolverName, storedData)
|
2018-03-05 19:54:04 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-19 09:52:04 +00:00
|
|
|
// LocalChallengeStore is an implementation of the ChallengeStore in memory.
|
|
|
|
type LocalChallengeStore struct {
|
|
|
|
storedData *StoredChallengeData
|
|
|
|
lock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLocalChallengeStore initializes a new LocalChallengeStore.
|
|
|
|
func NewLocalChallengeStore() *LocalChallengeStore {
|
|
|
|
return &LocalChallengeStore{
|
|
|
|
storedData: &StoredChallengeData{
|
|
|
|
HTTPChallenges: make(map[string]map[string][]byte),
|
|
|
|
TLSChallenges: make(map[string]*Certificate),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// GetHTTPChallengeToken Get the http challenge token from the store.
|
2019-07-19 09:52:04 +00:00
|
|
|
func (s *LocalChallengeStore) GetHTTPChallengeToken(token, domain string) ([]byte, error) {
|
2018-07-09 21:28:03 +00:00
|
|
|
s.lock.RLock()
|
|
|
|
defer s.lock.RUnlock()
|
|
|
|
|
|
|
|
if s.storedData.HTTPChallenges == nil {
|
|
|
|
s.storedData.HTTPChallenges = map[string]map[string][]byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := s.storedData.HTTPChallenges[token]; !ok {
|
|
|
|
return nil, fmt.Errorf("cannot find challenge for token %v", token)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := s.storedData.HTTPChallenges[token][domain]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("cannot find challenge for token %v", token)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// SetHTTPChallengeToken Set the http challenge token in the store.
|
2019-07-19 09:52:04 +00:00
|
|
|
func (s *LocalChallengeStore) SetHTTPChallengeToken(token, domain string, keyAuth []byte) error {
|
2018-07-09 21:28:03 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
if s.storedData.HTTPChallenges == nil {
|
|
|
|
s.storedData.HTTPChallenges = map[string]map[string][]byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := s.storedData.HTTPChallenges[token]; !ok {
|
|
|
|
s.storedData.HTTPChallenges[token] = map[string][]byte{}
|
|
|
|
}
|
|
|
|
|
2018-08-06 18:00:03 +00:00
|
|
|
s.storedData.HTTPChallenges[token][domain] = keyAuth
|
2018-07-09 21:28:03 +00:00
|
|
|
return nil
|
2018-03-05 19:54:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// RemoveHTTPChallengeToken Remove the http challenge token in the store.
|
2019-07-19 09:52:04 +00:00
|
|
|
func (s *LocalChallengeStore) RemoveHTTPChallengeToken(token, domain string) error {
|
2018-07-09 21:28:03 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
if s.storedData.HTTPChallenges == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := s.storedData.HTTPChallenges[token]; ok {
|
2019-02-05 16:10:03 +00:00
|
|
|
delete(s.storedData.HTTPChallenges[token], domain)
|
2018-07-09 21:28:03 +00:00
|
|
|
if len(s.storedData.HTTPChallenges[token]) == 0 {
|
|
|
|
delete(s.storedData.HTTPChallenges, token)
|
|
|
|
}
|
|
|
|
}
|
2018-03-05 19:54:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-07-03 10:44:04 +00:00
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// AddTLSChallenge Add a certificate to the ACME TLS-ALPN-01 certificates storage.
|
2019-07-19 09:52:04 +00:00
|
|
|
func (s *LocalChallengeStore) AddTLSChallenge(domain string, cert *Certificate) error {
|
2018-07-03 10:44:04 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
if s.storedData.TLSChallenges == nil {
|
|
|
|
s.storedData.TLSChallenges = make(map[string]*Certificate)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.storedData.TLSChallenges[domain] = cert
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// GetTLSChallenge Get a certificate from the ACME TLS-ALPN-01 certificates storage.
|
2019-07-19 09:52:04 +00:00
|
|
|
func (s *LocalChallengeStore) GetTLSChallenge(domain string) (*Certificate, error) {
|
2018-07-03 10:44:04 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
if s.storedData.TLSChallenges == nil {
|
|
|
|
s.storedData.TLSChallenges = make(map[string]*Certificate)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.storedData.TLSChallenges[domain], nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// RemoveTLSChallenge Remove a certificate from the ACME TLS-ALPN-01 certificates storage.
|
2019-07-19 09:52:04 +00:00
|
|
|
func (s *LocalChallengeStore) RemoveTLSChallenge(domain string) error {
|
2018-07-03 10:44:04 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
if s.storedData.TLSChallenges == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(s.storedData.TLSChallenges, domain)
|
|
|
|
return nil
|
|
|
|
}
|