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
This commit is contained in:
Alex Kavanagh 2020-02-18 17:53:24 +00:00
parent 98b0eb1178
commit 64a8e48a77

View File

@ -36,15 +36,10 @@ from charmhelpers.core.hookenv import (
ERROR, ERROR,
) )
# python-six in ensured by charmhelpers import so we put this here.
import six
try: try:
import psutil import psutil
except ImportError: except ImportError:
if six.PY2: apt_install('python3-psutil', fatal=True)
apt_install('python-psutil', fatal=True)
else:
apt_install('python3-psutil', fatal=True)
import psutil import psutil
@ -65,14 +60,23 @@ MAX_DEFAULT_THREADS = DEFAULT_MULTIPLIER * 2
def convert_from_base64(v): def convert_from_base64(v):
# Rabbit originally supported pem encoded key/cert in config, play """Speculatively convert the string `v` from base64 encoding if it is
# nice on upgrades as we now expect base64 encoded key/cert/ca. 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: if not v:
return v return v
if v.startswith('-----BEGIN'): if v.startswith('-----BEGIN'):
return v return v
try: try:
return base64.b64decode(v) return base64.b64decode(v).decode('utf-8')
except TypeError: except TypeError:
return v return v