Merge branch 'passwd' into develop

This commit is contained in:
Piotr Dec 2025-10-08 01:18:23 +02:00
commit 28706b9081
Signed by: stawros
GPG key ID: 74B18A3F0F1E99C0
9 changed files with 112 additions and 1 deletions

View file

@ -11,15 +11,23 @@ class AppConfig(BaseModel):
port: int = 8000 port: int = 8000
reload: bool = True reload: bool = True
class GitConfig(BaseModel): class GitConfig(BaseModel):
directory: str = "/opt/repo/sample" directory: str = "/opt/repo/sample"
branch: str = "master" branch: str = "master"
remote: str = "origin" remote: str = "origin"
class KeePassConfig(BaseModel):
file: str = "database.kdbx"
secret: Path | str = "/run/secrets/kp_secret"
class Settings(BaseSettings): class Settings(BaseSettings):
model_config = SettingsConfigDict(env_prefix="KARL_", env_nested_delimiter="__") model_config = SettingsConfigDict(env_prefix="KARL_", env_nested_delimiter="__")
app: AppConfig = AppConfig() app: AppConfig = AppConfig()
git: GitConfig = GitConfig() git: GitConfig = GitConfig()
kp: KeePassConfig = KeePassConfig()
@classmethod @classmethod
def from_yaml(cls, path: Path | str = "config/config.yaml") -> "Settings": def from_yaml(cls, path: Path | str = "config/config.yaml") -> "Settings":

49
app/model/passwords.py Normal file
View file

@ -0,0 +1,49 @@
from dataclasses import dataclass, field
from typing import Type
# TODO: unnecessary?
@dataclass
class PathItem:
name: str
t: Type
@dataclass
class Path:
path: list[PathItem] = field(default_factory=list)
def append(self, name, t):
self.path.append(PathItem(name, t))
def __str__(self):
return "/".join([i.name for i in self.path])
@dataclass
class Group:
name: str
passwords: list["Password"] = field(default_factory=list)
parent: "Group|None" = None
@property
def path(self):
if self.parent is None:
new_path = Path()
new_path.append(self.name, type(self))
return new_path
return self.parent.path.append(self.name, type(self))
@dataclass
class Password:
name: str
group: Group
@property
def path(self):
return self.group.path.append(self.name, type(self))
class UnencryptedPassword(Password):
def __init__(self, name: str, value: str, group: Group):
super().__init__(name, group)
self.value = value

38
app/services/passwords.py Normal file
View file

@ -0,0 +1,38 @@
import os.path
from pykeepass import PyKeePass, create_database, Group
class Passwords:
def __init__(self):
from app.config import get_settings
settings = get_settings()
with open(settings.kp.secret, "r") as fh:
secret = fh.read()
self._kp_org = self.__get_or_create_store(settings.kp.file, secret)
self._kp = self.__get_lock(settings.kp.file, secret)
@staticmethod
def __get_or_create_store(path, passwd) -> PyKeePass:
if os.path.exists(path):
return PyKeePass(
path,
password=passwd,
)
return create_database(path, passwd)
@staticmethod
def __get_lock(path, passwd) -> PyKeePass:
lock_path = path + ".lock"
import shutil
shutil.copyfile(path, lock_path)
return Passwords.__get_or_create_store(lock_path, passwd)
@property
def store(self):
return self._kp.root_group
def save(self, group: Group):
pass

1
app/services/system.py Normal file
View file

@ -0,0 +1 @@

0
app/util/__init__.py Normal file
View file

11
app/util/dicts.py Normal file
View file

@ -0,0 +1,11 @@
from types import SimpleNamespace
class NestedNamespace(SimpleNamespace):
def __init__(self, dictionary, **kwargs):
super().__init__(**kwargs)
for key, value in dictionary.items():
if isinstance(value, dict):
self.__setattr__(key, NestedNamespace(value))
else:
self.__setattr__(key, value)

View file

@ -2,3 +2,6 @@ app:
host: "127.0.0.1" host: "127.0.0.1"
port: 8000 port: 8000
reload: true reload: true
kp:
file: "config/kp.kdbx"
secret: "config/secret.txt"

View file

@ -11,7 +11,8 @@ dependencies = [
"jinja2>=3.1.4", "jinja2>=3.1.4",
"pydantic-settings>=2.4.0", "pydantic-settings>=2.4.0",
"pyyaml>=6.0.2", "pyyaml>=6.0.2",
"gitpython>=3.1.45" "gitpython>=3.1.45",
"pykeepass>=4.1.1.post1"
] ]
[project.optional-dependencies] [project.optional-dependencies]