Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Commit 228a4e8

Browse files
michalskrivanekmangelajo
authored andcommitted
load ssh identity lazily
on first use, to prevent a failure on startup if the file doesn't exist yet (cherry picked from commit af48116)
1 parent c6fab27 commit 228a4e8

2 files changed

Lines changed: 25 additions & 14 deletions

File tree

packages/jumpstarter-driver-ssh/jumpstarter_driver_ssh/driver.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ def __post_init__(self):
2424
if self.ssh_identity and self.ssh_identity_file:
2525
raise ConfigurationError("Cannot specify both ssh_identity and ssh_identity_file")
2626

27-
# If ssh_identity_file is provided, read it into ssh_identity
28-
if self.ssh_identity_file:
29-
try:
30-
self.ssh_identity = Path(self.ssh_identity_file).read_text()
31-
except Exception as e:
32-
raise ConfigurationError(f"Failed to read ssh_identity_file '{self.ssh_identity_file}': {e}") from None
33-
3427
@classmethod
3528
def client(cls) -> str:
3629
return "jumpstarter_driver_ssh.client.SSHWrapperClient"
@@ -48,4 +41,10 @@ def get_ssh_command(self):
4841
@export
4942
def get_ssh_identity(self):
5043
"""Get the SSH identity key content"""
44+
# If ssh_identity_file is provided, read it lazily and cache in ssh_identity
45+
if self.ssh_identity is None and self.ssh_identity_file:
46+
try:
47+
self.ssh_identity = Path(self.ssh_identity_file).read_text()
48+
except Exception as e:
49+
raise ConfigurationError(f"Failed to read ssh_identity_file '{self.ssh_identity_file}': {e}") from None
5150
return self.ssh_identity

packages/jumpstarter-driver-ssh/jumpstarter_driver_ssh/driver_test.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,17 @@ def test_ssh_identity_file_configuration():
391391
)
392392

393393
# Test that the instance was created correctly
394-
assert instance.ssh_identity == TEST_SSH_KEY
394+
# ssh_identity should be None until first use (lazy loading)
395+
assert instance.ssh_identity is None
395396
assert instance.ssh_identity_file == temp_file_path
396397

398+
# Test that get_ssh_identity() reads the file on first use
399+
identity = instance.get_ssh_identity()
400+
assert identity == TEST_SSH_KEY
401+
402+
# Test that ssh_identity is now cached
403+
assert instance.ssh_identity == TEST_SSH_KEY
404+
397405
# Test that the client class is correct
398406
assert instance.client() == "jumpstarter_driver_ssh.client.SSHWrapperClient"
399407
finally:
@@ -413,13 +421,17 @@ def test_ssh_identity_validation_error():
413421

414422

415423
def test_ssh_identity_file_read_error():
416-
"""Test SSH wrapper raises error when ssh_identity_file cannot be read"""
424+
"""Test SSH wrapper raises error when ssh_identity_file cannot be read on first use"""
425+
# Instance creation should succeed (lazy loading)
426+
instance = SSHWrapper(
427+
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
428+
default_username="testuser",
429+
ssh_identity_file="/nonexistent/path/to/key"
430+
)
431+
432+
# Error should be raised when get_ssh_identity() is called
417433
with pytest.raises(ConfigurationError, match="Failed to read ssh_identity_file"):
418-
SSHWrapper(
419-
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
420-
default_username="testuser",
421-
ssh_identity_file="/nonexistent/path/to/key"
422-
)
434+
instance.get_ssh_identity()
423435

424436

425437
def test_ssh_command_with_identity_string():

0 commit comments

Comments
 (0)