-
Notifications
You must be signed in to change notification settings - Fork 168
/
tls.go
58 lines (49 loc) · 1.69 KB
/
tls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package mysql
import (
"database/sql"
"database/sql/driver"
"net/url"
tools "github.com/go-sql-driver/mysql"
"github.com/pydio/cells/v4/common/crypto"
)
func init() {
sql.Register("mysql+tls", &MySQLDriver{})
}
// MySQLDriver is exported to make the driver directly accessible.
// In general the driver is used via the database/sql package.
type MySQLDriver struct{}
// Open new Connection.
// See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
// the DSN string is formatted
func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
mysqlConfig, err := tools.ParseDSN(dsn)
if err != nil {
return nil, err
}
if ssl, ok := mysqlConfig.Params["ssl"]; ok && ssl == "true" {
u := &url.URL{}
q := u.Query()
q.Add(crypto.KeyCertStoreName, mysqlConfig.Params[crypto.KeyCertStoreName])
q.Add(crypto.KeyCertInsecureHost, mysqlConfig.Params[crypto.KeyCertInsecureHost])
q.Add(crypto.KeyCertUUID, mysqlConfig.Params[crypto.KeyCertUUID])
q.Add(crypto.KeyCertKeyUUID, mysqlConfig.Params[crypto.KeyCertKeyUUID])
q.Add(crypto.KeyCertCAUUID, mysqlConfig.Params[crypto.KeyCertCAUUID])
u.RawQuery = q.Encode()
tlsConfig, err := crypto.TLSConfigFromURL(u)
if err != nil {
return nil, err
}
if tlsConfig != nil {
delete(mysqlConfig.Params, "ssl")
delete(mysqlConfig.Params, crypto.KeyCertStoreName)
delete(mysqlConfig.Params, crypto.KeyCertInsecureHost)
delete(mysqlConfig.Params, crypto.KeyCertUUID)
delete(mysqlConfig.Params, crypto.KeyCertKeyUUID)
delete(mysqlConfig.Params, crypto.KeyCertCAUUID)
tools.RegisterTLSConfig("cells", tlsConfig)
mysqlConfig.TLSConfig = "cells"
dsn = mysqlConfig.FormatDSN()
}
}
return tools.MySQLDriver{}.Open(dsn)
}