issue #615: use FileService for target->controll file transfers

pull/618/head
David Wilson 5 years ago
parent 8bac1cf368
commit 5af6c9b26f

@ -950,11 +950,12 @@ class Connection(ansible.plugins.connection.ConnectionBase):
:param str out_path:
Local filesystem path to write.
"""
output = self.get_chain().call(
ansible_mitogen.target.read_path,
mitogen.utils.cast(in_path),
self._connect()
ansible_mitogen.target.transfer_file(
context=self.context,
in_path=in_path,
out_path=out_path
)
ansible_mitogen.target.write_path(out_path, output)
def put_data(self, out_path, data, mode=None, utimes=None):
"""

@ -395,6 +395,12 @@ else:
return _partition(s, sep, s.find) or (s, '', '')
def _has_parent_authority(context_id):
return (
(context_id == mitogen.context_id) or
(context_id in mitogen.parent_ids)
)
def has_parent_authority(msg, _stream=None):
"""
Policy function for use with :class:`Receiver` and
@ -403,8 +409,7 @@ def has_parent_authority(msg, _stream=None):
<Stream.auth_id>` has been set to that of a parent context or the current
context.
"""
return (msg.auth_id == mitogen.context_id or
msg.auth_id in mitogen.parent_ids)
return _has_parent_authority(msg.auth_id)
def _signals(obj, signal):

@ -1023,7 +1023,11 @@ class FileService(Service):
:raises Error:
Unregistered path, or Sender did not match requestee context.
"""
if path not in self._paths and not self._prefix_is_authorized(path):
if (
(path not in self._paths) and
(not self._prefix_is_authorized(path)) and
(not mitogen.core._has_parent_authority(msg.auth_id))
):
msg.reply(mitogen.core.CallError(
Error(self.unregistered_msg % (path,))
))

@ -99,6 +99,21 @@ class OptionalIntTest(testlib.TestCase):
self.assertEquals(None, self.func({1:2}))
class FetchFileTest(ConnectionMixin, testlib.TestCase):
def test_success(self):
with tempfile.NamedTemporaryFile(prefix='mitotest') as ifp, \
tempfile.NamedTemporaryFile(prefix='mitotest') as ofp:
ifp.write('x' * (1048576 * 4))
ifp.flush()
ifp.seek(0)
self.conn.fetch_file(ifp.name, ofp.name)
# transfer_file() uses os.rename rather than direct data overwrite,
# so we must reopen.
with open(ofp.name, 'rb') as fp:
self.assertEquals(ifp.read(), fp.read())
class PutDataTest(ConnectionMixin, testlib.TestCase):
def test_out_path(self):
path = tempfile.mktemp(prefix='mitotest')

Loading…
Cancel
Save