Skip to content

OCPBUGS-99757: Include extra scopes in OIDC token cache key#2323

Open
michaelryanmcneill wants to merge 1 commit into
openshift:mainfrom
michaelryanmcneill:OCPBUGS-99757
Open

OCPBUGS-99757: Include extra scopes in OIDC token cache key#2323
michaelryanmcneill wants to merge 1 commit into
openshift:mainfrom
michaelryanmcneill:OCPBUGS-99757

Conversation

@michaelryanmcneill

@michaelryanmcneill michaelryanmcneill commented Jul 24, 2026

Copy link
Copy Markdown

Bug: https://issues.redhat.com/browse/OCPBUGS-99757

The OIDC exec plugin token cache key is computed from only IssuerURL and ClientID. When a user changes --extra-scopes, the stale cached token (missing the new scope claims) is returned instead of triggering a fresh auth code flow.

Changes:

  • Add ExtraScopes field to tokencache.Key so different scope sets produce distinct cache entries
  • Sort scopes before key construction to ensure deterministic cache filenames regardless of flag ordering
  • Add unit tests for token cache key computation and round-trip save/find

Workaround (prior to this fix): rm ~/.kube/cache/oc/* and re-authenticate.

Note: existing cached tokens will require a one-time re-authentication after this change, as the cache key format has changed.

Summary by CodeRabbit

  • Bug Fixes
    • Improved token caching so tokens are reliably reused only when the same extra scopes are requested, even if the scopes are provided in different orders.
    • Updated cache keying so cached tokens are correctly segregated by extra-scope sets.
    • Continued ensuring cached tokens are not returned when requested scopes (including extras) don’t match.
    • Preserved secure cache file permissions.
  • Tests
    • Consolidated cache filename coverage into a single table-driven test, including matching, missing, and order-difference scenarios.
    • Kept repository round-trip and file-permissions test coverage.

@openshift-merge-bot

Copy link
Copy Markdown
Contributor

Pipeline controller notification
This repo is configured to use the pipeline controller. Second-stage tests will be triggered either automatically or after lgtm label is added, depending on the repository configuration. The pipeline controller will automatically detect which contexts are required and will utilize /test Prow commands to trigger the second stage.

For optional jobs, comment /test ? to see a list of all defined jobs. To trigger manually all jobs from second stage use /pipeline required command.

This repository is configured in: LGTM mode

@openshift-ci-robot openshift-ci-robot added jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. jira/invalid-bug Indicates that a referenced Jira bug is invalid for the branch this PR is targeting. labels Jul 24, 2026
@openshift-ci-robot

Copy link
Copy Markdown

@michaelryanmcneill: This pull request references Jira Issue OCPBUGS-99757, which is invalid:

  • expected the bug to target the "5.0.0" version, but no target version was set

Comment /jira refresh to re-evaluate validity if changes to the Jira bug are made, or edit the title of this pull request to link to a different bug.

The bug has been updated to refer to the pull request using the external bug tracker.

Details

In response to this:

Bug: https://issues.redhat.com/browse/OCPBUGS-99757

The OIDC exec plugin token cache key is computed from only IssuerURL and ClientID. When a user changes --extra-scopes, the stale cached token (missing the new scope claims) is returned instead of triggering a fresh auth code flow.

Changes:

  • Add ExtraScopes field to tokencache.Key so different scope sets produce distinct cache entries
  • Sort scopes before key construction to ensure deterministic cache filenames regardless of flag ordering
  • Add unit tests for token cache key computation and round-trip save/find

Workaround (prior to this fix): rm ~/.kube/cache/oc/* and re-authenticate.

Note: existing cached tokens will require a one-time re-authentication after this change, as the cache key format has changed.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: d3398353-39af-4769-ad7c-66d2cc22c461

📥 Commits

Reviewing files that changed from the base of the PR and between 14695d6 and 9ce4e72.

📒 Files selected for processing (3)
  • pkg/cli/gettoken/gettoken.go
  • pkg/cli/gettoken/tokencache/tokencache.go
  • pkg/cli/gettoken/tokencache/tokencache_test.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • pkg/cli/gettoken/tokencache/tokencache.go
  • pkg/cli/gettoken/gettoken.go
  • pkg/cli/gettoken/tokencache/tokencache_test.go

Walkthrough

Token cache keys now include ExtraScopes, with callers sorting scopes before key construction. Tests cover filename identity, repository retrieval and misses, deterministic behavior, and file permissions.

Changes

Token cache scope identity

Layer / File(s) Summary
Key contract and scope normalization
pkg/cli/gettoken/gettoken.go, pkg/cli/gettoken/tokencache/tokencache.go
Key includes extra scopes, and GetTokenOptions.Run sorts a copy of ExtraScopes before constructing the cache key.
Cache key and repository validation
pkg/cli/gettoken/tokencache/tokencache_test.go
Tests verify scope-sensitive filenames, deterministic keys, save and retrieval behavior, differing-scope misses, and 0600 file permissions.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: atiratree, ingvagabund

🚥 Pre-merge checks | ✅ 14 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (14 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately states the main change: extra scopes are added to the OIDC token cache key.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Stable And Deterministic Test Names ✅ Passed No Ginkgo titles are present; the added tests use static, deterministic t.Run names and no dynamic values or generated identifiers.
Test Structure And Quality ✅ Passed PASS: The added tests are focused table-driven Go tests, use t.TempDir cleanup, include useful failure messages, and contain no Ginkgo/cluster waits.
Microshift Test Compatibility ✅ Passed PASS: The PR only changes unit tests in pkg/cli/gettoken/tokencache and gettoken.go; no Ginkgo e2e tests or MicroShift-unsupported APIs are added.
Single Node Openshift (Sno) Test Compatibility ✅ Passed Only standard Go unit tests were added; no Ginkgo e2e tests or multi-node assumptions are present, so the SNO check is not applicable.
Topology-Aware Scheduling Compatibility ✅ Passed PR only changes get-token cache key/tests; no deployment manifests, operator code, or scheduling constraints were added.
Ote Binary Stdout Contract ✅ Passed No process-level stdout writes were introduced; the PR only changes cache-key logic and tests, with existing output still routed via IOStreams/ErrOut.
Ipv6 And Disconnected Network Test Compatibility ✅ Passed No new Ginkgo e2e tests were added; the new tests are unit tests and contain no IPv4 or external-network dependencies.
No-Weak-Crypto ✅ Passed Changed files only use sha256 to hash cache keys; no MD5/SHA1/DES/RC4/3DES/Blowfish/ECB or secret comparisons were introduced.
Container-Privileges ✅ Passed PR only changes Go token-cache code/tests; no container/K8s manifests or privilege flags like privileged, hostPID, hostNetwork, hostIPC, SYS_ADMIN, or allowPrivilegeEscalation are present.
No-Sensitive-Data-In-Logs ✅ Passed Diff only adds ExtraScopes to cache key and tests; no new logging or secret-bearing output was introduced.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@michaelryanmcneill

Copy link
Copy Markdown
Author

/jira refresh

@openshift-ci-robot openshift-ci-robot added jira/valid-bug Indicates that a referenced Jira bug is valid for the branch this PR is targeting. and removed jira/invalid-bug Indicates that a referenced Jira bug is invalid for the branch this PR is targeting. labels Jul 24, 2026
@openshift-ci-robot

Copy link
Copy Markdown

@michaelryanmcneill: This pull request references Jira Issue OCPBUGS-99757, which is valid. The bug has been moved to the POST state.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target version (5.0.0) matches configured target version for branch (5.0.0)
  • bug is in the state New, which is one of the valid states (NEW, ASSIGNED, POST)
Details

In response to this:

/jira refresh

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@openshift-ci
openshift-ci Bot requested review from atiratree and ingvagabund July 24, 2026 20:55
@openshift-ci

openshift-ci Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: michaelryanmcneill
Once this PR has been reviewed and has the lgtm label, please assign atiratree 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

@openshift-ci-robot

Copy link
Copy Markdown

@michaelryanmcneill: This pull request references Jira Issue OCPBUGS-99757, which is valid.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target version (5.0.0) matches configured target version for branch (5.0.0)
  • bug is in the state POST, which is one of the valid states (NEW, ASSIGNED, POST)
Details

In response to this:

Bug: https://issues.redhat.com/browse/OCPBUGS-99757

The OIDC exec plugin token cache key is computed from only IssuerURL and ClientID. When a user changes --extra-scopes, the stale cached token (missing the new scope claims) is returned instead of triggering a fresh auth code flow.

Changes:

  • Add ExtraScopes field to tokencache.Key so different scope sets produce distinct cache entries
  • Sort scopes before key construction to ensure deterministic cache filenames regardless of flag ordering
  • Add unit tests for token cache key computation and round-trip save/find

Workaround (prior to this fix): rm ~/.kube/cache/oc/* and re-authenticate.

Note: existing cached tokens will require a one-time re-authentication after this change, as the cache key format has changed.

Summary by CodeRabbit

  • Bug Fixes

  • Improved token caching so equivalent scope requests consistently reuse the correct cached token.

  • Prevented cached tokens from being returned when requested scopes do not match.

  • Preserved secure cache file permissions.

  • Tests

  • Added coverage for scope-sensitive cache behavior, deterministic cache entries, token retrieval, cache misses, and file permissions.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🧹 Nitpick comments (3)
pkg/cli/gettoken/tokencache/tokencache_test.go (2)

11-154: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add descriptive comments to the new test functions.

Each added unexported test function needs a concise comment describing the cache behavior it verifies.

As per coding guidelines, “Add descriptive comments to all exported and unexported Go types, functions, and methods.”

🤖 Prompt for 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.

In `@pkg/cli/gettoken/tokencache/tokencache_test.go` around lines 11 - 154, Add
concise descriptive comments immediately above each newly added test function,
including TestComputeFilename_DifferentScopes,
TestComputeFilename_SortedScopesDeterministic,
TestComputeFilename_UnsortedScopesDiffer,
TestComputeFilename_SameKeysSameFilename, TestSaveAndFindByKey,
TestFindByKey_DifferentScopesMiss, and TestSave_FilePermissions, stating the
cache behavior each test verifies.

Source: Coding guidelines


11-83: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use a table-driven test for filename scenarios.

These four tests exercise variants of the same key-to-filename behavior; combine them into named t.Run cases with expected equal/different results.

As per coding guidelines, “Use table-driven tests with the standard testing.T framework.”

🤖 Prompt for 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.

In `@pkg/cli/gettoken/tokencache/tokencache_test.go` around lines 11 - 83, Combine
TestComputeFilename_DifferentScopes,
TestComputeFilename_SortedScopesDeterministic,
TestComputeFilename_UnsortedScopesDiffer, and
TestComputeFilename_SameKeysSameFilename into one table-driven test with named
t.Run cases. Define each case’s key pair and whether filenames should match,
then reuse shared computeFilename and assertion logic while preserving the
existing order-sensitive scope behavior.

Source: Coding guidelines

pkg/cli/gettoken/tokencache/tokencache.go (1)

24-26: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document the scope-ordering invariant.

ExtraScopes is filename-significant and must be sorted by callers. Add a Key doc comment stating this contract so direct Repository.Save/FindByKey callers do not create order-dependent cache entries.

Proposed documentation
+// Key identifies a token cache entry. ExtraScopes must be in canonical sorted order.
 type Key struct {

As per coding guidelines, “Add descriptive comments to all exported and unexported Go types, functions, and methods.”

🤖 Prompt for 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.

In `@pkg/cli/gettoken/tokencache/tokencache.go` around lines 24 - 26, The exported
ExtraScopes field in the token cache key lacks documentation for its ordering
contract. Add a Go doc comment directly above ExtraScopes stating that callers
must keep the scopes sorted, preserving consistent keys for direct
Repository.Save and FindByKey usage.

Source: Coding guidelines

🤖 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 `@pkg/cli/gettoken/tokencache/tokencache_test.go`:
- Line 146: Update the test flow around computeFilename to handle its returned
error instead of discarding it. Check the error immediately after the call and
fail or return through the test’s established error-handling mechanism, only
using the computed filename when computation succeeds.

---

Nitpick comments:
In `@pkg/cli/gettoken/tokencache/tokencache_test.go`:
- Around line 11-154: Add concise descriptive comments immediately above each
newly added test function, including TestComputeFilename_DifferentScopes,
TestComputeFilename_SortedScopesDeterministic,
TestComputeFilename_UnsortedScopesDiffer,
TestComputeFilename_SameKeysSameFilename, TestSaveAndFindByKey,
TestFindByKey_DifferentScopesMiss, and TestSave_FilePermissions, stating the
cache behavior each test verifies.
- Around line 11-83: Combine TestComputeFilename_DifferentScopes,
TestComputeFilename_SortedScopesDeterministic,
TestComputeFilename_UnsortedScopesDiffer, and
TestComputeFilename_SameKeysSameFilename into one table-driven test with named
t.Run cases. Define each case’s key pair and whether filenames should match,
then reuse shared computeFilename and assertion logic while preserving the
existing order-sensitive scope behavior.

In `@pkg/cli/gettoken/tokencache/tokencache.go`:
- Around line 24-26: The exported ExtraScopes field in the token cache key lacks
documentation for its ordering contract. Add a Go doc comment directly above
ExtraScopes stating that callers must keep the scopes sorted, preserving
consistent keys for direct Repository.Save and FindByKey usage.
🪄 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: Repository YAML (base), Central YAML (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: cccbd085-baa1-420f-8e5e-9d9850f8c2d5

📥 Commits

Reviewing files that changed from the base of the PR and between a88e785 and 6899b92.

📒 Files selected for processing (3)
  • pkg/cli/gettoken/gettoken.go
  • pkg/cli/gettoken/tokencache/tokencache.go
  • pkg/cli/gettoken/tokencache/tokencache_test.go

Comment thread pkg/cli/gettoken/tokencache/tokencache_test.go Outdated

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
pkg/cli/gettoken/tokencache/tokencache_test.go (1)

11-11: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add comments to the test functions.

These four test functions lack descriptive Go comments. Add concise comments explaining the behavior each test protects.

As per coding guidelines, “Add descriptive comments to all exported and unexported Go types, functions, and methods.”

Also applies to: 65-65, 93-93, 117-117

🤖 Prompt for 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.

In `@pkg/cli/gettoken/tokencache/tokencache_test.go` at line 11, Add concise
descriptive Go comments to the four test functions, including
TestComputeFilename and the tests at the other referenced locations. Each
comment should explain the behavior that test protects and follow Go
documentation comment conventions.

Source: Coding guidelines

🤖 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 `@pkg/cli/gettoken/tokencache/tokencache_test.go`:
- Around line 111-114: Update the FindByKey assertion in the differentKey test
case to verify os.IsNotExist(err) rather than only checking that err is non-nil,
while retaining the failure message for unexpected successful lookups.

---

Nitpick comments:
In `@pkg/cli/gettoken/tokencache/tokencache_test.go`:
- Line 11: Add concise descriptive Go comments to the four test functions,
including TestComputeFilename and the tests at the other referenced locations.
Each comment should explain the behavior that test protects and follow Go
documentation comment conventions.
🪄 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: Repository YAML (base), Central YAML (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: 11c7cd59-8499-4c2c-871a-d0bcd17f1bd5

📥 Commits

Reviewing files that changed from the base of the PR and between 6899b92 and 14695d6.

📒 Files selected for processing (3)
  • pkg/cli/gettoken/gettoken.go
  • pkg/cli/gettoken/tokencache/tokencache.go
  • pkg/cli/gettoken/tokencache/tokencache_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • pkg/cli/gettoken/tokencache/tokencache.go
  • pkg/cli/gettoken/gettoken.go

Comment thread pkg/cli/gettoken/tokencache/tokencache_test.go
The token cache key only included issuer URL and client ID, causing
cached tokens to be returned even when different --extra-scopes were
requested. This resulted in ID tokens missing scope-dependent claims
(e.g., email, profile) after the initial authentication.

Add ExtraScopes to the tokencache.Key struct so that different scope
sets produce distinct cache entries. Scopes are sorted before key
construction to ensure deterministic cache filenames regardless of
flag ordering.

Signed-off-by: michaelryanmcneill <michael@michaelryanmcneill.com>
@openshift-ci

openshift-ci Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

@michaelryanmcneill: all tests passed!

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

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

Labels

jira/valid-bug Indicates that a referenced Jira bug is valid for the branch this PR is targeting. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants