summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2020-10-02 10:22:43 +0300
committerDevaev Maxim <[email protected]>2020-10-09 03:51:19 +0300
commit5bfde6ceae5af9c47c7a86a6ee1d9fb180b39b54 (patch)
tree7d02fbc1b2170c009c4be099d1e5a75f35bc32e9
parent3c4d38f7551c9bd69ac4646d7704fc271e32bdc3 (diff)
otgnet stubs
-rw-r--r--PKGBUILD2
-rw-r--r--kvmd/apps/__init__.py22
-rw-r--r--kvmd/validators/net.py26
-rw-r--r--testenv/tests/validators/test_net.py54
4 files changed, 104 insertions, 0 deletions
diff --git a/PKGBUILD b/PKGBUILD
index 13c49bba..ac4bf62e 100644
--- a/PKGBUILD
+++ b/PKGBUILD
@@ -59,6 +59,8 @@ depends=(
make
patch
sudo
+ iptables
+ iproute2
"raspberrypi-io-access>=0.5"
"ustreamer>=1.19"
)
diff --git a/kvmd/apps/__init__.py b/kvmd/apps/__init__.py
index 10a331da..a882e0d7 100644
--- a/kvmd/apps/__init__.py
+++ b/kvmd/apps/__init__.py
@@ -77,7 +77,9 @@ from ..validators.os import valid_command
from ..validators.net import valid_ip_or_host
from ..validators.net import valid_ip
+from ..validators.net import valid_net
from ..validators.net import valid_port
+from ..validators.net import valid_ports_list
from ..validators.net import valid_mac
from ..validators.net import valid_ssl_ciphers
@@ -426,6 +428,26 @@ def _get_config_scheme() -> Dict:
},
},
+ "otgnet": {
+ "iface": {
+ "net": Option("169.254.0.100/31", type=functools.partial(valid_net, v6=False)),
+ },
+
+ "firewall": {
+ "allow_tcp": Option([], type=valid_ports_list),
+ "allow_udp": Option([], type=valid_ports_list),
+ },
+
+ "dhcp": {
+ "enabled": Option(True, type=valid_bool),
+ },
+
+ "commands": {
+ "ip_cmd": Option(["/usr/bin/ip"], type=valid_command),
+ "iptables_cmd": Option(["/usr/bin/iptables"], type=valid_command),
+ },
+ },
+
"ipmi": {
"server": {
"host": Option("::", type=valid_ip_or_host),
diff --git a/kvmd/validators/net.py b/kvmd/validators/net.py
index 4197ac8e..f4019272 100644
--- a/kvmd/validators/net.py
+++ b/kvmd/validators/net.py
@@ -28,11 +28,13 @@ from typing import Callable
from typing import Any
from . import ValidatorError
+from . import raise_error
from . import check_re_match
from . import check_any
from .basic import valid_number
from .basic import valid_stripped_string_not_empty
+from .basic import valid_string_list
# =====
@@ -66,6 +68,26 @@ def valid_ip(arg: Any, v4: bool=True, v6: bool=True) -> str:
)
+def valid_net(arg: Any, v4: bool=True, v6: bool=True) -> str:
+ assert v4 or v6
+ validators: List[Callable] = []
+ versions: List[str] = []
+ if v4:
+ validators.append(lambda arg: str(ipaddress.IPv4Network(arg)))
+ versions.append("4")
+ if v6:
+ validators.append(lambda arg: str(ipaddress.IPv6Network(arg)))
+ versions.append("6")
+ name = f"IPv{'/'.join(versions)} network"
+ if "/" not in str(arg):
+ raise_error(arg, name)
+ return check_any(
+ arg=valid_stripped_string_not_empty(arg, name),
+ name=name,
+ validators=validators,
+ )
+
+
def valid_rfc_host(arg: Any) -> str:
# http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address
pattern = r"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*" \
@@ -77,6 +99,10 @@ def valid_port(arg: Any) -> int:
return int(valid_number(arg, min=0, max=65535, name="network port"))
+def valid_ports_list(arg: Any) -> List[int]:
+ return list(map(int, valid_string_list(arg, subval=valid_port, name="ports list")))
+
+
def valid_mac(arg: Any) -> str:
pattern = ":".join([r"[0-9a-fA-F]{2}"] * 6)
return check_re_match(arg, "MAC address", pattern).lower()
diff --git a/testenv/tests/validators/test_net.py b/testenv/tests/validators/test_net.py
index 486154fc..d3d4703b 100644
--- a/testenv/tests/validators/test_net.py
+++ b/testenv/tests/validators/test_net.py
@@ -20,6 +20,7 @@
# ========================================================================== #
+from typing import List
from typing import Any
import pytest
@@ -27,8 +28,10 @@ import pytest
from kvmd.validators import ValidatorError
from kvmd.validators.net import valid_ip_or_host
from kvmd.validators.net import valid_ip
+from kvmd.validators.net import valid_net
from kvmd.validators.net import valid_rfc_host
from kvmd.validators.net import valid_port
+from kvmd.validators.net import valid_ports_list
from kvmd.validators.net import valid_mac
from kvmd.validators.net import valid_ssl_ciphers
@@ -88,6 +91,37 @@ def test_fail__valid_ip(arg: Any) -> None:
# =====
@pytest.mark.parametrize("arg", [
+ "127.0.0.0/24 ",
+ "8.8.8.8/31",
+ "::/16",
+ "::ffff:0:0:0/96",
+ "64:ff9b::/96",
+])
+def test_ok__valid_net(arg: Any) -> None:
+ assert valid_net(arg) == arg.strip()
+
+
+ "127.0.0.1/33",
+ "127.0.0.1/0",
+ "127.0.0.1/",
+ "127.0.0.1",
+ "8.8.8.8//31",
+ "ya.ru",
+ "1",
+ "1.1.1",
+ "1.1.1.",
+ ":",
+ "",
+ None,
+])
+def test_fail__valid_net(arg: Any) -> None:
+ with pytest.raises(ValidatorError):
+ print(valid_net(arg))
+
+
+# =====
"yandex.ru ",
"foobar",
"foo-bar.ru",
@@ -125,6 +159,26 @@ def test_fail__valid_port(arg: Any) -> None:
# =====
[email protected]("arg, retval", [
+ ("", []),
+ (",, , ", []),
+ ("0 ", [0]),
+ ("1,", [1]),
+ ("22,23", [22, 23]),
+ ("80,443,443,", [80, 443, 443]),
+ (65535, [65535]),
+])
+def test_ok__valid_ports_list(arg: Any, retval: List[int]) -> None:
+ assert valid_ports_list(arg) == retval
+
+
[email protected]("arg", ["test", "13,test", None, 1.1])
+def test_fail__valid_ports_list(arg: Any) -> None:
+ with pytest.raises(ValidatorError):
+ print(valid_ports_list(arg))
+
+
+# =====
@pytest.mark.parametrize("arg", [
" 00:00:00:00:00:00 ",
" 9f:00:00:00:00:00 ",