From 4d3aa4ea78d1867acc8c7520790a90c748b3285c Mon Sep 17 00:00:00 2001 From: Thiago da Silva Date: Tue, 28 Feb 2017 11:57:10 -0500 Subject: [PATCH] refactor some common code from crypto This patch moves some code from the crypto files to a more common modules that will be used by symlinks Co-Authored-By: Clay Gerrard Change-Id: I1758693c5dd428f9f2157966aac49d97c2c7ab12 Signed-off-by: Thiago da Silva --- swift/common/middleware/crypto/crypto_utils.py | 18 ++++++------------ swift/common/middleware/crypto/decrypter.py | 6 ------ swift/common/wsgi.py | 6 ++++++ .../middleware/crypto/test_crypto_utils.py | 5 +++++ test/unit/common/test_wsgi.py | 15 +++++++++++++++ 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/swift/common/middleware/crypto/crypto_utils.py b/swift/common/middleware/crypto/crypto_utils.py index ebcfb2d70c..0c4252cdea 100644 --- a/swift/common/middleware/crypto/crypto_utils.py +++ b/swift/common/middleware/crypto/crypto_utils.py @@ -28,6 +28,7 @@ from swift.common.exceptions import EncryptionException from swift.common.swob import HTTPInternalServerError from swift.common.utils import get_logger from swift.common.wsgi import WSGIContext +from cgi import parse_header CRYPTO_KEY_CALLBACK = 'swift.callback.fetch_crypto_keys' @@ -270,15 +271,8 @@ def extract_crypto_meta(value): :return: a tuple of the form: (, or None) """ - crypto_meta = None - # we only attempt to extract crypto meta from values that we know were - # encrypted and base64-encoded, or from etag values, so it's safe to split - # on ';' even if it turns out that the value was an unencrypted etag - parts = value.split(';') - 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 + swift_meta = None + value, meta = parse_header(value) + if 'swift_meta' in meta: + swift_meta = load_crypto_meta(meta['swift_meta']) + return value, swift_meta diff --git a/swift/common/middleware/crypto/decrypter.py b/swift/common/middleware/crypto/decrypter.py index 6fa950860c..4965122873 100644 --- a/swift/common/middleware/crypto/decrypter.py +++ b/swift/common/middleware/crypto/decrypter.py @@ -379,12 +379,6 @@ class DecrypterContContext(BaseDecrypterContext): 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): """ Parses json body listing and decrypt encrypted entries. Updates diff --git a/swift/common/wsgi.py b/swift/common/wsgi.py index 4432f32efd..7142752f20 100644 --- a/swift/common/wsgi.py +++ b/swift/common/wsgi.py @@ -1076,6 +1076,12 @@ class WSGIContext(object): return val 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, swift_source=None): diff --git a/test/unit/common/middleware/crypto/test_crypto_utils.py b/test/unit/common/middleware/crypto/test_crypto_utils.py index 56aca2ea0b..b3d60cc54b 100644 --- a/test/unit/common/middleware/crypto/test_crypto_utils.py +++ b/test/unit/common/middleware/crypto/test_crypto_utils.py @@ -244,6 +244,11 @@ class TestModuleMethods(unittest.TestCase): self.assertEqual('abc', val) 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): val = 'abc' actual = crypto_utils.extract_crypto_meta( diff --git a/test/unit/common/test_wsgi.py b/test/unit/common/test_wsgi.py index 93e7725cda..374cbdad7a 100644 --- a/test/unit/common/test_wsgi.py +++ b/test/unit/common/test_wsgi.py @@ -1273,6 +1273,21 @@ class TestWSGIContext(unittest.TestCase): iterable.close() 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):