From 0cd07eb3fd529297622b29aff93c2fa94fbb0ad0 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Thu, 25 Jun 2020 21:55:21 +0200 Subject: [PATCH] hash filter - fail when unsupported type is passed as an argument (#70292) Fixes #70258 --- .../70258-hash-filter-fail-unsupported-type.yml | 2 ++ lib/ansible/plugins/filter/core.py | 8 ++++---- test/integration/targets/filter_core/tasks/main.yml | 12 +++++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/70258-hash-filter-fail-unsupported-type.yml diff --git a/changelogs/fragments/70258-hash-filter-fail-unsupported-type.yml b/changelogs/fragments/70258-hash-filter-fail-unsupported-type.yml new file mode 100644 index 00000000000..36870e79d94 --- /dev/null +++ b/changelogs/fragments/70258-hash-filter-fail-unsupported-type.yml @@ -0,0 +1,2 @@ +minor_changes: + - hash filter - fail when unsupported hash type is passed as an argument (https://github.com/ansible/ansible/issues/70258) diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index 9eecd79b79d..30fbe019575 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -253,11 +253,11 @@ def randomize_list(mylist, seed=None): def get_hash(data, hashtype='sha1'): - - try: # see if hash is supported + try: h = hashlib.new(hashtype) - except Exception: - return None + except Exception as e: + # hash is not supported? + raise AnsibleFilterError(e) h.update(to_bytes(data, errors='surrogate_or_strict')) return h.hexdigest() diff --git a/test/integration/targets/filter_core/tasks/main.yml b/test/integration/targets/filter_core/tasks/main.yml index e8ef2345eb0..cd076475de2 100644 --- a/test/integration/targets/filter_core/tasks/main.yml +++ b/test/integration/targets/filter_core/tasks/main.yml @@ -110,7 +110,17 @@ that: - '"{{ "hash" | hash("sha1") }}" == "2346ad27d7568ba9896f1b7da6b5991251debdf2"' - '"{{ "café" | hash("sha1") }}" == "f424452a9673918c6f09b0cdd35b20be8e6ae7d7"' - - '"corned beef"|hash("haha, get it?") == None' + +- name: Test unsupported hash type + debug: + msg: "{{ 'hash' | hash('unsupported_hash_type') }}" + ignore_errors: yes + register: unsupported_hash_type_res + +- assert: + that: + - "unsupported_hash_type_res is failed" + - "'unsupported hash type' in unsupported_hash_type_res.msg" - name: Flatten tests block: