|
6 | 6 | # ---------------------------------------------------------------------- |
7 | 7 |
|
8 | 8 | class FarsshSshKeyHandler: |
9 | | - def __init__(self, farssh_args): |
| 9 | + def __init__(self, farssh_args) -> None: |
10 | 10 | self._tempdir = tempfile.TemporaryDirectory() |
11 | 11 |
|
12 | 12 | self.farssh_args = farssh_args |
13 | 13 | self.known_hosts_file = f"{self._tempdir.name}/known-hosts" |
14 | 14 |
|
15 | | - subprocess.run(["ssh-keygen", "-q", "-N", "", "-t", "rsa", "-f", f"{self._tempdir.name}/ssh_host_rsa_key"], check = True) |
16 | | - subprocess.run(["ssh-keygen", "-q", "-N", "", "-t", "rsa", "-f", f"{self._tempdir.name}/ssh_login_key"], check = True) |
17 | | - |
18 | | - self.host_key_file = f"{self._tempdir.name}/ssh_host_rsa_key" |
19 | | - self.host_key_pub_file = f"{self._tempdir.name}/ssh_host_rsa_key.pub" |
20 | | - self.login_key_file = f"{self._tempdir.name}/ssh_login_key" |
21 | | - self.login_key_pub_file = f"{self._tempdir.name}/ssh_login_key.pub" |
22 | | - |
23 | | - self.host_key = open(self.host_key_file, "r").read() |
24 | | - self.host_key_pub = open(self.host_key_pub_file, "r").read() |
25 | | - # self.login_key = open(self.login_key_file, "r").read() # not used |
26 | | - self.login_key_pub = open(self.login_key_pub_file, "r").read() |
27 | | - |
28 | | - def write_known_hosts(self, ip_address): |
| 15 | + subprocess.run(["ssh-keygen", "-q", "-N", "", "-t", "ed25519", "-f", f"{self._tempdir.name}/ssh_host_ed25519_key"], check = True) |
| 16 | + subprocess.run(["ssh-keygen", "-q", "-N", "", "-t", "rsa", "-f", f"{self._tempdir.name}/ssh_host_rsa_key"], check = True) |
| 17 | + subprocess.run(["ssh-keygen", "-q", "-N", "", "-t", "ed25519", "-f", f"{self._tempdir.name}/ssh_login_key"], check = True) |
| 18 | + |
| 19 | + self.ed25519_host_key_file = f"{self._tempdir.name}/ssh_host_ed25519_key" |
| 20 | + self.ed25519_host_key_pub_file = f"{self._tempdir.name}/ssh_host_ed25519_key.pub" |
| 21 | + self.rsa_host_key_file = f"{self._tempdir.name}/ssh_host_rsa_key" |
| 22 | + self.rsa_host_key_pub_file = f"{self._tempdir.name}/ssh_host_rsa_key.pub" |
| 23 | + self.login_key_file = f"{self._tempdir.name}/ssh_login_key" |
| 24 | + self.login_key_pub_file = f"{self._tempdir.name}/ssh_login_key.pub" |
| 25 | + |
| 26 | + self.ed25519_host_key = open(self.ed25519_host_key_file, "r").read() |
| 27 | + self.ed25519_host_key_pub = open(self.ed25519_host_key_pub_file, "r").read() |
| 28 | + self.rsa_host_key = open(self.rsa_host_key_file, "r").read() |
| 29 | + self.rsa_host_key_pub = open(self.rsa_host_key_pub_file, "r").read() |
| 30 | + self.login_key_pub = open(self.login_key_pub_file, "r").read() |
| 31 | + # self.login_key isn't used here, so don't read. |
| 32 | + |
| 33 | + def write_known_hosts(self, ip_address) -> None: |
29 | 34 | # Create temporary known-hosts file so the SSH client can verify the remote host's public key that we configured it with. |
30 | 35 | # The `host` *must* be without port number when the port is 22; this seems to be a quirk of OpenSSH's known-hosts file format. |
| 36 | + # Write Ed25519 key first (preferred), then RSA (fallback for backward compatibility). |
31 | 37 |
|
32 | 38 | with open(self.known_hosts_file, "w") as f: |
33 | 39 | host = ip_address |
34 | 40 |
|
35 | 41 | if self.farssh_args.ssh_port != "22": |
36 | 42 | host = f"[{host}]:{self.farssh_args.ssh_port}" |
37 | 43 |
|
38 | | - f.write(f"{host} {self.host_key_pub}\n") |
39 | | - |
| 44 | + f.write(f"{host} {self.ed25519_host_key_pub}\n") |
| 45 | + f.write(f"{host} {self.rsa_host_key_pub}\n") |
0 commit comments