Skip to content

Fix KeyError crash on unreadable login cache, write cache atomically - #10942

Open
draarivpatel-ui wants to merge 1 commit into
aws:v2from
draarivpatel-ui:fix-login-cache-keyerror
Open

draarivpatel-ui wants to merge 1 commit into
aws:v2from
draarivpatel-ui:fix-login-cache-keyerror

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Fixes #10931.

Problem

With a profile holding login_session, a login cache file that exists but is not valid JSON failed every command with only the cache key:

aws: [ERROR]: '35301825947ca2a97750d4b8e749de609d0313b0536da5d4c560c1565cd5cc5a'

JSONFileCache.__getitem__ converts a parse failure into KeyError(cache_key), and LoginCredentialsLoader.load_token only checked that the file exists before indexing the cache, so the KeyError escaped past the token is None branch in LoginCredentialFetcher.load_cached_credentials that raises LoginTokenLoadError (Unable to load a existing login session ... Please reauthenticate with 'aws login'.).

The cache file was also rewritten in place on every token refresh (O_WRONLY | O_CREAT + truncate() + write()), so a refresh interrupted by a killed process or power loss left it empty or partially written, permanently breaking the profile until aws login rewrote it.

Fix

  • LoginCredentialsLoader.load_token now returns None on KeyError, so an unreadable cache file produces the same error as a missing one.
  • JSONFileCache.__setitem__ writes to a temporary file in the same directory, flushes and os.fsyncs it, then os.replaces it over the cache file, so an interrupted write leaves either the old token or the new one. This also covers the other JSONFileCache users (SSO tokens, assume-role caches).

Tests

  • TestLoginCredentialsLoader.test_load_token_unreadable_cache_file: a cache file of NUL bytes now returns None (previously raised KeyError).
  • TestJSONCache.test_unreadable_cache_file_raises_key_error: documents that __getitem__ still raises KeyError for unparseable files.
  • TestJSONCache.test_failed_write_keeps_existing_value: a write interrupted before os.replace keeps the previous value and leaves no temp files.

Ran tests/unit/customizations/login/, tests/unit/botocore/test_credentials.py and tests/unit/botocore/test_utils.py: 580 tests, all passing.

A login cache file that exists but cannot be parsed made every command
fail with only the cache key ('aws: [ERROR]: <cache key>'), because
JSONFileCache.__getitem__ converts the parse error into KeyError and
LoginCredentialsLoader.load_token let it escape past the
LoginTokenLoadError branch that tells the user to run 'aws login'.
Return None on KeyError so an unreadable cache gets the same error as a
missing one.

The cache file was also rewritten in place on every token refresh, so a
refresh cut short left it empty or partial. Write to a temporary file in
the same directory, fsync it, and os.replace it over the cache file.

Fixes aws#10931
@draarivpatel-ui
draarivpatel-ui requested a review from a team as a code owner November 24, 2026 15:37
Copilot AI balanced review requested due to automatic review settings November 24, 2026 15:37

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
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.

An unreadable aws login cache fails every command with only the cache key, and the cache is rewritten in place

2 participants