2015-11-24 10:17:21 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
2022-08-10 08:37:51 -05:00
|
|
|
"os"
|
2019-05-16 06:45:23 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2015-11-24 10:17:21 -06:00
|
|
|
)
|
|
|
|
|
2019-05-16 06:45:23 -05:00
|
|
|
var tlslog = log.New("tls_mysql")
|
|
|
|
|
|
|
|
func makeCert(config DatabaseConfig) (*tls.Config, error) {
|
2015-11-24 10:17:21 -06:00
|
|
|
rootCertPool := x509.NewCertPool()
|
2022-08-10 08:37:51 -05:00
|
|
|
pem, err := os.ReadFile(config.CaCertPath)
|
2015-11-24 10:17:21 -06:00
|
|
|
if err != nil {
|
2020-11-05 04:57:20 -06:00
|
|
|
return nil, fmt.Errorf("could not read DB CA Cert path %q: %w", config.CaCertPath, err)
|
2015-11-24 10:17:21 -06:00
|
|
|
}
|
|
|
|
if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig := &tls.Config{
|
2019-05-16 06:45:23 -05:00
|
|
|
RootCAs: rootCertPool,
|
|
|
|
}
|
|
|
|
if config.ClientCertPath != "" && config.ClientKeyPath != "" {
|
|
|
|
tlsConfig.GetClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
|
|
|
tlslog.Debug("Loading client certificate")
|
|
|
|
cert, err := tls.LoadX509KeyPair(config.ClientCertPath, config.ClientKeyPath)
|
|
|
|
return &cert, err
|
|
|
|
}
|
2015-11-24 10:17:21 -06:00
|
|
|
}
|
|
|
|
tlsConfig.ServerName = config.ServerCertName
|
|
|
|
if config.SslMode == "skip-verify" {
|
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
}
|
|
|
|
// Return more meaningful error before it is too late
|
2015-12-22 07:10:34 -06:00
|
|
|
if config.ServerCertName == "" && !tlsConfig.InsecureSkipVerify {
|
2019-04-23 03:24:47 -05:00
|
|
|
return nil, fmt.Errorf("server_cert_name is missing. Consider using ssl_mode = skip-verify")
|
2015-11-24 10:17:21 -06:00
|
|
|
}
|
|
|
|
return tlsConfig, nil
|
|
|
|
}
|