]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
manager: better error handling for rundir
authorAleš Mrázek <ales.mrazek@nic.cz>
Fri, 4 Aug 2023 13:42:15 +0000 (15:42 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Thu, 17 Aug 2023 14:04:41 +0000 (14:04 +0000)
manager/knot_resolver_manager/datamodel/config_schema.py
manager/knot_resolver_manager/server.py

index 6046f7a8328b258c635b48ef9995c359c57e50e5..da68525cdc390e7a951d76f9ceeee18587e45349 100644 (file)
@@ -25,6 +25,8 @@ from knot_resolver_manager.datamodel.webmgmt_schema import WebmgmtSchema
 from knot_resolver_manager.utils.modeling import ConfigSchema
 from knot_resolver_manager.utils.modeling.base_schema import lazy_default
 
+_DEFAULT_RUNDIR = "/var/run/knot-resolver"
+
 logger = logging.getLogger(__name__)
 
 
@@ -105,7 +107,7 @@ class KresConfig(ConfigSchema):
         version: int = 1
         nsid: Optional[EscapedStr] = None
         hostname: Optional[EscapedStr] = None
-        rundir: Dir = lazy_default(Dir, "/var/run/knot-resolver")
+        rundir: Dir = lazy_default(Dir, _DEFAULT_RUNDIR)
         workers: Union[Literal["auto"], IntPositive] = IntPositive(1)
         max_workers: IntPositive = IntPositive(_default_max_worker_count())
         management: ManagementSchema = lazy_default(ManagementSchema, {"unix-socket": "./manager.sock"})
@@ -190,12 +192,10 @@ class KresConfig(ConfigSchema):
 
 def get_rundir_without_validation(data: Dict[str, Any]) -> Dir:
     """
-    Without fully parsing, try to get a rundir from a raw config data. When it fails,
-    attempts a full validation to produce a good error message.
+    Without fully parsing, try to get a rundir from a raw config data, otherwise use default.
+    Attempts a dir validation to produce a good error message.
 
     Used for initial manager startup.
     """
 
-    if "rundir" in data:
-        return Dir(data["rundir"], object_path="/rundir")
-    return KresConfig(data).rundir  # this should throw a descriptive error
+    return Dir(data["rundir"] if "rundir" in data else _DEFAULT_RUNDIR, object_path="/rundir")
index 345a3f8a934a6805191f3c7525598d4746fd024c..3aba44092558eec62a8d10a6cb8e48fc9e74dddf 100644 (file)
@@ -26,7 +26,6 @@ from knot_resolver_manager.constants import DEFAULT_MANAGER_CONFIG_FILE, PID_FIL
 from knot_resolver_manager.datamodel.config_schema import KresConfig, get_rundir_without_validation
 from knot_resolver_manager.datamodel.globals import (
     Context,
-    reset_global_validation_context,
     set_global_validation_context,
 )
 from knot_resolver_manager.datamodel.management_schema import ManagementSchema
@@ -406,7 +405,10 @@ async def _deny_working_directory_changes(config_old: KresConfig, config_new: Kr
 
 
 def _set_working_directory(config_raw: Dict[str, Any]) -> None:
-    rundir = get_rundir_without_validation(config_raw)
+    try:
+        rundir = get_rundir_without_validation(config_raw)
+    except ValueError as e:
+        raise DataValidationError(str(e), "/rundir")
 
     logger.debug(f"Changing working directory to '{rundir.to_path().absolute()}'.")
     os.chdir(rundir.to_path())
@@ -482,21 +484,21 @@ async def start_server(config: Path = DEFAULT_MANAGER_CONFIG_FILE) -> int:
         # Preprocess config - load from file or in general take it to the last step before validation.
         config_raw = await _load_raw_config(config)
 
+        # before processing any configuration, set validation context
+        #  - resolve_root = root against which all relative paths will be resolved
+        set_global_validation_context(Context(config.parent, True))
+
         # We want to change cwd as soon as possible. Some parts of the codebase are using os.getcwd() to get the
         # working directory.
         #
         # If we fail to read rundir from unparsed config, the first config validation error comes from here
-        set_global_validation_context(Context(config.parent, False))  # Strict validation for Paths is off.
         _set_working_directory(config_raw)
-        reset_global_validation_context()
 
         # We don't want more than one manager in a single working directory. So we lock it with a PID file.
         # Warning - this does not prevent multiple managers with the same naming of kresd service.
         _lock_working_directory()
 
-        # before processing any configuration, set validation context
-        #  - resolve_root = root against which all relative paths will be resolved
-        set_global_validation_context(Context(config.parent))
+        # set_global_validation_context(Context(config.parent))
 
         # After the working directory is set, we can initialize proper config store with a newly parsed configuration.
         config_store = await _init_config_store(config_raw)