Skip to content

feat: add --as/--as-group/--as-uid impersonation flags#114

Open
mikeshootzz wants to merge 9 commits into
crossplane:mainfrom
mikeshootzz:feat/privilege-elevation
Open

feat: add --as/--as-group/--as-uid impersonation flags#114
mikeshootzz wants to merge 9 commits into
crossplane:mainfrom
mikeshootzz:feat/privilege-elevation

Conversation

@mikeshootzz

@mikeshootzz mikeshootzz commented Jun 15, 2026

Copy link
Copy Markdown

Description of your changes

Adds the kubectl-compatible impersonation flags as, as-group, and as-uid to every CLI command that talks to a cluster. This lets you run a command as a different user, group, or service account without switching your kubeconfig context.

The flags are defined once in a shared ImpersonationFlags struct and embedded into each command. After a command builds its client config, it applies the flags, which sets the impersonation fields on the request. This works for every command regardless of how it loads its config.

Commands covered: resource trace, cluster top, version, xpkg install, xpkg update, and the shell-completion predictors.

The behavior matches kubectl: the flag names and help text are the same, as-group is repeatable, there are no short forms, and validation is left to the API server.

Fixes #110

I have:

Need help with this checklist? See the cheat sheet.

Signed-off-by: Mike Ditton <mike.ditton@vshn.net>
Signed-off-by: Mike Ditton <mike.ditton@vshn.net>
Signed-off-by: Mike Ditton <mike.ditton@vshn.net>
Signed-off-by: Mike Ditton <mike.ditton@vshn.net>
Signed-off-by: Mike Ditton <mike.ditton@vshn.net>
Signed-off-by: Mike Ditton <mike.ditton@vshn.net>
Signed-off-by: Mike Ditton <mike.ditton@vshn.net>
@mikeshootzz mikeshootzz requested review from a team, jcogilvie and tampakrap as code owners June 15, 2026 19:32
@mikeshootzz mikeshootzz requested review from negz and removed request for a team June 15, 2026 19:32
@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 74b5eb06-1bc6-497c-b3cd-6884f368a01c

📥 Commits

Reviewing files that changed from the base of the PR and between 19299c5 and cb3556e.

📒 Files selected for processing (10)
  • cmd/crossplane/completion/completion.go
  • cmd/crossplane/completion/completion_test.go
  • cmd/crossplane/top/top.go
  • cmd/crossplane/trace/trace.go
  • cmd/crossplane/version/fetch.go
  • cmd/crossplane/version/version.go
  • cmd/crossplane/xpkg/install.go
  • cmd/crossplane/xpkg/update.go
  • internal/kube/impersonation.go
  • internal/kube/impersonation_test.go
💤 Files with no reviewable changes (2)
  • internal/kube/impersonation_test.go
  • internal/kube/impersonation.go
🚧 Files skipped from review as they are similar to previous changes (6)
  • cmd/crossplane/trace/trace.go
  • cmd/crossplane/top/top.go
  • cmd/crossplane/version/fetch.go
  • cmd/crossplane/xpkg/update.go
  • cmd/crossplane/completion/completion_test.go
  • cmd/crossplane/completion/completion.go

📝 Walkthrough

Walkthrough

Adds kubectl-style --as, --as-group, and --as-uid impersonation flags to the Crossplane CLI. A shared ImpersonationFlags type with an Apply method is introduced in internal/kube, then embedded into trace, top, version, xpkg install, and xpkg update commands. Completion predictors gain a parseImpersonation helper to extract flags from completion arguments.

Changes

Kubernetes impersonation support across CLI surfaces

Layer / File(s) Summary
Shared ImpersonationFlags type and Apply method
internal/kube/impersonation.go, internal/kube/impersonation_test.go
Defines ImpersonationFlags with As, AsGroup, and AsUID fields annotated for Kong, and an Apply method that conditionally populates rest.Config impersonation fields and no-ops on nil config. Tests cover all field combinations, nil safety, Kong CLI parsing, repeated --as-group accumulation, and comma-non-splitting behavior.
Completion parseImpersonation and impersonated client builders
cmd/crossplane/completion/completion.go, cmd/crossplane/completion/completion_test.go
Introduces parseImpersonation to extract --as, --as-uid, and repeatable --as-group from complete.Args in both --flag=value and --flag value forms. Updates kubernetesClientset and kubernetesClient to accept and apply ImpersonationFlags; wires parsed flags into the three predictor call sites. TestParseImpersonation validates all parsing forms.
Runtime command wiring for trace, top, version, and xpkg
cmd/crossplane/trace/trace.go, cmd/crossplane/top/top.go, cmd/crossplane/version/fetch.go, cmd/crossplane/version/version.go, cmd/crossplane/xpkg/install.go, cmd/crossplane/xpkg/update.go
Embeds kube.ImpersonationFlags as Impersonation in each command struct. In each command's Run or setup function, calls c.Impersonation.Apply(cfg) after ctrl.GetConfig() and before constructing any Kubernetes or Metrics clientsets. FetchCrossplaneVersion signature is extended to accept and apply the flags.

