Skip to content

Commit 9fda3fb

Browse files
jshaRoland Bracewell Shoemaker
authored andcommitted
Switch to DSNs (letsencrypt#4044)
* Switch to DSNs We used to use "mysql+tcp://" URLs but we don't need those anymore, and there aren't any more of them in prod. * Fix test.
1 parent 7369bf0 commit 9fda3fb

File tree

13 files changed

+14
-65
lines changed

13 files changed

+14
-65
lines changed

cmd/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ func TestDBConfigURL(t *testing.T) {
1818
{
1919
// Test with one config file that has no trailing newline
2020
conf: DBConfig{DBConnectFile: "testdata/test_dburl"},
21-
expected: "mysql+tcp://test@testhost:3306/testDB?readTimeout=800ms&writeTimeout=800ms",
21+
expected: "test@tcp(testhost:3306)/testDB?readTimeout=800ms&writeTimeout=800ms",
2222
},
2323
{
2424
// Test with a config file that *has* a trailing newline
2525
conf: DBConfig{DBConnectFile: "testdata/test_dburl_newline"},
26-
expected: "mysql+tcp://test@testhost:3306/testDB?readTimeout=800ms&writeTimeout=800ms",
26+
expected: "test@tcp(testhost:3306)/testDB?readTimeout=800ms&writeTimeout=800ms",
2727
},
2828
}
2929

cmd/testdata/test_dburl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mysql+tcp://test@testhost:3306/testDB?readTimeout=800ms&writeTimeout=800ms
1+
test@tcp(testhost:3306)/testDB?readTimeout=800ms&writeTimeout=800ms

cmd/testdata/test_dburl_newline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
mysql+tcp://test@testhost:3306/testDB?readTimeout=800ms&writeTimeout=800ms
1+
test@tcp(testhost:3306)/testDB?readTimeout=800ms&writeTimeout=800ms
22

sa/database.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package sa
33
import (
44
"database/sql"
55
"fmt"
6-
"net/url"
7-
"strings"
86
"time"
97

108
"github.com/go-sql-driver/mysql"
@@ -22,12 +20,6 @@ import (
2220
func NewDbMap(dbConnect string, maxOpenConns int) (*gorp.DbMap, error) {
2321
var err error
2422
var config *mysql.Config
25-
if strings.HasPrefix(dbConnect, "mysql+tcp://") {
26-
dbConnect, err = recombineCustomMySQLURL(dbConnect)
27-
if err != nil {
28-
return nil, err
29-
}
30-
}
3123

3224
config, err = mysql.ParseDSN(dbConnect)
3325
if err != nil {
@@ -104,49 +96,6 @@ func adjustMySQLConfig(conf *mysql.Config) *mysql.Config {
10496
return conf
10597
}
10698

107-
// recombineCustomMySQLURL transforms the legacy database URLs into the
108-
// URL-like strings expected by the mysql database driver.
109-
//
110-
// In the past, changes to the connection string were achieved by passing it
111-
// into url.Parse and editing the query string that way, so the string had to
112-
// be a valid URL. The mysql driver needs the Host data to be wrapped in
113-
// "tcp()" but url.Parse will escape the parentheses and the mysql driver
114-
// doesn't understand them. So, we couldn't have "tcp()" in the configs, but
115-
// couldn't leave it out before passing it to the mysql driver. Similarly, the
116-
// driver needs the password and username unescaped. The compromise was to do
117-
// the leg work if the connection string's scheme is a fake one called
118-
// "mysql+tcp://".
119-
//
120-
// Upon the addition of
121-
// https://godoc.org/github.com/go-sql-driver/mysql#Config, this was no longer
122-
// necessary, as the changes could be made on the decomposed struct version of
123-
// the connection url. This method converts the old format into the format
124-
// expected by the library.
125-
func recombineCustomMySQLURL(dbConnect string) (string, error) {
126-
dbConnect = strings.TrimSpace(dbConnect)
127-
dbURL, err := url.Parse(dbConnect)
128-
if err != nil {
129-
return "", err
130-
}
131-
132-
if dbURL.Scheme != "mysql+tcp" {
133-
format := "given database connection string was not a mysql+tcp:// URL, was %#v"
134-
return "", fmt.Errorf(format, dbURL.Scheme)
135-
}
136-
137-
user := dbURL.User.Username()
138-
passwd, hasPass := dbURL.User.Password()
139-
dbConn := ""
140-
if user != "" {
141-
dbConn = url.QueryEscape(user)
142-
}
143-
if hasPass {
144-
dbConn += ":" + passwd
145-
}
146-
dbConn += "@tcp(" + dbURL.Host + ")"
147-
return dbConn + dbURL.EscapedPath() + "?" + dbURL.RawQuery, nil
148-
}
149-
15099
// SetSQLDebug enables GORP SQL-level Debugging
151100
func SetSQLDebug(dbMap *gorp.DbMap, log blog.Logger) {
152101
dbMap.TraceOn("SQL: ", &SQLLogger{log})

sa/database_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestMaxOpenConns(t *testing.T) {
3737
}
3838

3939
func TestNewDbMap(t *testing.T) {
40-
const mysqlConnectURL = "mysql+tcp://policy:password@boulder-mysql:3306/boulder_policy_integration?readTimeout=800ms&writeTimeout=800ms"
40+
const mysqlConnectURL = "policy:password@tcp(boulder-mysql:3306)/boulder_policy_integration?readTimeout=800ms&writeTimeout=800ms"
4141
const expected = "policy:password@tcp(boulder-mysql:3306)/boulder_policy_integration?clientFoundRows=true&parseTime=true&readTimeout=800ms&writeTimeout=800ms&long_query_time=0.6400000000000001&max_statement_time=0.76&sql_mode=STRICT_ALL_TABLES"
4242
oldSQLOpen := sqlOpen
4343
defer func() {
@@ -52,7 +52,7 @@ func TestNewDbMap(t *testing.T) {
5252

5353
dbMap, err := NewDbMap(mysqlConnectURL, 0)
5454
if err != errExpected {
55-
t.Errorf("got incorrect error: %v", err)
55+
t.Errorf("got incorrect error. Got %v, expected %v", err, errExpected)
5656
}
5757
if dbMap != nil {
5858
t.Errorf("expected nil, got %v", dbMap)

test/secrets/backfiller_dburl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mysql+tcp://sa@boulder-mysql:3306/boulder_sa_integration
1+
sa@tcp(boulder-mysql:3306)/boulder_sa_integration

test/secrets/cert_checker_dburl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mysql+tcp://cert_checker@boulder-mysql:3306/boulder_sa_integration
1+
cert_checker@tcp(boulder-mysql:3306)/boulder_sa_integration
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mysql+tcp://mailer@boulder-mysql:3306/boulder_sa_integration
1+
mailer@tcp(boulder-mysql:3306)/boulder_sa_integration

test/secrets/mailer_dburl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mysql+tcp://mailer@boulder-mysql:3306/boulder_sa_integration
1+
mailer@tcp(boulder-mysql:3306)/boulder_sa_integration

test/secrets/ocsp_updater_dburl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mysql+tcp://ocsp_update@boulder-mysql:3306/boulder_sa_integration?readTimeout=800ms&writeTimeout=800ms&timeout=100ms
1+
ocsp_update@tcp(boulder-mysql:3306)/boulder_sa_integration?readTimeout=800ms&writeTimeout=800ms&timeout=100ms

0 commit comments

Comments
 (0)