import subprocess from pathlib import Path from dataclasses import dataclass from pc_backup.keepass import KeePass from pc_backup.podman import Podman @dataclass class Secret: name: str mode: int def create(self, keepass: KeePass): ... @classmethod def from_line(cls, line: str): name, type_, *args = line.split(",") match type_: case "file": sub_class = SecretFile case "keepass-attribute": sub_class = SecretKeepassAttribute case "keepass-attachment": sub_class = SecretKeepassAttachment case _: raise ValueError(f"Cannot read `{line}` as a secret spec") return sub_class.from_line(name, *args) @classmethod def read_sources(cls, file: Path) -> list["Secret"]: with open(file) as f: lines = f.readlines() return [cls.from_line(l.strip()) for l in lines] @dataclass class SecretKeepassAttachment(Secret): key: str attachment: str def create(self, keepass: KeePass): value = keepass.read_entry_attachment(self.key, self.attachment) Podman.secret_create(self.name, value=value.encode()) @classmethod def from_line(cls, name: str, key: str, attachment: str): return cls(name=name, key=key, mode=0o0400, attachment=attachment) @dataclass class SecretKeepassAttribute(Secret): key: str attribute: str def create(self, keepass: KeePass): value = keepass.read_entry_attribute(self.key, self.attribute) Podman.secret_create(self.name, value=value.encode()) @classmethod def from_line(cls, name: str, key: str, attribute: str): return cls(name=name, key=key, mode=0o0400, attribute=attribute) @dataclass class SecretFile(Secret): host_path: Path def create(self, keepass: KeePass): args = ["--replace", self.name, self.host_path] Podman.secret_create(self.name, host_path=self.host_path) @classmethod def from_line(cls, name: str, path: str): path = Path(path).expanduser() return cls(host_path=path, name=name, mode=0o0400)