Skip to content

Invalidate cached credentials on auth failures - #3777

Merged
alexgromero merged 2 commits into
boto:feature/credential-refreshfrom
alexgromero:cred-refresh/invalidation
Aug 26, 2026
Merged

Invalidate cached credentials on auth failures#3777
alexgromero merged 2 commits into
boto:feature/credential-refreshfrom
alexgromero:cred-refresh/invalidation

Conversation

@alexgromero

Copy link
Copy Markdown
Contributor

Issue #, if available:
Python-9269

Overview

This updates credential refresh to invalidate cached refreshable credentials after service authentication failures.

When DEFAULT_NEW_CREDENTIAL_REFRESH is enabled, a service can reject otherwise unexpired credentials with ExpiredToken or InvalidToken, but botocore may still keep that credential set cached until its normal expiration time. This change marks the rejected credentials stale so the next request goes back through refresh instead of continuing to reuse the rejected credentials.

The legacy refresh path is unchanged.

What changed

On the signing side, RequestSigner now records the access key that signed the request in request.context. This is only done for the standard credential-chain case; request-scoped credentials and identity-cache flows are intentionally excluded.

On the response side, a new after-call handler watches for ExpiredToken and InvalidToken. When one of those errors is returned, it resolves the client's current credentials and invalidates them only if the cached access key still matches the access key that signed the rejected request. That avoids mutating fresh credentials when another thread has already completed a refresh.

The invalidation itself sets the cached credentials' expiry to now, so the next credential lookup routes through refresh. It also uses a non-blocking acquire on the refresh lock: if a refresh or another invalidation is already in progress, it returns immediately instead of blocking.

For providers with an intermediate cache layer, invalidating only the outer RefreshableCredentials object is not enough. AssumeRole, AssumeRoleWithWebIdentity, and SSO can otherwise refresh back into the same cached provider result. This change wires those providers to also expire the matching cached provider entry so the next refresh goes back to the underlying source.

Testing

Added coverage for:

  • signer behavior that records signing_access_key and skips request-scoped / identity-cache cases
  • auth-error invalidation event registration and handler behavior, including error_code_override
  • refreshable credential invalidation when the access key matches or does not match
  • provider-cache invalidation for AssumeRole, AssumeRoleWithWebIdentity, and SSO
  • invalidation preserving the existing refresh backoff flow rather than bypassing it
  • strict refreshable credentials still invalidating while preserving fail-fast refresh behavior when refresh itself fails

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@alexgromero
alexgromero requested a review from a team as a code owner August 15, 2026 03:05
@codecov-commenter

codecov-commenter commented Aug 15, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 93.10345% with 4 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (feature/credential-refresh@0056fae). Learn more about missing BASE report.

Files with missing lines Patch % Lines
botocore/credentials.py 86.20% 4 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@                      Coverage Diff                      @@
##             feature/credential-refresh    #3777   +/-   ##
=============================================================
  Coverage                              ?   92.57%           
=============================================================
  Files                                 ?       68           
  Lines                                 ?    16117           
  Branches                              ?        0           
=============================================================
  Hits                                  ?    14920           
  Misses                                ?     1197           
  Partials                              ?        0           

☔ 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.

@alexgromero
alexgromero force-pushed the cred-refresh/invalidation branch from de6a165 to 3c9a5a4 Compare August 21, 2026 20:13

@ubaskota ubaskota left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks good to me as is. I've added a comment to point out a test gap and will approve it after its addressed. Thanks

fetcher.refresh_credentials()


class TestInvalidate:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we add a test for the case where the refresh lock is already held when _invalidate is called?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep, added in ae17afe.

@alexgromero
alexgromero requested a review from ubaskota August 25, 2026 17:00
@alexgromero
alexgromero merged commit 2577d8d into boto:feature/credential-refresh Aug 26, 2026
44 checks passed
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.

3 participants