mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-05 07:25:08 +00:00
feat(kv)!: take KID:KEY pairs and key files as kv add arguments with optional --service and --vault
This commit is contained in:
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
import click
|
||||
import pytest
|
||||
from click.testing import CliRunner
|
||||
|
||||
@@ -12,6 +13,13 @@ from unshackle.vaults.SQLite import SQLite
|
||||
|
||||
KID = "0" * 31 + "1"
|
||||
KEY = "a" * 32
|
||||
DECRYPT_LABS_VAULT = {
|
||||
"type": "HTTP",
|
||||
"name": "labs",
|
||||
"host": "https://vault.example/",
|
||||
"api_key": "tok",
|
||||
"api_mode": "decrypt_labs",
|
||||
}
|
||||
QUERY_VAULT = {"type": "HTTP", "name": "shared", "host": "https://vault.example/", "password": "tok", "username": "u"}
|
||||
|
||||
|
||||
@@ -87,7 +95,7 @@ def test_kv_add_warns_and_skips_a_vault_that_refuses_and_adds_to_the_next(
|
||||
refuse_example(monkeypatch)
|
||||
|
||||
with caplog.at_level(logging.WARNING, logger="kv"):
|
||||
result = CliRunner().invoke(kv_module.kv, ["add", str(keys), "EXAMPLE", "shared", "local"])
|
||||
result = CliRunner().invoke(kv_module.kv, ["add", str(keys), "-s", "EXAMPLE", "-v", "shared", "-v", "local"])
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert SQLite("check", dest).get_key(KID, "EXAMPLE") == KEY
|
||||
@@ -101,12 +109,109 @@ def test_kv_add_skips_a_null_key_line_and_adds_the_rest(tmp_path: Path, monkeypa
|
||||
null_kid = "0" * 31 + "2"
|
||||
keys.write_text(f"{KID}:{KEY}\n{null_kid}:{'0' * 32}\n", encoding="utf8")
|
||||
dest = tmp_path / "local.db"
|
||||
monkeypatch.setattr(kv_module.config, "key_vaults", [{"type": "SQLite", "name": "local", "path": str(dest)}])
|
||||
monkeypatch.setattr(kv_module.Services, "get_vault_tag", lambda name: name)
|
||||
local_vaults(monkeypatch, dest)
|
||||
|
||||
result = CliRunner().invoke(kv_module.kv, ["add", str(keys), "TESTSVC", "local"])
|
||||
result = CliRunner().invoke(kv_module.kv, ["add", str(keys), "-s", "TESTSVC", "-v", "local"])
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
check = SQLite("check", dest)
|
||||
assert check.get_key(KID, "TESTSVC") == KEY
|
||||
assert check.get_key(null_kid, "TESTSVC") is None
|
||||
|
||||
|
||||
def local_vaults(monkeypatch: pytest.MonkeyPatch, *paths: Path, no_push: Path | None = None) -> None:
|
||||
"""Configure one SQLite vault per path, plus a no_push one when given, and leave service tags as given."""
|
||||
vaults = [{"type": "SQLite", "name": p.stem, "path": str(p)} for p in paths]
|
||||
if no_push:
|
||||
vaults.append({"type": "SQLite", "name": no_push.stem, "path": str(no_push), "no_push": True})
|
||||
monkeypatch.setattr(kv_module.config, "key_vaults", vaults)
|
||||
monkeypatch.setattr(kv_module.Services, "get_vault_tag", lambda name: name)
|
||||
|
||||
|
||||
def test_kv_add_without_vault_writes_every_configured_vault_even_where_one_already_holds_the_pair(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
first, second, read_only = tmp_path / "local.db", tmp_path / "remote.db", tmp_path / "read_only.db"
|
||||
SQLite("seed", first).add_key("EXAMPLE", KID, KEY)
|
||||
SQLite("seed", read_only).add_key("OTHER", KID, KEY)
|
||||
local_vaults(monkeypatch, first, second, no_push=read_only)
|
||||
monkeypatch.setattr(kv_module.config, "key_vaults", [*kv_module.config.key_vaults, DECRYPT_LABS_VAULT])
|
||||
|
||||
with caplog.at_level(logging.INFO, logger="kv"):
|
||||
result = CliRunner().invoke(kv_module.kv, ["add", f"{KID}:{KEY}", "--service", "EXAMPLE"])
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert SQLite("check", second).get_key(KID, "EXAMPLE") == KEY
|
||||
assert SQLite("check", read_only).get_key(KID, "EXAMPLE") is None
|
||||
lines = [r.getMessage() for r in caplog.records]
|
||||
assert not any("labs" in line for line in lines)
|
||||
assert "local SQLite: 0 newly added, 1 already existed (skipped)" in lines
|
||||
assert "remote SQLite: 1 newly added, 0 already existed (skipped)" in lines
|
||||
|
||||
|
||||
def test_kv_add_takes_a_dashed_pair_and_a_bom_key_file_into_a_vault_file(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
other_kid = "0" * 31 + "3"
|
||||
keys = tmp_path / "keys.txt"
|
||||
keys.write_text(f"{other_kid}:{KEY}\nnot a pair\n", encoding="utf-8-sig")
|
||||
dest = tmp_path / "file.db"
|
||||
dest.touch()
|
||||
local_vaults(monkeypatch)
|
||||
dashed = "00000000-0000-0000-0000-00000000000A"
|
||||
|
||||
result = CliRunner().invoke(
|
||||
kv_module.kv, ["add", "-s", "EXAMPLE", "-v", str(dest), f"{dashed}:{KEY.upper()}", str(keys)]
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
check = SQLite("check", dest)
|
||||
assert check.get_key("0" * 31 + "a", "EXAMPLE") == KEY
|
||||
assert check.get_key(other_kid, "EXAMPLE") == KEY
|
||||
|
||||
|
||||
def test_kv_add_without_service_writes_the_generic_table(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
dest = tmp_path / "local.db"
|
||||
local_vaults(monkeypatch, dest)
|
||||
|
||||
result = CliRunner().invoke(kv_module.kv, ["add", f"{KID}:{KEY}", "-v", "local"])
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert SQLite("check", dest).get_key(KID, "generic") == KEY
|
||||
|
||||
|
||||
@pytest.mark.parametrize("no_push", [False, True])
|
||||
def test_kv_add_without_vault_stops_when_no_configured_vault_takes_pushes(
|
||||
no_push: bool, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
local_vaults(monkeypatch, no_push=tmp_path / "read_only.db" if no_push else None)
|
||||
|
||||
result = CliRunner().invoke(kv_module.kv, ["add", f"{KID}:{KEY}", "-s", "EXAMPLE"], standalone_mode=False)
|
||||
|
||||
assert isinstance(result.exception, click.ClickException)
|
||||
assert result.exception.message == "No Key Vaults without no_push are configured."
|
||||
|
||||
|
||||
@pytest.mark.parametrize("arg", [f"{KID}:{KEY[:-1]}", f"{KID}{KEY}", "missing.txt"])
|
||||
def test_kv_add_rejects_an_argument_that_is_neither_a_pair_nor_a_file(
|
||||
arg: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
local_vaults(monkeypatch, tmp_path / "local.db")
|
||||
monkeypatch.chdir(tmp_path)
|
||||
|
||||
result = CliRunner().invoke(kv_module.kv, ["add", arg, "-s", "EXAMPLE"], standalone_mode=False)
|
||||
|
||||
assert isinstance(result.exception, click.ClickException)
|
||||
assert result.exception.message == f"Argument ({arg}) is not a KID:KEY or an existing file."
|
||||
assert not (tmp_path / "local.db").exists()
|
||||
|
||||
|
||||
def test_kv_add_names_a_key_file_that_is_not_utf8(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
keys = tmp_path / "keys.txt"
|
||||
keys.write_bytes(b"\xff\xfe" + f"{KID}:{KEY}".encode("utf-16-le"))
|
||||
local_vaults(monkeypatch, tmp_path / "local.db")
|
||||
|
||||
result = CliRunner().invoke(kv_module.kv, ["add", str(keys), "-s", "EXAMPLE"], standalone_mode=False)
|
||||
|
||||
assert isinstance(result.exception, click.ClickException)
|
||||
assert result.exception.message.startswith(f"Could not read {keys} (")
|
||||
|
||||
Reference in New Issue
Block a user