Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DSN without 'user:password@' #888

Merged
merged 5 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,14 @@ Configuration options can be provided by the standard DSN (Data Source Name).
[user[:password]@]addr[/db[?param=X]]
```

#### `collation`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i forgot to add this connection param in my previous commits


Set a collation during the Auth handshake.

| Type | Default | Example |
| --------- | --------------- | ----------------------------------------------------- |
| string | utf8_general_ci | user:pass@localhost/mydb?collation=latin1_general_ci |

#### `compress`

Enable compression between the client and the server. Valid values are 'zstd','zlib','uncompressed'.
Expand Down
6 changes: 2 additions & 4 deletions client/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,8 @@ func (c *Conn) writeAuthHandshake() error {
return fmt.Errorf("invalid collation name %s", collationName)
}

// the MySQL protocol calls for the collation id to be sent as 1, where only the
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updating this comment which was incorrect regarding the first byte of the 23 bytes of filter being used to store the right 8 bits of the collation id.

// lower 8 bits are used in this field. But wireshark shows that the first byte of
// the 23 bytes of filler is used to send the right middle 8 bits of the collation id.
// see https://github.com/mysql/mysql-server/pull/541
// the MySQL protocol calls for the collation id to be sent as 1 byte, where only the
// lower 8 bits are used in this field.
data[12] = byte(collation.ID & 0xff)

// SSL Connection Request Packet
Expand Down
13 changes: 9 additions & 4 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"net/url"
"regexp"
"strings"
"sync"
"time"

Expand All @@ -24,6 +25,7 @@ var customTLSMutex sync.Mutex

// Map of dsn address (makes more sense than full dsn?) to tls Config
var (
dsnRegex = regexp.MustCompile("@[^@]+/[^@/]+")
customTLSConfigMap = make(map[string]*tls.Config)
options = make(map[string]DriverOption)

Expand Down Expand Up @@ -55,13 +57,16 @@ type connInfo struct {
//
// Optional parameters are supported in the standard DSN form
func parseDSN(dsn string) (connInfo, error) {
var matchErr error
ci := connInfo{}

// If a "/" occurs after "@" and then no more "@" or "/" occur after that
ci.standardDSN, matchErr = regexp.MatchString("@[^@]+/[^@/]+", dsn)
if matchErr != nil {
return ci, errors.Errorf("invalid dsn, must be user:password@addr[/db[?param=X]]")
if strings.Contains(dsn, "@") {
ci.standardDSN = dsnRegex.MatchString(dsn)
} else {
// when the `@` char is not present in the dsn, then look for `/` as the db separator
// to indicate a standard DSN. The legacy form uses the `?` char as the db separator.
// If neither `/` or `?` are in the dsn, simply treat the dsn as the legacy form.
ci.standardDSN = strings.Contains(dsn, "/")
}

// Add a prefix so we can parse with url.Parse
Expand Down
6 changes: 6 additions & 0 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ func TestParseDSN(t *testing.T) {
"user:[email protected]/db?readTimeout=1m": {standardDSN: true, addr: "5.domain.com", user: "user", password: "password", db: "db", params: url.Values{"readTimeout": []string{"1m"}}},
"user:[email protected]/db?writeTimeout=1m": {standardDSN: true, addr: "5.domain.com", user: "user", password: "password", db: "db", params: url.Values{"writeTimeout": []string{"1m"}}},
"user:[email protected]/db?compress=zlib": {standardDSN: true, addr: "5.domain.com", user: "user", password: "password", db: "db", params: url.Values{"compress": []string{"zlib"}}},

// per the documentation in the README, the 'user:password@' is optional as are the '/db?param=X' portions of the DSN
"6.domain.com": {standardDSN: false, addr: "6.domain.com", user: "", password: "", db: "", params: url.Values{}},
"7.domain.com?db": {standardDSN: false, addr: "7.domain.com", user: "", password: "", db: "db", params: url.Values{}},
"8.domain.com/db": {standardDSN: true, addr: "8.domain.com", user: "", password: "", db: "db", params: url.Values{}},
"9.domain.com/db?compress=zlib": {standardDSN: true, addr: "9.domain.com", user: "", password: "", db: "db", params: url.Values{"compress": []string{"zlib"}}},
}

for supplied, expected := range testDSNs {
Expand Down