Skip to content

Commit 4974720

Browse files
committed
Change SetTLSConfig to exported RegisterTLSConfig and add documentation
1 parent 481dc97 commit 4974720

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ A MySQL-Driver for Go's [database/sql](http://golang.org/pkg/database/sql) packa
1919
* [Address](#address)
2020
* [Parameters](#parameters)
2121
* [Examples](#examples)
22+
* [TLS support](#tls-support)
2223
* [LOAD DATA LOCAL INFILE support](#load-data-local-infile-support)
2324
* [time.Time support](#timetime-support)
2425
* [Unicode support](#unicode-support)
@@ -113,7 +114,7 @@ Possible Parameters are:
113114
* `parseTime`: `parseTime=true` changes the output type of `DATE` and `DATETIME` values to `time.Time` instead of `[]byte` / `string`
114115
* `strict`: Enable strict mode. MySQL warnings are treated as errors.
115116
* `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)
117+
* `tls`: `true` enables TLS / SSL encrypted connection to the server. For other values see [TLS support](#tls-support).
117118

118119
All other parameters are interpreted as system variables:
119120
* `autocommit`: *"SET autocommit=`value`"*
@@ -143,6 +144,14 @@ No Database preselected:
143144
user:password@/
144145
```
145146

147+
### TLS support
148+
For TLS support set the `tls` parameter to one of the following values:
149+
150+
* `true`: Server certificate is signed by a trusted authority.
151+
* `skip-verify`: Server certificate is self-signed with no root authority.
152+
* `custom`: Server certifiate is signed by a self-managed authority, and/or a client certificate is used. `custom` can be any value that coorisponds to a custom `tls.Config` registered with [`mysql.RegisterTLSConfig`](http://godoc.org/github.com/go-sql-driver/mysql#RegisterTLSConfig).
153+
154+
146155
### `LOAD DATA LOCAL INFILE` support
147156
For this feature you need direct access to the package. Therefore you must change the import path (no `_`):
148157
```go

tlsconfig.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

utils.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,50 @@ 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+
// {
87+
// pem, err := ioutil.ReadFile("/path/ca-cert.pem")
88+
// if err != nil {
89+
// log.Fatal(err)
90+
// }
91+
// if ok := rootCAs.AppendCertsFromPEM(pem); !ok {
92+
// log.Fatal("Failed to append PEM.")
93+
// }
94+
// }
95+
// clientCert := make([]tls.Certificate, 0, 1)
96+
// {
97+
// certs, err := tls.LoadX509KeyPair("/path/client-cert.pem", "/path/client-key.pem")
98+
// if err != nil {
99+
// log.Fatal(err)
100+
// }
101+
// clientCert = append(clientCerts, certs)
102+
// }
103+
// mysql.RegisterTLSConfig("custom", tls.Config{
104+
// RootCAs: rootCertPool,
105+
// Certificates: clientCert,
106+
// })
107+
// db, err := sql.Open("mysql", "user@tcp(localhost:3306)/test?tls=custom")
108+
//
109+
func RegisterTLSConfig(key string, config *tls.Config) {
110+
if tlsConfigMap == nil {
111+
tlsConfigMap = make(map[string]*tls.Config)
112+
}
113+
tlsConfigMap[key] = config
114+
}
115+
116+
// Removes tls.Config associated with key.
117+
func DeregisterTLSConfig(key string) {
118+
if tlsConfigMap == nil {
119+
return
120+
}
121+
delete(tlsConfigMap, key)
122+
}
123+
80124
// Logger
81125
var (
82126
errLog *log.Logger
@@ -153,8 +197,7 @@ func parseDSN(dsn string) (cfg *config, err error) {
153197
} else if strings.ToLower(value) == "skip-verify" {
154198
cfg.tls = &tls.Config{InsecureSkipVerify: true}
155199
} else if tlsConfig, ok := tlsConfigMap[value]; ok {
156-
cfg.tls = &tls.Config{}
157-
*cfg.tls = *tlsConfig
200+
cfg.tls = tlsConfig
158201
}
159202

160203
default:

0 commit comments

Comments
 (0)