Skip to content

Commit 077c278

Browse files
Rebecca Chindprotaso
Rebecca Chin
authored andcommitted
Create methods to clone a tls.Config for older versions of go
We opted to copy the struct manually in order avoid copying internal objects (ie. the sync.Mutex and sync.Once in go1.7) Signed-off-by: Dave Protasowski <[email protected]>
1 parent 6898fad commit 077c278

7 files changed

+129
-2
lines changed

AUTHORS

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@ Zhenye Xie <xiezhenye at gmail.com>
6161

6262
Barracuda Networks, Inc.
6363
Google Inc.
64-
Stripe Inc.
6564
Pivotal Inc.
65+
Stripe Inc.

dsn.go

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

513513
if tlsConfig, ok := tlsConfigRegister[name]; ok {
514-
tlsConfig = tlsConfig.Clone()
514+
tlsConfig = cloneTLSConfig(tlsConfig)
515515

516516
if len(tlsConfig.ServerName) == 0 && !tlsConfig.InsecureSkipVerify {
517517
host, _, err := net.SplitHostPort(cfg.Addr)

tls_config_clone_go12.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// +build go1.2
2+
// +build !go1.3
3+
4+
package mysql
5+
6+
import "crypto/tls"
7+
8+
func cloneTLSConfig(c *tls.Config) *tls.Config {
9+
return &tls.Config{
10+
Rand: c.Rand,
11+
Time: c.Time,
12+
Certificates: c.Certificates,
13+
NameToCertificate: c.NameToCertificate,
14+
RootCAs: c.RootCAs,
15+
NextProtos: c.NextProtos,
16+
ServerName: c.ServerName,
17+
ClientAuth: c.ClientAuth,
18+
ClientCAs: c.ClientCAs,
19+
InsecureSkipVerify: c.InsecureSkipVerify,
20+
CipherSuites: c.CipherSuites,
21+
PreferServerCipherSuites: c.PreferServerCipherSuites,
22+
SessionTicketsDisabled: c.SessionTicketsDisabled,
23+
SessionTicketKey: c.SessionTicketKey,
24+
MinVersion: c.MinVersion,
25+
MaxVersion: c.MaxVersion,
26+
}
27+
}

tls_config_clone_go13.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// +build go1.3
2+
// +build !go1.4
3+
4+
package mysql
5+
6+
import "crypto/tls"
7+
8+
func cloneTLSConfig(c *tls.Config) *tls.Config {
9+
return &tls.Config{
10+
Rand: c.Rand,
11+
Time: c.Time,
12+
Certificates: c.Certificates,
13+
NameToCertificate: c.NameToCertificate,
14+
RootCAs: c.RootCAs,
15+
NextProtos: c.NextProtos,
16+
ServerName: c.ServerName,
17+
ClientAuth: c.ClientAuth,
18+
ClientCAs: c.ClientCAs,
19+
InsecureSkipVerify: c.InsecureSkipVerify,
20+
CipherSuites: c.CipherSuites,
21+
PreferServerCipherSuites: c.PreferServerCipherSuites,
22+
SessionTicketsDisabled: c.SessionTicketsDisabled,
23+
SessionTicketKey: c.SessionTicketKey,
24+
ClientSessionCache: c.ClientSessionCache,
25+
MinVersion: c.MinVersion,
26+
MaxVersion: c.MaxVersion,
27+
CurvePreferences: c.CurvePreferences,
28+
}
29+
}

tls_config_clone_go14-16.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// +build go1.4
2+
// +build !go1.7
3+
4+
package mysql
5+
6+
import "crypto/tls"
7+
8+
func cloneTLSConfig(c *tls.Config) *tls.Config {
9+
return &tls.Config{
10+
Rand: c.Rand,
11+
Time: c.Time,
12+
Certificates: c.Certificates,
13+
NameToCertificate: c.NameToCertificate,
14+
GetCertificate: c.GetCertificate,
15+
RootCAs: c.RootCAs,
16+
NextProtos: c.NextProtos,
17+
ServerName: c.ServerName,
18+
ClientAuth: c.ClientAuth,
19+
ClientCAs: c.ClientCAs,
20+
InsecureSkipVerify: c.InsecureSkipVerify,
21+
CipherSuites: c.CipherSuites,
22+
PreferServerCipherSuites: c.PreferServerCipherSuites,
23+
SessionTicketsDisabled: c.SessionTicketsDisabled,
24+
SessionTicketKey: c.SessionTicketKey,
25+
ClientSessionCache: c.ClientSessionCache,
26+
MinVersion: c.MinVersion,
27+
MaxVersion: c.MaxVersion,
28+
CurvePreferences: c.CurvePreferences,
29+
}
30+
}

tls_config_clone_go17.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// +build go1.7
2+
// +build !go1.8
3+
4+
package mysql
5+
6+
import "crypto/tls"
7+
8+
func cloneTLSConfig(c *tls.Config) *tls.Config {
9+
return &tls.Config{
10+
Rand: c.Rand,
11+
Time: c.Time,
12+
Certificates: c.Certificates,
13+
NameToCertificate: c.NameToCertificate,
14+
GetCertificate: c.GetCertificate,
15+
RootCAs: c.RootCAs,
16+
NextProtos: c.NextProtos,
17+
ServerName: c.ServerName,
18+
ClientAuth: c.ClientAuth,
19+
ClientCAs: c.ClientCAs,
20+
InsecureSkipVerify: c.InsecureSkipVerify,
21+
CipherSuites: c.CipherSuites,
22+
PreferServerCipherSuites: c.PreferServerCipherSuites,
23+
SessionTicketsDisabled: c.SessionTicketsDisabled,
24+
SessionTicketKey: c.SessionTicketKey,
25+
ClientSessionCache: c.ClientSessionCache,
26+
MinVersion: c.MinVersion,
27+
MaxVersion: c.MaxVersion,
28+
CurvePreferences: c.CurvePreferences,
29+
DynamicRecordSizingDisabled: c.DynamicRecordSizingDisabled,
30+
Renegotiation: c.Renegotiation,
31+
}
32+
}

tls_config_clone_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+
}

0 commit comments

Comments
 (0)