From 50eb0a95debaa20c034a7634cedf294245409cf2 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Tue, 20 Jun 2023 18:52:58 +0200 Subject: [PATCH] [PR #6720/6bff57ee backport][stable-7] Fix multiple issues with the TSS lookup plugin when using fetch_attachments (#6751) Fix multiple issues with the TSS lookup plugin when using fetch_attachments (#6720) * Treat files as binary when downloading attachments * Raise a warning when the attachment can't be read * Set the 'itemValue' for files, even when they can't be read * Always return the original secret content * Add changelog * Fix changelog * Update changelog Co-authored-by: Felix Fontein * Revert "Always return the original secret content" This reverts commit a9fb96e165dea581bd41ff8ebabfe7900e1d132a. --------- Co-authored-by: Felix Fontein (cherry picked from commit 6bff57ee6e85a450540ddd9151b9814cd24d294f) Co-authored-by: laszlojau <49835454+laszlojau@users.noreply.github.com> --- .../fragments/6720-tss-fix-fetch-attachments.yml | 2 ++ plugins/lookup/tss.py | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/6720-tss-fix-fetch-attachments.yml diff --git a/changelogs/fragments/6720-tss-fix-fetch-attachments.yml b/changelogs/fragments/6720-tss-fix-fetch-attachments.yml new file mode 100644 index 0000000000..312cbf5c30 --- /dev/null +++ b/changelogs/fragments/6720-tss-fix-fetch-attachments.yml @@ -0,0 +1,2 @@ +bugfixes: + - "tss lookup plugin - fix multiple issues when using ``fetch_attachments=true`` (https://github.com/ansible-collections/community.general/pull/6720)." diff --git a/plugins/lookup/tss.py b/plugins/lookup/tss.py index 581c4b704f..6235512520 100644 --- a/plugins/lookup/tss.py +++ b/plugins/lookup/tss.py @@ -289,11 +289,15 @@ class TSSClient(object): if file_download_path and os.path.isdir(file_download_path): if i['isFile']: try: - with open(os.path.join(file_download_path, str(obj['id']) + "_" + i['slug']), "w") as f: - f.write(i['itemValue'].text) - i['itemValue'] = "*** Not Valid For Display ***" + file_content = i['itemValue'].content + with open(os.path.join(file_download_path, str(obj['id']) + "_" + i['slug']), "wb") as f: + f.write(file_content) except ValueError: raise AnsibleOptionsError("Failed to download {0}".format(str(i['slug']))) + except AttributeError: + display.warning("Could not read file content for {0}".format(str(i['slug']))) + finally: + i['itemValue'] = "*** Not Valid For Display ***" else: raise AnsibleOptionsError("File download path does not exist") return obj