actions: Add a decorator for marking superuser actions

Any privileged action (a method) can be marked as such with the new decorator. A
call to the method will be serialized into a sudo call (or later into a D-Bus
call). The method arguments are turned to JSON and method is called as
superuser. Arguments are de-serialized and are verified for type before the
actual call as superuser. Return values are serialized and returned where they
are de-serialized. Exceptions are also serialized and de-serialized.

The method must have be strictly typed and should not have keyword-only
arguments. Currently supported types are int, float, str, dict/Dict, list/List
and Optional.

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-06-20 18:16:47 -04:00
1 parent 3ec086411b
commit 4fed6921d6
6 files changed
+583 -8

No files matched your search

+22 -6
View File
@@ -8,6 +8,7 @@ import importlib
import logging
import pathlib
import re
from typing import Optional
import django
@@ -83,12 +84,27 @@ def get_modules_to_load():
modules = []
for file_ in files:
with file_.open() as file_handle:
for line in file_handle:
line = re.sub('#.*', '', line)
line = line.strip()
if line:
modules.append(line)
module = _get_module_import_paths_from_file(file_)
if module:
modules.append(module)
_modules_to_load = modules
return modules
def get_module_import_path(module_name: str) -> str:
"""Return the import path for a module."""
file_path = pathlib.Path(cfg.config_dir) / 'modules-enabled' / module_name
return _get_module_import_paths_from_file(file_path)
def _get_module_import_paths_from_file(file_path: str) -> Optional[str]:
"""Read a module's import path from a file."""
with file_path.open() as file_handle:
for line in file_handle:
line = re.sub('#.*', '', line)
line = line.strip()
if line:
return line
return None