Skip to content

Commit

Permalink
Merge pull request #16 from teodor-pripoae/lazy-load-password-protect…
Browse files Browse the repository at this point in the history
…ed-certificates

Lazy load password protected certificates
  • Loading branch information
moshloop authored May 14, 2020
2 parents 8a70401 + a4e2b71 commit 3de708c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
20 changes: 18 additions & 2 deletions certs/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type Certificate struct {
X509 *x509.Certificate
PrivateKey *rsa.PrivateKey
Chain []*Certificate

lazyLoaded bool
certificateBytes []byte
privateKeyBytes []byte
password []byte
}

// DecryptCertificate decrypts a certificate / private key pair and returns a Certificate
Expand Down Expand Up @@ -119,11 +124,22 @@ func (c *Certificate) AsTLSSecret() map[string][]byte {
}
}

func (c *Certificate) AsTLSConfig() *tls.Config) {
func (c *Certificate) AsTLSConfig() *tls.Config {
caPool := x509.NewCertPool()
caPool.AppendCertsFromPEM(c.EncodedCertificate())
return &tls.Config{
RootCAs: caPool,
Certificates: []tls.Certificate{c.X509},
Certificates: []tls.Certificate{{Leaf: c.X509}},
}
}

func (c *Certificate) Load() (*Certificate, error) {
if !c.lazyLoaded {
return c, nil
}
return DecryptCertificate(c.certificateBytes, c.privateKeyBytes, c.password)
}

func (c *Certificate) IsLazyLoaded() bool {
return c.lazyLoaded
}
11 changes: 10 additions & 1 deletion certs/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path/filepath"
"strings"

log "github.com/sirupsen/logrus"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -42,10 +44,17 @@ func (c *Certificate) UnmarshalYAML(unmarshal func(interface{}) error) error {

certificate, err := DecryptCertificate(cert, privateKey, []byte(password))
if err != nil {
return errors.Wrap(err, "failed to decrypt certificate")
log.Errorf("failed to decrypt certificate: %v", err)
certificate = &Certificate{
lazyLoaded: true,
certificateBytes: cert,
privateKeyBytes: privateKey,
password: []byte(password),
}
}

*c = *certificate

return nil
}

Expand Down

0 comments on commit 3de708c

Please sign in to comment.