Grafana: Enables use of encrypted certificates with password for https (#91418)

This commit is contained in:
lean.dev 2024-08-06 19:18:32 -03:00 committed by GitHub
parent 1b6362a807
commit b5e32b31b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 181 additions and 3 deletions

View File

@ -65,6 +65,7 @@ enable_gzip = false
# https certs & key file
cert_file =
cert_key =
cert_pass =
# Certificates file watch interval
certs_watch_interval =

View File

@ -67,6 +67,9 @@
;cert_file =
;cert_key =
# optional password to be used to decrypt key file
;cert_pass =
# Certificates file watch interval
;certs_watch_interval =

View File

@ -109,6 +109,7 @@ import (
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/web"
"github.com/youmark/pkcs8"
)
type HTTPServer struct {
@ -819,6 +820,10 @@ func (hs *HTTPServer) readCertificates() (*tls.Certificate, error) {
return nil, fmt.Errorf(`cannot find SSL key_file at %q`, hs.Cfg.KeyFile)
}
if hs.Cfg.CertPassword != "" {
return handleEncryptedCertificates(hs.Cfg)
}
// previous implementation
tlsCert, err := tls.LoadX509KeyPair(hs.Cfg.CertFile, hs.Cfg.KeyFile)
if err != nil {
return nil, fmt.Errorf("could not load SSL certificate: %w", err)
@ -826,6 +831,50 @@ func (hs *HTTPServer) readCertificates() (*tls.Certificate, error) {
return &tlsCert, nil
}
func handleEncryptedCertificates(cfg *setting.Cfg) (*tls.Certificate, error) {
certKeyFilePassword := cfg.CertPassword
certData, err := os.ReadFile(cfg.CertFile)
if err != nil {
return nil, fmt.Errorf("failed to read certificate file: %w", err)
}
keyData, err := os.ReadFile(cfg.KeyFile)
if err != nil {
return nil, fmt.Errorf("failed to read private key file: %w", err)
}
// handle encrypted private key
keyPemBlock, _ := pem.Decode(keyData)
var keyBytes []byte
// Process the PKCS-encrypted PEM block.
if strings.Contains(keyPemBlock.Type, "ENCRYPTED") {
// The pkcs8 package only handles the PKCS #5 v2.0 scheme.
decrypted, err := pkcs8.ParsePKCS8PrivateKey(keyPemBlock.Bytes, []byte(certKeyFilePassword))
if err != nil {
return nil, fmt.Errorf("error parsing PKCS8 Private key: %w", err)
}
keyBytes, err = x509.MarshalPKCS8PrivateKey(decrypted)
if err != nil {
return nil, fmt.Errorf("error marshaling PKCS8 Private key: %w", err)
}
} else {
return nil, fmt.Errorf("password provided but Private key is not encrypted or not supported")
}
var encodedKey bytes.Buffer
err = pem.Encode(&encodedKey, &pem.Block{Type: keyPemBlock.Type, Bytes: keyBytes})
if err != nil {
return nil, fmt.Errorf("error encoding pem file: %w", err)
}
cert, err := tls.X509KeyPair(certData, encodedKey.Bytes())
if err != nil {
return nil, fmt.Errorf("failed to parse X509 key pair: %w", err)
}
return &cert, nil
}
func (hs *HTTPServer) configureTLS() error {
tlsCerts, err := hs.tlsCertificates()
if err != nil {
@ -869,7 +918,7 @@ func (hs *HTTPServer) GetCertificate(*tls.ClientHelloInfo) (*tls.Certificate, er
return tlsCerts, nil
}
// fsnotify module can be used to detect file changes and based on the event certs can be reloaded
// WatchAndUpdateCerts fsnotify module can be used to detect file changes and based on the event certs can be reloaded
// since it adds a direct dependency for the optional feature. So that is the reason periodic watching
// of cert files is chosen. If fsnotify is added as direct dependency in future, then the implementation
// can be revisited to align to fsnotify.

View File

@ -1,11 +1,12 @@
package api
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHTTPServer_MetricsBasicAuth(t *testing.T) {
@ -37,3 +38,124 @@ func TestHTTPServer_readCertificates(t *testing.T) {
assert.NotNil(t, err)
})
}
func TestHTTPServer_readEncryptedCertificates(t *testing.T) {
t.Run("readCertificates should return certificate if configuration is correct", func(t *testing.T) {
cfg, cleanUpFunc := getHttpServerCfg(t)
defer cleanUpFunc()
ts := &HTTPServer{
Cfg: cfg,
}
c, err := ts.readCertificates()
require.Nil(t, err)
require.NotNil(t, c)
})
t.Run("readCertificates should return error if the password provided is not the correct one", func(t *testing.T) {
cfg, cleanUpFunc := getHttpServerCfg(t)
defer cleanUpFunc()
// change for a wrong password - 32char for consistency
cfg.CertPassword = "somethingThatIsNotTheCorrectPass"
ts := &HTTPServer{
Cfg: cfg,
}
c, err := ts.readCertificates()
require.Nil(t, c)
require.NotNil(t, err)
require.Equal(t, err.Error(), "error parsing PKCS8 Private key: pkcs8: incorrect password")
})
}
// returns Cfg and cleanup function for the created files
func getHttpServerCfg(t *testing.T) (*setting.Cfg, func()) {
// create cert files
cert, err := os.CreateTemp("", "certWithPass*.crt")
require.NoError(t, err)
_, err = cert.Write(certWithPass)
require.NoError(t, err)
privateKey, err := os.CreateTemp("", "privateKey*.key")
require.NoError(t, err)
_, err = privateKey.Write(privateKeyWithPass)
require.NoError(t, err)
cfg := setting.NewCfg()
cfg.CertPassword = password
cfg.CertFile = cert.Name()
cfg.KeyFile = privateKey.Name()
cfg.Protocol = "https"
cleanupFunc := func() {
_ = os.Remove(cert.Name())
_ = os.Remove(privateKey.Name())
}
return cfg, cleanupFunc
}
/*
* Certificates encrypted with password used for testing. These are valid until Aug 1st 2027.
* To generate new ones, use this commands:
*
* # Generate RSA private key with a passphrase '12345678901234567890123456789012'
* sudo openssl genrsa -aes256 -passout pass:12345678901234567890123456789012 -out ./grafana_pass.key 2048
* # Create a new Certificate Signing Request (CSR) using the private key passing passphrase '12345678901234567890123456789012'
* sudo openssl req -new -nodes -sha256 -key ./grafana_pass.key -subj '/CN=testCertWithPass/C=us' -passin pass:12345678901234567890123456789012 -out ./grafana_pass.csr
* # Sign the CSR using the private key to create a self-signed certificate valid for 365 days
* sudo openssl x509 -req -days 1095 -in ./grafana_pass.csr -signkey ./grafana_pass.key -passin pass:12345678901234567890123456789012 -out ./grafana_pass.crt
*/
var certWithPass = []byte(`-----BEGIN CERTIFICATE-----
MIIC1zCCAb8CFGUb9G3+Dl7bTJgCsV0HatdD6jnkMA0GCSqGSIb3DQEBCwUAMCgx
GTAXBgNVBAMMEHRlc3RDZXJ0V2l0aF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KFiyJPL0rssMvv6qgelkBzbRwjctXjEa8SIR6s1nOumP2QlYHT1Di66k0+E
zAYm0FNSo2OleRR6pbbXZJXbkUDU931JnON2OPvZ7UhHM2hWfAQq5Nl2KcaqKx/C
eiRV8o8qOuXyNnckWtv7btFj8Y+MLMIt+Ee6ZWeUWQKEFUoGInPUj8KAN8w8K3Z7
BX1JyIJD/qNV9mgKFjmhCI3m2xox5b+RO1NDsDz3S33hsPdBHJHWwBCZLquwq+mM
aSiWiFL8KCK6Fc478J6iUg7Jzd8z3TC02VhCc4p+xWTYEgQN8yUxV2rxSk9mwsWq
v/iOCp07NN9uhNbF4KIrIX010sUYIq8iI1QeiFtQgmooBUHvd3RQH5fLaa5hwozt
hmVfJ7Wl0aBpD516QC09QhQS0jqnFRr433dVRI6zFNdxw3joZPUp4MKBlJ7g0CJV
Iv0fKNJwfT7Vmmwu2M3T5O0NzNx6VkGYXei5+NaJvUwXNwUzmdBUieXyP1bHMhr9
cobRX9pYWflHCH4n0PshBo/quh98Omy7MVcSQtP4S2kQ4uYtZV8pZj1L5K9DekK0
Fx113Ns6T2LzzdARMN7S3qsiRveFRrz+Xm0Rtrl//KB5
-----END ENCRYPTED PRIVATE KEY-----
`)
var password = "12345678901234567890123456789012"

View File

@ -92,6 +92,7 @@ type Cfg struct {
// HTTP Server Settings
CertFile string
KeyFile string
CertPassword string
CertWatchInterval time.Duration
HTTPAddr string
HTTPPort string
@ -1874,11 +1875,13 @@ func (cfg *Cfg) readServerSettings(iniFile *ini.File) error {
cfg.Protocol = HTTPSScheme
cfg.CertFile = server.Key("cert_file").String()
cfg.KeyFile = server.Key("cert_key").String()
cfg.CertPassword = server.Key("cert_pass").String()
}
if protocolStr == "h2" {
cfg.Protocol = HTTP2Scheme
cfg.CertFile = server.Key("cert_file").String()
cfg.KeyFile = server.Key("cert_key").String()
cfg.CertPassword = server.Key("cert_pass").String()
}
if protocolStr == "socket" {
cfg.Protocol = SocketScheme