Skip to content

Commit

Permalink
TOOLS-2963 Move prompting for password to earlier in options processi…
Browse files Browse the repository at this point in the history
…ng (mongodb#431)

This fixes an issue where we would not prompt for the password at all in some cases. We'd just error out saying that a password was required.
  • Loading branch information
autarch committed Jun 6, 2022
1 parent 48ad24e commit c528a3f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
10 changes: 0 additions & 10 deletions common/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/mongodb/mongo-tools/common/log"
"github.com/mongodb/mongo-tools/common/options"
"github.com/mongodb/mongo-tools/common/password"
"github.com/youmark/pkcs8"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -119,15 +118,6 @@ func (sp *SessionProvider) DB(name string) *mongo.Database {

// NewSessionProvider constructs a session provider, including a connected client.
func NewSessionProvider(opts options.ToolOptions) (*SessionProvider, error) {
// finalize auth options, filling in missing passwords
if opts.Auth.ShouldAskForPassword() {
pass, err := password.Prompt()
if err != nil {
return nil, fmt.Errorf("error reading password: %v", err)
}
opts.Auth.Password = pass
}

client, err := configureClient(opts)
if err != nil {
return nil, fmt.Errorf("error configuring the connector: %v", err)
Expand Down
11 changes: 11 additions & 0 deletions common/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
flags "github.com/jessevdk/go-flags"
"github.com/mongodb/mongo-tools/common/failpoint"
"github.com/mongodb/mongo-tools/common/log"
"github.com/mongodb/mongo-tools/common/password"
"github.com/mongodb/mongo-tools/common/util"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/mongo/readpref"
Expand Down Expand Up @@ -654,6 +655,16 @@ func (opts *ToolOptions) NormalizeOptionsAndURI() error {
if err != nil {
return err
}

// finalize auth options, filling in missing passwords
if opts.Auth.ShouldAskForPassword() {
pass, err := password.Prompt()
if err != nil {
return fmt.Errorf("error reading password: %v", err)
}
opts.Auth.Password = pass
}

err = opts.ConnString.Validate()
if err != nil {
return err
Expand Down

0 comments on commit c528a3f

Please sign in to comment.