Skip to content

Commit 6761c32

Browse files
committed
PMM-12219 support special characters in password.
1 parent 1e917bf commit 6761c32

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

main.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func main() {
139139
}
140140

141141
func buildExporter(opts GlobalFlags, uri string, log *logrus.Logger) *exporter.Exporter {
142-
uri = buildURI(uri, opts.User, opts.Password)
142+
uri = buildURI(uri, opts.User, opts.Password, log)
143143
log.Debugf("Connection URI: %s", uri)
144144

145145
uriParsed, _ := url.Parse(uri)
@@ -266,26 +266,23 @@ func parseURIList(uriList []string, logger *logrus.Logger, splitCluster bool) []
266266
return URIs
267267
}
268268

269-
func buildURI(uri string, user string, password string) string {
270-
prefix := "mongodb://" // default prefix
269+
func buildURI(uri string, user string, password string, log *logrus.Logger) string {
270+
defaultPrefix := "mongodb://" // default prefix
271271
matchRegexp := regexp.MustCompile(`^mongodb(\+srv)?://`)
272272

273-
// Split the uri prefix if there is any
274-
if matchRegexp.MatchString(uri) {
275-
uriArray := strings.SplitN(uri, "://", 2)
276-
prefix = uriArray[0] + "://"
277-
uri = uriArray[1]
273+
// Split the uri defaultPrefix if there is any
274+
if !matchRegexp.MatchString(uri) {
275+
uri = defaultPrefix + uri
278276
}
279-
280-
// IF user@pass not contained in uri AND custom user and pass supplied in arguments
281-
// DO concat a new uri with user and pass arguments value
282-
if !strings.Contains(uri, "@") && user != "" && password != "" {
283-
// add user and pass to the uri
284-
uri = fmt.Sprintf("%s:%s@%s", user, password, uri)
277+
parsedURI, err := url.Parse(uri)
278+
if err != nil {
279+
log.Fatalf("Failed to parse URI %s: %v", uri, err)
280+
return uri
285281
}
286282

287-
// add back prefix after adding the user and pass
288-
uri = prefix + uri
283+
if parsedURI.User == nil && user != "" && password != "" {
284+
parsedURI.User = url.UserPassword(user, password)
285+
}
289286

290-
return uri
287+
return parsedURI.String()
291288
}

main_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,19 @@ func TestBuildURI(t *testing.T) {
219219
newPassword: "",
220220
expect: "mongodb+srv://xxx:[email protected]",
221221
},
222+
{
223+
situation: "url with special characters in username and password",
224+
origin: "mongodb://127.0.0.1",
225+
newUser: "xxx?!#$%^&*()_+",
226+
newPassword: "yyy?!#$%^&*()_+",
227+
expect: "mongodb://xxx%3F%21%23$%25%5E&%2A%28%29_+:yyy%3F%21%23$%25%5E&%2A%28%[email protected]",
228+
},
222229
}
223230
for _, tc := range tests {
224-
newUri := buildURI(tc.origin, tc.newUser, tc.newPassword)
231+
newUri := buildURI(tc.origin, tc.newUser, tc.newPassword, logrus.New())
225232
// t.Logf("Origin: %s", tc.origin)
226233
// t.Logf("Expect: %s", tc.expect)
227234
// t.Logf("Result: %s", newUri)
228-
assert.Equal(t, newUri, tc.expect)
235+
assert.Equal(t, tc.expect, newUri)
229236
}
230237
}

0 commit comments

Comments
 (0)