You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.9 KiB
Python
85 lines
2.9 KiB
Python
"""The only local durable configuration is encrypted database bootstrap data."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import stat
|
|
|
|
import pytest
|
|
|
|
from virsh_panel.database_bootstrap import (
|
|
DatabaseBootstrap, DatabaseSetupError, validate_database_config,
|
|
)
|
|
|
|
|
|
CONFIG = {
|
|
"host": "db.example.test", "port": 3306, "database": "virsh_panel",
|
|
"username": "panel", "password": "not-a-public-database-password",
|
|
}
|
|
|
|
|
|
def test_bootstrap_requires_token_and_encrypts_connection_settings(tmp_path):
|
|
bootstrap = DatabaseBootstrap(tmp_path / "data")
|
|
token = bootstrap.ensure_token()
|
|
assert bootstrap.ensure_token() == token
|
|
calls = []
|
|
|
|
def connect(config, key):
|
|
calls.append((config, key))
|
|
return "connected"
|
|
|
|
with pytest.raises(DatabaseSetupError) as wrong:
|
|
bootstrap.configure(CONFIG, "wrong-token", connect)
|
|
assert wrong.value.status == 403
|
|
assert calls == []
|
|
assert bootstrap.configure(CONFIG, token, connect) == "connected"
|
|
assert len(calls[0][1]) == 32
|
|
assert bootstrap.load_config() == CONFIG
|
|
assert CONFIG["password"].encode() not in bootstrap.config_file.read_bytes()
|
|
assert CONFIG["username"].encode() not in bootstrap.config_file.read_bytes()
|
|
assert stat.S_IMODE(bootstrap.data_dir.stat().st_mode) == 0o700
|
|
assert stat.S_IMODE(bootstrap.key_file.stat().st_mode) == 0o600
|
|
assert stat.S_IMODE(bootstrap.config_file.stat().st_mode) == 0o600
|
|
with pytest.raises(DatabaseSetupError) as repeated:
|
|
bootstrap.configure(CONFIG, token, connect)
|
|
assert repeated.value.status == 409
|
|
|
|
|
|
def test_bootstrap_does_not_commit_failed_connection(tmp_path):
|
|
bootstrap = DatabaseBootstrap(tmp_path / "data")
|
|
token = bootstrap.ensure_token()
|
|
|
|
def fail(_config, _key):
|
|
raise ConnectionError("database unavailable")
|
|
|
|
with pytest.raises(ConnectionError):
|
|
bootstrap.configure(CONFIG, token, fail)
|
|
assert not bootstrap.configured()
|
|
assert bootstrap.ensure_token() == token
|
|
|
|
|
|
def test_bootstrap_rejects_tampering_and_unsafe_files(tmp_path):
|
|
bootstrap = DatabaseBootstrap(tmp_path / "data")
|
|
token = bootstrap.ensure_token()
|
|
bootstrap.configure(CONFIG, token, lambda *_: None)
|
|
envelope = json.loads(bootstrap.config_file.read_text())
|
|
envelope["ciphertext"] = "AAAA"
|
|
bootstrap.config_file.write_text(json.dumps(envelope))
|
|
with pytest.raises(ValueError, match="损坏"):
|
|
bootstrap.load_config()
|
|
bootstrap.config_file.chmod(0o644)
|
|
with pytest.raises(ValueError, match="0600"):
|
|
bootstrap.load_config()
|
|
|
|
|
|
@pytest.mark.parametrize("bad", [
|
|
{**CONFIG, "host": "https://evil.example"},
|
|
{**CONFIG, "port": 0},
|
|
{**CONFIG, "database": "other;DROP TABLE admin"},
|
|
{**CONFIG, "username": "bad\nname"},
|
|
{**CONFIG, "password": ""},
|
|
])
|
|
def test_database_config_validation_rejects_unsafe_values(bad):
|
|
with pytest.raises(DatabaseSetupError):
|
|
validate_database_config(bad)
|