| 
									
										
										
										
											2015-11-25 00:17:21 +08:00
										 |  |  | package sqlstore | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"crypto/tls" | 
					
						
							|  |  |  | 	"crypto/x509" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2022-08-10 21:37:51 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2019-05-16 19:45:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							| 
									
										
										
										
											2015-11-25 00:17:21 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 19:45:23 +08:00
										 |  |  | var tlslog = log.New("tls_mysql") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func makeCert(config DatabaseConfig) (*tls.Config, error) { | 
					
						
							| 
									
										
										
										
											2015-11-25 00:17:21 +08:00
										 |  |  | 	rootCertPool := x509.NewCertPool() | 
					
						
							| 
									
										
										
										
											2022-08-10 21:37:51 +08:00
										 |  |  | 	pem, err := os.ReadFile(config.CaCertPath) | 
					
						
							| 
									
										
										
										
											2015-11-25 00:17:21 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-05 18:57:20 +08:00
										 |  |  | 		return nil, fmt.Errorf("could not read DB CA Cert path %q: %w", config.CaCertPath, err) | 
					
						
							| 
									
										
										
										
											2015-11-25 00:17:21 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if ok := rootCertPool.AppendCertsFromPEM(pem); !ok { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tlsConfig := &tls.Config{ | 
					
						
							| 
									
										
										
										
											2019-05-16 19:45:23 +08: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-25 00:17:21 +08: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 21:10:34 +08:00
										 |  |  | 	if config.ServerCertName == "" && !tlsConfig.InsecureSkipVerify { | 
					
						
							| 
									
										
										
										
											2019-04-23 16:24:47 +08:00
										 |  |  | 		return nil, fmt.Errorf("server_cert_name is missing. Consider using ssl_mode = skip-verify") | 
					
						
							| 
									
										
										
										
											2015-11-25 00:17:21 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return tlsConfig, nil | 
					
						
							|  |  |  | } |