Skip to content

Commit 60d456a

Browse files
dveedenjulienschmidt
authored andcommitted
Implement support of Optional TLS (#900)
Issue: #899 Add `preferred` config value to the `tls` config variable on the DSN. This results in a TLS connection when the server advertises this by the flag send in the initial packet.
1 parent 6be42e0 commit 60d456a

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,11 @@ Timeout for establishing connections, aka dial timeout. The value must be a deci
328328

329329
```
330330
Type: bool / string
331-
Valid Values: true, false, skip-verify, <name>
331+
Valid Values: true, false, skip-verify, preferred, <name>
332332
Default: false
333333
```
334334

335-
`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`](https://godoc.org/github.com/go-sql-driver/mysql#RegisterTLSConfig).
335+
`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 `preferred` to use TLS only when advertised by the server, this is similar to `skip-verify`, but additionally allows a fallback to a connection which is not encrypted. Use a custom value registered with [`mysql.RegisterTLSConfig`](https://godoc.org/github.com/go-sql-driver/mysql#RegisterTLSConfig).
336336

337337

338338
##### `writeTimeout`

driver_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ func TestFoundRows(t *testing.T) {
13041304
}
13051305

13061306
func TestTLS(t *testing.T) {
1307-
tlsTest := func(dbt *DBTest) {
1307+
tlsTestReq := func(dbt *DBTest) {
13081308
if err := dbt.db.Ping(); err != nil {
13091309
if err == ErrNoTLS {
13101310
dbt.Skip("server does not support TLS")
@@ -1321,19 +1321,27 @@ func TestTLS(t *testing.T) {
13211321
dbt.Fatal(err.Error())
13221322
}
13231323

1324-
if value == nil {
1325-
dbt.Fatal("no Cipher")
1324+
if (*value == nil) || (len(*value) == 0) {
1325+
dbt.Fatalf("no Cipher")
1326+
} else {
1327+
dbt.Logf("Cipher: %s", *value)
13261328
}
13271329
}
13281330
}
1331+
tlsTestOpt := func(dbt *DBTest) {
1332+
if err := dbt.db.Ping(); err != nil {
1333+
dbt.Fatalf("error on Ping: %s", err.Error())
1334+
}
1335+
}
13291336

1330-
runTests(t, dsn+"&tls=skip-verify", tlsTest)
1337+
runTests(t, dsn+"&tls=preferred", tlsTestOpt)
1338+
runTests(t, dsn+"&tls=skip-verify", tlsTestReq)
13311339

13321340
// Verify that registering / using a custom cfg works
13331341
RegisterTLSConfig("custom-skip-verify", &tls.Config{
13341342
InsecureSkipVerify: true,
13351343
})
1336-
runTests(t, dsn+"&tls=custom-skip-verify", tlsTest)
1344+
runTests(t, dsn+"&tls=custom-skip-verify", tlsTestReq)
13371345
}
13381346

13391347
func TestReuseClosedConnection(t *testing.T) {

dsn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ func parseDSNParams(cfg *Config, params string) (err error) {
560560
} else {
561561
cfg.TLSConfig = "false"
562562
}
563-
} else if vl := strings.ToLower(value); vl == "skip-verify" {
563+
} else if vl := strings.ToLower(value); vl == "skip-verify" || vl == "preferred" {
564564
cfg.TLSConfig = vl
565565
cfg.tls = &tls.Config{InsecureSkipVerify: true}
566566
} else {

packets.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,11 @@ func (mc *mysqlConn) readHandshakePacket() (data []byte, plugin string, err erro
194194
return nil, "", ErrOldProtocol
195195
}
196196
if mc.flags&clientSSL == 0 && mc.cfg.tls != nil {
197-
return nil, "", ErrNoTLS
197+
if mc.cfg.TLSConfig == "preferred" {
198+
mc.cfg.tls = nil
199+
} else {
200+
return nil, "", ErrNoTLS
201+
}
198202
}
199203
pos += 2
200204

0 commit comments

Comments
 (0)