hash filter - fail when unsupported type is passed as an argument (#70292)
Fixes #70258
This commit is contained in:
parent
b019029bf3
commit
0cd07eb3fd
3 changed files with 17 additions and 5 deletions
|
@ -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)
|
|
@ -253,11 +253,11 @@ def randomize_list(mylist, seed=None):
|
||||||
|
|
||||||
|
|
||||||
def get_hash(data, hashtype='sha1'):
|
def get_hash(data, hashtype='sha1'):
|
||||||
|
try:
|
||||||
try: # see if hash is supported
|
|
||||||
h = hashlib.new(hashtype)
|
h = hashlib.new(hashtype)
|
||||||
except Exception:
|
except Exception as e:
|
||||||
return None
|
# hash is not supported?
|
||||||
|
raise AnsibleFilterError(e)
|
||||||
|
|
||||||
h.update(to_bytes(data, errors='surrogate_or_strict'))
|
h.update(to_bytes(data, errors='surrogate_or_strict'))
|
||||||
return h.hexdigest()
|
return h.hexdigest()
|
||||||
|
|
|
@ -110,7 +110,17 @@
|
||||||
that:
|
that:
|
||||||
- '"{{ "hash" | hash("sha1") }}" == "2346ad27d7568ba9896f1b7da6b5991251debdf2"'
|
- '"{{ "hash" | hash("sha1") }}" == "2346ad27d7568ba9896f1b7da6b5991251debdf2"'
|
||||||
- '"{{ "café" | hash("sha1") }}" == "f424452a9673918c6f09b0cdd35b20be8e6ae7d7"'
|
- '"{{ "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
|
- name: Flatten tests
|
||||||
block:
|
block:
|
||||||
|
|
Loading…
Reference in a new issue