clan-cli: Expose private_key to Machine class, in the future we should merge Machine and Host class

This commit is contained in:
Qubasa
2025-04-25 13:38:03 +02:00
parent 1b49751144
commit f1a7f2aa69
5 changed files with 15 additions and 9 deletions

View File

@@ -32,6 +32,7 @@ class Machine:
cached_deployment: None | dict[str, Any] = None cached_deployment: None | dict[str, Any] = None
override_target_host: None | str = None override_target_host: None | str = None
override_build_host: None | str = None override_build_host: None | str = None
private_key: Path | None = None
host_key_check: HostKeyCheck = HostKeyCheck.STRICT host_key_check: HostKeyCheck = HostKeyCheck.STRICT
def get_id(self) -> str: def get_id(self) -> str:
@@ -150,6 +151,7 @@ class Machine:
self.name, self.name,
self.target_host_address, self.target_host_address,
self.host_key_check, self.host_key_check,
private_key=self.private_key,
meta={"machine": self}, meta={"machine": self},
) )
@@ -168,6 +170,7 @@ class Machine:
build_host, build_host,
self.host_key_check, self.host_key_check,
forward_agent=True, forward_agent=True,
private_key=self.private_key,
meta={"machine": self, "target_host": self.target_host}, meta={"machine": self, "target_host": self.target_host},
) )

View File

@@ -6,6 +6,7 @@ import shlex
import socket import socket
import subprocess import subprocess
from dataclasses import dataclass, field from dataclasses import dataclass, field
from pathlib import Path
from shlex import quote from shlex import quote
from typing import Any from typing import Any
@@ -27,7 +28,7 @@ class Host:
host: str host: str
user: str | None = None user: str | None = None
port: int | None = None port: int | None = None
key: str | None = None private_key: Path | None = None
forward_agent: bool = False forward_agent: bool = False
command_prefix: str | None = None command_prefix: str | None = None
host_key_check: HostKeyCheck = HostKeyCheck.ASK host_key_check: HostKeyCheck = HostKeyCheck.ASK
@@ -54,7 +55,7 @@ class Host:
host=host.host, host=host.host,
user=host.user, user=host.user,
port=host.port, port=host.port,
key=host.key, private_key=host.private_key,
forward_agent=host.forward_agent, forward_agent=host.forward_agent,
command_prefix=host.command_prefix, command_prefix=host.command_prefix,
host_key_check=host.host_key_check, host_key_check=host.host_key_check,
@@ -176,6 +177,9 @@ class Host:
ssh_opts.extend(self.host_key_check.to_ssh_opt()) ssh_opts.extend(self.host_key_check.to_ssh_opt())
if self.private_key:
ssh_opts.extend(["-i", str(self.private_key)])
return ssh_opts return ssh_opts
def ssh_cmd( def ssh_cmd(
@@ -201,11 +205,6 @@ class Host:
if tty: if tty:
ssh_opts.extend(["-t"]) ssh_opts.extend(["-t"])
if self.port:
ssh_opts.extend(["-p", str(self.port)])
if self.key:
ssh_opts.extend(["-i", self.key])
if tor_socks: if tor_socks:
packages.append("netcat") packages.append("netcat")
ssh_opts.append("-o") ssh_opts.append("-o")

View File

@@ -1,5 +1,6 @@
import re import re
import urllib.parse import urllib.parse
from pathlib import Path
from typing import Any from typing import Any
from clan_cli.errors import ClanError from clan_cli.errors import ClanError
@@ -13,6 +14,7 @@ def parse_deployment_address(
host_key_check: HostKeyCheck, host_key_check: HostKeyCheck,
forward_agent: bool = True, forward_agent: bool = True,
meta: dict[str, Any] | None = None, meta: dict[str, Any] | None = None,
private_key: Path | None = None,
) -> Host: ) -> Host:
parts = host.split("?", maxsplit=1) parts = host.split("?", maxsplit=1)
endpoint, maybe_options = parts if len(parts) == 2 else (parts[0], "") endpoint, maybe_options = parts if len(parts) == 2 else (parts[0], "")
@@ -58,6 +60,7 @@ def parse_deployment_address(
hostname, hostname,
user=user, user=user,
port=port, port=port,
private_key=private_key,
host_key_check=host_key_check, host_key_check=host_key_check,
command_prefix=machine_name, command_prefix=machine_name,
forward_agent=forward_agent, forward_agent=forward_agent,

View File

@@ -1,5 +1,6 @@
import os import os
import pwd import pwd
from pathlib import Path
import pytest import pytest
from clan_cli.ssh.host import Host from clan_cli.ssh.host import Host
@@ -15,7 +16,7 @@ def hosts(sshd: Sshd) -> list[Host]:
"127.0.0.1", "127.0.0.1",
port=sshd.port, port=sshd.port,
user=login, user=login,
key=sshd.key, private_key=Path(sshd.key),
host_key_check=HostKeyCheck.NONE, host_key_check=HostKeyCheck.NONE,
) )
] ]

View File

@@ -40,7 +40,7 @@ def test_secrets_upload(
config = flake.machines["vm1"] config = flake.machines["vm1"]
config["nixpkgs"]["hostPlatform"] = "x86_64-linux" config["nixpkgs"]["hostPlatform"] = "x86_64-linux"
host = hosts[0] host = hosts[0]
addr = f"{host.user}@{host.host}:{host.port}?StrictHostKeyChecking=no&UserKnownHostsFile=/dev/null&IdentityFile={host.key}" addr = f"{host.user}@{host.host}:{host.port}?StrictHostKeyChecking=no&UserKnownHostsFile=/dev/null&IdentityFile={host.private_key}"
config["clan"]["networking"]["targetHost"] = addr config["clan"]["networking"]["targetHost"] = addr
config["clan"]["core"]["facts"]["secretUploadDirectory"] = str(flake.path / "facts") config["clan"]["core"]["facts"]["secretUploadDirectory"] = str(flake.path / "facts")
flake.refresh() flake.refresh()