Skip to content

Commit

Permalink
Add support for run_service(..., filter_esc=True) in a VM
Browse files Browse the repository at this point in the history
Since qrexec-client-vm got support for filtering escape characters, use
it here too.

QubesOS/qubes-issues#5322

(cherry picked from commit 1fcb031)
  • Loading branch information
marmarek committed Oct 9, 2019
1 parent f867856 commit a7f8ae0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
15 changes: 10 additions & 5 deletions qubesadmin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,14 +678,16 @@ def run_service(self, dest, service, filter_esc=False, user=None,
:param bool wait: wait for process to finish
:rtype: subprocess.Popen
"""
if filter_esc:
raise NotImplementedError(
'filter_esc not implemented for calls from VM')
if user:
raise ValueError(
'non-default user not possible for calls from VM')
if not wait and localcmd:
raise ValueError('wait=False incompatible with localcmd')
qrexec_opts = []
if filter_esc:
qrexec_opts.extend(['-t'])
if filter_esc or os.isatty(sys.stderr.fileno()):
qrexec_opts.extend(['-T'])
if not wait:
# qrexec-client-vm can only request service calls, which are
# started using MSG_EXEC_CMDLINE qrexec protocol message; this
Expand All @@ -700,6 +702,9 @@ def run_service(self, dest, service, filter_esc=False, user=None,
kwargs.setdefault('stdout', subprocess.PIPE)
kwargs.setdefault('stderr', subprocess.PIPE)
proc = subprocess.Popen(
[qubesadmin.config.QREXEC_CLIENT_VM, dest or '', service] + (
shlex.split(localcmd) if localcmd else []), **kwargs)
[qubesadmin.config.QREXEC_CLIENT_VM] +
qrexec_opts +
[dest or '', service] +
(shlex.split(localcmd) if localcmd else []),
**kwargs)
return proc
16 changes: 12 additions & 4 deletions qubesadmin/tests/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,26 +880,34 @@ def test_003_qubesd_call_payload_stream(self):
])
self.assertEqual(value, b'return-value')

@mock.patch('os.isatty', lambda fd: fd == 2)
def test_010_run_service(self):
self.app.run_service('some-vm', 'service.name')
self.proc_mock.assert_called_once_with([
qubesadmin.config.QREXEC_CLIENT_VM,
'some-vm', 'service.name'],
'-T', 'some-vm', 'service.name'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

@mock.patch('os.isatty', lambda fd: fd == 2)
def test_011_run_service_filter_esc(self):
with self.assertRaises(NotImplementedError):
p = self.app.run_service('some-vm', 'service.name', filter_esc=True)
self.app.run_service('some-vm', 'service.name', filter_esc=True)
self.proc_mock.assert_called_once_with([
qubesadmin.config.QREXEC_CLIENT_VM,
'-t', '-T', 'some-vm', 'service.name'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

@mock.patch('os.isatty', lambda fd: fd == 2)
def test_012_run_service_user(self):
with self.assertRaises(ValueError):
p = self.app.run_service('some-vm', 'service.name', user='user')

@mock.patch('os.isatty', lambda fd: fd == 2)
def test_013_run_service_default_target(self):
self.app.run_service('', 'service.name')
self.proc_mock.assert_called_once_with([
qubesadmin.config.QREXEC_CLIENT_VM,
'', 'service.name'],
'-T', '', 'service.name'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

0 comments on commit a7f8ae0

Please sign in to comment.