-
Notifications
You must be signed in to change notification settings - Fork 55
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
+59
−35
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") { | ||
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" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.