Repository navigation
Fix KeyError crash on unreadable login cache, write cache atomically - #10942
Open
draarivpatel-ui wants to merge 1 commit into
Open
draarivpatel-ui wants to merge 1 commit into
draarivpatel-ui wants to merge 1 commit into
Conversation
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
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:JSONFileCache.__getitem__converts a parse failure intoKeyError(cache_key), andLoginCredentialsLoader.load_tokenonly checked that the file exists before indexing the cache, so theKeyErrorescaped past thetoken is Nonebranch inLoginCredentialFetcher.load_cached_credentialsthat raisesLoginTokenLoadError(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 untilaws loginrewrote it.Fix
LoginCredentialsLoader.load_tokennow returnsNoneonKeyError, 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 andos.fsyncs it, thenos.replaces it over the cache file, so an interrupted write leaves either the old token or the new one. This also covers the otherJSONFileCacheusers (SSO tokens, assume-role caches).Tests
TestLoginCredentialsLoader.test_load_token_unreadable_cache_file: a cache file of NUL bytes now returnsNone(previously raisedKeyError).TestJSONCache.test_unreadable_cache_file_raises_key_error: documents that__getitem__still raisesKeyErrorfor unparseable files.TestJSONCache.test_failed_write_keeps_existing_value: a write interrupted beforeos.replacekeeps the previous value and leaves no temp files.Ran
tests/unit/customizations/login/,tests/unit/botocore/test_credentials.pyandtests/unit/botocore/test_utils.py: 580 tests, all passing.