From 6761c32ec1d73423cfa9b87e24a1bc0ffe74f761 Mon Sep 17 00:00:00 2001 From: Nurlan Moldomurov Date: Wed, 6 Nov 2024 15:40:12 +0300 Subject: [PATCH 1/6] PMM-12219 support special characters in password. --- main.go | 31 ++++++++++++++----------------- main_test.go | 11 +++++++++-- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index 02bcbc01..ec2c64d5 100644 --- a/main.go +++ b/main.go @@ -139,7 +139,7 @@ func main() { } func buildExporter(opts GlobalFlags, uri string, log *logrus.Logger) *exporter.Exporter { - uri = buildURI(uri, opts.User, opts.Password) + uri = buildURI(uri, opts.User, opts.Password, log) log.Debugf("Connection URI: %s", uri) uriParsed, _ := url.Parse(uri) @@ -266,26 +266,23 @@ func parseURIList(uriList []string, logger *logrus.Logger, splitCluster bool) [] return URIs } -func buildURI(uri string, user string, password string) string { - prefix := "mongodb://" // default prefix +func buildURI(uri string, user string, password string, log *logrus.Logger) string { + defaultPrefix := "mongodb://" // default prefix matchRegexp := regexp.MustCompile(`^mongodb(\+srv)?://`) - // Split the uri prefix if there is any - if matchRegexp.MatchString(uri) { - uriArray := strings.SplitN(uri, "://", 2) - prefix = uriArray[0] + "://" - uri = uriArray[1] + // Split the uri defaultPrefix if there is any + if !matchRegexp.MatchString(uri) { + uri = defaultPrefix + uri } - - // IF user@pass not contained in uri AND custom user and pass supplied in arguments - // DO concat a new uri with user and pass arguments value - if !strings.Contains(uri, "@") && user != "" && password != "" { - // add user and pass to the uri - uri = fmt.Sprintf("%s:%s@%s", user, password, uri) + parsedURI, err := url.Parse(uri) + if err != nil { + log.Fatalf("Failed to parse URI %s: %v", uri, err) + return uri } - // add back prefix after adding the user and pass - uri = prefix + uri + if parsedURI.User == nil && user != "" && password != "" { + parsedURI.User = url.UserPassword(user, password) + } - return uri + return parsedURI.String() } diff --git a/main_test.go b/main_test.go index b716a298..8007d4e8 100644 --- a/main_test.go +++ b/main_test.go @@ -219,12 +219,19 @@ func TestBuildURI(t *testing.T) { newPassword: "", expect: "mongodb+srv://xxx:zzz@127.0.0.1", }, + { + situation: "url with special characters in username and password", + origin: "mongodb://127.0.0.1", + newUser: "xxx?!#$%^&*()_+", + newPassword: "yyy?!#$%^&*()_+", + expect: "mongodb://xxx%3F%21%23$%25%5E&%2A%28%29_+:yyy%3F%21%23$%25%5E&%2A%28%29_+@127.0.0.1", + }, } for _, tc := range tests { - newUri := buildURI(tc.origin, tc.newUser, tc.newPassword) + newUri := buildURI(tc.origin, tc.newUser, tc.newPassword, logrus.New()) // t.Logf("Origin: %s", tc.origin) // t.Logf("Expect: %s", tc.expect) // t.Logf("Result: %s", newUri) - assert.Equal(t, newUri, tc.expect) + assert.Equal(t, tc.expect, newUri) } } From 4ab163b59c58e193e60a00a6b33eb0fe4e3602d5 Mon Sep 17 00:00:00 2001 From: Nurlan Moldomurov Date: Wed, 6 Nov 2024 16:06:43 +0300 Subject: [PATCH 2/6] PMM-12219 fix linter. --- main_test.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/main_test.go b/main_test.go index 8007d4e8..0efe3553 100644 --- a/main_test.go +++ b/main_test.go @@ -228,10 +228,12 @@ func TestBuildURI(t *testing.T) { }, } for _, tc := range tests { - newUri := buildURI(tc.origin, tc.newUser, tc.newPassword, logrus.New()) - // t.Logf("Origin: %s", tc.origin) - // t.Logf("Expect: %s", tc.expect) - // t.Logf("Result: %s", newUri) - assert.Equal(t, tc.expect, newUri) + t.Run(tc.situation, func(t *testing.T) { + newURI := buildURI(tc.origin, tc.newUser, tc.newPassword, logrus.New()) + // t.Logf("Origin: %s", tc.origin) + // t.Logf("Expect: %s", tc.expect) + // t.Logf("Result: %s", newURI) + assert.Equal(t, tc.expect, newURI) + }) } } From 5d1de79e3e58473ed4e4354c55931fc2a5f6ddbe Mon Sep 17 00:00:00 2001 From: Nurlan Moldomurov Date: Thu, 7 Nov 2024 13:50:04 +0300 Subject: [PATCH 3/6] Update main_test.go --- main_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/main_test.go b/main_test.go index 0efe3553..0ab66bb4 100644 --- a/main_test.go +++ b/main_test.go @@ -230,9 +230,6 @@ func TestBuildURI(t *testing.T) { for _, tc := range tests { t.Run(tc.situation, func(t *testing.T) { newURI := buildURI(tc.origin, tc.newUser, tc.newPassword, logrus.New()) - // t.Logf("Origin: %s", tc.origin) - // t.Logf("Expect: %s", tc.expect) - // t.Logf("Result: %s", newURI) assert.Equal(t, tc.expect, newURI) }) } From 4eb43f8129f64c623c530b05b87fc06d66b13d06 Mon Sep 17 00:00:00 2001 From: Nurlan Moldomurov Date: Fri, 8 Nov 2024 10:31:06 +0300 Subject: [PATCH 4/6] PMM-12219 force first mongodb being a primary. --- docker/scripts/setup.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/docker/scripts/setup.sh b/docker/scripts/setup.sh index c7a5dbdd..de02ba49 100755 --- a/docker/scripts/setup.sh +++ b/docker/scripts/setup.sh @@ -47,6 +47,7 @@ function cnf_servers() { { "_id": 0, "host": "${mongodb1}:${port}" + "priority": 1000 }, { "_id": 1, From 3969846c7c51dff706b667864934306510f3afaa Mon Sep 17 00:00:00 2001 From: Nurlan Moldomurov Date: Fri, 8 Nov 2024 11:40:47 +0300 Subject: [PATCH 5/6] PMM-12219 force first mongodb being a primary. --- docker/scripts/setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/scripts/setup.sh b/docker/scripts/setup.sh index de02ba49..56f88041 100755 --- a/docker/scripts/setup.sh +++ b/docker/scripts/setup.sh @@ -46,7 +46,7 @@ function cnf_servers() { "members": [ { "_id": 0, - "host": "${mongodb1}:${port}" + "host": "${mongodb1}:${port}", "priority": 1000 }, { From 36daa606c8b8b3395de6a5402e13b6e788aa2162 Mon Sep 17 00:00:00 2001 From: Nurlan Moldomurov Date: Fri, 8 Nov 2024 16:42:54 +0300 Subject: [PATCH 6/6] PMM-12219 force first mongodb being a primary. --- docker/scripts/setup.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docker/scripts/setup.sh b/docker/scripts/setup.sh index 56f88041..6ccfac0d 100755 --- a/docker/scripts/setup.sh +++ b/docker/scripts/setup.sh @@ -78,7 +78,8 @@ function general_servers() { "members": [ { "_id": 0, - "host": "${mongodb1}:${port}" + "host": "${mongodb1}:${port}", + "priority": 1000 }, { "_id": 1,