@ -23,6 +23,7 @@ from virsh_panel.database import (
DatabaseTemplateStore ,
)
from virsh_panel . remote_host import HostProfile , RemoteHostError
from virsh_panel . ssh_agent import SSHAgentError
PRIVATE_KEY = " -----BEGIN OPENSSH PRIVATE KEY----- \n ZmFrZQ== \n -----END OPENSSH PRIVATE KEY----- \n "
@ -52,6 +53,24 @@ class MemoryStore:
if record_type == kind }
class EncryptedMemoryStore ( MemoryStore ) :
""" Exercise the production record envelope without a MariaDB server. """
def __init__ ( self ) - > None :
super ( ) . __init__ ( )
self . records : dict [ tuple [ str , str ] , bytes ] = { }
self . _cipher_store = DatabaseStore . __new__ ( DatabaseStore )
self . _cipher_store . _cipher = AESGCM ( b " a " * 32 )
def get ( self , kind : str , key : str ) - > str | None :
payload = self . records . get ( ( kind , key ) )
return ( None if payload is None else
self . _cipher_store . _decrypt ( kind , key , payload ) )
def set ( self , kind : str , key : str , value : str ) - > None :
self . records [ ( kind , key ) ] = self . _cipher_store . _encrypt ( kind , key , value )
def test_database_ciphertext_is_randomized_and_bound_to_record_key ( ) :
store = DatabaseStore . __new__ ( DatabaseStore )
store . _cipher = AESGCM ( b " a " * 32 )
@ -137,7 +156,7 @@ def _encrypted_ssh(argv, **kwargs):
class FakeAgent :
def __init__ ( self , generation : Path , _key_path : Path , passphrase : str ) - > None :
if passphrase != " open-sesame " :
raise Runtime Error( " bad passphrase " )
raise SSHAgent Error( " bad passphrase " )
self . socket_path = generation / " fake-agent.sock "
self . public_key = _host_key ( )
self . alive = True
@ -191,10 +210,40 @@ def test_remote_host_is_rebuilt_from_database_without_durable_private_key(tmp_pa
third . close ( )
def test_ encrypted_host_key_is_locked_again_after_process_restart ( tmp_path : Path ) :
def test_ host_commit_does_not_read_database_after_activation ( tmp_path : Path ) :
store = MemoryStore ( )
runtime_parent = tmp_path / " simulated-tmpfs "
runtime_parent . mkdir ( )
manager = DatabaseRemoteHostManager (
store , tmp_path / " panel " , runtime_parent = runtime_parent , runner = _fake_ssh ,
)
scan = manager . scan ( " host.example " , 22 )
staged = manager . stage ( HostProfile (
host = " host.example " , port = 22 , private_key = PRIVATE_KEY ,
host_key = scan [ " known_hosts_line " ] ,
) , scan [ " fingerprint " ] )
manager . test ( staged )
original_get = store . get
reads = 0
def one_read_only ( kind : str , key : str ) - > str | None :
nonlocal reads
reads + = 1
if reads > 1 :
raise RuntimeError ( " database read unavailable after activation " )
return original_get ( kind , key )
store . get = one_read_only
assert manager . commit ( staged ) [ " host " ] == " host.example "
assert reads == 1
assert ( " host " , " current " ) in store . records
manager . close ( )
def test_encrypted_host_key_auto_unlocks_after_process_restart ( tmp_path : Path ) :
store = EncryptedMemoryStore ( )
runtime_parent = tmp_path / " simulated-tmpfs "
runtime_parent . mkdir ( )
data_dir = tmp_path / " panel "
first = DatabaseRemoteHostManager (
store , data_dir , runtime_parent = runtime_parent ,
@ -206,16 +255,184 @@ def test_encrypted_host_key_is_locked_again_after_process_restart(tmp_path: Path
host_key = scan [ " known_hosts_line " ] , private_key_passphrase = " open-sesame " ,
) , scan [ " fingerprint " ] )
first . test ( staged )
assert first . commit ( staged ) [ " key_locked " ] is False
saved = first . commit ( staged )
assert saved [ " key_locked " ] is False
assert saved [ " private_key_passphrase_saved " ] is True
ciphertext = store . records [ ( " host " , " current " ) ]
assert ciphertext . startswith ( b " vp1 " )
assert b " open-sesame " not in ciphertext
assert b " open-sesame " not in b " " . join (
path . read_bytes ( ) for path in data_dir . rglob ( " * " ) if path . is_file ( )
)
first . close ( )
second = DatabaseRemoteHostManager (
store , data_dir , runtime_parent = runtime_parent ,
runner = _encrypted_ssh , agent_factory = FakeAgent ,
)
assert second . current ( ) [ " key_locked " ] is False
assert second . current ( ) [ " private_key_passphrase_saved " ] is True
second . close ( )
def test_bad_saved_passphrase_keeps_host_locked_without_blocking_startup ( tmp_path : Path ) :
store = MemoryStore ( )
runtime_parent = tmp_path / " simulated-tmpfs "
runtime_parent . mkdir ( )
data_dir = tmp_path / " panel "
first = DatabaseRemoteHostManager (
store , data_dir , runtime_parent = runtime_parent ,
runner = _encrypted_ssh , agent_factory = FakeAgent ,
)
scan = first . scan ( " host.example " , 22 )
staged = first . stage ( HostProfile (
host = " host.example " , port = 22 , private_key = PRIVATE_KEY ,
host_key = scan [ " known_hosts_line " ] , private_key_passphrase = " open-sesame " ,
) , scan [ " fingerprint " ] )
first . test ( staged )
first . commit ( staged )
first . close ( )
record = json . loads ( store . get ( " host " , " current " ) )
record [ " private_key_passphrase " ] = " wrong-secret "
store . set ( " host " , " current " , json . dumps ( record ) )
attempts = [ ]
def counting_agent ( * args ) :
attempts . append ( 1 )
return FakeAgent ( * args )
second = DatabaseRemoteHostManager (
store , data_dir , runtime_parent = runtime_parent ,
runner = _encrypted_ssh , agent_factory = counting_agent ,
)
assert len ( attempts ) == 1
assert second . current ( ) [ " key_locked " ] is True
assert second . current ( ) [ " private_key_passphrase_saved " ] is True
assert second . current_for_background ( ) [ " key_locked " ] is True
assert second . current_for_background ( ) [ " key_locked " ] is True
assert len ( attempts ) == 2
assert second . unlock_current ( " open-sesame " ) [ " key_locked " ] is False
second . close ( )
def test_background_recovers_ssh_agent_after_it_exits ( tmp_path : Path ) :
store = MemoryStore ( )
runtime_parent = tmp_path / " simulated-tmpfs "
runtime_parent . mkdir ( )
manager = DatabaseRemoteHostManager (
store , tmp_path / " panel " , runtime_parent = runtime_parent ,
runner = _encrypted_ssh , agent_factory = FakeAgent ,
)
scan = manager . scan ( " host.example " , 22 )
staged = manager . stage ( HostProfile (
host = " host.example " , port = 22 , private_key = PRIVATE_KEY ,
host_key = scan [ " known_hosts_line " ] , private_key_passphrase = " open-sesame " ,
) , scan [ " fingerprint " ] )
manager . test ( staged )
manager . commit ( staged )
generation = manager . _current_generation ( )
manager . _agents [ generation ] . close ( )
assert manager . current ( ) [ " key_locked " ] is True
assert manager . current_for_background ( ) [ " key_locked " ] is False
manager . close ( )
def test_resaving_same_key_can_reuse_saved_passphrase ( tmp_path : Path ) :
store = MemoryStore ( )
runtime_parent = tmp_path / " simulated-tmpfs "
runtime_parent . mkdir ( )
manager = DatabaseRemoteHostManager (
store , tmp_path / " panel " , runtime_parent = runtime_parent ,
runner = _encrypted_ssh , agent_factory = FakeAgent ,
)
scan = manager . scan ( " host.example " , 22 )
initial = manager . stage ( HostProfile (
host = " host.example " , port = 22 , private_key = PRIVATE_KEY ,
host_key = scan [ " known_hosts_line " ] , private_key_passphrase = " open-sesame " ,
) , scan [ " fingerprint " ] )
manager . test ( initial )
manager . commit ( initial )
for key in ( " " , PRIVATE_KEY ) :
staged = manager . stage ( HostProfile (
host = " host.example " , port = 22 , private_key = key ,
host_key = scan [ " known_hosts_line " ] , private_key_passphrase = " " ,
) , scan [ " fingerprint " ] )
manager . test ( staged )
assert manager . commit ( staged ) [ " private_key_passphrase_saved " ] is True
assert json . loads ( store . get ( " host " , " current " ) ) [ " private_key_passphrase " ] == " open-sesame "
manager . close ( )
def test_replacing_key_cannot_inherit_previous_passphrase ( tmp_path : Path ) :
store = MemoryStore ( )
runtime_parent = tmp_path / " simulated-tmpfs "
runtime_parent . mkdir ( )
manager = DatabaseRemoteHostManager (
store , tmp_path / " panel " , runtime_parent = runtime_parent ,
runner = _encrypted_ssh , agent_factory = FakeAgent ,
)
scan = manager . scan ( " host.example " , 22 )
initial = manager . stage ( HostProfile (
host = " host.example " , port = 22 , private_key = PRIVATE_KEY ,
host_key = scan [ " known_hosts_line " ] , private_key_passphrase = " open-sesame " ,
) , scan [ " fingerprint " ] )
manager . test ( initial )
manager . commit ( initial )
previous = store . get ( " host " , " current " )
different_key = PRIVATE_KEY . replace ( " ZmFrZQ== " , " b3RoZXI= " )
with pytest . raises ( RemoteHostError , match = " 填写私钥口令 " ) :
manager . stage ( HostProfile (
host = " host.example " , port = 22 , private_key = different_key ,
host_key = scan [ " known_hosts_line " ] , private_key_passphrase = " " ,
) , scan [ " fingerprint " ] )
assert store . get ( " host " , " current " ) == previous
manager . close ( )
def test_legacy_saved_host_requiring_password_remains_locked_until_resaved ( tmp_path : Path ) :
store = MemoryStore ( )
runtime_parent = tmp_path / " simulated-tmpfs "
runtime_parent . mkdir ( )
data_dir = tmp_path / " panel "
first = DatabaseRemoteHostManager (
store , data_dir , runtime_parent = runtime_parent ,
runner = _encrypted_ssh , agent_factory = FakeAgent ,
)
scan = first . scan ( " host.example " , 22 )
initial = first . stage ( HostProfile (
host = " host.example " , port = 22 , private_key = PRIVATE_KEY ,
host_key = scan [ " known_hosts_line " ] , private_key_passphrase = " open-sesame " ,
) , scan [ " fingerprint " ] )
first . test ( initial )
first . commit ( initial )
record = json . loads ( store . get ( " host " , " current " ) )
record . pop ( " private_key_passphrase " )
store . set ( " host " , " current " , json . dumps ( record ) )
first . close ( )
second = DatabaseRemoteHostManager (
store , data_dir , runtime_parent = runtime_parent ,
runner = _encrypted_ssh , agent_factory = FakeAgent ,
)
assert second . current ( ) [ " key_locked " ] is True
assert second . current ( ) [ " private_key_passphrase_saved " ] is False
assert second . unlock_current ( " open-sesame " ) [ " key_locked " ] is False
assert second . current ( ) [ " private_key_passphrase_saved " ] is False
staged = second . stage ( HostProfile (
host = " host.example " , port = 22 , private_key = " " ,
host_key = scan [ " known_hosts_line " ] , private_key_passphrase = " open-sesame " ,
) , scan [ " fingerprint " ] )
second . test ( staged )
assert second . commit ( staged ) [ " private_key_passphrase_saved " ] is True
second . close ( )
third = DatabaseRemoteHostManager (
store , data_dir , runtime_parent = runtime_parent ,
runner = _encrypted_ssh , agent_factory = FakeAgent ,
)
assert third . current ( ) [ " key_locked " ] is False
third . close ( )
def test_startup_prunes_only_marked_runtime_of_dead_process ( tmp_path : Path , monkeypatch ) :
dead = tmp_path / " v99999999-dead "