| 
									
										
										
										
											2015-11-25 00:17:21 +08:00
										 |  |  | package sqlstore | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"crypto/tls" | 
					
						
							|  |  |  | 	"crypto/x509" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io/ioutil" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-23 07:29:22 +08:00
										 |  |  | func makeCert(tlsPoolName string, config DatabaseConfig) (*tls.Config, error) { | 
					
						
							| 
									
										
										
										
											2015-11-25 00:17:21 +08:00
										 |  |  | 	rootCertPool := x509.NewCertPool() | 
					
						
							|  |  |  | 	pem, err := ioutil.ReadFile(config.CaCertPath) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("Could not read DB CA Cert path: %v", config.CaCertPath) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if ok := rootCertPool.AppendCertsFromPEM(pem); !ok { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	clientCert := make([]tls.Certificate, 0, 1) | 
					
						
							| 
									
										
										
										
											2015-12-22 21:10:34 +08:00
										 |  |  | 	if config.ClientCertPath != "" && config.ClientKeyPath != "" { | 
					
						
							| 
									
										
										
										
											2015-11-25 00:17:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		certs, err := tls.LoadX509KeyPair(config.ClientCertPath, config.ClientKeyPath) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		clientCert = append(clientCert, certs) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	tlsConfig := &tls.Config{ | 
					
						
							|  |  |  | 		RootCAs:      rootCertPool, | 
					
						
							|  |  |  | 		Certificates: clientCert, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	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 { | 
					
						
							| 
									
										
										
										
											2015-11-25 00:17:21 +08:00
										 |  |  | 		return nil, fmt.Errorf("server_cert_name is missing. Consider using ssl_mode = skip-verify.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return tlsConfig, nil | 
					
						
							|  |  |  | } |