From 6e25063c2cd4954ec079049e116a41d814c9d808 Mon Sep 17 00:00:00 2001 From: Melanija Cvetic Date: Wed, 23 Jul 2025 14:19:55 +0100 Subject: [PATCH 1/2] replace register flow with login flow --- docs/command/atlas-setup.txt | 2 +- internal/cli/auth/login.go | 2 +- internal/cli/setup/setup_cmd.go | 65 ++++++++++------------------ internal/cli/setup/setup_cmd_test.go | 25 +++++------ 4 files changed, 38 insertions(+), 56 deletions(-) diff --git a/docs/command/atlas-setup.txt b/docs/command/atlas-setup.txt index 01a4eed2d1..d8e2b36114 100644 --- a/docs/command/atlas-setup.txt +++ b/docs/command/atlas-setup.txt @@ -79,7 +79,7 @@ Options * - --gov - - false - - Register with Atlas for Government. + - Login with Atlas for Government. * - -h, --help - - false diff --git a/internal/cli/auth/login.go b/internal/cli/auth/login.go index a2fd9a9601..28cb36634f 100644 --- a/internal/cli/auth/login.go +++ b/internal/cli/auth/login.go @@ -338,7 +338,7 @@ func (opts *LoginOpts) handleBrowser(uri string) { } if !opts.force { - _, _ = fmt.Fprintf(opts.OutWriter, "\nPress Enter to open the browser to complete authentication...") + _, _ = fmt.Fprintf(opts.OutWriter, "\nPress Enter to open the browser and complete authentication...") _, _ = fmt.Scanln() } if errBrowser := browser.OpenURL(uri); errBrowser != nil { diff --git a/internal/cli/setup/setup_cmd.go b/internal/cli/setup/setup_cmd.go index 0ffe0ce364..60f5cbe1e4 100644 --- a/internal/cli/setup/setup_cmd.go +++ b/internal/cli/setup/setup_cmd.go @@ -135,7 +135,7 @@ type AtlasClusterQuickStarter interface { type Opts struct { cli.ProjectOpts cli.WatchOpts - register auth.RegisterOpts + login auth.LoginOpts config profileReader store AtlasClusterQuickStarter defaultName string @@ -164,8 +164,7 @@ type Opts struct { connectionString string // control - skipRegister bool - skipLogin bool + skipLogin bool } type clusterSettings struct { @@ -408,26 +407,18 @@ func (opts *Opts) setupCloseHandler() { } func (opts *Opts) Run(ctx context.Context) error { - if !opts.skipRegister { - _, _ = fmt.Fprintf(opts.OutWriter, ` -This command will help you: -1. Create and verify your MongoDB Atlas account in your browser. -2. Return to the terminal to create your first free MongoDB database in Atlas. -`) - if err := opts.register.RegisterRun(ctx); err != nil { - return err - } - } else if !opts.skipLogin { + if !opts.skipLogin { _, _ = fmt.Fprintf(opts.OutWriter, `Next steps: 1. Log in and verify your MongoDB Atlas account in your browser. 2. Return to the terminal to create your first free MongoDB database in Atlas. + +If you wish to register a new account, run: 'atlas auth register'. `) - if err := opts.register.LoginRun(ctx); err != nil { + if err := opts.login.LoginRun(ctx); err != nil { return err } } - if err := opts.clusterPreRun(ctx, opts.OutWriter); err != nil { return err } @@ -445,8 +436,8 @@ func (opts *Opts) clusterPreRun(ctx context.Context, outWriter io.Writer) error return opts.PreRunE( opts.initStore(ctx), - opts.register.SyncWithOAuthAccessProfile(defaultProfile), - opts.register.InitFlow(defaultProfile), + opts.login.SyncWithOAuthAccessProfile(defaultProfile), + opts.login.InitFlow(defaultProfile), opts.InitOutput(outWriter, ""), ) } @@ -586,7 +577,6 @@ func (opts *Opts) promptConnect() error { } func (opts *Opts) PreRun(ctx context.Context) error { - opts.skipRegister = true opts.skipLogin = true if err := validate.NoAPIKeys(); err != nil { @@ -596,14 +586,14 @@ func (opts *Opts) PreRun(ctx context.Context) error { // The error is useful in other components that call `validate.NoAPIKeys()` return nil } - if err := opts.register.RefreshAccessToken(ctx); err != nil && commonerrors.IsInvalidRefreshToken(err) { - opts.skipLogin = false - return nil - } + + // if profile has access token and refresh token is valid, we can skip login if _, err := auth.AccountWithAccessToken(); err == nil { - return nil + if err := opts.login.RefreshAccessToken(ctx); err != nil && !commonerrors.IsInvalidRefreshToken(err) { + return nil + } } - opts.skipRegister = false + opts.skipLogin = false return nil } @@ -679,29 +669,22 @@ func Builder() *cobra.Command { defaultProfile := config.Default() opts.config = defaultProfile opts.OutWriter = cmd.OutOrStdout() - opts.register.OutWriter = opts.OutWriter + opts.login.OutWriter = opts.OutWriter - if err := opts.register.SyncWithOAuthAccessProfile(defaultProfile)(); err != nil { + if err := opts.login.SyncWithOAuthAccessProfile(defaultProfile)(); err != nil { return err } - if err := opts.register.InitFlow(defaultProfile)(); err != nil { + if err := opts.login.InitFlow(defaultProfile)(); err != nil { return err } if err := opts.PreRun(cmd.Context()); err != nil { return nil } var preRun []prerun.CmdOpt - // registration pre run if applicable - if !opts.skipRegister { - preRun = append(preRun, - opts.register.LoginPreRun(cmd.Context()), - validate.NoAPIKeys, - validate.NoAccessToken, - ) - } - - if !opts.skipLogin && opts.skipRegister { - preRun = append(preRun, opts.register.LoginPreRun(cmd.Context())) + // login pre run if applicable + if !opts.skipLogin { + opts.login.Asker = &telemetry.Ask{} + preRun = append(preRun, opts.login.LoginPreRun(cmd.Context())) } preRun = append(preRun, opts.validateTier) preRun = append(preRun, validate.AutoScalingMode(opts.AutoScalingMode)) @@ -714,9 +697,9 @@ func Builder() *cobra.Command { }, } - // Register and login related - cmd.Flags().BoolVar(&opts.register.IsGov, "gov", false, "Register with Atlas for Government.") - cmd.Flags().BoolVar(&opts.register.NoBrowser, "noBrowser", false, "Don't try to open a browser session.") + // Login related + cmd.Flags().BoolVar(&opts.login.IsGov, "gov", false, "Login with Atlas for Government.") + cmd.Flags().BoolVar(&opts.login.NoBrowser, "noBrowser", false, "Don't try to open a browser session.") // Setup related cmd.Flags().StringVar(&opts.MDBVersion, flag.MDBVersion, "", usage.DeploymentMDBVersion) cmd.Flags().StringVar(&opts.connectWith, flag.ConnectWith, "", usage.ConnectWithAtlasSetup) diff --git a/internal/cli/setup/setup_cmd_test.go b/internal/cli/setup/setup_cmd_test.go index e293f39254..8a9ebd865c 100644 --- a/internal/cli/setup/setup_cmd_test.go +++ b/internal/cli/setup/setup_cmd_test.go @@ -41,34 +41,33 @@ func Test_setupOpts_PreRunWithAPIKeys(t *testing.T) { opts := &Opts{} opts.OutWriter = buf - opts.register.WithFlow(mockFlow) + opts.login.WithFlow(mockFlow) config.SetPublicAPIKey("publicKey") config.SetPrivateAPIKey("privateKey") require.NoError(t, opts.PreRun(ctx)) - assert.True(t, opts.skipRegister) assert.Equal(t, 0, buf.Len()) assert.True(t, opts.skipLogin) + t.Cleanup(func() { + config.SetPublicAPIKey("") + config.SetPrivateAPIKey("") + }) } -func Test_setupOpts_RunSkipRegister(t *testing.T) { +func Test_setupOpts_RunSkipLogin(t *testing.T) { ctrl := gomock.NewController(t) mockFlow := mocks.NewMockRefresher(ctrl) ctx := t.Context() buf := new(bytes.Buffer) - opts := &Opts{ - skipLogin: true, - } - opts.register.WithFlow(mockFlow) - - config.SetAccessToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ") + opts := &Opts{} + opts.login.WithFlow(mockFlow) opts.OutWriter = buf require.NoError(t, opts.PreRun(ctx)) - assert.True(t, opts.skipRegister) + assert.False(t, opts.skipLogin) } func TestCluster_Run(t *testing.T) { @@ -109,7 +108,7 @@ func TestCluster_Run(t *testing.T) { Tag: map[string]string{"env": "test"}, AutoScalingMode: clusterWideScaling, } - opts.register.WithFlow(mockFlow) + opts.login.WithFlow(mockFlow) projectIPAccessList := opts.newProjectIPAccessList() @@ -169,7 +168,7 @@ func TestCluster_Run_LatestAPI(t *testing.T) { Tag: map[string]string{"env": "test"}, AutoScalingMode: "independentShardingScaling", } - opts.register.WithFlow(mockFlow) + opts.login.WithFlow(mockFlow) projectIPAccessList := opts.newProjectIPAccessList() @@ -237,7 +236,7 @@ func TestCluster_Run_CheckFlagsSet(t *testing.T) { MDBVersion: "7.0", AutoScalingMode: clusterWideScaling, } - opts.register.WithFlow(mockFlow) + opts.login.WithFlow(mockFlow) projectIPAccessList := opts.newProjectIPAccessList() From 248c6fd514551c5a58ba56c9d692efcdbb63362c Mon Sep 17 00:00:00 2001 From: Melanija Cvetic Date: Wed, 23 Jul 2025 14:24:30 +0100 Subject: [PATCH 2/2] Replaces mentions of registration from help text with login --- docs/command/atlas-setup.txt | 4 ++-- docs/command/atlas.txt | 2 +- internal/cli/setup/setup_cmd.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/command/atlas-setup.txt b/docs/command/atlas-setup.txt index d8e2b36114..7f8d5ddf73 100644 --- a/docs/command/atlas-setup.txt +++ b/docs/command/atlas-setup.txt @@ -12,14 +12,14 @@ atlas setup :depth: 1 :class: singlecol -Register, authenticate, create, and access an Atlas cluster. +Login, authenticate, create, and access an Atlas cluster. Public Preview: The atlas api sub-command, automatically generated from the MongoDB Atlas Admin API, offers full coverage of the Admin API and is currently in Public Preview (please provide feedback at https://feedback.mongodb.com/forums/930808-atlas-cli). Admin API capabilities have their own release lifecycle, which you can check via the provided API endpoint documentation link. -This command takes you through registration, login, default profile creation, creating your first free tier cluster and connecting to it using MongoDB Shell. +This command takes you through login, default profile creation, creating your first free tier cluster and connecting to it using MongoDB Shell. Syntax ------ diff --git a/docs/command/atlas.txt b/docs/command/atlas.txt index a19aa90b59..ad7e825d89 100644 --- a/docs/command/atlas.txt +++ b/docs/command/atlas.txt @@ -87,7 +87,7 @@ Related Commands * :ref:`atlas-processes` - Manage MongoDB processes for your project. * :ref:`atlas-projects` - Manage your Atlas projects. * :ref:`atlas-security` - Manage security configuration for your project. -* :ref:`atlas-setup` - Register, authenticate, create, and access an Atlas cluster. +* :ref:`atlas-setup` - Login, authenticate, create, and access an Atlas cluster. * :ref:`atlas-streams` - Manage your Atlas Stream Processing deployments. * :ref:`atlas-teams` - Manage your Atlas teams. * :ref:`atlas-users` - Manage your Atlas users. diff --git a/internal/cli/setup/setup_cmd.go b/internal/cli/setup/setup_cmd.go index 60f5cbe1e4..df56368626 100644 --- a/internal/cli/setup/setup_cmd.go +++ b/internal/cli/setup/setup_cmd.go @@ -659,8 +659,8 @@ func Builder() *cobra.Command { cmd := &cobra.Command{ Use: "setup", Aliases: []string{"quickstart"}, - Short: "Register, authenticate, create, and access an Atlas cluster.", - Long: `This command takes you through registration, login, default profile creation, creating your first free tier cluster and connecting to it using MongoDB Shell.`, + Short: "Login, authenticate, create, and access an Atlas cluster.", + Long: `This command takes you through login, default profile creation, creating your first free tier cluster and connecting to it using MongoDB Shell.`, Example: ` # Override default cluster settings like name, provider, or database username by using the command options atlas setup --clusterName Test --provider GCP --username dbuserTest`, Hidden: false,