From c0b7db8d649bf5f411472696459ec8644b555024 Mon Sep 17 00:00:00 2001 From: Ma77C0nd0n Date: Thu, 19 Oct 2023 10:33:24 +0100 Subject: [PATCH] CLOUDP-205313: patch dbuser update to support external DB --- .../atlascli/command/atlas-dbusers-update.txt | 4 ++ .../command/mongocli-atlas-dbusers-update.txt | 4 ++ internal/cli/atlas/dbusers/update.go | 23 +++++++-- internal/cli/atlas/dbusers/update_test.go | 48 +++++++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/docs/atlascli/command/atlas-dbusers-update.txt b/docs/atlascli/command/atlas-dbusers-update.txt index 750a0c0012..9b4cf086f3 100644 --- a/docs/atlascli/command/atlas-dbusers-update.txt +++ b/docs/atlascli/command/atlas-dbusers-update.txt @@ -53,6 +53,10 @@ Options - Type - Required - Description + * - --authDB + - string + - false + - Authentication database name. If the user authenticates with AWS IAM, x.509, or LDAP, this value should be $external. If the user authenticates with SCRAM-SHA, this value should be admin. * - -h, --help - - false diff --git a/docs/mongocli/command/mongocli-atlas-dbusers-update.txt b/docs/mongocli/command/mongocli-atlas-dbusers-update.txt index 0cc0a4a166..89be6f7940 100644 --- a/docs/mongocli/command/mongocli-atlas-dbusers-update.txt +++ b/docs/mongocli/command/mongocli-atlas-dbusers-update.txt @@ -53,6 +53,10 @@ Options - Type - Required - Description + * - --authDB + - string + - false + - Authentication database name. If the user authenticates with AWS IAM, x.509, or LDAP, this value should be $external. If the user authenticates with SCRAM-SHA, this value should be admin. * - -h, --help - - false diff --git a/internal/cli/atlas/dbusers/update.go b/internal/cli/atlas/dbusers/update.go index 786d95014f..00d5b8e86c 100644 --- a/internal/cli/atlas/dbusers/update.go +++ b/internal/cli/atlas/dbusers/update.go @@ -26,6 +26,7 @@ import ( "github.com/mongodb/mongodb-atlas-cli/internal/pointer" "github.com/mongodb/mongodb-atlas-cli/internal/store" "github.com/mongodb/mongodb-atlas-cli/internal/usage" + "github.com/mongodb/mongodb-atlas-cli/internal/validate" "github.com/spf13/cobra" "go.mongodb.org/atlas-sdk/v20230201004/admin" ) @@ -38,6 +39,7 @@ type UpdateOpts struct { username string currentUsername string password string + authDB string roles []string scopes []string store store.DatabaseUserUpdater @@ -79,13 +81,26 @@ func (opts *UpdateOpts) update(out *admin.CloudDatabaseUser) { if opts.password != "" { out.Password = pointer.GetStringPointerIfNotEmpty(opts.password) } - out.Scopes = convert.BuildAtlasScopes(opts.scopes) out.Roles = convert.BuildAtlasRoles(opts.roles) - out.DatabaseName = convert.GetAuthDB(out) + out.DatabaseName = opts.authDB + if opts.authDB == "" { + out.DatabaseName = convert.GetAuthDB(out) + } +} + +func (opts *UpdateOpts) validateAuthDB() error { + if opts.authDB == "" { + return nil + } + validAuthDBs := []string{convert.AdminDB, convert.ExternalAuthDB} + if err := validate.FlagInSlice(opts.authDB, flag.AuthDB, validAuthDBs); err != nil { + return err + } + return nil } -// atlas dbuser(s) update [--password password] [--role roleName@dbName] [--projectId projectId]. +// atlas dbuser(s) update [--password password] [--role roleName@dbName] [--projectId projectId] [--authDB authDB] func UpdateBuilder() *cobra.Command { opts := &UpdateOpts{} cmd := &cobra.Command{ @@ -106,6 +121,7 @@ func UpdateBuilder() *cobra.Command { PreRunE: func(cmd *cobra.Command, args []string) error { return opts.PreRunE( opts.ValidateProjectID, + opts.validateAuthDB, opts.initStore(cmd.Context()), opts.InitOutput(cmd.OutOrStdout(), updateTemplate), ) @@ -118,6 +134,7 @@ func UpdateBuilder() *cobra.Command { cmd.Flags().StringVarP(&opts.username, flag.Username, flag.UsernameShort, "", usage.DBUsername) cmd.Flags().StringVarP(&opts.password, flag.Password, flag.PasswordShort, "", usage.DBUserPassword) + cmd.Flags().StringVar(&opts.authDB, flag.AuthDB, "", usage.AtlasAuthDB) cmd.Flags().StringSliceVar(&opts.roles, flag.Role, []string{}, usage.Roles+usage.UpdateWarning) cmd.Flags().StringSliceVar(&opts.scopes, flag.Scope, []string{}, usage.Scopes+usage.UpdateWarning) diff --git a/internal/cli/atlas/dbusers/update_test.go b/internal/cli/atlas/dbusers/update_test.go index c0ab96c36f..bfc7b5003b 100644 --- a/internal/cli/atlas/dbusers/update_test.go +++ b/internal/cli/atlas/dbusers/update_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/golang/mock/gomock" + "github.com/mongodb/mongodb-atlas-cli/internal/convert" "github.com/mongodb/mongodb-atlas-cli/internal/mocks" "go.mongodb.org/atlas-sdk/v20230201004/admin" ) @@ -57,3 +58,50 @@ func TestDBUserUpdate_Run(t *testing.T) { t.Fatalf("Run() unexpected error: %v", err) } } + +func TestDBUserUpdate_validateAuthDB(t *testing.T) { + type fields struct { + authDB string + } + tests := []struct { + name string + fields fields + wantErr bool + }{ + { + name: "invalid authDB", + fields: fields{ + authDB: "fakeAuthDB", + }, + wantErr: true, + }, + { + name: "valid adminDB", + fields: fields{ + authDB: convert.AdminDB, + }, + wantErr: false, + }, + { + name: "valid externalAuthDB", + fields: fields{ + authDB: convert.ExternalAuthDB, + }, + wantErr: false, + }, + } + for _, tt := range tests { + fields := tt.fields + wantErr := tt.wantErr + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + updateOpts := &UpdateOpts{ + authDB: fields.authDB, + } + + if err := updateOpts.validateAuthDB(); (err != nil) != wantErr { + t.Errorf("validate() error = %v, wantErr %v", err, wantErr) + } + }) + } +}