Skip to content

fix: share auth caches only between clients with the same credentials - #2288

Merged
TerryHowe merged 2 commits into
oras-project:mainfrom
sarankumar1325:fix-auth-cache-per-credential
Oct 24, 2026
Merged

TerryHowe merged 2 commits into
oras-project:mainfrom
sarankumar1325:fix-auth-cache-per-credential

Conversation

@sarankumar1325

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:

authClient used to make the first auth client a package-level shared client, and every later client reused its auth.Cache. auth.Cache is keyed by registry and scope, not by credential. So in oras cp, backup and restore, when --from and --to point at the same registry with different credentials, each client could send the other's Basic or bearer token.

This PR replaces sharedClient (and Set/Get/ClearSharedClient) with shared caches keyed by credential identity:

  • static credentials: username plus a SHA-256 of the password, refresh token and access token (the secrets are hashed, not stored as plaintext keys)
  • credential store: the --registry-config paths

Clients with the same identity still share a cache across command executions, which keeps the in-process re-auth saving from #1923. Clients with different identities get separate caches.

Tests:

  • TestRemote_authClient_SharedCache still checks that same-credential clients share cached tokens. It now resets the caches with clearSharedCaches.
  • New TestRemote_authClient_SharedCacheByCredential covers a different username, password, identity token and credential store.
  • New TestRemote_authClient_SharedCacheByConfig covers the same and different registry config paths.
  • The new cases fail when every client is given one cache (the old behaviour).
  • go test ./cmd/... ./internal/... and golangci-lint run ./cmd/... ./internal/... pass. I did not run the e2e suite.

The oras-go v3 branch (#2002) will need the same keying in its CacheFactory. I left that out of this PR.

AI assistance was used while preparing this change. I reviewed and tested it myself.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #2276

Please check the following list:

  • Does the affected code have corresponding tests, e.g. unit test, E2E test?
  • Does this change require a documentation update?
  • Does this introduce breaking changes that would require an announcement or bumping the major version?
  • Do all new files have an appropriate license header? (no new files)

The first auth client became a package-level shared client, and every
later client reused its auth.Cache. auth.Cache is keyed by registry and
scope, not by credential, so in cp, backup and restore the --from and
--to clients for the same registry could use each other's tokens.

Key the shared caches by credential identity instead: username plus a
hash of the secrets for static credentials, or the registry config
paths for credential stores. Clients with the same credentials still
share a cache across command executions, which keeps the behaviour
added in oras-project#1923.

Fixes oras-project#2276

Signed-off-by: sarankumar1325 <[email protected]>

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

/lgtm

Comment thread cmd/oras/internal/option/remote.go Fixed
@codecov

codecov Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 60.94%. Comparing base (9218890) to head (402ed3b).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2288      +/-   ##
==========================================
+ Coverage   60.78%   60.94%   +0.15%     
==========================================
  Files         140      140              
  Lines        6092     6096       +4     
==========================================
+ Hits         3703     3715      +12     
+ Misses       2167     2161       -6     
+ Partials      222      220       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

CodeQL flags SHA-256 over the password as weak password hashing. The key is only held in memory, so compare the credential directly and drop the hash.

Signed-off-by: sarankumar1325 <[email protected]>
@TerryHowe
TerryHowe merged commit 27c1796 into oras-project:main Oct 24, 2026
10 checks passed
@TerryHowe

Copy link
Copy Markdown
Member

Thanks!

vmvarela added a commit to vmvarela/oras that referenced this pull request Nov 2, 2026
Replace the process-global auth cache map with a per-invocation store owned by option.Common. Clients with the same credentials still share a cache within one invocation, while different credentials and different invocations stay isolated.

This is a follow-up to oras-project#2288, which stopped clients with different credentials from sharing tokens but kept the cache store global.

Refs oras-project#2305

Signed-off-by: Victor M Varela <[email protected]>
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.

bug: cp/backup/restore share one auth cache across clients with different credentials

3 participants