Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions backend/app/keystore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,36 @@
# File is created with 0600 permissions (owner read/write only).

import json
import platform
import subprocess
from pathlib import Path

from app.logger import log

_CONFIG_DIR = Path.home() / ".bodhirax"
_CONFIG_FILE = _CONFIG_DIR / "config.json"


def _restrict_permissions(path: Path) -> None:
if platform.system() == "Windows":
result = subprocess.run(
["icacls", str(path), "/inheritance:r", "/grant:r", "%USERNAME%:F"],
capture_output=True,
)
if result.returncode != 0:
log.warning(
"keystore file ACLs could not be restricted on Windows; "
"file is protected only by your user profile"
)
else:
path.chmod(0o600)


def _ensure_config() -> None:
_CONFIG_DIR.mkdir(mode=0o700, parents=True, exist_ok=True)
if not _CONFIG_FILE.exists():
_CONFIG_FILE.write_text("{}")
_CONFIG_FILE.chmod(0o600)
_restrict_permissions(_CONFIG_FILE)


def _read() -> dict:
Expand All @@ -26,7 +45,7 @@ def _read() -> dict:
def _write(data: dict) -> None:
_ensure_config()
_CONFIG_FILE.write_text(json.dumps(data, indent=2))
_CONFIG_FILE.chmod(0o600)
_restrict_permissions(_CONFIG_FILE)


def get_key(provider: str) -> str | None:
Expand Down