tls_mysql.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package sqlstore
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "fmt"
  6. "io/ioutil"
  7. )
  8. func makeCert(tlsPoolName string, config DatabaseConfig) (*tls.Config, error) {
  9. rootCertPool := x509.NewCertPool()
  10. pem, err := ioutil.ReadFile(config.CaCertPath)
  11. if err != nil {
  12. return nil, fmt.Errorf("Could not read DB CA Cert path: %v", config.CaCertPath)
  13. }
  14. if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
  15. return nil, err
  16. }
  17. clientCert := make([]tls.Certificate, 0, 1)
  18. if config.ClientCertPath != "" && config.ClientKeyPath != "" {
  19. certs, err := tls.LoadX509KeyPair(config.ClientCertPath, config.ClientKeyPath)
  20. if err != nil {
  21. return nil, err
  22. }
  23. clientCert = append(clientCert, certs)
  24. }
  25. tlsConfig := &tls.Config{
  26. RootCAs: rootCertPool,
  27. Certificates: clientCert,
  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. }