From 2c63450f3d8832f5b3f30d9ecfb211797f49c278 Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Tue, 30 May 2017 09:33:22 -0400 Subject: [PATCH] Don't mutate registered tls configs Signed-off-by: Rebecca Chin --- AUTHORS | 3 +++ dsn.go | 2 ++ dsn_test.go | 2 ++ utils.go | 2 ++ utils_go17.go | 40 ++++++++++++++++++++++++++++++++++++++++ utils_go18.go | 17 +++++++++++++++++ utils_legacy.go | 18 ++++++++++++++++++ 7 files changed, 84 insertions(+) create mode 100644 utils_go17.go create mode 100644 utils_go18.go create mode 100644 utils_legacy.go diff --git a/AUTHORS b/AUTHORS index 1928dac89..4601d08f2 100644 --- a/AUTHORS +++ b/AUTHORS @@ -18,6 +18,7 @@ Carlos Nieto Chris Moos Daniel Nichter Daniël van Eeden +Dave Protasowski DisposaBoy Egor Smolyakov Frederick Mayle @@ -46,6 +47,7 @@ Nicola Peduzzi Olivier Mengué Paul Bonser Peter Schultz +Rebecca Chin Runrioter Wung Soroush Pour Stan Putrya @@ -59,4 +61,5 @@ Zhenye Xie Barracuda Networks, Inc. Google Inc. +Pivotal Inc. Stripe Inc. diff --git a/dsn.go b/dsn.go index 5c828bf90..c49827f5d 100644 --- a/dsn.go +++ b/dsn.go @@ -511,6 +511,8 @@ func parseDSNParams(cfg *Config, params string) (err error) { } if tlsConfig, ok := tlsConfigRegister[name]; ok { + tlsConfig = cloneTLSConfig(tlsConfig) + if len(tlsConfig.ServerName) == 0 && !tlsConfig.InsecureSkipVerify { host, _, err := net.SplitHostPort(cfg.Addr) if err == nil { diff --git a/dsn_test.go b/dsn_test.go index 0693192ad..4fd76b813 100644 --- a/dsn_test.go +++ b/dsn_test.go @@ -159,6 +159,8 @@ func TestDSNWithCustomTLS(t *testing.T) { t.Error(err.Error()) } else if cfg.tls.ServerName != name { t.Errorf("did not get the correct ServerName (%s) parsing DSN (%s).", name, tst) + } else if tlsCfg.ServerName != "" { + t.Errorf("tlsCfg was mutated ServerName (%s) should be empty parsing DSN (%s).", name, tst) } DeregisterTLSConfig("utils_test") diff --git a/utils.go b/utils.go index d523b7ffd..bd11c6975 100644 --- a/utils.go +++ b/utils.go @@ -26,6 +26,8 @@ var ( // RegisterTLSConfig registers a custom tls.Config to be used with sql.Open. // Use the key as a value in the DSN where tls=value. // +// Note: The tls.Config provided to needs to be exclusively owned by the driver after registering. +// // rootCertPool := x509.NewCertPool() // pem, err := ioutil.ReadFile("/path/ca-cert.pem") // if err != nil { diff --git a/utils_go17.go b/utils_go17.go new file mode 100644 index 000000000..f59563456 --- /dev/null +++ b/utils_go17.go @@ -0,0 +1,40 @@ +// Go MySQL Driver - A MySQL-Driver for Go's database/sql package +// +// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved. +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at http://mozilla.org/MPL/2.0/. + +// +build go1.7 +// +build !go1.8 + +package mysql + +import "crypto/tls" + +func cloneTLSConfig(c *tls.Config) *tls.Config { + return &tls.Config{ + Rand: c.Rand, + Time: c.Time, + Certificates: c.Certificates, + NameToCertificate: c.NameToCertificate, + GetCertificate: c.GetCertificate, + RootCAs: c.RootCAs, + NextProtos: c.NextProtos, + ServerName: c.ServerName, + ClientAuth: c.ClientAuth, + ClientCAs: c.ClientCAs, + InsecureSkipVerify: c.InsecureSkipVerify, + CipherSuites: c.CipherSuites, + PreferServerCipherSuites: c.PreferServerCipherSuites, + SessionTicketsDisabled: c.SessionTicketsDisabled, + SessionTicketKey: c.SessionTicketKey, + ClientSessionCache: c.ClientSessionCache, + MinVersion: c.MinVersion, + MaxVersion: c.MaxVersion, + CurvePreferences: c.CurvePreferences, + DynamicRecordSizingDisabled: c.DynamicRecordSizingDisabled, + Renegotiation: c.Renegotiation, + } +} diff --git a/utils_go18.go b/utils_go18.go new file mode 100644 index 000000000..2aa9d0f18 --- /dev/null +++ b/utils_go18.go @@ -0,0 +1,17 @@ +// Go MySQL Driver - A MySQL-Driver for Go's database/sql package +// +// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved. +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at http://mozilla.org/MPL/2.0/. + +// +build go1.8 + +package mysql + +import "crypto/tls" + +func cloneTLSConfig(c *tls.Config) *tls.Config { + return c.Clone() +} diff --git a/utils_legacy.go b/utils_legacy.go new file mode 100644 index 000000000..a03b10de2 --- /dev/null +++ b/utils_legacy.go @@ -0,0 +1,18 @@ +// Go MySQL Driver - A MySQL-Driver for Go's database/sql package +// +// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved. +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at http://mozilla.org/MPL/2.0/. + +// +build !go1.7 + +package mysql + +import "crypto/tls" + +func cloneTLSConfig(c *tls.Config) *tls.Config { + clone := *c + return &clone +}