Merge "rename utils function less like stdlib"

This commit is contained in:
Zuul 2017-12-15 02:33:20 +00:00 committed by Gerrit Code Review
commit f925d31599
3 changed files with 21 additions and 16 deletions

View File

@ -18,7 +18,7 @@ import os
from swift.common.middleware.crypto.crypto_utils import CRYPTO_KEY_CALLBACK from swift.common.middleware.crypto.crypto_utils import CRYPTO_KEY_CALLBACK
from swift.common.swob import Request, HTTPException from swift.common.swob import Request, HTTPException
from swift.common.utils import readconf, base64decode from swift.common.utils import readconf, strict_b64decode
from swift.common.wsgi import WSGIContext from swift.common.wsgi import WSGIContext
@ -137,8 +137,8 @@ class KeyMaster(object):
conf = readconf(self.keymaster_config_path, 'keymaster') conf = readconf(self.keymaster_config_path, 'keymaster')
b64_root_secret = conf.get('encryption_root_secret') b64_root_secret = conf.get('encryption_root_secret')
try: try:
binary_root_secret = base64decode(b64_root_secret, binary_root_secret = strict_b64decode(b64_root_secret,
allow_line_breaks=True) allow_line_breaks=True)
if len(binary_root_secret) < 32: if len(binary_root_secret) < 32:
raise ValueError raise ValueError
return binary_root_secret return binary_root_secret

View File

@ -4343,7 +4343,7 @@ def safe_json_loads(value):
return None return None
def base64decode(value, allow_line_breaks=False): def strict_b64decode(value, allow_line_breaks=False):
''' '''
Validate and decode Base64-encoded data. Validate and decode Base64-encoded data.

View File

@ -38,6 +38,7 @@ import string
import sys import sys
import json import json
import math import math
import inspect
import six import six
from six import BytesIO, StringIO from six import BytesIO, StringIO
@ -3895,7 +3896,7 @@ cluster_dfw1 = http://dfw1.host/v1/
self.fail('Invalid results from pure function:\n%s' % self.fail('Invalid results from pure function:\n%s' %
'\n'.join(failures)) '\n'.join(failures))
def test_base64decode(self): def test_strict_b64decode(self):
expectations = { expectations = {
None: ValueError, None: ValueError,
0: ValueError, 0: ValueError,
@ -3920,19 +3921,23 @@ cluster_dfw1 = http://dfw1.host/v1/
failures = [] failures = []
for value, expected in expectations.items(): for value, expected in expectations.items():
if expected is ValueError: try:
try: result = utils.strict_b64decode(value)
result = utils.base64decode(value) except Exception as e:
except ValueError: if inspect.isclass(expected) and issubclass(
pass expected, Exception):
if not isinstance(e, expected):
failures.append('%r raised %r (expected to raise %r)' %
(value, e, expected))
else: else:
failures.append('%r => %r (expected to raise ValueError)' % failures.append('%r raised %r (expected to return %r)' %
(value, result)) (value, e, expected))
else: else:
try: if inspect.isclass(expected) and issubclass(
result = utils.base64decode(value) expected, Exception):
self.assertEqual(expected, result) failures.append('%r => %r (expected to raise %r)' %
except AssertionError: (value, result, expected))
elif result != expected:
failures.append('%r => %r (expected %r)' % ( failures.append('%r => %r (expected %r)' % (
value, result, expected)) value, result, expected))
if failures: if failures: