From 7e6d09a247e092d67d2ccd67088981c0b2bf4b3b Mon Sep 17 00:00:00 2001 From: James Houlahan Date: Mon, 3 Aug 2020 09:24:39 +0200 Subject: [PATCH] test: generate tls cert/key in test --- pkg/config/tls.go | 5 +++-- pkg/config/tls_test.go | 2 +- test/context/config.go | 11 ++++++++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pkg/config/tls.go b/pkg/config/tls.go index 38149634..8fd714f9 100644 --- a/pkg/config/tls.go +++ b/pkg/config/tls.go @@ -64,7 +64,7 @@ func GetTLSConfig(cfg tlsConfiger) (tlsConfig *tls.Config, err error) { tlsConfig, err = loadTLSConfig(certPath, keyPath) if err != nil { log.WithError(err).Warn("Cannot load cert, generating a new one") - tlsConfig, err = generateTLSConfig(certPath, keyPath) + tlsConfig, err = GenerateTLSConfig(certPath, keyPath) if err != nil { return } @@ -126,8 +126,9 @@ func loadTLSConfig(certPath, keyPath string) (tlsConfig *tls.Config, err error) return } +// GenerateTLSConfig generates certs and keys at the given filepaths and returns a TLS Config which holds them. // See https://golang.org/src/crypto/tls/generate_cert.go -func generateTLSConfig(certPath, keyPath string) (tlsConfig *tls.Config, err error) { +func GenerateTLSConfig(certPath, keyPath string) (tlsConfig *tls.Config, err error) { priv, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { err = fmt.Errorf("failed to generate private key: %s", err) diff --git a/pkg/config/tls_test.go b/pkg/config/tls_test.go index c3a3be08..84717120 100644 --- a/pkg/config/tls_test.go +++ b/pkg/config/tls_test.go @@ -43,7 +43,7 @@ func TestTLSKeyRenewal(t *testing.T) { // Put old key there. tlsTemplate.NotBefore = time.Now().Add(-365 * 24 * time.Hour) tlsTemplate.NotAfter = time.Now() - cert, err := generateTLSConfig(certPath, keyPath) + cert, err := GenerateTLSConfig(certPath, keyPath) require.Equal(t, err, ErrTLSCertExpireSoon) require.Equal(t, len(cert.Certificates), 1) time.Sleep(time.Second) diff --git a/test/context/config.go b/test/context/config.go index 6c5914f4..71dfe89e 100644 --- a/test/context/config.go +++ b/test/context/config.go @@ -23,8 +23,10 @@ import ( "os" "path/filepath" + "github.com/ProtonMail/proton-bridge/pkg/config" "github.com/ProtonMail/proton-bridge/pkg/constants" "github.com/ProtonMail/proton-bridge/pkg/pmapi" + "github.com/sirupsen/logrus" ) type fakeConfig struct { @@ -39,9 +41,16 @@ func newFakeConfig() *fakeConfig { panic(err) } - return &fakeConfig{ + cfg := &fakeConfig{ dir: dir, } + + // We must generate cert.pem and key.pem to prevent errors when attempting to open them. + if _, err = config.GenerateTLSConfig(cfg.GetTLSCertPath(), cfg.GetTLSKeyPath()); err != nil { + logrus.WithError(err).Fatal() + } + + return cfg } func (c *fakeConfig) ClearData() error {