Skip to content

fix(cli): honor kubeconfig env and exec auth tokens - #1389

Open
dronenb wants to merge 1 commit into
tektoncd:mainfrom
dronenb:fix/cli-kubeconfig-exec-auth
Open

fix(cli): honor kubeconfig env and exec auth tokens#1389
dronenb wants to merge 1 commit into
tektoncd:mainfrom
dronenb:fix/cli-kubeconfig-exec-auth

Conversation

@dronenb

@dronenb dronenb commented Jul 20, 2026

Copy link
Copy Markdown

Changes

Fixes #1388

tkn-results built its Kubernetes client configuration differently from oc, kubectl, and tkn, which broke common kubeconfig-based authentication flows.

This PR fixes two related CLI authentication issues:

  • Honor standard client-go kubeconfig precedence: explicit --kubeconfig, then $KUBECONFIG, then ~/.kube/config, including merged kubeconfig files.
  • Resolve bearer tokens through client-go credential transport behavior instead of only reading rest.Config.BearerToken, so kubeconfig exec credentials such as oc get-token are invoked and their bearer token can be forwarded to the Results API.
  • Preserve explicit --token and configured token overrides.
  • Leave non-bearer-token authentication, such as client certificate auth, as an empty token result rather than an error.
  • Correct the CLI help text for --kubeconfig and --namespace to match the actual precedence/default behavior.
  • Add unit coverage for kubeconfig precedence, NewConfig, static token handling, nil config handling, non-token auth, exec credential token resolution, and CLI flag help text.

/kind bug

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you review them:

  • Has Docs included if any changes are user facing
  • Has Tests included if any functionality added or changed
  • Tested your changes locally (if this is a code change)
  • Follows the commit message standard
  • Meets the Tekton contributor standards (including functionality, content, code)
  • Has a kind label. You can add a comment on this PR that contains /kind <type>. Valid types are bug, cleanup, design, documentation, feature, flake, misc, question, tep
  • Release notes block below has been updated with any user-facing changes (API changes, bug fixes, changes requiring upgrade notices or deprecation warnings)
  • Release notes contain the string "action required" if the change requires additional action from users switching to the new release

Testing

  • go test ./pkg/cli/...
  • Tested locally on work machine against real clusters, including kubeconfig-backed cluster authentication flows.

AI Assistance

This PR was prepared with assistance from OpenCode using GPT-5.5. The submitter reviewed the changes and ran the testing noted above.

Release Notes

tkn-results now honors $KUBECONFIG when loading kubeconfig and resolves bearer tokens from kubeconfig exec/auth-provider credentials, allowing CLI authentication flows such as OpenShift external OIDC `oc get-token` to work without manually supplying --token.

@tekton-robot tekton-robot added kind/bug Categorizes issue or PR as related to a bug. release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Jul 20, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 20, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: dronenb / name: Ben Dronen (f9bd147)

@tekton-robot tekton-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jul 20, 2026
@dronenb

dronenb commented Jul 20, 2026

Copy link
Copy Markdown
Author

/kind bug

@dronenb
dronenb force-pushed the fix/cli-kubeconfig-exec-auth branch from f9bd147 to 239f455 Compare July 20, 2026 23:49
@tekton-robot tekton-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 20, 2026
@divyansh42
divyansh42 requested a review from Copilot July 23, 2026 17:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes tkn-results CLI authentication to match standard client-go/kubectl behavior by honoring kubeconfig loading precedence (including $KUBECONFIG) and correctly resolving bearer tokens from kubeconfig-backed credential providers (notably exec plugins like oc get-token), with accompanying unit tests and updated flag help text.

Changes:

  • Switch kubeconfig loading to clientcmd.NewDefaultClientConfigLoadingRules() so precedence is --kubeconfig$KUBECONFIG~/.kube/config (including merged kubeconfigs).
  • Add bearer-token resolution that drives client-go’s auth transport so exec/auth-provider credentials can mint a token for forwarding to the Results API.
  • Update CLI help text and add unit tests for kubeconfig precedence and token resolution paths.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pkg/cli/flags/flags.go Updates --kubeconfig / --namespace help strings to match actual kubeconfig precedence/default behavior.
