From 64a8e48a77a6723401c1c730992885d2b5390c85 Mon Sep 17 00:00:00 2001 From: Alex Kavanagh Date: Tue, 18 Feb 2020 17:53:24 +0000 Subject: [PATCH] Fix the response of convert_from_base64() The function takes a string, but could return a bytes in Python3. This ensures that it returns a str if the b64decode() function is used. Also, strip out the six module from rabbitmq_context.py as it's not needed now the charm is py3 only. Change-Id: Idd2bcf79152f8db90d674ea28fb99504536ab6cf --- hooks/rabbitmq_context.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/hooks/rabbitmq_context.py b/hooks/rabbitmq_context.py index 175c72cd..f92d15fb 100644 --- a/hooks/rabbitmq_context.py +++ b/hooks/rabbitmq_context.py @@ -36,15 +36,10 @@ from charmhelpers.core.hookenv import ( ERROR, ) -# python-six in ensured by charmhelpers import so we put this here. -import six try: import psutil except ImportError: - if six.PY2: - apt_install('python-psutil', fatal=True) - else: - apt_install('python3-psutil', fatal=True) + apt_install('python3-psutil', fatal=True) import psutil @@ -65,14 +60,23 @@ MAX_DEFAULT_THREADS = DEFAULT_MULTIPLIER * 2 def convert_from_base64(v): - # Rabbit originally supported pem encoded key/cert in config, play - # nice on upgrades as we now expect base64 encoded key/cert/ca. + """Speculatively convert the string `v` from base64 encoding if it is + base64 encoded. + + Rabbit originally supported pem encoded key/cert in config, play + nice on upgrades as we now expect base64 encoded key/cert/ca. + + :param v: the string to maybe convert + :type v: str + :returns: string that may have been converted from base64 encoding. + :rtype: str + """ if not v: return v if v.startswith('-----BEGIN'): return v try: - return base64.b64decode(v) + return base64.b64decode(v).decode('utf-8') except TypeError: return v