Skip to content
28 changes: 27 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,12 @@ func NewVeleroRootCommand(baseName string) *cobra.Command {
c.AddCommand(nabsl.NewNABSLRequestCommand(f))

// Custom subcommands - use NonAdmin factory
c.AddCommand(nonadmin.NewNonAdminCommand(f))
nonadminCmd := nonadmin.NewNonAdminCommand(f)
c.AddCommand(nonadminCmd)
Comment thread
NicholasYancey marked this conversation as resolved.

// Hide --namespace flag from nonadmin commands
// NonAdmin operations are namespace-scoped to the user's current context for security
hideNamespaceFlagFromCommand(nonadminCmd)

// Must-gather command - diagnostic tool
c.AddCommand(mustgather.NewMustGatherCommand(f))
Expand Down Expand Up @@ -501,3 +506,24 @@ func NewVeleroRootCommand(baseName string) *cobra.Command {
c.PersistentFlags().AddGoFlagSet(flag.CommandLine)
return c
}

// hideNamespaceFlagFromCommand recursively hides the --namespace flag from a command and all its subcommands
func hideNamespaceFlagFromCommand(cmd *cobra.Command) {
// For each command, we need to hide the inherited namespace flag in its help output
// We do this by overriding the HelpFunc to filter out the namespace flag
originalHelpFunc := cmd.HelpFunc()
cmd.SetHelpFunc(func(c *cobra.Command, args []string) {
// Temporarily hide the namespace flag for this help output
if flag := c.InheritedFlags().Lookup("namespace"); flag != nil {
originalHidden := flag.Hidden
flag.Hidden = true
defer func() { flag.Hidden = originalHidden }()
}
originalHelpFunc(c, args)
})

// Recursively apply to all subcommands
for _, subCmd := range cmd.Commands() {
hideNamespaceFlagFromCommand(subCmd)
}
}
Loading