pkg/cli/config/config.go Loads kubeconfig via default client-go rules and resolves tokens via the new token resolution helper.
pkg/cli/config/token.go Introduces resolveBearerToken helper to obtain bearer tokens via client-go transport credential behavior.
pkg/cli/config/token_test.go Adds unit tests for static token, nil config, wrap-transport token injection, and exec credential token resolution.
pkg/cli/config/config_auth_test.go Adds end-to-end unit coverage for $KUBECONFIG precedence and exec credential token resolution through NewConfig().
Comments suppressed due to low confidence (1)

pkg/cli/config/token.go:79

  • resolveBearerToken() claims to return a bearer token, but currently returns any non-empty Authorization header verbatim when it isn't "Bearer". That can accidentally forward Basic/custom auth material as a Results token; non-bearer schemes should resolve to an empty token.
	authz := capture.authorization
	if authz == "" {
		return "", nil
	}
	// Strip the "Bearer " scheme prefix if present.
	if parts := strings.SplitN(authz, " ", 2); len(parts) == 2 && strings.EqualFold(parts[0], "Bearer") {
		return parts[1], nil
	}
	return authz, nil

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/cli/config/token.go Outdated
Comment thread pkg/cli/config/token_test.go
@dronenb
dronenb force-pushed the fix/cli-kubeconfig-exec-auth branch from 239f455 to 0a15540 Compare August 5, 2026 18:00
@dronenb

dronenb commented Aug 5, 2026

Copy link
Copy Markdown
Author

Addressed Copilot review comments in 0a155407:

  • resolveBearerToken now always runs client-go HTTP auth wrappers after the static-token fast path, so BearerTokenFile / kubeconfig tokenFile credentials resolve even when WrapTransport is nil. Added TestResolveBearerTokenFile coverage.
  • Non-Bearer Authorization schemes now resolve to an empty token instead of being returned verbatim, and the test expectation was updated accordingly.

@aThorp96 aThorp96 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.

This is great, thanks! Clever idea to extract token injected by middleware as well. I have one suggestion for simplifying the token resolution logic. It should also reduce the required tests a bit

Comment thread pkg/cli/config/token.go Outdated
@dronenb

dronenb commented Aug 6, 2026

Copy link
Copy Markdown
Author

Addressed @aThorp96 review feedback in bf61273c by removing the dedicated authHeaderCapturingRoundTripper and using a small terminal roundTripperFunc instead. One implementation note: client-go bearer auth can clone the request, so the resolver reads Authorization from resp.Request after the wrapper chain runs rather than from the original request. Validation: go test ./pkg/cli/..., ./test/presubmit-tests.sh --unit-tests, and make all all pass.

@dronenb
dronenb marked this pull request as draft August 6, 2026 13:14
@tekton-robot tekton-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 6, 2026
@dronenb

dronenb commented Aug 6, 2026

Copy link
Copy Markdown
Author

I will do some testing with this against our Tekton clusters and mark as ready for review when I'm comfortable things are good to go

@aThorp96

aThorp96 commented Aug 6, 2026

Copy link
Copy Markdown
Member

One implementation note: client-go bearer auth can clone the request, so the resolver reads Authorization from resp.Request after the wrapper chain runs rather than from the original request.

Ah, good catch. I was wondering about that possibility as well but wasn't sure.

@enarha

enarha commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

I will do some testing with this against our Tekton clusters and mark as ready for review when I'm comfortable things are good to go

Thank you for this PR. Can you please remove the draft/not ready indicator.

@enarha

enarha commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

@divyansh42 PTAL when you have a chance.

@dronenb

dronenb commented Aug 6, 2026

Copy link
Copy Markdown
Author

I think I identified an issue with how I was testing this, so I marked it as draft. Will validate today or tomorrow...

@divyansh42 divyansh42 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.

@dronenb Quick question on the config set interactive prompt: for exec/OIDC credentials (e.g. oc get-token), it now pre-fills the "Token" field with a live, short-lived token instead of leaving it blank. If a user just hits Enter, that token gets saved permanently in Extension.Token, and future commands will keep using that saved value instead of asking oc for a fresh one — so things would start failing again once it expires, until the user reruns config set.

