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 internally in computeFilename to ensure deterministic cache filenames regardless of flag ordering, with explicit nil handling
  • Add unit tests for token cache key computation (table-driven), round-trip save/find, scope-based cache miss, and file permissions

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
    • Fixed token caching so cached entries are separated by the requested extra-scope set.
    • Cache keying now accounts for extra scopes consistently, treating different orderings of the same scopes as equivalent.
    • Prevents reuse of cached tokens when extra scopes differ.
    • Kept secure cache file permissions intact.
  • Tests
    • Expanded table-driven coverage for cache key equality/inequality based on extra scopes.
    • Added/strengthened repository round-trip and cache-miss assertions for mismatched scopes.
    • Verified cache file permissions remain restrictive.

@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

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Token cache keys now include ExtraScopes, with filename generation sorting scopes for deterministic identity. Tests cover filename equality, repository retrieval and misses, and file permissions.

Changes

Token cache scope identity

Layer / File(s) Summary
Scope-aware cache key generation
pkg/cli/gettoken/gettoken.go, pkg/cli/gettoken/tokencache/tokencache.go
Key includes extra scopes, token retrieval passes them into cache lookups, and filename generation sorts a copy before encoding.
Cache behavior validation
pkg/cli/gettoken/tokencache/tokencache_test.go
Tests verify deterministic filenames, save-and-find round trips, 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
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 were added; the new Go test names and subtests are static, descriptive strings with no dynamic data.
Test Structure And Quality ✅ Passed Standard Go unit tests, not Ginkgo; each test is focused, uses t.TempDir cleanup, and includes clear failure messages.
Microshift Test Compatibility ✅ Passed No new Ginkgo e2e tests were added; the new tokencache tests use plain testing and reference no MicroShift-unsupported APIs or namespaces.
Single Node Openshift (Sno) Test Compatibility ✅ Passed PASS: The PR adds only plain Go unit tests in tokencache_test.go; no Ginkgo e2e constructs or multi-node/SNO-sensitive assumptions are present.
Topology-Aware Scheduling Compatibility ✅ Passed Only CLI token-cache files changed; no deployment manifests, operators, or controllers were modified, so the scheduling check is not applicable.
Ote Binary Stdout Contract ✅ Passed No stdout writes were added in main/init/TestMain/suite setup; the PR only changes token-cache keying and adds ordinary tests.
Ipv6 And Disconnected Network Test Compatibility ✅ Passed No new Ginkgo e2e tests were added; the added tests are standard unit tests with temp dirs and no IPv4 or external-connectivity assumptions.
No-Weak-Crypto ✅ Passed Touched code uses SHA-256 for cache filenames; no MD5/SHA1/DES/RC4/3DES/Blowfish/ECB, custom crypto, or secret comparisons found.
Container-Privileges ✅ Passed Only three Go files changed; the diff contains no privileged container settings or K8s manifests.
No-Sensitive-Data-In-Logs ✅ Passed Patch only updates token-cache keying and tests; it adds no new logging of tokens, secrets, PII, or hostnames.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding extra scopes to the OIDC token cache key.
✨ 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-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
Comment thread pkg/cli/gettoken/gettoken.go Outdated
Comment thread pkg/cli/gettoken/tokencache/tokencache.go Outdated
Comment thread pkg/cli/gettoken/tokencache/tokencache_test.go Outdated
Comment thread pkg/cli/gettoken/tokencache/tokencache.go Outdated
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>
@michaelryanmcneill

Copy link
Copy Markdown
Author

@ardaguclu ready for final review! Thanks!

@ardaguclu

Copy link
Copy Markdown
Member

Thank you
/lgtm
/approve
Unit tests cover the functionality. But none of the e2e tests are verifying this behavior. Did you manually test this on live cluster?.

@openshift-ci openshift-ci Bot added the lgtm Indicates that a PR is ready to be merged. label Jul 28, 2026
@openshift-merge-bot

Copy link
Copy Markdown
Contributor

Scheduling required tests:
/test e2e-agnostic-ovn-cmd
/test e2e-aws-ovn
/test e2e-aws-ovn-serial-1of2
/test e2e-aws-ovn-serial-2of2
/test e2e-aws-ovn-upgrade

@openshift-ci

openshift-ci Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ardaguclu, michaelryanmcneill

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

@openshift-ci openshift-ci Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 28, 2026
@openshift-ci

openshift-ci Bot commented Jul 28, 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

approved Indicates a PR has been approved by an approver from all required OWNERS files. 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. lgtm Indicates that a PR is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants