-
Notifications
You must be signed in to change notification settings - Fork 15
OADP-7955: Hides --namespace flag from nonadmin commands #194
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
Open
NicholasYancey
wants to merge
9
commits into
migtools:oadp-dev
Choose a base branch
from
NicholasYancey:namespace-flag-ignored
base: oadp-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+130
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bd29af5
Hides --namespace flag from nonadmin commands
11cb0ce
Hiding namespace flag when used in nonadmin help
3cddc5d
Fixed --namespace flag in nonadmin command
3d30709
Hiding --namespace flag in nonadmin command
6c7c6e3
Hiding --namespace flag in nonadmin command
93dfcc4
Hiding --namespace flag in nonadmin command
2b7242a
Move nonadmin namespace logic into cmd/non-admin
ce82417
Added Changes from copilot and added to the tests
b220cc5
Updated If statement to look for -n and --namespace string
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,77 @@ | ||
| /* | ||
| Copyright 2025 The OADP CLI Contributors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package nonadmin | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // ConfigureNamespaceBehavior hides the inherited --namespace flag from nonadmin | ||
| // help output and rejects runtime use of -n/--namespace. Nonadmin operations are | ||
| // scoped to the user's current context namespace for security. | ||
| func ConfigureNamespaceBehavior(cmd *cobra.Command) { | ||
| hideNamespaceFlagFromCommand(cmd) | ||
|
|
||
| existingPersistentPreRunE := cmd.PersistentPreRunE | ||
| existingPersistentPreRun := cmd.PersistentPreRun | ||
|
|
||
| cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error { | ||
| if c.Flags().Changed("namespace") { | ||
| return fmt.Errorf("-n/--namespace is not supported for nonadmin commands; namespace is determined by your current context") | ||
| } | ||
| if existingPersistentPreRunE != nil { | ||
| return existingPersistentPreRunE(c, args) | ||
| } | ||
| if existingPersistentPreRun != nil { | ||
| existingPersistentPreRun(c, args) | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func hideNamespaceFlagFromCommand(cmd *cobra.Command) { | ||
| originalHelpFunc := cmd.HelpFunc() | ||
| cmd.SetHelpFunc(func(c *cobra.Command, args []string) { | ||
| withHiddenNamespaceFlag(c, func() { | ||
| originalHelpFunc(c, args) | ||
| }) | ||
| }) | ||
|
|
||
| originalUsageFunc := cmd.UsageFunc() | ||
| cmd.SetUsageFunc(func(c *cobra.Command) error { | ||
| var err error | ||
| withHiddenNamespaceFlag(c, func() { | ||
| err = originalUsageFunc(c) | ||
| }) | ||
| return err | ||
| }) | ||
|
|
||
| for _, subCmd := range cmd.Commands() { | ||
| hideNamespaceFlagFromCommand(subCmd) | ||
| } | ||
| } | ||
|
|
||
| func withHiddenNamespaceFlag(cmd *cobra.Command, fn func()) { | ||
| if flag := cmd.InheritedFlags().Lookup("namespace"); flag != nil { | ||
| originalHidden := flag.Hidden | ||
| flag.Hidden = true | ||
| defer func() { flag.Hidden = originalHidden }() | ||
| } | ||
| fn() | ||
| } | ||
This file contains hidden or 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,51 @@ | ||
| /* | ||
| Copyright 2025 The OADP CLI Contributors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package nonadmin | ||
|
|
||
| import ( | ||
| "context" | ||
| "os/exec" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/migtools/oadp-cli/internal/testutil" | ||
| ) | ||
|
|
||
| func TestNonAdminNamespaceFlagBehavior(t *testing.T) { | ||
| binaryPath := testutil.BuildCLIBinary(t) | ||
|
|
||
| t.Run("help hides namespace flag", func(t *testing.T) { | ||
| output, _ := testutil.RunCommand(t, binaryPath, "nonadmin", "backup", "get", "--help") | ||
| if strings.Contains(output, "-n, --namespace") || strings.Contains(output, "--namespace string") { | ||
| t.Errorf("Expected help output not to contain the namespace flag\nFull output:\n%s", output) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("rejects namespace flag at runtime", func(t *testing.T) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), testutil.TestTimeout) | ||
| defer cancel() | ||
|
|
||
| output, err := exec.CommandContext(ctx, binaryPath, "nonadmin", "backup", "get", "-n", "other-namespace").CombinedOutput() | ||
| if err == nil { | ||
| t.Fatalf("Expected error when -n/--namespace is provided, got output:\n%s", string(output)) | ||
| } | ||
| expected := "-n/--namespace is not supported for nonadmin commands; namespace is determined by your current context" | ||
| if !strings.Contains(string(output), expected) { | ||
| t.Errorf("Expected output to contain %q, got:\n%s", expected, string(output)) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.