Add key validation utilities and integrate

This commit is contained in:
thePR0M3TH3AN
2025-08-01 10:38:40 -04:00
parent 20896812a4
commit cc8fba9f12
14 changed files with 206 additions and 12 deletions

View File

@@ -24,7 +24,7 @@ class DummyPM:
add_totp=lambda label, seed, index=None, secret=None, period=30, digits=6: "totp://",
add_ssh_key=lambda label, seed, index=None, notes="": 2,
add_pgp_key=lambda label, seed, index=None, key_type="ed25519", user_id="", notes="": 3,
add_nostr_key=lambda label, index=None, notes="": 4,
add_nostr_key=lambda label, seed, index=None, notes="": 4,
add_seed=lambda label, seed, index=None, words_num=24, notes="": 5,
add_key_value=lambda label, key, value, notes="": 6,
add_managed_account=lambda label, seed, index=None, notes="": 7,

View File

@@ -4,6 +4,7 @@ from typer.testing import CliRunner
from seedpass.cli import app
from seedpass import cli
from helpers import TEST_SEED
runner = CliRunner()
@@ -98,7 +99,7 @@ runner = CliRunner()
"add-nostr",
"add_nostr_key",
["Label", "--index", "4", "--notes", "n"],
("Label",),
("Label", "seed"),
{"index": 4, "notes": "n"},
"5",
),

View File

@@ -116,7 +116,7 @@ def test_legacy_entry_defaults_to_password():
("add_totp", ("totp", TEST_SEED)),
("add_ssh_key", ("ssh", TEST_SEED)),
("add_pgp_key", ("pgp", TEST_SEED)),
("add_nostr_key", ("nostr",)),
("add_nostr_key", ("nostr", TEST_SEED)),
("add_seed", ("seed", TEST_SEED)),
("add_key_value", ("label", "k1", "val")),
("add_managed_account", ("acct", TEST_SEED)),

View File

@@ -49,7 +49,7 @@ class FakeEntries:
self.added.append(("pgp", label))
return 1
def add_nostr_key(self, label):
def add_nostr_key(self, label, seed=None):
self.added.append(("nostr", label))
return 1

View File

@@ -0,0 +1,66 @@
import pytest
from pathlib import Path
from helpers import create_vault, TEST_SEED, TEST_PASSWORD
from seedpass.core.entry_management import EntryManager
from seedpass.core.backup import BackupManager
from seedpass.core.config_manager import ConfigManager
def setup_mgr(tmp_path: Path) -> EntryManager:
vault, _ = create_vault(tmp_path, TEST_SEED, TEST_PASSWORD)
cfg = ConfigManager(vault, tmp_path)
backup = BackupManager(tmp_path, cfg)
return EntryManager(vault, backup)
def test_add_totp_invalid_secret(tmp_path: Path):
mgr = setup_mgr(tmp_path)
with pytest.raises(ValueError):
mgr.add_totp("bad", TEST_SEED, secret="notbase32!")
def test_add_ssh_key_validation_failure(monkeypatch, tmp_path: Path):
mgr = setup_mgr(tmp_path)
monkeypatch.setattr(
"seedpass.core.entry_management.validate_ssh_key_pair", lambda p, q: False
)
with pytest.raises(ValueError):
mgr.add_ssh_key("ssh", TEST_SEED)
def test_add_pgp_key_validation_failure(monkeypatch, tmp_path: Path):
mgr = setup_mgr(tmp_path)
monkeypatch.setattr(
"seedpass.core.entry_management.validate_pgp_private_key", lambda p, q: False
)
with pytest.raises(ValueError):
mgr.add_pgp_key("pgp", TEST_SEED, user_id="test")
def test_add_nostr_key_validation_failure(monkeypatch, tmp_path: Path):
mgr = setup_mgr(tmp_path)
monkeypatch.setattr(
"seedpass.core.entry_management.validate_nostr_keys", lambda p, q: False
)
with pytest.raises(ValueError):
mgr.add_nostr_key("nostr", TEST_SEED)
def test_add_seed_validation_failure(monkeypatch, tmp_path: Path):
mgr = setup_mgr(tmp_path)
monkeypatch.setattr(
"seedpass.core.entry_management.validate_seed_phrase", lambda p: False
)
with pytest.raises(ValueError):
mgr.add_seed("seed", TEST_SEED)
def test_add_managed_account_validation_failure(monkeypatch, tmp_path: Path):
mgr = setup_mgr(tmp_path)
monkeypatch.setattr(
"seedpass.core.entry_management.validate_seed_phrase", lambda p: False
)
with pytest.raises(ValueError):
mgr.add_managed_account("acct", TEST_SEED)

