From 8ad90aa44f646488e3c4c9b90c550b4791a9dbca Mon Sep 17 00:00:00 2001 From: DavHau Date: Sun, 1 Sep 2024 16:10:00 +0200 Subject: [PATCH] vars: fix listing vars + add test --- pkgs/clan-cli/clan_cli/vars/_types.py | 8 ++++++++ pkgs/clan-cli/clan_cli/vars/list.py | 12 +++++++++--- .../clan_cli/vars/public_modules/__init__.py | 4 ++++ .../clan_cli/vars/secret_modules/__init__.py | 4 ++++ pkgs/clan-cli/tests/test_vars.py | 11 ++++++++++- 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/vars/_types.py b/pkgs/clan-cli/clan_cli/vars/_types.py index d030f7659..9612af8c6 100644 --- a/pkgs/clan-cli/clan_cli/vars/_types.py +++ b/pkgs/clan-cli/clan_cli/vars/_types.py @@ -33,10 +33,18 @@ class StoreBase(ABC): def get(self, service: str, name: str, shared: bool = False) -> bytes: pass + @property + @abstractmethod + def is_secret_store(self) -> bool: + pass + def get_all(self) -> list[Var]: all_vars = [] for gen_name, generator in self.machine.vars_generators.items(): for f_name, file in generator["files"].items(): + # only handle vars compatible to this store + if self.is_secret_store != file["secret"]: + continue all_vars.append( Var( store=self, diff --git a/pkgs/clan-cli/clan_cli/vars/list.py b/pkgs/clan-cli/clan_cli/vars/list.py index 1778ebc31..c06862bb9 100644 --- a/pkgs/clan-cli/clan_cli/vars/list.py +++ b/pkgs/clan-cli/clan_cli/vars/list.py @@ -18,11 +18,17 @@ def get_all_vars(machine: Machine) -> list[Var]: return public_vars_store.get_all() + secret_vars_store.get_all() +def stringify_vars(_vars: list[Var]) -> str: + return "\n".join([str(var) for var in _vars]) + + +def stringify_all_vars(machine: Machine) -> str: + return stringify_vars(get_all_vars(machine)) + + def get_command(args: argparse.Namespace) -> None: machine = Machine(name=args.machine, flake=args.flake) - - for var in get_all_vars(machine): - print(var) + print(stringify_all_vars(machine)) def register_list_parser(parser: argparse.ArgumentParser) -> None: diff --git a/pkgs/clan-cli/clan_cli/vars/public_modules/__init__.py b/pkgs/clan-cli/clan_cli/vars/public_modules/__init__.py index 56e81ae9b..add64b018 100644 --- a/pkgs/clan-cli/clan_cli/vars/public_modules/__init__.py +++ b/pkgs/clan-cli/clan_cli/vars/public_modules/__init__.py @@ -5,6 +5,10 @@ from clan_cli.vars._types import StoreBase class FactStoreBase(StoreBase): + @property + def is_secret_store(self) -> bool: + return False + @abstractmethod def set( self, service: str, name: str, value: bytes, shared: bool = False diff --git a/pkgs/clan-cli/clan_cli/vars/secret_modules/__init__.py b/pkgs/clan-cli/clan_cli/vars/secret_modules/__init__.py index 777f7ac08..41d107220 100644 --- a/pkgs/clan-cli/clan_cli/vars/secret_modules/__init__.py +++ b/pkgs/clan-cli/clan_cli/vars/secret_modules/__init__.py @@ -17,6 +17,10 @@ class SecretStoreBase(StoreBase): ) -> Path | None: pass + @property + def is_secret_store(self) -> bool: + return True + def update_check(self) -> bool: return False diff --git a/pkgs/clan-cli/tests/test_vars.py b/pkgs/clan-cli/tests/test_vars.py index cdb55e29e..abd52295c 100644 --- a/pkgs/clan-cli/tests/test_vars.py +++ b/pkgs/clan-cli/tests/test_vars.py @@ -13,6 +13,7 @@ from root import CLAN_CORE from clan_cli.clan_uri import FlakeId from clan_cli.machines.machines import Machine from clan_cli.nix import nix_shell +from clan_cli.vars.list import stringify_all_vars from clan_cli.vars.public_modules import in_repo from clan_cli.vars.secret_modules import password_store, sops @@ -66,7 +67,6 @@ def test_dependencies_as_files() -> None: def test_generate_public_var( monkeypatch: pytest.MonkeyPatch, temporary_home: Path, - # age_keys: list["KeyPair"], ) -> None: config = nested_dict() my_generator = config["clan"]["core"]["vars"]["generators"]["my_generator"] @@ -84,6 +84,9 @@ def test_generate_public_var( ) assert store.exists("my_generator", "my_value") assert store.get("my_generator", "my_value").decode() == "hello\n" + machine = Machine(name="my_machine", flake=FlakeId(str(flake.path))) + vars_text = stringify_all_vars(machine) + assert "my_generator/my_value: hello" in vars_text @pytest.mark.impure @@ -113,6 +116,9 @@ def test_generate_secret_var_sops( ) assert sops_store.exists("my_generator", "my_secret") assert sops_store.get("my_generator", "my_secret").decode() == "hello\n" + machine = Machine(name="my_machine", flake=FlakeId(str(flake.path))) + vars_text = stringify_all_vars(machine) + assert "my_generator/my_secret" in vars_text @pytest.mark.impure @@ -194,6 +200,9 @@ def test_generate_secret_var_password_store( ) assert store.exists("my_generator", "my_secret") assert store.get("my_generator", "my_secret").decode() == "hello\n" + machine = Machine(name="my_machine", flake=FlakeId(str(flake.path))) + vars_text = stringify_all_vars(machine) + assert "my_generator/my_secret" in vars_text @pytest.mark.impure