diff --git a/cmd/auth.go b/cmd/auth.go index 0f0a675a1..b894f8e4e 100644 --- a/cmd/auth.go +++ b/cmd/auth.go @@ -1,13 +1,14 @@ package cmd import ( + "fmt" "io" - "github.com/astronomer/astro-cli/pkg/domainutil" - astro "github.com/astronomer/astro-cli/astro-client" cloudAuth "github.com/astronomer/astro-cli/cloud/auth" "github.com/astronomer/astro-cli/context" + "github.com/astronomer/astro-cli/pkg/domainutil" + "github.com/astronomer/astro-cli/pkg/input" softwareAuth "github.com/astronomer/astro-cli/software/auth" "github.com/spf13/cobra" @@ -59,7 +60,20 @@ func login(cmd *cobra.Command, args []string, astroClient astro.Client, out io.W cmd.SilenceUsage = true if len(args) == 1 { + // check if user provided a valid cloud domain if !context.IsCloudDomain(args[0]) { + // get the domain from context as an extra check + ctx, _ := context.GetCurrentContext() + if context.IsCloudDomain(ctx.Domain) { + // print an error if context domain is a valid cloud domain + fmt.Fprintf(out, "Error: %s is an invalid domain to login into Astro.\n", args[0]) + // give the user an option to login to software + y, _ := input.Confirm("Are you trying to authenticate to Astronomer Software?") + if !y { + fmt.Println("Canceling login...") + return nil + } + } return softwareLogin(args[0], oAuth, "", "", houstonVersion, houstonClient, out) } return cloudLogin(args[0], "", token, astroClient, out, shouldDisplayLoginLink) diff --git a/cmd/auth_test.go b/cmd/auth_test.go index ad0e08a00..aee6a50bb 100644 --- a/cmd/auth_test.go +++ b/cmd/auth_test.go @@ -40,6 +40,7 @@ func TestLogin(t *testing.T) { login(&cobra.Command{}, []string{cloudDomain}, nil, buf) // software login success + testUtil.InitTestConfig(testUtil.Initial) login(&cobra.Command{}, []string{softwareDomain}, nil, buf) // no domain, cloud login @@ -53,6 +54,18 @@ func TestLogin(t *testing.T) { // no domain, no current context set config.ResetCurrentContext() login(&cobra.Command{}, []string{}, nil, buf) + + testUtil.InitTestConfig(testUtil.CloudPlatform) + defer testUtil.MockUserInput(t, "n")() + login(&cobra.Command{}, []string{"fail.astronomer.io"}, nil, buf) + assert.Contains(t, buf.String(), "fail.astronomer.io is an invalid domain to login into Astro.\n") + + testUtil.InitTestConfig(testUtil.CloudPlatform) + softwareDomain = "software.astronomer.io" + buf = new(bytes.Buffer) + defer testUtil.MockUserInput(t, "y")() + login(&cobra.Command{}, []string{"software.astronomer.io"}, nil, buf) + assert.Contains(t, buf.String(), "software.astronomer.io is an invalid domain to login into Astro.\n") } func TestLogout(t *testing.T) {