tls_mysql.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package sqlstore
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "fmt"
  6. "io/ioutil"
  7. "github.com/grafana/grafana/pkg/infra/log"
  8. )
  9. var tlslog = log.New("tls_mysql")
  10. func makeCert(config DatabaseConfig) (*tls.Config, error) {
  11. rootCertPool := x509.NewCertPool()
  12. pem, err := ioutil.ReadFile(config.CaCertPath)
  13. if err != nil {
  14. return nil, fmt.Errorf("Could not read DB CA Cert path: %v", config.CaCertPath)
  15. }
  16. if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
  17. return nil, err
  18. }
  19. tlsConfig := &tls.Config{
  20. RootCAs: rootCertPool,
  21. }
  22. if config.ClientCertPath != "" && config.ClientKeyPath != "" {
  23. tlsConfig.GetClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
  24. tlslog.Debug("Loading client certificate")
  25. cert, err := tls.LoadX509KeyPair(config.ClientCertPath, config.ClientKeyPath)
  26. return &cert, err
  27. }
  28. }
  29. tlsConfig.ServerName = config.ServerCertName
  30. if config.SslMode == "skip-verify" {
  31. tlsConfig.InsecureSkipVerify = true
  32. }
  33. // Return more meaningful error before it is too late
  34. if config.ServerCertName == "" && !tlsConfig.InsecureSkipVerify {
  35. return nil, fmt.Errorf("server_cert_name is missing. Consider using ssl_mode = skip-verify")
  36. }
  37. return tlsConfig, nil
  38. }