Skip to content

Commit

Permalink
Update QueryOutFileArgument permission logic
Browse files Browse the repository at this point in the history
This allows the permission mode to be set prior to writing any
content for new created files.
  • Loading branch information
kyleknap committed Feb 1, 2024
1 parent 843a678 commit ae97409
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion awscli/customizations/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import re

from awscli.arguments import CustomArgument
from awscli.compat import compat_open
import jmespath


Expand Down Expand Up @@ -126,12 +127,18 @@ def save_query(self, parsed, **kwargs):
"""
if is_parsed_result_successful(parsed):
contents = jmespath.search(self.query, parsed)
with open(self.value, 'w') as fp:
with compat_open(
self.value, 'w', access_permissions=self.perm) as fp:
# Don't write 'None' to a file -- write ''.
if contents is None:
fp.write('')
else:
fp.write(contents)
# Even though the file is opened using self.perm permissions,
# the file may already exist and as a result, retain it's
# preexisting file permissions. The os.chmod is retained here
# to preserve behavior of this argument always clobbering
# a preexisting file's permissions.
os.chmod(self.value, self.perm)


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/customizations/test_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def test_saves_query_to_file_as_empty_string_when_none_result(self):

@skip_if_windows("Test not valid on windows.")
def test_permissions_on_created_file(self):
outfile = self.files.create_file('not-empty-test', '')
outfile = self.files.full_path('not-empty-test')
session = mock.Mock()
arg = QueryOutFileArgument(session, 'foo', 'baz', 'event', 0o600)
arg.add_to_params({}, outfile)
Expand Down

0 comments on commit ae97409

Please sign in to comment.