diff --git a/oslo/messaging/_drivers/common.py b/oslo/messaging/_drivers/common.py index 724f09a21..854e87dc5 100644 --- a/oslo/messaging/_drivers/common.py +++ b/oslo/messaging/_drivers/common.py @@ -33,6 +33,8 @@ _ = lambda s: s LOG = logging.getLogger(__name__) +_EXCEPTIONS_MODULE = 'exceptions' if six.PY2 else 'builtins' + '''RPC Envelope Version. @@ -76,7 +78,7 @@ _exception_opts = [ default=['oslo.messaging.exceptions', 'nova.exception', 'cinder.exception', - 'exceptions', + _EXCEPTIONS_MODULE, ], help='Modules of exceptions that are permitted to be recreated' '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 # 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) try: diff --git a/tests/test_exception_serialization.py b/tests/test_exception_serialization.py index 4c06c3a2c..efc4d50fc 100644 --- a/tests/test_exception_serialization.py +++ b/tests/test_exception_serialization.py @@ -25,6 +25,8 @@ from tests import utils as test_utils load_tests = testscenarios.load_tests_apply_scenarios +EXCEPTIONS_MODULE = 'exceptions' if six.PY2 else 'builtins' + class NovaStyleException(Exception): @@ -74,7 +76,7 @@ class SerializeRemoteExceptionTestCase(test_utils.BaseTestCase): args=['test'], kwargs={}, clsname='Exception', - modname='exceptions', + modname=EXCEPTIONS_MODULE, msg='test')), ('nova_style', dict(cls=NovaStyleException, args=[], @@ -157,7 +159,7 @@ class DeserializeRemoteExceptionTestCase(test_utils.BaseTestCase): ('bog_standard', dict(allowed=_standard_allowed, clsname='Exception', - modname='exceptions', + modname=EXCEPTIONS_MODULE, cls=Exception, args=['test'], kwargs={}, @@ -206,9 +208,9 @@ class DeserializeRemoteExceptionTestCase(test_utils.BaseTestCase): args=[], kwargs={}, str=("Remote error: NovaStyleException test\n" - "[u'traceback\\ntraceback\\n']."), + "[%r]." % u'traceback\ntraceback\n'), msg=("Remote error: NovaStyleException test\n" - "[u'traceback\\ntraceback\\n']."), + "[%r]." % u'traceback\ntraceback\n'), remote_name='RemoteError', remote_args=(), remote_kwargs={'exc_type': 'NovaStyleException', @@ -222,9 +224,9 @@ class DeserializeRemoteExceptionTestCase(test_utils.BaseTestCase): args=[], kwargs={}, str=("Remote error: Exception test\n" - "[u'traceback\\ntraceback\\n']."), + "[%r]." % u'traceback\ntraceback\n'), msg=("Remote error: Exception test\n" - "[u'traceback\\ntraceback\\n']."), + "[%r]." % u'traceback\ntraceback\n'), remote_name='RemoteError', remote_args=(), remote_kwargs={'exc_type': 'Exception', @@ -233,14 +235,14 @@ class DeserializeRemoteExceptionTestCase(test_utils.BaseTestCase): ('unknown_exception', dict(allowed=[], clsname='FarcicalError', - modname='exceptions', + modname=EXCEPTIONS_MODULE, cls=messaging.RemoteError, args=[], kwargs={}, str=("Remote error: FarcicalError test\n" - "[u'traceback\\ntraceback\\n']."), + "[%r]." % u'traceback\ntraceback\n'), msg=("Remote error: FarcicalError test\n" - "[u'traceback\\ntraceback\\n']."), + "[%r]." % u'traceback\ntraceback\n'), remote_name='RemoteError', remote_args=(), remote_kwargs={'exc_type': 'FarcicalError', @@ -249,14 +251,14 @@ class DeserializeRemoteExceptionTestCase(test_utils.BaseTestCase): ('unknown_kwarg', dict(allowed=[], clsname='Exception', - modname='exceptions', + modname=EXCEPTIONS_MODULE, cls=messaging.RemoteError, args=[], kwargs={'foobar': 'blaa'}, str=("Remote error: Exception test\n" - "[u'traceback\\ntraceback\\n']."), + "[%r]." % u'traceback\ntraceback\n'), msg=("Remote error: Exception test\n" - "[u'traceback\\ntraceback\\n']."), + "[%r]." % u'traceback\ntraceback\n'), remote_name='RemoteError', remote_args=(), remote_kwargs={'exc_type': 'Exception', @@ -265,14 +267,14 @@ class DeserializeRemoteExceptionTestCase(test_utils.BaseTestCase): ('system_exit', dict(allowed=[], clsname='SystemExit', - modname='exceptions', + modname=EXCEPTIONS_MODULE, cls=messaging.RemoteError, args=[], kwargs={}, str=("Remote error: SystemExit test\n" - "[u'traceback\\ntraceback\\n']."), + "[%r]." % u'traceback\ntraceback\n'), msg=("Remote error: SystemExit test\n" - "[u'traceback\\ntraceback\\n']."), + "[%r]." % u'traceback\ntraceback\n'), remote_name='RemoteError', remote_args=(), remote_kwargs={'exc_type': 'SystemExit',