diff options
Diffstat (limited to 'kvmd')
-rw-r--r-- | kvmd/apps/__init__.py | 7 | ||||
-rw-r--r-- | kvmd/validators/auth.py | 7 | ||||
-rw-r--r-- | kvmd/validators/basic.py | 29 | ||||
-rw-r--r-- | kvmd/validators/fs.py | 19 |
4 files changed, 56 insertions, 6 deletions
diff --git a/kvmd/apps/__init__.py b/kvmd/apps/__init__.py index 77a9644b..3a653d11 100644 --- a/kvmd/apps/__init__.py +++ b/kvmd/apps/__init__.py @@ -51,9 +51,12 @@ from ..validators.basic import valid_number from ..validators.basic import valid_int_f1 from ..validators.basic import valid_float_f01 +from ..validators.auth import valid_users_list + from ..validators.fs import valid_abs_path from ..validators.fs import valid_abs_path_exists from ..validators.fs import valid_unix_mode +from ..validators.fs import valid_command from ..validators.net import valid_ip_or_host from ..validators.net import valid_port @@ -150,7 +153,7 @@ def _get_config_scheme() -> Dict: }, "auth": { - "internal_users": Option([]), + "internal_users": Option([], type=valid_users_list), "internal_type": Option("htpasswd"), "external_type": Option(""), # "internal": {}, @@ -222,7 +225,7 @@ def _get_config_scheme() -> Dict: "unix": Option("", type=valid_abs_path, only_if="!port", unpack_as="unix_path"), "timeout": Option(2.0, type=valid_float_f01), - "cmd": Option(["/bin/true"]), # TODO: Validator + "cmd": Option(["/bin/true"], type=valid_command), }, }, diff --git a/kvmd/validators/auth.py b/kvmd/validators/auth.py index 7e110162..535ba445 100644 --- a/kvmd/validators/auth.py +++ b/kvmd/validators/auth.py @@ -20,8 +20,11 @@ # ========================================================================== # +from typing import List from typing import Any +from .basic import valid_string_list + from . import check_re_match @@ -30,6 +33,10 @@ def valid_user(arg: Any) -> str: return check_re_match(arg, "username characters", r"^[a-z_][a-z0-9_-]*$") +def valid_users_list(arg: Any) -> List[str]: + return valid_string_list(arg, subval=valid_user, name="users list") + + def valid_passwd(arg: Any) -> str: return check_re_match(arg, "passwd characters", r"^[\x20-\x7e]*\Z$", strip=False, hide=True) diff --git a/kvmd/validators/basic.py b/kvmd/validators/basic.py index e4a2336e..62f9b36d 100644 --- a/kvmd/validators/basic.py +++ b/kvmd/validators/basic.py @@ -20,7 +20,12 @@ # ========================================================================== # +import re + +from typing import List from typing import Type +from typing import Callable +from typing import Optional from typing import Union from typing import Any @@ -71,3 +76,27 @@ def valid_int_f1(arg: Any) -> int: def valid_float_f01(arg: Any) -> float: return float(valid_number(arg, min=0.1, type=float)) + + +def valid_string_list( + arg: Any, + delim: str=r"[,\t ]+", + subval: Optional[Callable[[Any], Any]]=None, + name: str="", +) -> List[str]: + + if not name: + name = "string list" + + if subval is None: + subval = (lambda item: check_not_none_string(item, name + " item")) + + if not isinstance(arg, (list, tuple)): + arg = check_not_none_string(arg, name) + arg = list(filter(None, re.split(delim, arg))) + if subval is not None: + try: + arg = list(map(subval, arg)) + except Exception: + raise ValidatorError("Failed sub-validator on one of the item of %r" % (arg)) + return arg diff --git a/kvmd/validators/fs.py b/kvmd/validators/fs.py index ed895818..505df8e2 100644 --- a/kvmd/validators/fs.py +++ b/kvmd/validators/fs.py @@ -22,17 +22,20 @@ import os +from typing import List from typing import Any from . import raise_error from . import check_not_none_string from .basic import valid_number +from .basic import valid_string_list # ===== -def valid_abs_path(arg: Any, exists: bool=False) -> str: - name = ("existent absolute path" if exists else "absolute path") +def valid_abs_path(arg: Any, exists: bool=False, name: str="") -> str: + if not name: + name = ("existent absolute path" if exists else "absolute path") if len(str(arg).strip()) == 0: arg = None @@ -44,9 +47,17 @@ def valid_abs_path(arg: Any, exists: bool=False) -> str: return arg -def valid_abs_path_exists(arg: Any) -> str: - return valid_abs_path(arg, exists=True) +def valid_abs_path_exists(arg: Any, name: str="") -> str: + return valid_abs_path(arg, exists=True, name=name) def valid_unix_mode(arg: Any) -> int: return int(valid_number(arg, min=0, name="UNIX mode")) + + +def valid_command(arg: Any) -> List[str]: + cmd = valid_string_list(arg, delim=r"[,\t]+", name="command") + if len(cmd) == 0: + raise_error(arg, "command") + cmd[0] = valid_abs_path_exists(cmd[0], name="command entry point") + return cmd |