View File

@@ -246,7 +246,7 @@ def test_show_nostr_entry_details(monkeypatch, capsys):
with TemporaryDirectory() as tmpdir:
tmp_path = Path(tmpdir)
pm, entry_mgr = _setup_manager(tmp_path)
idx = entry_mgr.add_nostr_key("nostr")
idx = entry_mgr.add_nostr_key("nostr", TEST_SEED)
called = _detail_common(monkeypatch, pm)
@@ -339,7 +339,7 @@ def test_show_entry_details_sensitive(monkeypatch, capsys, entry_type):
expected = priv
extra = fp
elif entry_type == "nostr":
idx = entry_mgr.add_nostr_key("nostr")
idx = entry_mgr.add_nostr_key("nostr", TEST_SEED)
_npub, nsec = entry_mgr.get_nostr_key_pair(idx, TEST_SEED)
expected = nsec
elif entry_type == "totp":

View File

@@ -22,7 +22,7 @@ def test_nostr_key_determinism():
backup_mgr = BackupManager(tmp_path, cfg_mgr)
entry_mgr = EntryManager(vault, backup_mgr)
idx = entry_mgr.add_nostr_key("main")
idx = entry_mgr.add_nostr_key("main", TEST_SEED)
entry = entry_mgr.retrieve_entry(idx)
assert entry == {
"type": "nostr",

View File

@@ -42,7 +42,7 @@ def test_show_qr_for_nostr_keys(monkeypatch):
pm.is_dirty = False
pm.secret_mode_enabled = False
idx = entry_mgr.add_nostr_key("main")
idx = entry_mgr.add_nostr_key("main", TEST_SEED)
npub, _ = entry_mgr.get_nostr_key_pair(idx, TEST_SEED)
inputs = iter([str(idx), "q", "p", ""])
@@ -78,7 +78,7 @@ def test_show_private_key_qr(monkeypatch, capsys):
pm.is_dirty = False
pm.secret_mode_enabled = False
idx = entry_mgr.add_nostr_key("main")
idx = entry_mgr.add_nostr_key("main", TEST_SEED)
_, nsec = entry_mgr.get_nostr_key_pair(idx, TEST_SEED)
inputs = iter([str(idx), "q", "k", ""])
@@ -116,7 +116,7 @@ def test_qr_menu_case_insensitive(monkeypatch):
pm.is_dirty = False
pm.secret_mode_enabled = False
idx = entry_mgr.add_nostr_key("main")
idx = entry_mgr.add_nostr_key("main", TEST_SEED)
npub, _ = entry_mgr.get_nostr_key_pair(idx, TEST_SEED)
# Modify index to use uppercase type/kind

View File

@@ -20,7 +20,7 @@ import pytest
(lambda mgr: mgr.add_seed("seed", TEST_SEED), True),
(lambda mgr: mgr.add_pgp_key("pgp", TEST_SEED, user_id="test"), True),
(lambda mgr: mgr.add_ssh_key("ssh", TEST_SEED), True),
(lambda mgr: mgr.add_nostr_key("nostr"), False),
(lambda mgr: mgr.add_nostr_key("nostr", TEST_SEED), False),
],
)
def test_pause_before_entry_actions(monkeypatch, adder, needs_confirm):