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

meta/mysql: dont escape for mysql password #4069

Merged
merged 5 commits into from
Sep 27, 2023
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
20 changes: 20 additions & 0 deletions pkg/meta/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ type dbSnap struct {
chunk map[string]*chunk
}

func recoveryMysqlPwd(addr string) string {
colonIndex := strings.Index(addr, ":")
atIndex := strings.LastIndex(addr, "@")
if colonIndex != -1 && colonIndex < atIndex {
pwd := addr[colonIndex+1 : atIndex]
if parse, err := url.Parse("mysql://root:" + pwd + "@127.0.0.1"); err == nil {
if originPwd, ok := parse.User.Password(); ok {
addr = fmt.Sprintf("%s:%s%s", addr[:colonIndex], originPwd, addr[atIndex:])
}
}
}
return addr
}

func newSQLMeta(driver, addr string, conf *Config) (Meta, error) {
var searchPath string
if driver == "postgres" {
Expand All @@ -215,6 +229,12 @@ func newSQLMeta(driver, addr string, conf *Config) (Meta, error) {
}
}
}

// escaping is not necessary for mysql password https://github.com/go-sql-driver/mysql#password
if driver == "mysql" {
addr = recoveryMysqlPwd(addr)
}

engine, err := xorm.NewEngine(driver, addr)
if err != nil {
return nil, fmt.Errorf("unable to use data source %s: %s", driver, err)
Expand Down
64 changes: 64 additions & 0 deletions pkg/meta/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,67 @@ func TestPostgreSQLClientWithSearchPath(t *testing.T) { //skip mutate
t.Fatalf("TestPostgreSQLClientWithSearchPath error: %s", err)
}
}

func TestRecoveryMysqlPwd(t *testing.T) { //skip mutate
testCase := []struct {
addr string
expect string
}{
// no password
{"root@(localhost:3306)/db1",
"root@(localhost:3306)/db1",
},
// no password
{"root:@(localhost:3306)/db1",
"root:@(localhost:3306)/db1",
},

{"root::@@(localhost:3306)/db1",
"root::@@(localhost:3306)/db1",
},

{"root:@:@(localhost:3306)/db1",
"root:@:@(localhost:3306)/db1",
},

// no special char
{"root:password@(localhost:3306)/db1",
"root:password@(localhost:3306)/db1",
},

// set from env @
{"root:pass%40word@(localhost:3306)/db1",
"root:pass@word@(localhost:3306)/db1",
},

// direct pass special char @
{"root:pass@word@(localhost:3306)/db1",
"root:pass@word@(localhost:3306)/db1",
},

// set from env |
{"root:pass%7Cword@(localhost:3306)/db1",
"root:pass|word@(localhost:3306)/db1",
},

// direct pass special char |
{"root:pass|word@(localhost:3306)/db1",
"root:pass|word@(localhost:3306)/db1",
},

// set from env :
{"root:pass%3Aword@(localhost:3306)/db1",
"root:pass:word@(localhost:3306)/db1",
},

// direct pass special char :
{"root:pass:word@(localhost:3306)/db1",
"root:pass:word@(localhost:3306)/db1",
},
}
for _, tc := range testCase {
if got := recoveryMysqlPwd(tc.addr); got != tc.expect {
t.Fatalf("recoveryMysqlPwd error: expect %s but got %s", tc.expect, got)
}
}
}
Loading