Skip to content

Commit

Permalink
meta/mysql: dont escape for mysql password (#4069)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijian-pro authored and davies committed Nov 25, 2023
1 parent 4b55d44 commit e1a4c4d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
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)
}
}
}

0 comments on commit e1a4c4d

Please sign in to comment.