Skip to content

Commit

Permalink
Truncate file before writing
Browse files Browse the repository at this point in the history
Fixes aws#1684.
  • Loading branch information
jamesls committed Feb 3, 2016
1 parent 234ad16 commit a508d66
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions awscli/customizations/assumerole.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def __setitem__(self, cache_key, value):
os.makedirs(self._working_dir)
with os.fdopen(os.open(full_key,
os.O_WRONLY | os.O_CREAT, 0o600), 'w') as f:
f.truncate()
f.write(file_content)

def _convert_cache_key(self, cache_key):
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/customizations/test_assumerole.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ def test_key_error_raised_when_cache_key_does_not_exist(self):
with self.assertRaises(KeyError):
self.cache['foo']

def test_file_is_truncated_before_writing(self):
self.cache['mykey'] = {
'really long key in the cache': 'really long value in cache'}
# Now overwrite it with a smaller value.
self.cache['mykey'] = {'a': 'b'}
self.assertEqual(self.cache['mykey'], {'a': 'b'})

@skip_if_windows('File permissions tests not supported on Windows.')
def test_permissions_for_file_restricted(self):
self.cache['mykey'] = {'foo': 'bar'}
Expand Down

0 comments on commit a508d66

Please sign in to comment.