Skip to content

Commit

Permalink
rpk: profile updates for UAT
Browse files Browse the repository at this point in the history
  • Loading branch information
twmb committed Mar 14, 2024
1 parent 0e45b2d commit 2a6609c
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/cloud/auth/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func newCreateCommand(fs afero.Fs, p *config.Params) *cobra.Command {
Short: "Create an rpk cloud auth",
Hidden: true,
Run: func(*cobra.Command, []string) {
fmt.Println("'rpk cloud auth create' is deprecated as a no-op; use 'rpk cloud login --reload' instead.")
fmt.Println("'rpk cloud auth create' is deprecated as a no-op; use 'rpk cloud login' instead.")
},
}
var slice []string
Expand Down
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/cloud/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ between which cluster you are talking to.
}

cmd.AddCommand(
newUseCommand(fs, p),
newSelectCommand(fs, p),
)

return cmd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"github.com/spf13/cobra"
)

func newUseCommand(fs afero.Fs, p *config.Params) *cobra.Command {
func newSelectCommand(fs afero.Fs, p *config.Params) *cobra.Command {
var profileName string

cmd := &cobra.Command{
Use: "use [NAME]",
Use: "select [NAME]",
Short: "Update your rpk profile to talk to the requested cluster",
Long: `Update your rpk profile to talk to the requested cluster.
Expand Down Expand Up @@ -56,11 +56,11 @@ an existing self-hosted profile, please rename that profile or use the
--profile flag to explicitly name your new profile.
Either:
rpk profile use %[1]q
rpk profile select %[1]q
rpk profile rename-to $something_else
rpk cloud cluster use %[2]q
rpk cloud cluster select %[2]q
Or:
rpk cloud cluster use %[2]q --profile $another_something
rpk cloud cluster select %[2]q --profile $another_something
`, ee.Name, args[0])
os.Exit(1)
}
Expand Down
85 changes: 53 additions & 32 deletions src/go/rpk/pkg/cli/cloud/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"os"

"github.com/redpanda-data/redpanda/src/go/rpk/pkg/cli/container/common"
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/cli/profile"
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/config"
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/oauth"
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/oauth/providers/auth0"
Expand All @@ -24,7 +25,7 @@ import (
)

func newLoginCommand(fs afero.Fs, p *config.Params) *cobra.Command {
var save, noProfile, noBrowser, forceReload bool
var save, noProfile, noBrowser bool
cmd := &cobra.Command{
Use: "login",
Short: "Log in to the Redpanda cloud",
Expand All @@ -38,11 +39,6 @@ request the token.
You may use either SSO or client credentials to log in.
Logging in uses an existing current token if the token is still valid and not
expired. If you switched organizations in your browser and want to log into the
new organization, you can use the --reload flag to force a new token to be
requested.
SSO
This will automatically launch your default web browser and prompt you to
Expand Down Expand Up @@ -73,7 +69,7 @@ token and client ID is always synced.
out.MaybeDie(err, "rpk unable to load config: %v", err)

p := y.Profile(y.CurrentProfile)
authAct, authVir, clearedProfile, isNewAuth, err := oauth.LoadFlow(cmd.Context(), fs, cfg, auth0.NewClient(cfg.DevOverrides()), noBrowser, forceReload, cfg.DevOverrides().CloudAPIURL)
authAct, authVir, clearedProfile, _, err := oauth.LoadFlow(cmd.Context(), fs, cfg, auth0.NewClient(cfg.DevOverrides()), noBrowser, true, cfg.DevOverrides().CloudAPIURL)
if err != nil {
fmt.Printf("Unable to login to Redpanda Cloud (%v).\n", err)
if e := (*oauth.BadClientTokenError)(nil); errors.As(err, &e) && authVir != nil && authVir.HasClientCredentials() {
Expand All @@ -91,64 +87,89 @@ and then re-specify the client credentials next time you log in.`)
}

fmt.Printf("Successfully logged into organization %q (%s) via %s.\n", authAct.Organization, authAct.OrgID, authAct.AnyKind())
if !isNewAuth {
fmt.Println("If you are trying to authenticate to a different organization, use 'rpk cloud login --reload'.")
}
fmt.Println()

// When you have no profile, or you have one that is not selected.
// No profile, or you have profiles but none were selected.
// * If --no-profile, print a message and exit
// * Otherwise, prompt for cloud cluster selection
if p == nil || len(y.Profiles) == 0 {
fmt.Println(`To create an rpk profile to talk to an existing cloud cluster, use 'rpk cloud cluster use'.
if noProfile {
fmt.Println(`To create an rpk profile to talk to an existing cloud cluster, use 'rpk cloud cluster select'.
To learn more about profiles, check 'rpk profile --help'.
You are not currently in a profile; rpk talks to a localhost:9092 cluster by default.`)
} else {
fmt.Println("rpk will switch to a cloud cluster profile automatically, if you want to interrupt this process, feel free to ctrl+c now.")
err = profile.CreateFlow(cmd.Context(), fs, cfg, y, "", "", "prompt", false, nil, "", "")
profile.MaybeDieExistingName(err)
}
return
}

// When you had a profile, but it was cleared due to your browser
// having a different org's auth.
// User had a profile, but it was cleared due to the browser having
// a different org's auth.
// * If --no-profile, print a message and exit
// * Otherwise, prompt for cloud cluster selection
if p != nil && clearedProfile {
priorAuth := p.ActualAuth()
fmt.Printf(`rpk swapped away from your prior profile %q which authenticated with organization %q (%s).
fmt.Printf("rpk swapped away from your prior profile %q which was authenticated with organization %q (%s).\n", p.Name, priorAuth.Organization, priorAuth.OrgID)
if noProfile {
fmt.Printf(`
To create a new rpk profile for a cluster in this organization, try either:
rpk profile create --from-cloud
rpk cloud cluster use
rpk cloud cluster select
rpk will talk to a localhost:9092 cluster until you swap to a different profile.
`, p.Name, priorAuth.Organization, priorAuth.OrgID)
`)
} else {
fmt.Println("rpk will switch to a cloud cluster profile automatically, if you want to interrupt this process, feel free to ctrl+c now.")
err = profile.CreateFlow(cmd.Context(), fs, cfg, y, "", "", "prompt", false, nil, "", "")
profile.MaybeDieExistingName(err)
}
return
}

// When your current profile is a cloud cluster.
// The current profile was auth'd to the current organization.
// We tell the status of what org the user is talking to.
if p.FromCloud {
fmt.Printf("Your current profile %q is talking to your %q cloud cluster.\n", p.Name, p.CloudCluster.FullName())
fmt.Println("To switch which cluster you are talking to, use 'rpk cloud cluster use'.")
fmt.Println("To switch which cluster you are talking to, use 'rpk cloud cluster select'.")
return
}

// Below here, the current profile is pointed to a
// local container cluster or a self hosted cluster.
// We want to create or swap to the cloud profile,
// unless the user used --no-profile.
if noProfile {
// The current profile is seemingly pointing to a container cluster.
if p.Name == common.ContainerProfileName {
fmt.Printf("Your current profile %q is talking to a localhost 'rpk container' cluster.\n", p.Name)
fmt.Println("To talk to a cloud cluster, use 'rpk cloud cluster select'.")
return
}
// The current profile is a self hosted cluster.
fmt.Printf("Your current profile %q is talking to a self hosted cluster.\n", p.Name)
fmt.Println("To talk to a cloud cluster, use 'rpk cloud cluster select'.")
return
}

// When your current profile is pointing at the
// localhost cluster from rpk container start.
if p.Name == common.ContainerProfileName {
fmt.Printf("Your current profile %q is talking to a localhost 'rpk container' cluster.\n", p.Name)
fmt.Println("To talk to a cloud cluster, use 'rpk cloud cluster use'.")
return
} else {
fmt.Printf("Your current profile %q is talking to a self hosted cluster.\n", p.Name)
}
fmt.Println("rpk will switch to a cloud cluster profile automatically, if you want to interrupt this process and keep your current profile, feel free to ctrl+c now.")

// When your current profile is a self hosted cluster.
fmt.Printf("Your current profile %q is talking to a self hosted cluster.\n", p.Name)
fmt.Println("To talk to a cloud cluster, use 'rpk cloud cluster use'.")
// Prompt and switch.
err = profile.CreateFlow(cmd.Context(), fs, cfg, y, "", "", "prompt", false, nil, "", "")
profile.MaybeDieExistingName(err)
},
}

p.InstallCloudFlags(cmd)
cmd.Flags().BoolVar(&noBrowser, "no-browser", false, "Opt out of auto-opening authentication URL")
cmd.Flags().BoolVar(&save, "save", false, "Save environment or flag specified client ID and client secret to the configuration file")
cmd.Flags().BoolVar(&forceReload, "reload", false, "Force a new token to be requested, even if the current token is still valid")

// Hide the deprecated no-profile flag.
// We used to automatically create a profile.
cmd.Flags().BoolVar(&noProfile, "no-profile", false, "Skip automatic profile creation and any associated prompts")
_ = cmd.Flags().MarkHidden("no-profile")

return cmd
}
5 changes: 3 additions & 2 deletions src/go/rpk/pkg/cli/profile/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ func CreateFlow(
if !old.FromCloud {
return &ProfileExistsError{name}
}
p = old
*old = *p // update
p = old // the values
updatingRpkCloudProfile = true
}
}
Expand Down Expand Up @@ -604,7 +605,7 @@ func PromptCloudClusterProfile(ctx context.Context, yAuth *config.RpkCloudAuth,
} else {
ncs := combineClusterNames(nss, vcs, cs)
names := ncs.names()
idx, err := out.PickIndex(names, "Which cloud namespace/cluster would you like to create a profile for?")
idx, err := out.PickIndex(names, "Which cloud namespace/cluster would you like to talk to?")
if err != nil {
return CloudClusterOutputs{}, err
}
Expand Down
15 changes: 15 additions & 0 deletions src/go/rpk/pkg/cli/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
package profile

import (
"errors"
"fmt"
"os"
"strings"

"github.com/redpanda-data/redpanda/src/go/rpk/pkg/config"
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/out"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -92,3 +95,15 @@ type ProfileExistsError struct {
func (e *ProfileExistsError) Error() string {
return fmt.Sprintf("profile %q already exists", e.Name)
}

// MaybeDieExistingName exits if err is non-nil, but if err is
// ProfileExistsError, this exits with a more detailed message.
func MaybeDieExistingName(err error) {
if ee := (*ProfileExistsError)(nil); errors.As(err, &ee) {
fmt.Printf(`Unable to automatically create profile %[1]q due to a name conflict with
an existing profile, please rename the existing profile.
`, ee.Name)
os.Exit(1)
}
out.MaybeDieErr(err)
}
6 changes: 3 additions & 3 deletions src/go/rpk/pkg/config/rpk_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,14 +486,14 @@ func MaybePrintAuthSwitchMessage(priorAuth *RpkCloudAuth, currentAuth *RpkCloudA
if currentAuth == nil {
return
}
fmt.Println("rpk cloud commands are now talking to organization %q (%s).", currentAuth.Organization, currentAuth.OrgID)
fmt.Printf("rpk cloud commands are now talking to organization %q (%s).\n", currentAuth.Organization, currentAuth.OrgID)
return
}
if currentAuth == nil {
fmt.Println("rpk cloud commands are no longer talking to organization %q (%s) and are now talking to a self hosted cluster.", priorAuth.Organization, priorAuth.OrgID)
fmt.Printf("rpk cloud commands are no longer talking to organization %q (%s) and are now talking to a self hosted cluster.\n", priorAuth.Organization, priorAuth.OrgID)
return
}
if priorAuth.Name != currentAuth.Name {
fmt.Println("rpk switched from talking to organization %q (%s) to %q (%s).", priorAuth.Organization, priorAuth.OrgID, currentAuth.Organization, currentAuth.OrgID)
fmt.Printf("rpk switched from talking to organization %q (%s) to %q (%s).\n", priorAuth.Organization, priorAuth.OrgID, currentAuth.Organization, currentAuth.OrgID)
}
}

0 comments on commit 2a6609c

Please sign in to comment.