Skip to content

Commit 7187728

Browse files
dprotasoRebecca Chin
authored and
Rebecca Chin
committed
Don't mutate registered tls configs
Signed-off-by: Rebecca Chin <[email protected]>
1 parent 382e13d commit 7187728

File tree

6 files changed

+28
-0
lines changed

6 files changed

+28
-0
lines changed

AUTHORS

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Carlos Nieto <jose.carlos at menteslibres.net>
1818
Chris Moos <chris at tech9computers.com>
1919
Daniel Nichter <nil at codenode.com>
2020
Daniël van Eeden <git at myname.nl>
21+
Dave Protasowski <dprotaso at gmail.com>
2122
DisposaBoy <disposaboy at dby.me>
2223
Egor Smolyakov <egorsmkv at gmail.com>
2324
Frederick Mayle <frederickmayle at gmail.com>
@@ -46,6 +47,7 @@ Nicola Peduzzi <thenikso at gmail.com>
4647
Olivier Mengué <dolmen at cpan.org>
4748
Paul Bonser <misterpib at gmail.com>
4849
Peter Schultz <peter.schultz at classmarkets.com>
50+
Rebecca Chin <rchin at pivotal.io>
4951
Runrioter Wung <runrioter at gmail.com>
5052
Soroush Pour <me at soroushjp.com>
5153
Stan Putrya <root.vagner at gmail.com>
@@ -59,4 +61,5 @@ Zhenye Xie <xiezhenye at gmail.com>
5961

6062
Barracuda Networks, Inc.
6163
Google Inc.
64+
Pivotal Inc.
6265
Stripe Inc.

dsn.go

+2
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ func parseDSNParams(cfg *Config, params string) (err error) {
511511
}
512512

513513
if tlsConfig, ok := tlsConfigRegister[name]; ok {
514+
tlsConfig = cloneTLSConfig(tlsConfig)
515+
514516
if len(tlsConfig.ServerName) == 0 && !tlsConfig.InsecureSkipVerify {
515517
host, _, err := net.SplitHostPort(cfg.Addr)
516518
if err == nil {

dsn_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ func TestDSNWithCustomTLS(t *testing.T) {
159159
t.Error(err.Error())
160160
} else if cfg.tls.ServerName != name {
161161
t.Errorf("did not get the correct ServerName (%s) parsing DSN (%s).", name, tst)
162+
} else if tlsCfg.ServerName != "" {
163+
t.Errorf("tlsCfg was mutated ServerName (%s) should be empty parsing DSN (%s).", name, tst)
162164
}
163165

164166
DeregisterTLSConfig("utils_test")

utils.go

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ var (
2626
// RegisterTLSConfig registers a custom tls.Config to be used with sql.Open.
2727
// Use the key as a value in the DSN where tls=value.
2828
//
29+
// Note: The tls.Config provided to needs to be exclusively owned by the driver after registering.
30+
//
2931
// rootCertPool := x509.NewCertPool()
3032
// pem, err := ioutil.ReadFile("/path/ca-cert.pem")
3133
// if err != nil {

utils_go18.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// +build go1.8
2+
3+
package mysql
4+
5+
import "crypto/tls"
6+
7+
func cloneTLSConfig(c *tls.Config) *tls.Config {
8+
return c.Clone()
9+
}

utils_legacy.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// +build !go1.8
2+
3+
package mysql
4+
5+
import "crypto/tls"
6+
7+
func cloneTLSConfig(c *tls.Config) *tls.Config {
8+
clone := *c
9+
return &clone
10+
}

0 commit comments

Comments
 (0)