Skip to content
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
22 changes: 14 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,25 @@ func getKernelClient(cmd *cobra.Command) kernel.Client {
return util.GetKernelClient(cmd)
}

// isAuthExempt returns true if the command or any of its parents should skip auth.
// isAuthExempt returns true if the command should skip auth.
func isAuthExempt(cmd *cobra.Command) bool {
// bare root command does not need auth
// Root command doesn't need auth
if cmd == rootCmd {
return true
}
for c := cmd; c != nil; c = c.Parent() {
switch c.Name() {
case "login", "logout", "auth", "help", "completion",
"create":
return true
}

// Walk up to find the top-level command (direct child of rootCmd)
topLevel := cmd
for topLevel.Parent() != nil && topLevel.Parent() != rootCmd {
topLevel = topLevel.Parent()
}

// Check if the top-level command is in the exempt list
switch topLevel.Name() {
case "login", "logout", "auth", "help", "completion", "create":
return true
}

return false
}

Expand Down
79 changes: 79 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmd

import (
"testing"

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

func TestIsAuthExempt(t *testing.T) {
tests := []struct {
name string
cmd *cobra.Command
expected bool
}{
{
name: "root command is exempt",
cmd: rootCmd,
expected: true,
},
{
name: "login command is exempt",
cmd: loginCmd,
expected: true,
},
{
name: "logout command is exempt",
cmd: logoutCmd,
expected: true,
},
{
name: "top-level create command is exempt",
cmd: createCmd,
expected: true,
},
{
name: "browser-pools create subcommand requires auth",
cmd: browserPoolsCreateCmd,
expected: false,
},
{
name: "browsers create subcommand requires auth",
cmd: browsersCreateCmd,
expected: false,
},
{
name: "profiles create subcommand requires auth",
cmd: profilesCreateCmd,
expected: false,
},
{
name: "browser-pools list requires auth",
cmd: browserPoolsListCmd,
expected: false,
},
{
name: "browsers list requires auth",
cmd: browsersListCmd,
expected: false,
},
{
name: "deploy command requires auth",
cmd: deployCmd,
expected: false,
},
{
name: "invoke command requires auth",
cmd: invokeCmd,
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isAuthExempt(tt.cmd)
assert.Equal(t, tt.expected, result, "isAuthExempt(%s) = %v, want %v", tt.cmd.Name(), result, tt.expected)
})
}
}