-
Notifications
You must be signed in to change notification settings - Fork 14
feat: add --as/--as-group/--as-uid impersonation flags #114
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
mikeshootzz
wants to merge
9
commits into
crossplane:main
Choose a base branch
from
mikeshootzz:feat/privilege-elevation
base: main
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a9b4694
feat(kube): add shared kubectl-compatible impersonation flags
mikeshootzz 025b892
feat(trace): support --as/--as-group/--as-uid impersonation
mikeshootzz 46038c1
feat(top): support --as/--as-group/--as-uid impersonation
mikeshootzz ded3b76
feat(xpkg): support --as/--as-group/--as-uid on install and update
mikeshootzz 7684964
feat(version): support --as/--as-group/--as-uid impersonation
mikeshootzz 4bb5a40
feat(completion): impersonate when predicting resources and namespaces
mikeshootzz 19299c5
style: satisfy tagalign, godoclint and revive linters
mikeshootzz a6e44e1
test: make impersonation flag parse tests table-driven
mikeshootzz cb3556e
refactor: move impersonation flags to internal, drop
mikeshootzz 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 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
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,48 @@ | ||
| package completion | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/posener/complete" | ||
|
|
||
| "github.com/crossplane/cli/v2/internal/kube" | ||
| ) | ||
|
|
||
| func TestParseImpersonation(t *testing.T) { | ||
| cases := map[string]struct { | ||
| reason string | ||
| args []string | ||
| want kube.ImpersonationFlags | ||
| }{ | ||
| "Equals": { | ||
| reason: "The --flag=value form is parsed.", | ||
| args: []string{"trace", "x", "--as=jane", "--as-uid=42", "--as-group=team-a"}, | ||
| want: kube.ImpersonationFlags{As: "jane", AsUID: "42", AsGroup: []string{"team-a"}}, | ||
| }, | ||
| "Space": { | ||
| reason: "The --flag value form is parsed.", | ||
| args: []string{"--as", "jane", "--as-uid", "42", "--as-group", "team-a"}, | ||
| want: kube.ImpersonationFlags{As: "jane", AsUID: "42", AsGroup: []string{"team-a"}}, | ||
| }, | ||
| "RepeatableGroups": { | ||
| reason: "--as-group can be repeated.", | ||
| args: []string{"--as-group=team-a", "--as-group", "team-b"}, | ||
| want: kube.ImpersonationFlags{AsGroup: []string{"team-a", "team-b"}}, | ||
| }, | ||
| "None": { | ||
| reason: "No impersonation flags yields the zero value.", | ||
| args: []string{"trace", "x"}, | ||
| want: kube.ImpersonationFlags{}, | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range cases { | ||
| t.Run(name, func(t *testing.T) { | ||
| got := parseImpersonation(complete.Args{All: tc.args}) | ||
| if diff := cmp.Diff(tc.want, got); diff != "" { | ||
| t.Errorf("%s\nparseImpersonation(): -want, +got:\n%s", tc.reason, diff) | ||
| } | ||
| }) | ||
| } | ||
| } |
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
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
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
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
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
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
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 2026 The Crossplane Authors. | ||
|
|
||
| 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 kube contains shared helpers for crossplane CLI commands that talk | ||
| // to a Kubernetes cluster. | ||
| package kube | ||
|
|
||
| import "k8s.io/client-go/rest" | ||
|
|
||
| // ImpersonationFlags are the kubectl-compatible privilege-elevation flags | ||
| // (--as, --as-group, --as-uid). Embed it into a command's Kong flag struct with | ||
| // the `embed:""` tag, then call Apply on the command's *rest.Config before | ||
| // building its client. | ||
| type ImpersonationFlags struct { | ||
| As string `help:"Username to impersonate for the operation. User could be a regular user or a service account in a namespace." name:"as"` | ||
| AsGroup []string `help:"Group to impersonate for the operation, this flag can be repeated to specify multiple groups." name:"as-group" sep:"none"` | ||
| AsUID string `help:"UID to impersonate for the operation." name:"as-uid"` | ||
| } | ||
|
|
||
| // Apply sets impersonation on the given rest.Config. Unset fields and a nil cfg | ||
| // are no-ops, so it is always safe to call. | ||
| func (f ImpersonationFlags) Apply(cfg *rest.Config) { | ||
| if cfg == nil { | ||
| return | ||
| } | ||
|
|
||
| if f.As != "" { | ||
| cfg.Impersonate.UserName = f.As | ||
| } | ||
|
|
||
| if f.AsUID != "" { | ||
| cfg.Impersonate.UID = f.AsUID | ||
| } | ||
|
|
||
| if len(f.AsGroup) > 0 { | ||
| cfg.Impersonate.Groups = f.AsGroup | ||
| } | ||
| } |
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.
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.
I'd prefer to put this functionality in
internalrather thancmd/crossplane/common. It's CLI-specific, so I don't see any reason for it to be importable by external codebases, and we should get rid of thiscommondirectory eventually (see below).This directory is a leftover from the CLI being in the core crossplane/crossplane repository (which intentionally doesn't have a
pkg/for exported packages) and wanting to expose some code for CLI-adjacent utilities like crossplane-diff to use. We'd like to start moving the code that lives here into eitherinternal/orpkg/as appropriate to make it clear what can/should be imported externally (it's a bit of a smell to import code fromcmd/).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.
Thanks for letting me know! I'll move it to internal later today.