Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 91 additions & 7 deletions command/ca/renew.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ca

import (
"bytes"
"crypto"
cryptoRand "crypto/rand"
"crypto/tls"
Expand Down Expand Up @@ -329,6 +330,11 @@ func renewCertificateAction(ctx *cli.Context) error {
if isDaemon {
// Force is always enabled when daemon mode is used
ctx.Set("force", "true")
if kmsURI == "" {
renewer.reloadCert = func() (tls.Certificate, error) {
return reloadKeyPairFromFiles(certFile, outFile, keyFile, passFile)
}
}
next := nextRenewDuration(cert.Leaf, expiresIn, renewPeriod)
return renewer.Daemon(outFile, next, expiresIn, renewPeriod, afterRenew)
}
Expand Down Expand Up @@ -413,13 +419,14 @@ func runExecCmd(execCmd string) error {
}

type renewer struct {
client cautils.CaClient
transport *http.Transport
key crypto.PrivateKey
offline bool
cert tls.Certificate
caURL *url.URL
mtls bool
client cautils.CaClient
transport *http.Transport
key crypto.PrivateKey
offline bool
cert tls.Certificate
caURL *url.URL
mtls bool
reloadCert func() (tls.Certificate, error)
}

func newRenewer(ctx *cli.Context, caURL string, cert tls.Certificate, rootFile string) (*renewer, error) {
Expand Down Expand Up @@ -549,6 +556,10 @@ func (r *renewer) RenewAndPrepareNext(outFile string, expiresIn, renewPeriod tim
const durationOnErrors = 1 * time.Minute
infoLog := log.New(os.Stdout, "INFO: ", log.LstdFlags)

if err := r.reload(); err != nil {
return durationOnErrors, err
}

resp, err := r.Renew(outFile)
if err != nil {
return durationOnErrors, err
Expand Down Expand Up @@ -582,6 +593,79 @@ func (r *renewer) RenewAndPrepareNext(outFile string, expiresIn, renewPeriod tim
return next, nil
}

func (r *renewer) reload() error {
if r.reloadCert == nil {
return nil
}
cert, err := r.reloadCert()
if err != nil {
return err
}
changed := r.cert.Leaf == nil || !bytes.Equal(r.cert.Leaf.Raw, cert.Leaf.Raw)
r.cert = cert
r.key = cert.PrivateKey
if time.Now().Before(cert.Leaf.NotAfter) {
r.transport.TLSClientConfig.Certificates = []tls.Certificate{cert}
} else {
r.transport.TLSClientConfig.Certificates = nil
}
if changed {
r.transport.CloseIdleConnections()
}
return nil
}

func reloadKeyPairFromFiles(certFile, outFile, keyFile, passFile string) (tls.Certificate, error) {
opts := []pemutil.Options{pemutil.WithFilename(keyFile)}
if passFile != "" {
opts = append(opts, pemutil.WithPasswordFile(passFile))
}
signer, err := cryptoutil.CreateSigner("", keyFile, opts...)
if err != nil {
return tls.Certificate{}, errs.Wrap(err, "error loading private key")
}

if outFile != "" && outFile != certFile {
if cert, err := readCertificateChain(outFile); err == nil && publicKeysEqual(cert.Leaf.PublicKey, signer.Public()) {
cert.PrivateKey = signer
return cert, nil
}
}

cert, err := readCertificateChain(certFile)
if err != nil {
return tls.Certificate{}, err
}
if !publicKeysEqual(cert.Leaf.PublicKey, signer.Public()) {
return tls.Certificate{}, errors.New("error reloading certificate and key: private key does not match certificate public key")
}
cert.PrivateKey = signer
return cert, nil
}

func readCertificateChain(certFile string) (tls.Certificate, error) {
x509Chain, err := pemutil.ReadCertificateBundle(certFile)
if err != nil {
return tls.Certificate{}, errs.Wrap(err, "error reading certificate chain")
}
if len(x509Chain) == 0 {
return tls.Certificate{}, errors.New("error loading certificate: certificate chain is empty")
}
x509ChainBytes := make([][]byte, len(x509Chain))
for i, c := range x509Chain {
x509ChainBytes[i] = c.Raw
}
return tls.Certificate{
Certificate: x509ChainBytes,
Leaf: x509Chain[0],
}, nil
}

func publicKeysEqual(a, b crypto.PublicKey) bool {
eq, ok := a.(interface{ Equal(crypto.PublicKey) bool })
return ok && eq.Equal(b)
}

func (r *renewer) Daemon(outFile string, next, expiresIn, renewPeriod time.Duration, afterRenew func() error) error {
// Loggers
infoLog := log.New(os.Stdout, "INFO: ", log.LstdFlags)
Expand Down
Loading