Skip to content

Commit 6ed58cd

Browse files
BupycHukidoqo
andauthored
PMM-12219 support special characters in password. (#949)
* PMM-12219 support special characters in password. * PMM-12219 fix linter. * Update main_test.go * PMM-12219 force first mongodb being a primary. * PMM-12219 force first mongodb being a primary. * PMM-12219 force first mongodb being a primary. --------- Co-authored-by: Michael Okoko <[email protected]>
1 parent 6d48a93 commit 6ed58cd

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

docker/scripts/setup.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ function cnf_servers() {
4646
"members": [
4747
{
4848
"_id": 0,
49-
"host": "${mongodb1}:${port}"
49+
"host": "${mongodb1}:${port}",
50+
"priority": 1000
5051
},
5152
{
5253
"_id": 1,
@@ -77,7 +78,8 @@ function general_servers() {
7778
"members": [
7879
{
7980
"_id": 0,
80-
"host": "${mongodb1}:${port}"
81+
"host": "${mongodb1}:${port}",
82+
"priority": 1000
8183
},
8284
{
8385
"_id": 1,

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: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,18 @@ 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)
225-
// t.Logf("Origin: %s", tc.origin)
226-
// t.Logf("Expect: %s", tc.expect)
227-
// t.Logf("Result: %s", newUri)
228-
assert.Equal(t, newUri, tc.expect)
231+
t.Run(tc.situation, func(t *testing.T) {
232+
newURI := buildURI(tc.origin, tc.newUser, tc.newPassword, logrus.New())
233+
assert.Equal(t, tc.expect, newURI)
234+
})
229235
}
230236
}

0 commit comments

Comments
 (0)