]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
datamodel: validation function for listening configuration
authorAleš <ales.mrazek@nic.cz>
Thu, 27 Jan 2022 14:47:10 +0000 (15:47 +0100)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 8 Apr 2022 14:17:53 +0000 (16:17 +0200)
manager/knot_resolver_manager/datamodel/network_schema.py
manager/knot_resolver_manager/datamodel/server_schema.py

index f6af86c1d877464e01813864abf00cc1499e262c..09351ce9388c151c73804c6fc322a517182556fb 100644 (file)
@@ -43,6 +43,19 @@ class TLSSchema(SchemaNode):
             raise ValueError("'padding' must be number in range<0-512>")
 
 
+def listen_config_validate(obj: object) -> None:
+    present = {
+        "ip-address" if hasattr(obj, "ip_address") and getattr(obj, "ip_address") is not None else ...,
+        "unix-socket" if hasattr(obj, "unix_socket") and getattr(obj, "unix_socket") is not None else ...,
+        "interface" if hasattr(obj, "interface") and getattr(obj, "interface") is not None else ...,
+    }
+    if not (present == {"ip-address", ...} or present == {"unix-socket", ...} or present == {"interface", ...}):
+        raise ValueError(
+            "Listen configuration contains incompatible configuration options."
+            f" Expected one of 'ip-address', 'interface' and 'unix-socket' options, got '{present}'."
+        )
+
+
 class ListenSchema(SchemaNode):
     class Raw(SchemaNode):
         unix_socket: Union[None, CheckedPath, List[CheckedPath]] = None
@@ -103,16 +116,7 @@ class ListenSchema(SchemaNode):
         return None
 
     def _validate(self) -> None:
-        present = {
-            "ip_address" if self.ip_address is not None else ...,
-            "unix_socket" if self.unix_socket is not None else ...,
-            "interface" if self.interface is not None else ...,
-        }
-        if not (present == {"ip_address", ...} or present == {"unix_socket", ...} or present == {"interface", ...}):
-            raise ValueError(
-                "Listen configuration contains multiple incompatible options at once. "
-                "Only one of 'ip-address', 'interface' and 'unix-socket' optionscan be configured at once."
-            )
+        listen_config_validate(self)
         if self.port and self.unix_socket:
             raise ValueError(
                 "'unix-socket' and 'port' are not compatible options. "
index fe6b4f50da68d1d61884b789da270e09c492f8ed..002adf1e29a2039144b01b90cfcabc0c3a9e8dae 100644 (file)
@@ -5,6 +5,7 @@ from typing import Any, Optional, Union
 
 from typing_extensions import Literal
 
+from knot_resolver_manager.datamodel.network_schema import listen_config_validate
 from knot_resolver_manager.datamodel.types import (
     CheckedPath,
     DomainName,
@@ -63,16 +64,7 @@ class WebmgmtSchema(SchemaNode):
     key_file: Optional[CheckedPath] = None
 
     def _validate(self) -> None:
-        present = {
-            "ip_address" if self.ip_address is not None else ...,
-            "unix_socket" if self.unix_socket is not None else ...,
-            "interface" if self.interface is not None else ...,
-        }
-        if not (present == {"ip_address", ...} or present == {"unix_socket", ...} or present == {"interface", ...}):
-            raise ValueError(
-                "Listen configuration contains multiple incompatible options at once. "
-                "One of 'ip-address', 'interface' or 'unix-socket' must be configured."
-            )
+        listen_config_validate(self)
 
 
 class ServerSchema(SchemaNode):