Merge "Reuse encodeutils.to_utf8()"

This commit is contained in:
Jenkins 2016-02-23 22:56:15 +00:00 committed by Gerrit Code Review
commit b85a44f1b3
5 changed files with 10 additions and 16 deletions

View File

@ -24,6 +24,7 @@ import base64
from Crypto.Cipher import AES from Crypto.Cipher import AES
from Crypto import Random from Crypto import Random
from Crypto.Random import random from Crypto.Random import random
from oslo_utils import encodeutils
import six import six
# NOTE(jokke): simplified transition to py3, behaves like py2 xrange # NOTE(jokke): simplified transition to py3, behaves like py2 xrange
from six.moves import range 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 # We use chr(0) as a delimiter between text and padding
return text + b'\0' + pad return text + b'\0' + pad
if isinstance(plaintext, six.text_type): plaintext = encodeutils.to_utf8(plaintext)
plaintext = plaintext.encode('utf-8') key = encodeutils.to_utf8(key)
if isinstance(key, six.text_type):
key = key.encode('utf-8')
# random initial 16 bytes for CBC # random initial 16 bytes for CBC
init_vector = Random.get_random_bytes(16) init_vector = Random.get_random_bytes(16)
cypher = AES.new(key, AES.MODE_CBC, init_vector) cypher = AES.new(key, AES.MODE_CBC, init_vector)
@ -76,10 +75,8 @@ def urlsafe_decrypt(key, ciphertext):
:returns: Resulting plaintext :returns: Resulting plaintext
""" """
# Cast from unicode # Cast from unicode
if isinstance(ciphertext, six.text_type): ciphertext = encodeutils.to_utf8(ciphertext)
ciphertext = ciphertext.encode('utf-8') key = encodeutils.to_utf8(key)
if isinstance(key, six.text_type):
key = key.encode('utf-8')
ciphertext = base64.urlsafe_b64decode(ciphertext) ciphertext = base64.urlsafe_b64decode(ciphertext)
cypher = AES.new(key, AES.MODE_CBC, ciphertext[:16]) cypher = AES.new(key, AES.MODE_CBC, ciphertext[:16])
padded = cypher.decrypt(ciphertext[16:]) padded = cypher.decrypt(ciphertext[16:])

View File

@ -29,7 +29,6 @@ import debtcollector
from oslo_log import log as logging from oslo_log import log as logging
from oslo_serialization import base64 from oslo_serialization import base64
from oslo_utils import encodeutils from oslo_utils import encodeutils
import six
from glance.common import exception from glance.common import exception
from glance.i18n import _LE 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' 'Required image properties for signature verification do not'
' exist. Cannot verify signature.') ' exist. Cannot verify signature.')
if isinstance(checksum_hash, six.text_type): checksum_hash = encodeutils.to_utf8(checksum_hash)
checksum_hash = checksum_hash.encode('utf-8')
signature = get_signature(image_properties[OLD_SIGNATURE]) signature = get_signature(image_properties[OLD_SIGNATURE])
hash_method = get_hash_method(image_properties[OLD_HASH_METHOD]) hash_method = get_hash_method(image_properties[OLD_HASH_METHOD])

View File

@ -819,8 +819,7 @@ class JSONResponseSerializer(object):
def default(self, response, result): def default(self, response, result):
response.content_type = 'application/json' response.content_type = 'application/json'
body = self.to_json(result) body = self.to_json(result)
if isinstance(body, six.text_type): body = encodeutils.to_utf8(body)
body = body.encode('utf-8')
response.body = body response.body = body

View File

@ -18,6 +18,7 @@ import datetime
from oslo_config import cfg from oslo_config import cfg
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
from oslo_utils import encodeutils
import routes import routes
import six import six
import webob import webob
@ -211,8 +212,7 @@ class TestRPCClient(base.IsolatedUnitTest):
def fake_request(self, method, url, body, headers): def fake_request(self, method, url, body, headers):
req = webob.Request.blank(url.path) req = webob.Request.blank(url.path)
if isinstance(body, six.text_type): body = encodeutils.to_utf8(body)
body = body.encode('utf-8')
req.body = body req.body = body
req.method = method req.method = method

View File

@ -18,7 +18,7 @@ oslo.config>=3.4.0 # Apache-2.0
oslo.concurrency>=2.3.0 # Apache-2.0 oslo.concurrency>=2.3.0 # Apache-2.0
oslo.context>=0.2.0 # Apache-2.0 oslo.context>=0.2.0 # Apache-2.0
oslo.service>=1.0.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 stevedore>=1.5.0 # Apache-2.0
futurist>=0.11.0 # Apache-2.0 futurist>=0.11.0 # Apache-2.0
taskflow>=1.26.0 # Apache-2.0 taskflow>=1.26.0 # Apache-2.0