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 <clay.gerrard@gmail.com>

Change-Id: I1758693c5dd428f9f2157966aac49d97c2c7ab12
Signed-off-by: Thiago da Silva <thiago@redhat.com>
This commit is contained in:
Thiago da Silva 2017-02-28 11:57:10 -05:00
parent 21e593fabe
commit 4d3aa4ea78
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.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:
(<value without crypto meta>, <deserialized crypto meta> 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

View File

@ -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

View File

@ -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):

View File

@ -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(

View File

@ -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):