-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HA acme support #625
Merged
Merged
HA acme support #625
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5a0440d
Add KV datastore
emilevauge bea5ad3
Add leadership election
emilevauge a428455
Add ACME store
emilevauge e72e658
Challenge certs PEM encoding
emilevauge bb29d9c
Add documentation
emilevauge 4ad4b8e
Add ACME account to storeconfig command
emilevauge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
glide.lock binary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
gen.go | ||
.idea | ||
.intellij | ||
log | ||
*.iml | ||
traefik | ||
traefik.toml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
package acme | ||
|
||
import ( | ||
"crypto" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"errors" | ||
"github.com/containous/traefik/log" | ||
"github.com/xenolf/lego/acme" | ||
"reflect" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// Account is used to store lets encrypt registration info | ||
type Account struct { | ||
Email string | ||
Registration *acme.RegistrationResource | ||
PrivateKey []byte | ||
DomainsCertificate DomainsCertificates | ||
ChallengeCerts map[string]*ChallengeCert | ||
} | ||
|
||
// ChallengeCert stores a challenge certificate | ||
type ChallengeCert struct { | ||
Certificate []byte | ||
PrivateKey []byte | ||
certificate *tls.Certificate | ||
} | ||
|
||
// Init inits acccount struct | ||
func (a *Account) Init() error { | ||
err := a.DomainsCertificate.Init() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, cert := range a.ChallengeCerts { | ||
if cert.certificate == nil { | ||
certificate, err := tls.X509KeyPair(cert.Certificate, cert.PrivateKey) | ||
if err != nil { | ||
return err | ||
} | ||
cert.certificate = &certificate | ||
} | ||
if cert.certificate.Leaf == nil { | ||
leaf, err := x509.ParseCertificate(cert.certificate.Certificate[0]) | ||
if err != nil { | ||
return err | ||
} | ||
cert.certificate.Leaf = leaf | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// NewAccount creates an account | ||
func NewAccount(email string) (*Account, error) { | ||
// Create a user. New accounts need an email and private key to start | ||
privateKey, err := rsa.GenerateKey(rand.Reader, 4096) | ||
if err != nil { | ||
return nil, err | ||
} | ||
domainsCerts := DomainsCertificates{Certs: []*DomainsCertificate{}} | ||
domainsCerts.Init() | ||
return &Account{ | ||
Email: email, | ||
PrivateKey: x509.MarshalPKCS1PrivateKey(privateKey), | ||
DomainsCertificate: DomainsCertificates{Certs: domainsCerts.Certs}, | ||
ChallengeCerts: map[string]*ChallengeCert{}}, nil | ||
} | ||
|
||
// GetEmail returns email | ||
func (a *Account) GetEmail() string { | ||
return a.Email | ||
} | ||
|
||
// GetRegistration returns lets encrypt registration resource | ||
func (a *Account) GetRegistration() *acme.RegistrationResource { | ||
return a.Registration | ||
} | ||
|
||
// GetPrivateKey returns private key | ||
func (a *Account) GetPrivateKey() crypto.PrivateKey { | ||
if privateKey, err := x509.ParsePKCS1PrivateKey(a.PrivateKey); err == nil { | ||
return privateKey | ||
} | ||
log.Errorf("Cannot unmarshall private key %+v", a.PrivateKey) | ||
return nil | ||
} | ||
|
||
// Certificate is used to store certificate info | ||
type Certificate struct { | ||
Domain string | ||
CertURL string | ||
CertStableURL string | ||
PrivateKey []byte | ||
Certificate []byte | ||
} | ||
|
||
// DomainsCertificates stores a certificate for multiple domains | ||
type DomainsCertificates struct { | ||
Certs []*DomainsCertificate | ||
lock sync.RWMutex | ||
} | ||
|
||
// Init inits DomainsCertificates | ||
func (dc *DomainsCertificates) Init() error { | ||
dc.lock.Lock() | ||
defer dc.lock.Unlock() | ||
for _, domainsCertificate := range dc.Certs { | ||
tlsCert, err := tls.X509KeyPair(domainsCertificate.Certificate.Certificate, domainsCertificate.Certificate.PrivateKey) | ||
if err != nil { | ||
return err | ||
} | ||
domainsCertificate.tlsCert = &tlsCert | ||
} | ||
return nil | ||
} | ||
|
||
func (dc *DomainsCertificates) renewCertificates(acmeCert *Certificate, domain Domain) error { | ||
dc.lock.Lock() | ||
defer dc.lock.Unlock() | ||
|
||
for _, domainsCertificate := range dc.Certs { | ||
if reflect.DeepEqual(domain, domainsCertificate.Domains) { | ||
tlsCert, err := tls.X509KeyPair(acmeCert.Certificate, acmeCert.PrivateKey) | ||
if err != nil { | ||
return err | ||
} | ||
domainsCertificate.Certificate = acmeCert | ||
domainsCertificate.tlsCert = &tlsCert | ||
return nil | ||
} | ||
} | ||
return errors.New("Certificate to renew not found for domain " + domain.Main) | ||
} | ||
|
||
func (dc *DomainsCertificates) addCertificateForDomains(acmeCert *Certificate, domain Domain) (*DomainsCertificate, error) { | ||
dc.lock.Lock() | ||
defer dc.lock.Unlock() | ||
|
||
tlsCert, err := tls.X509KeyPair(acmeCert.Certificate, acmeCert.PrivateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cert := DomainsCertificate{Domains: domain, Certificate: acmeCert, tlsCert: &tlsCert} | ||
dc.Certs = append(dc.Certs, &cert) | ||
return &cert, nil | ||
} | ||
|
||
func (dc *DomainsCertificates) getCertificateForDomain(domainToFind string) (*DomainsCertificate, bool) { | ||
dc.lock.RLock() | ||
defer dc.lock.RUnlock() | ||
for _, domainsCertificate := range dc.Certs { | ||
domains := []string{} | ||
domains = append(domains, domainsCertificate.Domains.Main) | ||
domains = append(domains, domainsCertificate.Domains.SANs...) | ||
for _, domain := range domains { | ||
if domain == domainToFind { | ||
return domainsCertificate, true | ||
} | ||
} | ||
} | ||
return nil, false | ||
} | ||
|
||
func (dc *DomainsCertificates) exists(domainToFind Domain) (*DomainsCertificate, bool) { | ||
dc.lock.RLock() | ||
defer dc.lock.RUnlock() | ||
for _, domainsCertificate := range dc.Certs { | ||
if reflect.DeepEqual(domainToFind, domainsCertificate.Domains) { | ||
return domainsCertificate, true | ||
} | ||
} | ||
return nil, false | ||
} | ||
|
||
// DomainsCertificate contains a certificate for multiple domains | ||
type DomainsCertificate struct { | ||
Domains Domain | ||
Certificate *Certificate | ||
tlsCert *tls.Certificate | ||
} | ||
|
||
func (dc *DomainsCertificate) needRenew() bool { | ||
for _, c := range dc.tlsCert.Certificate { | ||
crt, err := x509.ParseCertificate(c) | ||
if err != nil { | ||
// If there's an error, we assume the cert is broken, and needs update | ||
return true | ||
} | ||
// <= 7 days left, renew certificate | ||
if crt.NotAfter.Before(time.Now().Add(time.Duration(24 * 30 * time.Hour))) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should adjust the comment to 30 days left.