Sequence Diagram(s)

sequenceDiagram
  actor User
  participant CLI as crossplane CLI (e.g. trace, top, version)
  participant ImpersonationFlags
  participant ctrl as ctrl.GetConfig()
  participant RestConfig
  participant K8sClient as Kubernetes Client

  User->>CLI: run command --as=jane --as-group=team
  CLI->>ctrl: GetConfig()
  ctrl-->>CLI: *rest.Config
  CLI->>ImpersonationFlags: Apply(*rest.Config)
  ImpersonationFlags->>RestConfig: set Impersonate.UserName, Groups, UID
  CLI->>K8sClient: create client from impersonated config
  K8sClient-->>CLI: client operating as impersonated identity
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes


Important

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

❌ Failed checks (1 error)

Check name Status Explanation Resolution
Breaking Changes ❌ Error PR adds new required parameter to public function FetchCrossplaneVersion in cmd/crossplane/version/fetch.go, changing signature from FetchCrossplaneVersion(ctx context.Context) to FetchCrossplaneVe... Add 'breaking-change' label to PR or make the impersonation parameter optional with zero value default to preserve backward compatibility.
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding kubectl-compatible impersonation flags to CLI commands.
Description check ✅ Passed The description is well-detailed and directly related to the changeset, explaining the purpose, implementation approach, and affected commands.
Linked Issues check ✅ Passed The PR fully addresses issue #110 by adding --as, --as-group, and --as-uid flags to all required CLI commands with a shared implementation [#110].
Out of Scope Changes check ✅ Passed All changes are in scope, implementing impersonation flag support across specified commands and introducing the shared ImpersonationFlags helper.
Feature Gate Requirement ✅ Passed PR adds kubectl-compatible CLI flags for impersonation to CLI commands. Does not affect apis/** directory, is not marked experimental, and is a non-breaking additive CLI feature matching kubectl's...

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cmd/crossplane/trace/trace_test.go`:
- Around line 108-126: The impersonation parse tests across multiple files do
not follow the repository's table-driven test convention. Convert all four test
functions to table-driven structure:
cmd/crossplane/trace/trace_test.go#L108-L126 TestImpersonationFlagsParse should
use table cases with reason, args, and want fields for different flag
combinations; cmd/crossplane/top/top_test.go#L20-L35 TestImpersonationFlagsParse
should be refactored similarly but use diff-based assertions (cmp.Diff) instead
of manual error checks; cmd/crossplane/version/version_test.go#L25-L43 should
adopt table-driven structure with args and want fields for parse test cases;
cmd/crossplane/xpkg/impersonation_test.go#L25-L57 should fold the separate
install and update test logic into individual table-driven test cases (either as
a single table or two separate table-driven tests) with explicit reason fields
and expected impersonation field values. In all cases, use the standard pattern:
define a struct with test case fields, iterate over test cases, parse flags with
kong.New and Parse, and assert results using cmp.Diff for consistency with
repository coding guidelines.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 691564f3-cff2-4a70-b07d-10209e1e553a

📥 Commits

Reviewing files that changed from the base of the PR and between 3cbfbf6 and 19299c5.

📒 Files selected for processing (14)
  • cmd/crossplane/common/kube/impersonation.go
  • cmd/crossplane/common/kube/impersonation_test.go
  • cmd/crossplane/completion/completion.go
  • cmd/crossplane/completion/completion_test.go
  • cmd/crossplane/top/top.go
  • cmd/crossplane/top/top_test.go
  • cmd/crossplane/trace/trace.go
  • cmd/crossplane/trace/trace_test.go
  • cmd/crossplane/version/fetch.go
  • cmd/crossplane/version/version.go
  • cmd/crossplane/version/version_test.go
  • cmd/crossplane/xpkg/impersonation_test.go
  • cmd/crossplane/xpkg/install.go
  • cmd/crossplane/xpkg/update.go

Comment thread cmd/crossplane/trace/trace_test.go Outdated
Signed-off-by: Mike Ditton <mike.ditton@vshn.net>

@adamwg adamwg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left a couple of notes on code organization and tests, but the actual implementation here looks great. Thanks for the contribution, @mikeshootzz!

Copy link
Copy Markdown
Member

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 internal rather than cmd/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 this common directory 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 either internal/ or pkg/ as appropriate to make it clear what can/should be imported externally (it's a bit of a smell to import code from cmd/).

Copy link
Copy Markdown
Author

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.

Comment thread cmd/crossplane/top/top_test.go Outdated
"github.com/crossplane/cli/v2/cmd/crossplane/common/kube"
)

func TestImpersonationFlagsParse(t *testing.T) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this test is very valuable - it's mostly testing the internals of kong rather than our code.

If you do want to keep it, I'd suggest putting it alongside the ImpersonationFlags struct in the kube package rather than duplicating it in each command that uses the flags.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. I'll fix that. Thanks!

  redundant parse tests

Signed-off-by: Mike Ditton <mike.ditton@vshn.net>
@mikeshootzz mikeshootzz requested a review from adamwg June 18, 2026 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement privilege elevation flag

2 participants