From 4a407e61cd7b6ea0d24d9c354b5410e60156af38 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Thu, 14 Jul 2022 13:15:52 -0500 Subject: [PATCH] :bug: Hardcode MongoDB authenticationDatabase to admin This will be configurable via a flag once per-database flags are added --- internal/database/dialect/mongodb.go | 3 +++ internal/database/dialect/mongodb_test.go | 26 +++++++++++------------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/internal/database/dialect/mongodb.go b/internal/database/dialect/mongodb.go index 277b2491..0d9bbda4 100644 --- a/internal/database/dialect/mongodb.go +++ b/internal/database/dialect/mongodb.go @@ -71,6 +71,7 @@ func (MongoDB) ExecCommand(conf config.Exec) *command.Builder { "--host=127.0.0.1", "--username="+conf.Username, "--password="+conf.Password, + "--authenticationDatabase=admin", ) if conf.DisableHeaders { cmd.Push("--quiet") @@ -91,6 +92,7 @@ func (MongoDB) DumpCommand(conf config.Dump) *command.Builder { "--host=127.0.0.1", "--username="+conf.Username, "--password="+conf.Password, + "--authenticationDatabase=admin", ) if conf.Database != "" { cmd.Push("--db=" + conf.Database) @@ -114,6 +116,7 @@ func (MongoDB) RestoreCommand(conf config.Restore, inputFormat sqlformat.Format) "--host=127.0.0.1", "--username="+conf.Username, "--password="+conf.Password, + "--authenticationDatabase=admin", ) if conf.Database != "" { if conf.Clean { diff --git a/internal/database/dialect/mongodb_test.go b/internal/database/dialect/mongodb_test.go index 0524bb9d..291e65d0 100644 --- a/internal/database/dialect/mongodb_test.go +++ b/internal/database/dialect/mongodb_test.go @@ -111,27 +111,27 @@ func TestMongoDB_DumpCommand(t *testing.T) { { "default", args{config.Dump{Global: config.Global{Database: "d", Username: "u", Password: "p"}}}, - command.NewBuilder("mongodump", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--db=d"), + command.NewBuilder("mongodump", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--authenticationDatabase=admin", "--db=d"), }, { "clean", args{config.Dump{Clean: true, Global: config.Global{Database: "d", Username: "u", Password: "p"}}}, - command.NewBuilder("mongodump", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--db=d"), + command.NewBuilder("mongodump", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--authenticationDatabase=admin", "--db=d"), }, { "tables", args{config.Dump{Tables: []string{"table1"}, Global: config.Global{Database: "d", Username: "u", Password: "p"}}}, - command.NewBuilder("mongodump", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--db=d", "--collection=table1"), + command.NewBuilder("mongodump", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--authenticationDatabase=admin", "--db=d", "--collection=table1"), }, { "exclude-table", args{config.Dump{ExcludeTable: []string{"table1", "table2"}, Global: config.Global{Database: "d", Username: "u", Password: "p"}}}, - command.NewBuilder("mongodump", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--db=d", "--excludeCollection=table1", "--excludeCollection=table2"), + command.NewBuilder("mongodump", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--authenticationDatabase=admin", "--db=d", "--excludeCollection=table1", "--excludeCollection=table2"), }, { "quiet", args{config.Dump{Global: config.Global{Database: "d", Username: "u", Password: "p", Quiet: true}}}, - command.NewBuilder("mongodump", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--db=d", "--quiet"), + command.NewBuilder("mongodump", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--authenticationDatabase=admin", "--db=d", "--quiet"), }, } for _, tt := range tests { @@ -156,17 +156,17 @@ func TestMongoDB_ExecCommand(t *testing.T) { { "default", args{config.Exec{Global: config.Global{Database: "d", Username: "u", Password: "p"}}}, - command.NewBuilder("mongosh", "--host=127.0.0.1", "--username=u", "--password=p", "d"), + command.NewBuilder("mongosh", "--host=127.0.0.1", "--username=u", "--password=p", "--authenticationDatabase=admin", "d"), }, { "disable-headers", args{config.Exec{DisableHeaders: true, Global: config.Global{Database: "d", Username: "u", Password: "p"}}}, - command.NewBuilder("mongosh", "--host=127.0.0.1", "--username=u", "--password=p", "--quiet", "d"), + command.NewBuilder("mongosh", "--host=127.0.0.1", "--username=u", "--password=p", "--authenticationDatabase=admin", "--quiet", "d"), }, { "command", args{config.Exec{Command: "show databases", Global: config.Global{Database: "d", Username: "u", Password: "p"}}}, - command.NewBuilder("mongosh", "--host=127.0.0.1", "--username=u", "--password=p", "--eval=show databases", "d"), + command.NewBuilder("mongosh", "--host=127.0.0.1", "--username=u", "--password=p", "--authenticationDatabase=admin", "--eval=show databases", "d"), }, } for _, tt := range tests { @@ -311,27 +311,27 @@ func TestMongoDB_RestoreCommand(t *testing.T) { { "gzip", args{config.Restore{Global: config.Global{Database: "d", Username: "u", Password: "p"}}, sqlformat.Gzip}, - command.NewBuilder("mongorestore", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--db=d"), + command.NewBuilder("mongorestore", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--authenticationDatabase=admin", "--db=d"), }, { "plain", args{config.Restore{Global: config.Global{Database: "d", Username: "u", Password: "p"}}, sqlformat.Plain}, - command.NewBuilder("mongorestore", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--db=d"), + command.NewBuilder("mongorestore", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--authenticationDatabase=admin", "--db=d"), }, { "custom", args{config.Restore{Global: config.Global{Database: "d", Username: "u", Password: "p"}}, sqlformat.Custom}, - command.NewBuilder("mongorestore", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--db=d"), + command.NewBuilder("mongorestore", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--authenticationDatabase=admin", "--db=d"), }, { "clean", args{config.Restore{Clean: true, Global: config.Global{Database: "d", Username: "u", Password: "p"}}, sqlformat.Gzip}, - command.NewBuilder("mongorestore", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--drop", "--db=d"), + command.NewBuilder("mongorestore", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--authenticationDatabase=admin", "--drop", "--db=d"), }, { "quiet", args{config.Restore{Global: config.Global{Database: "d", Username: "u", Password: "p", Quiet: true}}, sqlformat.Gzip}, - command.NewBuilder("mongorestore", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--db=d", "--quiet"), + command.NewBuilder("mongorestore", "--archive", "--host=127.0.0.1", "--username=u", "--password=p", "--authenticationDatabase=admin", "--db=d", "--quiet"), }, } for _, tt := range tests {