From 422baea136f839598b8dec15e4f19df560e9e5cd Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Wed, 22 Mar 2017 22:26:40 -0700 Subject: [PATCH] base64 functions must be given byte strings The base64 stdlib functions require byte strings. So we have to transform the strings into bytes before handing to the stdlib and then transform them back into text before handing back to ansible. Fixes #22873 (cherry picked from commit 589e217278fd4d4fc8bb167326079f18d788011b) --- lib/ansible/plugins/filter/core.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index f109c3e72e9..6c8eb2c5097 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -459,6 +459,14 @@ def do_groupby(environment, value, attribute): return [tuple(t) for t in _do_groupby(environment, value, attribute)] +def b64encode(string): + return to_text(base64.b64encode(to_bytes(string, errors='surrogate_then_strict'))) + + +def b64decode(string): + return to_text(base64.b64decode(to_bytes(string, errors='surrogate_then_strict'))) + + class FilterModule(object): ''' Ansible core jinja2 filters ''' @@ -468,8 +476,8 @@ class FilterModule(object): 'groupby': do_groupby, # base 64 - 'b64decode': partial(unicode_wrap, base64.b64decode), - 'b64encode': partial(unicode_wrap, base64.b64encode), + 'b64decode': b64decode, + 'b64encode': b64encode, # uuid 'to_uuid': to_uuid,