Skip to content

Record OIDC token expiry so refresh hook refreshes expired tokens - #2781

Open
xh123637 wants to merge 1 commit into
kubernetes-client:masterfrom
xh123637:fix-oidc-expiry-2780
Open

xh123637 wants to merge 1 commit into
kubernetes-client:masterfrom
xh123637:fix-oidc-expiry-2780

Conversation

@xh123637

@xh123637 xh123637 commented Nov 13, 2026 •

Copy link
Copy Markdown

What type of PR is this?

/kind bug

What this PR does / why we need it:

KubeConfigLoader._load_oid_token read the JWT exp claim to decide whether to refresh the token at load time, but never recorded it on the loader. The refresh callback installed by _set_config only reloads authentication when self.expiry exists and is expired, so OIDC tokens were never refreshed after client initialization and later requests failed with 401 Unauthorized once the token expired.

This PR records the expiry of the id-token in use (via a new _get_jwt_expiry helper) so the existing refresh hook refreshes OIDC tokens like it already does for GCP and exec-plugin credentials. The expiry is read from the final id-token because a refresh replaces it, and the existing five-minute skew in _is_expired still applies, so tokens are refreshed shortly before expiry rather than on every request. Tokens without an exp claim, and non-JWT refresh results, leave expiry unset exactly as before.

The asyncio client is intentionally untouched: it has no refresh_api_key_hook / expiry refresh mechanism, so this issue is specific to the sync client.

Which issue(s) this PR fixes:

Fixes #2780

Special notes for your reviewer:

New tests in kubernetes/config/kube_config_test.py:

  • test_oidc_sets_expiry_from_id_token: loading a config with an unexpired OIDC token records its expiry.
  • test_oidc_refresh_api_key_hook: end-to-end through load_and_set — the installed hook refreshes the token (mocked IdP) once the recorded expiry passes and records the new token's expiry.

Both tests fail on unmodified code (AttributeError: 'KubeConfigLoader' object has no attribute 'expiry') and pass with the fix. Full kubernetes/config/kube_config_test.py: 79 passed.

Fix OIDC token refresh: record the expiry of the id-token in use so expired tokens are refreshed on later requests instead of failing with 401 Unauthorized.

_load_oid_token read the JWT exp claim to decide whether to refresh
at load time but never set self.expiry, so the refresh callback
installed by _set_config never reloaded OIDC credentials and later
requests failed with 401 once the token expired. Record the expiry
of the id-token in use, read from the final token since a refresh
replaces it, so OIDC refreshes like GCP and exec-plugin credentials.

Signed-off-by: xh123637 <3324733895@qq.com>
@kubernetes-prow kubernetes-prow Bot added kind/bug Categorizes issue or PR as related to a bug. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Nov 13, 2026
@linux-foundation-easycla

Copy link
Copy Markdown

CLA Not Signed

@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @xh123637!

It looks like this is your first PR to kubernetes-client/python 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/python has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@kubernetes-prow kubernetes-prow Bot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Nov 13, 2026
@kubernetes-prow
kubernetes-prow Bot requested a review from roycaihw November 13, 2026 08:21
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: xh123637
Once this PR has been reviewed and has the lgtm label, please assign yliaog for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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

@kubernetes-prow
kubernetes-prow Bot requested a review from yliaog November 13, 2026 08:21
@kubernetes-prow kubernetes-prow Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. 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/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OIDC tokens are not refreshed after client initialization

1 participant