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
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,13 @@ func brandRootCommand() {
}

func lockToDCI() {
cli.Root.Args = cobra.NoArgs
cli.Root.Run = nil
cli.Root.RunE = func(cmd *cobra.Command, args []string) error {
return cmd.Help()
}
cli.Root.ValidArgsFunction = nil

// Remove API management commands, generic RESTish commands, and any
// additional API entrypoints so users can only call the DCI API.
allowed := map[string]bool{
Expand Down
42 changes: 42 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,48 @@ func TestLockToDCI(t *testing.T) {
}
}

func TestLockToDCIDisablesGenericRootRequests(t *testing.T) {
oldRoot := cli.Root
requestExecuted := false
root := &cobra.Command{
Use: "dci",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
requestExecuted = true
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"example.com"}, cobra.ShellCompDirectiveDefault
},
}
root.SetOut(io.Discard)
root.SetErr(io.Discard)
cli.Root = root
t.Cleanup(func() {
cli.Root = oldRoot
})

lockToDCI()

if root.Run != nil {
t.Fatal("generic root request handler remains installed")
}
if root.RunE == nil {
t.Fatal("safe root handler was not installed")
}
if root.ValidArgsFunction != nil {
t.Fatal("generic hostname completion remains installed")
}
if err := root.Args(root, []string{""}); err == nil {
t.Fatal("empty hostname argument was accepted")
}
if err := root.RunE(root, nil); err != nil {
t.Fatal(err)
}
if requestExecuted {
t.Fatal("generic root request handler executed")
}
}

func TestBrandRootAndDCICommands(t *testing.T) {
oldRoot := cli.Root
root := &cobra.Command{Use: "dci"}
Expand Down
Loading