Is that the intended behavior, or should the prompt stay blank by default for non-static credentials so the CLI keeps auto-refreshing the token each time?

@divyansh42

Copy link
Copy Markdown
Member

I think I identified an issue with how I was testing this, so I marked it as draft. Will validate today or tomorrow...

@dronenb just wanted to check, if you were able to validate it?

@dronenb
dronenb force-pushed the fix/cli-kubeconfig-exec-auth branch from bf61273 to 5603dba Compare August 27, 2026 21:29
@dronenb
dronenb marked this pull request as ready for review August 27, 2026 21:29
@tekton-robot tekton-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 27, 2026
@dronenb

dronenb commented Aug 27, 2026

Copy link
Copy Markdown
Author

@dronenb Quick question on the config set interactive prompt: for exec/OIDC credentials (e.g. oc get-token), it now pre-fills the "Token" field with a live, short-lived token instead of leaving it blank. If a user just hits Enter, that token gets saved permanently in Extension.Token, and future commands will keep using that saved value instead of asking oc for a fresh one — so things would start failing again once it expires, until the user reruns config set.

Is that the intended behavior, or should the prompt stay blank by default for non-static credentials so the CLI keeps auto-refreshing the token each time?

I have addressed this, PTAL

@dronenb just wanted to check, if you were able to validate it?

Just validated, should be g2g. I have rebased as well.

@divyansh42 divyansh42 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.

Thanks @dronenb!
Could you please squash the commits into one?

@tekton-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: divyansh42

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tekton-robot tekton-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 28, 2026
@dronenb
dronenb force-pushed the fix/cli-kubeconfig-exec-auth branch from 5603dba to 5cf8205 Compare August 28, 2026 10:46
The Results CLI built its client config in a way that diverged from oc/kubectl/tkn in two ways:

1. NewConfig hardcoded clientcmd.RecommendedHomeFile and getRawKubeConfigLoader used a single ExplicitPath, so the KUBECONFIG environment variable was ignored unless --kubeconfig was set.

2. Token() read rest.Config.BearerToken directly. For contexts that authenticate via exec credential plugins or legacy auth-provider configuration, BearerToken is empty because credentials are injected through the transport chain.

Load kubeconfig through NewDefaultClientConfigLoadingRules so KUBECONFIG, including merged files, is honored while --kubeconfig still takes precedence.

Resolve bearer tokens by driving the configured credential round-tripper once when no static token is present, capturing the Authorization header produced by exec/auth-provider auth. Non-token auth continues to return an empty token.

Add unit coverage for KUBECONFIG precedence, NewConfig behavior, static and nil token handling, exec credential resolution, and the CLI flag help text.

Fixes tektoncd#1388

/kind bug

Signed-off-by: Ben Dronen <dronenb@users.noreply.github.com>
@dronenb
dronenb force-pushed the fix/cli-kubeconfig-exec-auth branch from 5cf8205 to 90317de Compare August 28, 2026 10:46
@dronenb

dronenb commented Aug 28, 2026

Copy link
Copy Markdown
Author

Thanks @dronenb! Could you please squash the commits into one?

Thanks, @divyansh42! I have squashed the commits and rebased, should be g2g!

@divyansh42

Copy link
Copy Markdown
Member

Thanks @dronenb! Could you please squash the commits into one?

Thanks, @divyansh42! I have squashed the commits and rebased, should be g2g!

/cc @enarha @khrm

@tekton-robot
tekton-robot requested a review from khrm August 28, 2026 12:03
@dronenb

dronenb commented Sep 2, 2026

Copy link
Copy Markdown
Author

@divyansh42 @enarha @khrm any update on this? Anything needed from my end? Let me know! 🙂

@enarha

enarha commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

@divyansh42 @enarha @khrm any update on this? Anything needed from my end? Let me know! 🙂

Not for now. I want to perform a test on OpenShift cluster to verify if a smaller fix isn't sufficient. Please give me a day or max two to check that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/bug Categorizes issue or PR as related to a bug. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tkn-results CLI ignores $KUBECONFIG and does not honor kubeconfig exec credentials (OpenShift external OIDC / oc get-token)

6 participants