fix: add talos_nic validation

Signed-off-by: Devin Buhl <devin@buhl.casa>
This commit is contained in:
Devin Buhl committed 2024-04-03 14:27:39 -04:00
1 parent 28ae26d3a8
commit 35df2fb0f7
1 file changed
+11 -7
+11 -7
View File
@@ -56,16 +56,20 @@ def validate_node(node: dict, node_cidr: str, distribution: str) -> None:
raise ValueError(f"A node is missing a name") raise ValueError(f"A node is missing a name")
if not re.match(r"^[a-z0-9-\.]+$", node.get('name')): if not re.match(r"^[a-z0-9-\.]+$", node.get('name')):
raise ValueError(f"Node {node.get('name')} has an invalid name") raise ValueError(f"Node {node.get('name')} has an invalid name")
if not node.get("ssh_user") and distribution in ["k3s"]: if distribution in ["k3s"]:
raise ValueError(f"Node {node.get('name')} is missing ssh_user") if not node.get("ssh_user") :
if not node.get("talos_disk") and distribution in ["talos"]: raise ValueError(f"Node {node.get('name')} is missing ssh_user")
raise ValueError(f"Node {node.get('name')} is missing talos_disk") if distribution in ["talos"]:
if not node.get("talos_nic") and distribution in ["talos"]: if not node.get("talos_disk"):
raise ValueError(f"Node {node.get('name')} is missing talos_nic") raise ValueError(f"Node {node.get('name')} is missing talos_disk")
if not node.get("talos_nic"):
raise ValueError(f"Node {node.get('name')} is missing talos_nic")
if not re.match(r"(?:[0-9a-fA-F]:?){12}", node.get("talos_nic")):
raise ValueError(f"Node {node.get('name')} has an invalid talos_nic, is this a MAC address?")
ip = validate_ip(node.get("address")) ip = validate_ip(node.get("address"))
if netaddr.IPAddress(ip, 4) not in netaddr.IPNetwork(node_cidr): if netaddr.IPAddress(ip, 4) not in netaddr.IPNetwork(node_cidr):
raise ValueError(f"Node {node.get('name')} is not in the node CIDR {node_cidr}") raise ValueError(f"Node {node.get('name')} is not in the node CIDR {node_cidr}")
port = 50000 if distribution == "talos" else 22 port = 50000 if distribution in ["talos"] else 22
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.settimeout(5) sock.settimeout(5)
result = sock.connect_ex((ip, port)) result = sock.connect_ex((ip, port))