From fc210a45844595e38df19b0bb4388d5a5ed848d0 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Sat, 5 May 2018 09:16:58 +1000 Subject: [PATCH] base64 filter: Added ability to specify encoding (#39714) * base64 filter: Added ability to specify encoding * Added unicode chars for further testing * Removed errors to keep previous behaviour in place * Removed surrogate pairs due to issues loading YAML in CI --- changelogs/fragments/base64_filter_encoding.yaml | 3 +++ docs/docsite/rst/user_guide/playbooks_filters.rst | 7 +++++++ lib/ansible/plugins/filter/core.py | 8 ++++---- test/integration/targets/filters/tasks/main.yml | 8 ++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/base64_filter_encoding.yaml diff --git a/changelogs/fragments/base64_filter_encoding.yaml b/changelogs/fragments/base64_filter_encoding.yaml new file mode 100644 index 0000000000..e1fd600e55 --- /dev/null +++ b/changelogs/fragments/base64_filter_encoding.yaml @@ -0,0 +1,3 @@ +minor_changes: +- Added an ``encoding`` option to the ``b64encode`` and ``b64decode`` filters + to specify the encoding of the string that is base64 encoded. diff --git a/docs/docsite/rst/user_guide/playbooks_filters.rst b/docs/docsite/rst/user_guide/playbooks_filters.rst index cee1500e7e..a908605945 100644 --- a/docs/docsite/rst/user_guide/playbooks_filters.rst +++ b/docs/docsite/rst/user_guide/playbooks_filters.rst @@ -935,6 +935,13 @@ To work with Base64 encoded strings:: {{ encoded | b64decode }} {{ decoded | b64encode }} +As of version 2.6, you can define the type of encoding to use, the default is ``utf-8``:: + + {{ encoded | b64decode(encoding='utf-16-le') }} + {{ decoded | b64encode(encoding='utf-16-le') }} + +.. versionadded:: 2.6 + To create a UUID from a string (new in version 1.9):: {{ hostname | to_uuid }} diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index 6b39f2fbe1..1b2be0a984 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -459,12 +459,12 @@ 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_or_strict'))) +def b64encode(string, encoding='utf-8'): + return to_text(base64.b64encode(to_bytes(string, encoding=encoding, errors='surrogate_or_strict'))) -def b64decode(string): - return to_text(base64.b64decode(to_bytes(string, errors='surrogate_or_strict'))) +def b64decode(string, encoding='utf-8'): + return to_text(base64.b64decode(to_bytes(string, errors='surrogate_or_strict')), encoding=encoding) def flatten(mylist, levels=None): diff --git a/test/integration/targets/filters/tasks/main.yml b/test/integration/targets/filters/tasks/main.yml index 28821bd258..a2394dc1fa 100644 --- a/test/integration/targets/filters/tasks/main.yml +++ b/test/integration/targets/filters/tasks/main.yml @@ -185,3 +185,11 @@ - flat_two == [1, 2, 3, 4, [5], 6, 7] vars: orig_list: [1, 2, [3, [4, [5]], 6], 7] + +- name: Test base64 filter + assert: + that: + - "'Ansible - くらとみ\n' | b64encode == 'QW5zaWJsZSAtIOOBj+OCieOBqOOBvwo='" + - "'QW5zaWJsZSAtIOOBj+OCieOBqOOBvwo=' | b64decode == 'Ansible - くらとみ\n'" + - "'Ansible - くらとみ\n' | b64encode(encoding='utf-16-le') == 'QQBuAHMAaQBiAGwAZQAgAC0AIABPMIkwaDB/MAoA'" + - "'QQBuAHMAaQBiAGwAZQAgAC0AIABPMIkwaDB/MAoA' | b64decode(encoding='utf-16-le') == 'Ansible - くらとみ\n'" \ No newline at end of file