From a508d66b914df8446b6f86b9725e48d0227e9cda Mon Sep 17 00:00:00 2001 From: James Saryerwinnie Date: Tue, 2 Feb 2016 17:08:34 -0800 Subject: [PATCH] Truncate file before writing Fixes #1684. --- awscli/customizations/assumerole.py | 1 + tests/unit/customizations/test_assumerole.py | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/awscli/customizations/assumerole.py b/awscli/customizations/assumerole.py index 5f94d2450b99..ee9ce47c1328 100644 --- a/awscli/customizations/assumerole.py +++ b/awscli/customizations/assumerole.py @@ -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): diff --git a/tests/unit/customizations/test_assumerole.py b/tests/unit/customizations/test_assumerole.py index ccbefcbd1649..8a876ceb513f 100644 --- a/tests/unit/customizations/test_assumerole.py +++ b/tests/unit/customizations/test_assumerole.py @@ -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'}