Преглед изворни кода

Allow to read attachment as well

jherve пре 1 месец
родитељ
комит
7e7d17caa0
1 измењених фајлова са 18 додато и 1 уклоњено
  1. 18 1
      start.py

+ 18 - 1
start.py

@@ -25,8 +25,15 @@ class KeePass:
     def read_entry_attribute(self, key, attribute):
     def read_entry_attribute(self, key, attribute):
         return self._exec(["show", "-a", attribute, self.path, key]).strip()
         return self._exec(["show", "-a", attribute, self.path, key]).strip()
 
 
+    def read_entry_attachment(self, key, attachment):
+        return self._exec(["attachment-export", "--stdout", self.path, key, attachment, "/dev/null"])
+
     def _exec(self, args: list[Any]):
     def _exec(self, args: list[Any]):
-        return subprocess.check_output([self.bin] + args, text=True)
+        try:
+            return subprocess.check_output([self.bin] + args, text=True)
+        except subprocess.CalledProcessError as e:
+            print("\nThere was an error on call to keepass, please check the outout")
+            exit(1)
 
 
     @classmethod
     @classmethod
     def new(cls, path: Path):
     def new(cls, path: Path):
@@ -37,6 +44,7 @@ class KeePass:
 class SecretType(StrEnum):
 class SecretType(StrEnum):
     File="file"
     File="file"
     KeepassAttribute="keepass-attribute"
     KeepassAttribute="keepass-attribute"
+    KeepassAttachment="keepass-attachment"
 
 
 
 
 @dataclass
 @dataclass
@@ -47,6 +55,7 @@ class Secret:
     host_path: Path | None = None
     host_path: Path | None = None
     key: str | None = None
     key: str | None = None
     attribute: str | None  = None
     attribute: str | None  = None
+    attachment: str | None = None
 
 
     def create(self, keepass: KeePass):
     def create(self, keepass: KeePass):
         match self.type:
         match self.type:
@@ -61,6 +70,12 @@ class Secret:
                 print(args)
                 print(args)
                 subprocess.run(args, input=value.encode())
                 subprocess.run(args, input=value.encode())
 
 
+            case SecretType.KeepassAttachment:
+                value = keepass.read_entry_attachment(self.key, self.attachment)
+                args = ["podman", "secret", "create", "--replace", self.name, "-"]
+                print(args)
+                subprocess.run(args, input=value.encode())
+
     @classmethod
     @classmethod
     def from_line(cls, line: str):
     def from_line(cls, line: str):
         type_, *args = line.split(",")
         type_, *args = line.split(",")
@@ -70,6 +85,8 @@ class Secret:
                 return cls(host_path=path, name=path.name, mode=0o0400, type=SecretType.File)
                 return cls(host_path=path, name=path.name, mode=0o0400, type=SecretType.File)
             case (SecretType.KeepassAttribute, key, attribute):
             case (SecretType.KeepassAttribute, key, attribute):
                 return cls(name=key, key=key, mode=0o0400, type=SecretType.KeepassAttribute, attribute=attribute)
                 return cls(name=key, key=key, mode=0o0400, type=SecretType.KeepassAttribute, attribute=attribute)
+            case (SecretType.KeepassAttachment, key, attachment):
+                return cls(name=key, key=key, mode=0o0400, type=SecretType.KeepassAttachment, attachment=attachment)
 
 
     @classmethod
     @classmethod
     def read_sources(cls, hostname: str, login: str) -> list["Secret"]:
     def read_sources(cls, hostname: str, login: str) -> list["Secret"]: