hash filter - fail when unsupported type is passed as an argument (#70292)

Fixes #70258
This commit is contained in:
Martin Krizek 2020-06-25 21:55:21 +02:00 committed by GitHub
parent b019029bf3
commit 0cd07eb3fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 5 deletions

View file

@ -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)

View file

@ -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()

View file

@ -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: