From cee8cc2a52d1e3d1a8b66cf1d786ba53ea5253f6 Mon Sep 17 00:00:00 2001 From: Lucas Alvares Gomes Date: Sat, 8 Apr 2017 00:22:39 +0100 Subject: [PATCH] rpm_key: Decode bytes to string to work with Python 3 (Fixes #20326) (#20385) * rpm_key: Decode bytes to string to work with Python 3 (#20326) The read() method will return bytes we need to then decode() those bytes to a string before trying to match() it using the re module. * Make the rpm_key pgp regex more robust on both py2 and py3 --- lib/ansible/modules/packaging/os/rpm_key.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/ansible/modules/packaging/os/rpm_key.py b/lib/ansible/modules/packaging/os/rpm_key.py index 74899fd311..73c7cc9670 100644 --- a/lib/ansible/modules/packaging/os/rpm_key.py +++ b/lib/ansible/modules/packaging/os/rpm_key.py @@ -75,12 +75,19 @@ import re import os.path import tempfile +# import module snippets +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.urls import fetch_url +from ansible.module_utils._text import to_native + + def is_pubkey(string): """Verifies if string is a pubkey""" pgp_regex = ".*?(-----BEGIN PGP PUBLIC KEY BLOCK-----.*?-----END PGP PUBLIC KEY BLOCK-----).*" - return re.match(pgp_regex, string, re.DOTALL) + return bool(re.match(pgp_regex, to_native(string, errors='surrogate_or_strict'), re.DOTALL)) -class RpmKey: + +class RpmKey(object): def __init__(self, module): # If the key is a url, we need to check if it's present to be idempotent, @@ -122,7 +129,6 @@ class RpmKey: else: module.exit_json(changed=False) - def fetch_key(self, url): """Downloads a key from url, returns a valid path to a gpg key""" rsp, info = fetch_url(self.module, url) @@ -212,9 +218,5 @@ def main(): RpmKey(module) - -# import module snippets -from ansible.module_utils.basic import * -from ansible.module_utils.urls import * if __name__ == '__main__': main()