Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logic that permits some commands to run without auth #741

Merged
merged 1 commit into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 24 additions & 35 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,56 +84,45 @@ func buildRootCmd(cli *cli) *cobra.Command {
ansi.Initialize(cli.noColor)
prepareInteractivity(cmd)

// If the user is trying to login, no need to go
// through setup.
if cmd.Use == "login" && cmd.Parent().Use == "auth0" {
if !commandRequiresAuthentication(cmd.CommandPath()) {
return nil
}

// We're tracking the login command in its Run method
// so we'll only add this defer if the command is not login
// We're tracking the login command in its Run method, so
// we'll only add this defer if the command is not login.
defer func() {
if cli.tracker != nil && cli.isLoggedIn() {
if cli.tracker != nil && cmd.Name() != "login" && cli.isLoggedIn() {
cli.tracker.TrackCommandRun(cmd, cli.config.InstallID)
}
}()

// If the user is trying to logout, session information
// isn't important as well.
if cmd.Use == "logout" && cmd.Parent().Use == "auth0" {
return nil
}

// Selecting tenants shouldn't really trigger a login.
if cmd.Parent().Use == "tenants" && (cmd.Use == "use" || cmd.Use == "add") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tenants add command was removed. So this shouldn't have been here.

return nil
}

// Getting the CLI completion script shouldn't trigger a login.
if cmd.Use == "completion" && cmd.Parent().Use == "auth0" {
return nil
}

// Getting help shouldn't trigger a login.
if cmd.CalledAs() == "help" && cmd.Parent().Use == "auth0" {
return nil
}

// config init shouldn't trigger a login.
if cmd.CalledAs() == "init" && cmd.Parent().Use == "config" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config command was removed. So this shouldn't have been here.

return nil
}

// Initialize everything once. Later callers can then
// freely assume that config is fully primed and ready
// to go.
// Initialize everything once.
return cli.setup(cmd.Context())
},
}

return rootCmd
}

func commandRequiresAuthentication(invokedCommandName string) bool {
commandsWithNoAuthRequired := []string{
"auth0 completion",
"auth0 help",
"auth0 login",
"auth0 logout",
"auth0 tenants use",
"auth0 tenants list",
}

for _, cmd := range commandsWithNoAuthRequired {
if cmd == invokedCommandName {
return false
}
}

return true
}

func addPersistentFlags(rootCmd *cobra.Command, cli *cli) {
rootCmd.PersistentFlags().StringVar(&cli.tenant,
"tenant", cli.config.DefaultTenant, "Specific tenant to use.")
Expand Down
35 changes: 35 additions & 0 deletions internal/cli/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cli

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCommandRequiresAuthentication(t *testing.T) {
var testCases = []struct {
givenCommand string
expectedToRequireAuthentication bool
}{
{"auth0 user list", true},
{"auth0 user create", true},
{"auth0 api", true},
{"auth0 apps list", true},
{"auth0 apps create", true},
{"auth0 orgs members list", true},
{"auth0 completion", false},
{"auth0 help", false},
{"auth0 login", false},
{"auth0 logout", false},
{"auth0 tenants use", false},
{"auth0 tenants list", false},
}

for index, testCase := range testCases {
t.Run(fmt.Sprintf("TestCase #%d Command: %s", index, testCase.givenCommand), func(t *testing.T) {
actualAuth := commandRequiresAuthentication(testCase.givenCommand)
assert.Equal(t, testCase.expectedToRequireAuthentication, actualAuth)
})
}
}