Merge "refactor some common code from crypto"

This commit is contained in:
Jenkins 2017-03-14 17:22:30 +00:00 committed by Gerrit Code Review
commit 2dd8357269
5 changed files with 32 additions and 18 deletions

View File

@ -28,6 +28,7 @@ from swift.common.exceptions import EncryptionException
from swift.common.swob import HTTPInternalServerError from swift.common.swob import HTTPInternalServerError
from swift.common.utils import get_logger from swift.common.utils import get_logger
from swift.common.wsgi import WSGIContext from swift.common.wsgi import WSGIContext
from cgi import parse_header
CRYPTO_KEY_CALLBACK = 'swift.callback.fetch_crypto_keys' CRYPTO_KEY_CALLBACK = 'swift.callback.fetch_crypto_keys'
@ -270,15 +271,8 @@ def extract_crypto_meta(value):
:return: a tuple of the form: :return: a tuple of the form:
(<value without crypto meta>, <deserialized crypto meta> or None) (<value without crypto meta>, <deserialized crypto meta> or None)
""" """
crypto_meta = None swift_meta = None
# we only attempt to extract crypto meta from values that we know were value, meta = parse_header(value)
# encrypted and base64-encoded, or from etag values, so it's safe to split if 'swift_meta' in meta:
# on ';' even if it turns out that the value was an unencrypted etag swift_meta = load_crypto_meta(meta['swift_meta'])
parts = value.split(';') return value, swift_meta
if len(parts) == 2:
value, param = parts
crypto_meta_tag = 'swift_meta='
if param.strip().startswith(crypto_meta_tag):
param = param.strip()[len(crypto_meta_tag):]
crypto_meta = load_crypto_meta(param)
return value, crypto_meta

View File

@ -379,12 +379,6 @@ class DecrypterContContext(BaseDecrypterContext):
return app_resp return app_resp
def update_content_length(self, new_total_len):
self._response_headers = [
(h, v) for h, v in self._response_headers
if h.lower() != 'content-length']
self._response_headers.append(('Content-Length', str(new_total_len)))
def process_json_resp(self, key, resp_iter): def process_json_resp(self, key, resp_iter):
""" """
Parses json body listing and decrypt encrypted entries. Updates Parses json body listing and decrypt encrypted entries. Updates

View File

@ -1076,6 +1076,12 @@ class WSGIContext(object):
return val return val
return None return None
def update_content_length(self, new_total_len):
self._response_headers = [
(h, v) for h, v in self._response_headers
if h.lower() != 'content-length']
self._response_headers.append(('Content-Length', str(new_total_len)))
def make_env(env, method=None, path=None, agent='Swift', query_string=None, def make_env(env, method=None, path=None, agent='Swift', query_string=None,
swift_source=None): swift_source=None):

View File

@ -244,6 +244,11 @@ class TestModuleMethods(unittest.TestCase):
self.assertEqual('abc', val) self.assertEqual('abc', val)
self.assertIsNone(meta) self.assertIsNone(meta)
val, meta = crypto_utils.extract_crypto_meta(
'abc; swift_meta=%s; foo=bar' % self.serialized_meta_with_key)
self.assertEqual('abc', val)
self.assertDictEqual(self.meta_with_key, meta)
def test_append_then_extract_crypto_meta(self): def test_append_then_extract_crypto_meta(self):
val = 'abc' val = 'abc'
actual = crypto_utils.extract_crypto_meta( actual = crypto_utils.extract_crypto_meta(

View File

@ -1273,6 +1273,21 @@ class TestWSGIContext(unittest.TestCase):
iterable.close() iterable.close()
self.assertRaises(StopIteration, iterator.next) self.assertRaises(StopIteration, iterator.next)
def test_update_content_length(self):
statuses = ['200 Ok']
def app(env, start_response):
start_response(statuses.pop(0), [('Content-Length', '30')])
yield 'Ok\n'
wc = wsgi.WSGIContext(app)
r = Request.blank('/')
it = wc._app_call(r.environ)
wc.update_content_length(35)
self.assertEqual(wc._response_status, '200 Ok')
self.assertEqual(''.join(it), 'Ok\n')
self.assertEqual(wc._response_headers, [('Content-Length', '35')])
class TestPipelineWrapper(unittest.TestCase): class TestPipelineWrapper(unittest.TestCase):