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

PMM-12219 support special characters in password. #949

Merged
merged 7 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
31 changes: 14 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
16 changes: 11 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,18 @@ func TestBuildURI(t *testing.T) {
newPassword: "",
expect: "mongodb+srv://xxx:[email protected]",
},
{
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%[email protected]",
},
}
for _, tc := range tests {
newUri := buildURI(tc.origin, tc.newUser, tc.newPassword)
// t.Logf("Origin: %s", tc.origin)
// t.Logf("Expect: %s", tc.expect)
// t.Logf("Result: %s", newUri)
assert.Equal(t, newUri, tc.expect)
t.Run(tc.situation, func(t *testing.T) {
newURI := buildURI(tc.origin, tc.newUser, tc.newPassword, logrus.New())
assert.Equal(t, tc.expect, newURI)
})
}
}