actions: Use separate IPC for communicating results

Currently privileged actions use stdout for returning the results. If any of the
sub-processes accidentally output to stdout, decoding errors occur. Prevent this
by opening a pipe to the privileged action and returning the output in that
pipe.

Tests:

- Run unit tests
- Functional tests for other apps pass

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2022-10-08 18:50:37 -04:00
1 parent 585092ca63
commit 6f5410931e
7 files changed
+190 -55

No files matched your search

+14 -3
View File
@@ -8,6 +8,7 @@ import json
import logging
import os
import sys
import traceback
import typing
import plinth.log
@@ -26,16 +27,25 @@ def main():
parser = argparse.ArgumentParser()
parser.add_argument('module', help='Module to trigger action in')
parser.add_argument('action', help='Action to trigger in module')
parser.add_argument('--write-fd', type=int, default=1,
help='File descriptor to write output to')
parser.add_argument('--no-args', default=False, action='store_true',
help='Do not read arguments from stdin')
args = parser.parse_args()
try:
try:
arguments = json.loads(sys.stdin.read())
arguments = {'args': [], 'kwargs': {}}
if not args.no_args:
input_ = sys.stdin.read()
if input_:
arguments = json.loads(input_)
except json.JSONDecodeError as exception:
raise SyntaxError('Arguments on stdin not JSON.') from exception
return_value = _call(args.module, args.action, arguments)
print(json.dumps(return_value))
with os.fdopen(args.write_fd, 'w') as write_file_handle:
write_file_handle.write(json.dumps(return_value))
except PermissionError as exception:
logger.error(exception.args[0])
sys.exit(EXIT_PERM)
@@ -91,7 +101,8 @@ def _call(module_name, action_name, arguments):
'exception': {
'module': type(exception).__module__,
'name': type(exception).__name__,
'args': exception.args
'args': exception.args,
'traceback': traceback.format_tb(exception.__traceback__)
}
}