Problem Statement/Rationale
From a Slack discussion:
try and authentication with PLAIN mech and observe the error within the driver via the call to opts.ConnString.Validate() .
$ ./bin/mongotop --host localhost --port 27017 --authenticationMechanism PLAIN --ssl --username admin 5
Enter password for mongo user:
2023-02-06T12:20:39.949-0500 error parsing command line options: password required for PLAIN
2023-02-06T12:20:39.949-0500 try 'mongotop --help' for more information
Steps to Reproduce
See above
Expected Results
The password provided to the prompt should be used.
Actual Results
See above
Additional Notes
John proposed this patch:
diff --git a/common/options/options.go b/common/options/options.go index 3c8661e5..d5687b77 100644 --- a/common/options/options.go +++ b/common/options/options.go @@ -668,6 +668,15 @@ func (opts *ToolOptions) setURIFromPositionalArg(args []string) ([]string, error // connection string. If a value is set on the connection string, but not the options, // that value is added to the options. func (opts *ToolOptions) NormalizeOptionsAndURI() error { + // finalize auth options, filling in missing passwords + if opts.Auth.ShouldAskForPassword() { + pass, err := password.Prompt("mongo user") + if err != nil { + return fmt.Errorf("error reading password: %v", err) + } + opts.Auth.Password = pass + } + if opts.URI == nil || opts.URI.ConnectionString == "" { // If URI not provided, get replica set name and generate connection string _, opts.ReplicaSetName = util.SplitHostArg(opts.Host) @@ -687,15 +696,6 @@ func (opts *ToolOptions) NormalizeOptionsAndURI() error { return err } - // finalize auth options, filling in missing passwords - if opts.Auth.ShouldAskForPassword() { - pass, err := password.Prompt("mongo user") - if err != nil { - return fmt.Errorf("error reading password: %v", err) - } - opts.Auth.Password = pass - } - shouldAskForSSLPassword, err := opts.SSL.ShouldAskForPassword() if err != nil { return fmt.Errorf("error determining whether client cert needs password: %v", err)}}
But from discussion in Slack, this may not be right:
I’m not sure it is [right]. We shouldn’t check if we need to prompt for the password before we parse the connstring. Otherwise we ignore passwords set in the connstring. I think the correct solution here is to set the password in the connstring as well as the opts once we get it from the prompt.