From 2ee0e070374982dc13b0bd3a759ec35a102e629d Mon Sep 17 00:00:00 2001 From: David Wilson Date: Fri, 9 Aug 2019 17:10:04 +0100 Subject: [PATCH] core: MitogenProtocol.is_privileged was not set in children Follow the previous unidirectional routing fix, now errors are occurring where they should not. --- mitogen/core.py | 35 ++++++++++++++++++++++++++--------- mitogen/unix.py | 3 +-- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/mitogen/core.py b/mitogen/core.py index c228079d..a14286f9 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -1937,18 +1937,28 @@ class MitogenProtocol(Protocol): :class:`Protocol` implementing mitogen's :ref:`stream protocol `. """ - #: If not :data:`None`, :class:`Router` stamps this into - #: :attr:`Message.auth_id` of every message received on this stream. - auth_id = None - #: If not :data:`False`, indicates the stream has :attr:`auth_id` set and #: its value is the same as :data:`mitogen.context_id` or appears in #: :data:`mitogen.parent_ids`. is_privileged = False - def __init__(self, router, remote_id): + def __init__(self, router, remote_id, auth_id=None, + local_id=None, parent_ids=None): self._router = router self.remote_id = remote_id + #: If not :data:`None`, :class:`Router` stamps this into + #: :attr:`Message.auth_id` of every message received on this stream. + self.auth_id = auth_id + + if parent_ids is None: + parent_ids = mitogen.parent_ids + if local_id is None: + local_id = mitogen.context_id + + self.is_privileged = ( + (remote_id in parent_ids) or + auth_id in ([local_id] + parent_ids) + ) self.sent_modules = set(['mitogen', 'mitogen.core']) self._input_buf = collections.deque() self._input_buf_len = 0 @@ -2800,8 +2810,8 @@ class Router(object): broker_exit_msg = 'Broker has exitted' no_route_msg = 'no route to %r, my ID is %r' unidirectional_msg = ( - 'routing mode prevents forward of message from context %d via ' - 'context %d' + 'routing mode prevents forward of message from context %d to ' + 'context %d via context %d' ) def __init__(self, broker): @@ -3152,7 +3162,9 @@ class Router(object): (in_stream.protocol.is_privileged or out_stream.protocol.is_privileged): self._maybe_send_dead(msg, self.unidirectional_msg, - in_stream.protocol.remote_id, out_stream.protocol.remote_id) + in_stream.protocol.remote_id, + out_stream.protocol.remote_id, + mitogen.context_id) return out_stream.protocol._send(msg) @@ -3641,7 +3653,12 @@ class ExternalContext(object): os.close(in_fd) out_fp = os.fdopen(os.dup(self.config.get('out_fd', 1)), 'wb', 0) - self.stream = MitogenProtocol.build_stream(self.router, parent_id) + self.stream = MitogenProtocol.build_stream( + self.router, + parent_id, + local_id=self.config['context_id'], + parent_ids=self.config['parent_ids'] + ) self.stream.accept(in_fp, out_fp) self.stream.name = 'parent' self.stream.receive_side.keep_alive = False diff --git a/mitogen/unix.py b/mitogen/unix.py index 645b061d..1af1c0ec 100644 --- a/mitogen/unix.py +++ b/mitogen/unix.py @@ -162,10 +162,9 @@ class Listener(mitogen.core.Protocol): stream = mitogen.core.MitogenProtocol.build_stream( router=self._router, remote_id=context_id, + auth_id=mitogen.context_id, ) stream.name = u'unix_client.%d' % (pid,) - stream.protocol.auth_id = mitogen.context_id - stream.protocol.is_privileged = True stream.accept(sock, sock) LOG.debug('listener: accepted connection from PID %d: %s', pid, stream.name)