Merge "Fix exception deserialiation on Python 3"

This commit is contained in:
Jenkins 2014-01-08 18:35:26 +00:00 committed by Gerrit Code Review
commit 04cb7223a5
2 changed files with 21 additions and 17 deletions

View File

@ -33,6 +33,8 @@ _ = lambda s: s
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
_EXCEPTIONS_MODULE = 'exceptions' if six.PY2 else 'builtins'
'''RPC Envelope Version. '''RPC Envelope Version.
@ -76,7 +78,7 @@ _exception_opts = [
default=['oslo.messaging.exceptions', default=['oslo.messaging.exceptions',
'nova.exception', 'nova.exception',
'cinder.exception', 'cinder.exception',
'exceptions', _EXCEPTIONS_MODULE,
], ],
help='Modules of exceptions that are permitted to be recreated' help='Modules of exceptions that are permitted to be recreated'
'upon receiving exception data from an rpc call.'), 'upon receiving exception data from an rpc call.'),
@ -337,7 +339,7 @@ def deserialize_remote_exception(data, allowed_remote_exmods):
# NOTE(ameade): We DO NOT want to allow just any module to be imported, in # NOTE(ameade): We DO NOT want to allow just any module to be imported, in
# order to prevent arbitrary code execution. # order to prevent arbitrary code execution.
if module != 'exceptions' and module not in allowed_remote_exmods: if module != _EXCEPTIONS_MODULE and module not in allowed_remote_exmods:
return messaging.RemoteError(name, failure.get('message'), trace) return messaging.RemoteError(name, failure.get('message'), trace)
try: try:

View File

@ -25,6 +25,8 @@ from tests import utils as test_utils
load_tests = testscenarios.load_tests_apply_scenarios load_tests = testscenarios.load_tests_apply_scenarios
EXCEPTIONS_MODULE = 'exceptions' if six.PY2 else 'builtins'
class NovaStyleException(Exception): class NovaStyleException(Exception):
@ -74,7 +76,7 @@ class SerializeRemoteExceptionTestCase(test_utils.BaseTestCase):
args=['test'], args=['test'],
kwargs={}, kwargs={},
clsname='Exception', clsname='Exception',
modname='exceptions', modname=EXCEPTIONS_MODULE,
msg='test')), msg='test')),
('nova_style', dict(cls=NovaStyleException, ('nova_style', dict(cls=NovaStyleException,
args=[], args=[],
@ -157,7 +159,7 @@ class DeserializeRemoteExceptionTestCase(test_utils.BaseTestCase):
('bog_standard', ('bog_standard',
dict(allowed=_standard_allowed, dict(allowed=_standard_allowed,
clsname='Exception', clsname='Exception',
modname='exceptions', modname=EXCEPTIONS_MODULE,
cls=Exception, cls=Exception,
args=['test'], args=['test'],
kwargs={}, kwargs={},
@ -206,9 +208,9 @@ class DeserializeRemoteExceptionTestCase(test_utils.BaseTestCase):
args=[], args=[],
kwargs={}, kwargs={},
str=("Remote error: NovaStyleException test\n" str=("Remote error: NovaStyleException test\n"
"[u'traceback\\ntraceback\\n']."), "[%r]." % u'traceback\ntraceback\n'),
msg=("Remote error: NovaStyleException test\n" msg=("Remote error: NovaStyleException test\n"
"[u'traceback\\ntraceback\\n']."), "[%r]." % u'traceback\ntraceback\n'),
remote_name='RemoteError', remote_name='RemoteError',
remote_args=(), remote_args=(),
remote_kwargs={'exc_type': 'NovaStyleException', remote_kwargs={'exc_type': 'NovaStyleException',
@ -222,9 +224,9 @@ class DeserializeRemoteExceptionTestCase(test_utils.BaseTestCase):
args=[], args=[],
kwargs={}, kwargs={},
str=("Remote error: Exception test\n" str=("Remote error: Exception test\n"
"[u'traceback\\ntraceback\\n']."), "[%r]." % u'traceback\ntraceback\n'),
msg=("Remote error: Exception test\n" msg=("Remote error: Exception test\n"
"[u'traceback\\ntraceback\\n']."), "[%r]." % u'traceback\ntraceback\n'),
remote_name='RemoteError', remote_name='RemoteError',
remote_args=(), remote_args=(),
remote_kwargs={'exc_type': 'Exception', remote_kwargs={'exc_type': 'Exception',
@ -233,14 +235,14 @@ class DeserializeRemoteExceptionTestCase(test_utils.BaseTestCase):
('unknown_exception', ('unknown_exception',
dict(allowed=[], dict(allowed=[],
clsname='FarcicalError', clsname='FarcicalError',
modname='exceptions', modname=EXCEPTIONS_MODULE,
cls=messaging.RemoteError, cls=messaging.RemoteError,
args=[], args=[],
kwargs={}, kwargs={},
str=("Remote error: FarcicalError test\n" str=("Remote error: FarcicalError test\n"
"[u'traceback\\ntraceback\\n']."), "[%r]." % u'traceback\ntraceback\n'),
msg=("Remote error: FarcicalError test\n" msg=("Remote error: FarcicalError test\n"
"[u'traceback\\ntraceback\\n']."), "[%r]." % u'traceback\ntraceback\n'),
remote_name='RemoteError', remote_name='RemoteError',
remote_args=(), remote_args=(),
remote_kwargs={'exc_type': 'FarcicalError', remote_kwargs={'exc_type': 'FarcicalError',
@ -249,14 +251,14 @@ class DeserializeRemoteExceptionTestCase(test_utils.BaseTestCase):
('unknown_kwarg', ('unknown_kwarg',
dict(allowed=[], dict(allowed=[],
clsname='Exception', clsname='Exception',
modname='exceptions', modname=EXCEPTIONS_MODULE,
cls=messaging.RemoteError, cls=messaging.RemoteError,
args=[], args=[],
kwargs={'foobar': 'blaa'}, kwargs={'foobar': 'blaa'},
str=("Remote error: Exception test\n" str=("Remote error: Exception test\n"
"[u'traceback\\ntraceback\\n']."), "[%r]." % u'traceback\ntraceback\n'),
msg=("Remote error: Exception test\n" msg=("Remote error: Exception test\n"
"[u'traceback\\ntraceback\\n']."), "[%r]." % u'traceback\ntraceback\n'),
remote_name='RemoteError', remote_name='RemoteError',
remote_args=(), remote_args=(),
remote_kwargs={'exc_type': 'Exception', remote_kwargs={'exc_type': 'Exception',
@ -265,14 +267,14 @@ class DeserializeRemoteExceptionTestCase(test_utils.BaseTestCase):
('system_exit', ('system_exit',
dict(allowed=[], dict(allowed=[],
clsname='SystemExit', clsname='SystemExit',
modname='exceptions', modname=EXCEPTIONS_MODULE,
cls=messaging.RemoteError, cls=messaging.RemoteError,
args=[], args=[],
kwargs={}, kwargs={},
str=("Remote error: SystemExit test\n" str=("Remote error: SystemExit test\n"
"[u'traceback\\ntraceback\\n']."), "[%r]." % u'traceback\ntraceback\n'),
msg=("Remote error: SystemExit test\n" msg=("Remote error: SystemExit test\n"
"[u'traceback\\ntraceback\\n']."), "[%r]." % u'traceback\ntraceback\n'),
remote_name='RemoteError', remote_name='RemoteError',
remote_args=(), remote_args=(),
remote_kwargs={'exc_type': 'SystemExit', remote_kwargs={'exc_type': 'SystemExit',