Skip to content

Commit 87f2050

Browse files
committed
Merge pull request #101 from lukescott/master
Add support for custom tls.Configs
2 parents ff69942 + 17ee918 commit 87f2050

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Possible Parameters are:
113113
* `parseTime`: `parseTime=true` changes the output type of `DATE` and `DATETIME` values to `time.Time` instead of `[]byte` / `string`
114114
* `strict`: Enable strict mode. MySQL warnings are treated as errors.
115115
* `timeout`: **Driver** side connection timeout. The value must be a string of decimal numbers, each with optional fraction and a unit suffix ( *"ms"*, *"s"*, *"m"*, *"h"* ), such as *"30s"*, *"0.5m"* or *"1m30s"*. To set a server side timeout, use the parameter [`wait_timeout`](http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_wait_timeout).
116-
* `tls`: `true` enables TLS / SSL encrypted connection to the server. Use `skip-verify` if you want to use a self-signed or invalid certificate (server side)
116+
* `tls`: `true` enables TLS / SSL encrypted connection to the server. Use `skip-verify` if you want to use a self-signed or invalid certificate (server side). Use a custom value registered with [`mysql.RegisterTLSConfig`](http://godoc.org/github.com/go-sql-driver/mysql#RegisterTLSConfig).
117117

118118
All other parameters are interpreted as system variables:
119119
* `autocommit`: *"SET autocommit=`value`"*

driver_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mysql
22

33
import (
4+
"crypto/tls"
45
"database/sql"
56
"fmt"
67
"io"
@@ -840,7 +841,7 @@ func TestStrict(t *testing.T) {
840841
}
841842

842843
func TestTLS(t *testing.T) {
843-
runTests(t, dsn+"&tls=skip-verify", func(dbt *DBTest) {
844+
tlsTest := func(dbt *DBTest) {
844845
if err := dbt.db.Ping(); err != nil {
845846
if err == errNoTLS {
846847
dbt.Skip("Server does not support TLS")
@@ -861,7 +862,15 @@ func TestTLS(t *testing.T) {
861862
dbt.Fatal("No Cipher")
862863
}
863864
}
865+
}
866+
867+
runTests(t, dsn+"&tls=skip-verify", tlsTest)
868+
869+
// Verify that registering / using a custom cfg works
870+
RegisterTLSConfig("custom-skip-verify", &tls.Config{
871+
InsecureSkipVerify: true,
864872
})
873+
runTests(t, dsn+"&tls=custom-skip-verify", tlsTest)
865874
}
866875

867876
// Special cases

utils.go

+43
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,46 @@ func (nt NullTime) Value() (driver.Value, error) {
7777
return nt.Time, nil
7878
}
7979

80+
var tlsConfigMap map[string]*tls.Config
81+
82+
// Registers a custom tls.Config to be used with sql.Open.
83+
// Use the key as a value in the DSN where tls=value.
84+
//
85+
// rootCertPool := x509.NewCertPool()
86+
// pem, err := ioutil.ReadFile("/path/ca-cert.pem")
87+
// if err != nil {
88+
// log.Fatal(err)
89+
// }
90+
// if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
91+
// log.Fatal("Failed to append PEM.")
92+
// }
93+
// clientCert := make([]tls.Certificate, 0, 1)
94+
// certs, err := tls.LoadX509KeyPair("/path/client-cert.pem", "/path/client-key.pem")
95+
// if err != nil {
96+
// log.Fatal(err)
97+
// }
98+
// clientCert = append(clientCert, certs)
99+
// mysql.RegisterTLSConfig("custom", &tls.Config{
100+
// RootCAs: rootCertPool,
101+
// Certificates: clientCert,
102+
// })
103+
// db, err := sql.Open("mysql", "user@tcp(localhost:3306)/test?tls=custom")
104+
//
105+
func RegisterTLSConfig(key string, config *tls.Config) {
106+
if tlsConfigMap == nil {
107+
tlsConfigMap = make(map[string]*tls.Config)
108+
}
109+
tlsConfigMap[key] = config
110+
}
111+
112+
// Removes tls.Config associated with key.
113+
func DeregisterTLSConfig(key string) {
114+
if tlsConfigMap == nil {
115+
return
116+
}
117+
delete(tlsConfigMap, key)
118+
}
119+
80120
// Logger
81121
var (
82122
errLog *log.Logger
@@ -152,6 +192,9 @@ func parseDSN(dsn string) (cfg *config, err error) {
152192
cfg.tls = &tls.Config{}
153193
} else if strings.ToLower(value) == "skip-verify" {
154194
cfg.tls = &tls.Config{InsecureSkipVerify: true}
195+
// TODO: Check for Boolean false
196+
} else if tlsConfig, ok := tlsConfigMap[value]; ok {
197+
cfg.tls = tlsConfig
155198
}
156199

157200
default:

0 commit comments

Comments
 (0)