-
Notifications
You must be signed in to change notification settings - Fork 516
/
driver_mysql.go
69 lines (61 loc) · 1.67 KB
/
driver_mysql.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
59
60
61
62
63
64
65
66
67
68
69
// +build !no_mysql
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"regexp"
"github.com/go-sql-driver/mysql"
_ "github.com/ziutek/mymysql/godrv"
)
// normalizeMySQLDSN parses the dsn used with the mysql driver to always have
// the parameter `parseTime` set to true. This allows internal goose logic
// to assume that DATETIME/DATE/TIMESTAMP can be scanned into the time.Time
// type.
func normalizeDBString(driver string, str string, certfile string) string {
if driver == "mysql" {
var isTLS = certfile != ""
if isTLS {
if err := registerTLSConfig(certfile); err != nil {
log.Fatalf("goose run: %v", err)
}
}
var err error
str, err = normalizeMySQLDSN(str, isTLS)
if err != nil {
log.Fatalf("failed to normalize MySQL connection string: %v", err)
}
}
return str
}
const tlsConfigKey = "custom"
var tlsReg = regexp.MustCompile(`(\?|&)tls=[^&]*(?:&|$)`)
func normalizeMySQLDSN(dsn string, tls bool) (string, error) {
// If we are sharing a DSN in a different environment, it may contain a TLS
// setting key with a value name that is not "custom," so clear it.
dsn = tlsReg.ReplaceAllString(dsn, `$1`)
config, err := mysql.ParseDSN(dsn)
if err != nil {
return "", err
}
config.ParseTime = true
if tls {
config.TLSConfig = tlsConfigKey
}
return config.FormatDSN(), nil
}
func registerTLSConfig(pemfile string) error {
rootCertPool := x509.NewCertPool()
pem, err := ioutil.ReadFile(pemfile)
if err != nil {
return err
}
if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
return fmt.Errorf("failed to append PEM: %q", pemfile)
}
return mysql.RegisterTLSConfig(tlsConfigKey, &tls.Config{
RootCAs: rootCertPool,
})
}