From cce06c5a3b885d2e4945b8eb78084914198c9923 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Fri, 28 Jul 2017 09:14:08 -0700 Subject: [PATCH] Fix _get_file_contents to return bytes and checking that a different file exists than it opens --- lib/ansible/parsing/dataloader.py | 23 +++++++++++++++-------- lib/ansible/plugins/lookup/ini.py | 3 ++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/ansible/parsing/dataloader.py b/lib/ansible/parsing/dataloader.py index 568cb801a8..2844a890dd 100644 --- a/lib/ansible/parsing/dataloader.py +++ b/lib/ansible/parsing/dataloader.py @@ -179,22 +179,32 @@ class DataLoader: except AttributeError: pass # older versions of yaml don't have dispose function, ignore - def _get_file_contents(self, file_name, encoding='utf-8'): + def _get_file_contents(self, file_name): ''' - Reads the file contents from the given file name, and will decrypt them - if they are found to be vault-encrypted. + Reads the file contents from the given file name + + If the contents are vault-encrypted, it will decrypt them and return + the decrypted data + + :arg file_name: The name of the file to read. If this is a relative + path, it will be expanded relative to the basedir + :raises AnsibleFileNotFOund: if the file_name does not refer to a file + :raises AnsibleParserError: if we were unable to read the file + :return: Returns a byte string of the file contents ''' if not file_name or not isinstance(file_name, (binary_type, text_type)): raise AnsibleParserError("Invalid filename: '%s'" % str(file_name)) - b_file_name = to_bytes(file_name) + b_file_name = to_bytes(self.path_dwim(file_name)) + # This is what we really want but have to fix unittests to make it pass + # if not os.path.exists(b_file_name) or not os.path.isfile(b_file_name): if not self.path_exists(b_file_name) or not self.is_file(b_file_name): raise AnsibleFileNotFound("Unable to retrieve file contents", file_name=file_name) show_content = True try: with open(b_file_name, 'rb') as f: - data = to_text(f.read(), encoding=encoding) + data = f.read() if is_encrypted(data): data = self._vault.decrypt(data, filename=b_file_name) show_content = False @@ -416,9 +426,6 @@ class DataLoader: if not self.path_exists(b_file_path) or not self.is_file(b_file_path): raise AnsibleFileNotFound(file_name=file_path) - if not self._vault: - self._vault = VaultLib(b_password="") - real_path = self.path_dwim(file_path) try: diff --git a/lib/ansible/plugins/lookup/ini.py b/lib/ansible/plugins/lookup/ini.py index 0eb6459d2a..d9418265ad 100644 --- a/lib/ansible/plugins/lookup/ini.py +++ b/lib/ansible/plugins/lookup/ini.py @@ -102,7 +102,8 @@ class LookupModule(LookupBase): paramvals['section'] = 'java_properties' # Open file using encoding - contents, show_data = self._loader._get_file_contents(path, encoding=paramvals['encoding']) + contents, show_data = self._loader._get_file_contents(path) + contents = to_text(contents, errors='surrogate_or_strict', encoding=paramvals['encoding']) config.write(contents) config.seek(0, os.SEEK_SET)