diff --git a/glance/common/crypt.py b/glance/common/crypt.py index 74a25103..59d28eb1 100644 --- a/glance/common/crypt.py +++ b/glance/common/crypt.py @@ -24,6 +24,7 @@ import base64 from Crypto.Cipher import AES from Crypto import Random from Crypto.Random import random +from oslo_utils import encodeutils import six # NOTE(jokke): simplified transition to py3, behaves like py2 xrange from six.moves import range @@ -51,10 +52,8 @@ def urlsafe_encrypt(key, plaintext, blocksize=16): # We use chr(0) as a delimiter between text and padding return text + b'\0' + pad - if isinstance(plaintext, six.text_type): - plaintext = plaintext.encode('utf-8') - if isinstance(key, six.text_type): - key = key.encode('utf-8') + plaintext = encodeutils.to_utf8(plaintext) + key = encodeutils.to_utf8(key) # random initial 16 bytes for CBC init_vector = Random.get_random_bytes(16) cypher = AES.new(key, AES.MODE_CBC, init_vector) @@ -76,10 +75,8 @@ def urlsafe_decrypt(key, ciphertext): :returns: Resulting plaintext """ # Cast from unicode - if isinstance(ciphertext, six.text_type): - ciphertext = ciphertext.encode('utf-8') - if isinstance(key, six.text_type): - key = key.encode('utf-8') + ciphertext = encodeutils.to_utf8(ciphertext) + key = encodeutils.to_utf8(key) ciphertext = base64.urlsafe_b64decode(ciphertext) cypher = AES.new(key, AES.MODE_CBC, ciphertext[:16]) padded = cypher.decrypt(ciphertext[16:]) diff --git a/glance/common/signature_utils.py b/glance/common/signature_utils.py index df5be511..3df28aa5 100644 --- a/glance/common/signature_utils.py +++ b/glance/common/signature_utils.py @@ -29,7 +29,6 @@ import debtcollector from oslo_log import log as logging from oslo_serialization import base64 from oslo_utils import encodeutils -import six from glance.common import exception from glance.i18n import _LE @@ -175,8 +174,7 @@ def verify_signature(context, checksum_hash, image_properties): 'Required image properties for signature verification do not' ' exist. Cannot verify signature.') - if isinstance(checksum_hash, six.text_type): - checksum_hash = checksum_hash.encode('utf-8') + checksum_hash = encodeutils.to_utf8(checksum_hash) signature = get_signature(image_properties[OLD_SIGNATURE]) hash_method = get_hash_method(image_properties[OLD_HASH_METHOD]) diff --git a/glance/common/wsgi.py b/glance/common/wsgi.py index 49acce8d..61e0d564 100644 --- a/glance/common/wsgi.py +++ b/glance/common/wsgi.py @@ -819,8 +819,7 @@ class JSONResponseSerializer(object): def default(self, response, result): response.content_type = 'application/json' body = self.to_json(result) - if isinstance(body, six.text_type): - body = body.encode('utf-8') + body = encodeutils.to_utf8(body) response.body = body diff --git a/glance/tests/unit/common/test_rpc.py b/glance/tests/unit/common/test_rpc.py index a6a83a46..6d5fc6c3 100644 --- a/glance/tests/unit/common/test_rpc.py +++ b/glance/tests/unit/common/test_rpc.py @@ -18,6 +18,7 @@ import datetime from oslo_config import cfg from oslo_serialization import jsonutils +from oslo_utils import encodeutils import routes import six import webob @@ -211,8 +212,7 @@ class TestRPCClient(base.IsolatedUnitTest): def fake_request(self, method, url, body, headers): req = webob.Request.blank(url.path) - if isinstance(body, six.text_type): - body = body.encode('utf-8') + body = encodeutils.to_utf8(body) req.body = body req.method = method diff --git a/requirements.txt b/requirements.txt index 50f0602c..034868c5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,7 +18,7 @@ oslo.config>=3.4.0 # Apache-2.0 oslo.concurrency>=2.3.0 # Apache-2.0 oslo.context>=0.2.0 # Apache-2.0 oslo.service>=1.0.0 # Apache-2.0 -oslo.utils>=3.4.0 # Apache-2.0 +oslo.utils>=3.5.0 # Apache-2.0 stevedore>=1.5.0 # Apache-2.0 futurist>=0.11.0 # Apache-2.0 taskflow>=1.26.0 # Apache-2.0