Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix: support Windows data volume permission checks

FriedorixNietzsche80da6cee239a
diff --git a/deeptutor/knowledge/add_documents.py b/deeptutor/knowledge/add_documents.py
index 2a1680fc8..3832be829 100644
--- a/deeptutor/knowledge/add_documents.py
+++ b/deeptutor/knowledge/add_documents.py
@@ -11,7 +11,6 @@ import hashlib
import itertools
import json
import logging
-import os
from pathlib import Path
import shutil
from typing import TYPE_CHECKING, List, Optional
@@ -415,9 +414,7 @@ class DocumentAdder:
except PermissionError as e:
logger.exception("Permission denied while indexing %s: %s", doc_file.name, e)
raise DataVolumePermissionError(
- format_data_volume_permission_error(
- self.kb_dir, uid=os.geteuid(), gid=os.getegid(), cause=e
- )
+ format_data_volume_permission_error(self.kb_dir, cause=e)
) from e
except Exception as e:
logger.exception(f"Failed {doc_file.name}: {e}")
diff --git a/deeptutor/knowledge/initializer.py b/deeptutor/knowledge/initializer.py
index f59af7441..74ee212f3 100644
--- a/deeptutor/knowledge/initializer.py
+++ b/deeptutor/knowledge/initializer.py
@@ -8,7 +8,6 @@ import asyncio
from datetime import datetime
import json
import logging
-import os
from pathlib import Path
import shutil
from typing import TYPE_CHECKING, Optional
@@ -271,9 +270,7 @@ class KnowledgeBaseInitializer:
exc_info=True,
)
except PermissionError as e:
- error_msg = format_data_volume_permission_error(
- self.kb_dir, uid=os.geteuid(), gid=os.getegid(), cause=e
- )
+ error_msg = format_data_volume_permission_error(self.kb_dir, cause=e)
logger.error("Error processing documents: %s", error_msg)
self.progress_tracker.update(
ProgressStage.ERROR,
diff --git a/deeptutor/services/setup/data_volume.py b/deeptutor/services/setup/data_volume.py
index edf1d5328..7072bb191 100644
--- a/deeptutor/services/setup/data_volume.py
+++ b/deeptutor/services/setup/data_volume.py
@@ -27,6 +27,15 @@ class DataVolumePermissionError(PermissionError):
"""The data volume cannot be written by the process that will run the app."""
+def current_process_ids() -> tuple[int, int]:
+ """Return effective UID/GID, or -1 where POSIX process ids are unavailable."""
+ get_euid = getattr(os, "geteuid", None)
+ get_egid = getattr(os, "getegid", None)
+ uid = int(get_euid()) if callable(get_euid) else -1
+ gid = int(get_egid()) if callable(get_egid) else -1
+ return uid, gid
+
+
def parse_id(value: str | None, *, default: int, name: str) -> int:
"""Parse a PUID/PGID-style identifier, rejecting root and non-integers."""
raw = (value or "").strip() or str(default)
@@ -52,8 +61,11 @@ def resolve_runtime_ids(
try to ``setuid`` without ``CAP_SETUID``. Rootful Docker remaps to
``PUID``/``PGID`` (default 1000/1000).
"""
- current_uid = os.geteuid() if euid is None else euid
- current_gid = os.getegid() if egid is None else egid
+ current_uid, current_gid = current_process_ids()
+ if euid is not None:
+ current_uid = euid
+ if egid is not None:
+ current_gid = egid
if current_uid != 0:
return current_uid, current_gid
return (
@@ -75,11 +87,16 @@ def describe_path_ownership(path: Path) -> str:
def format_data_volume_permission_error(
path: Path,
*,
- uid: int,
- gid: int,
+ uid: int | None = None,
+ gid: int | None = None,
cause: BaseException | None = None,
) -> str:
"""Explain a UID mismatch in terms operators can act on (PUID/PGID)."""
+ current_uid, current_gid = current_process_ids()
+ if uid is None:
+ uid = current_uid
+ if gid is None:
+ gid = current_gid
owner = describe_path_ownership(path)
cause_txt = f" ({cause})" if cause else ""
return (
@@ -105,14 +122,19 @@ def ensure_data_volume_writable(
(the FastAPI backend) probe as themselves.
"""
target = Path(path)
- probe_uid = os.geteuid() if uid is None else uid
- probe_gid = os.getegid() if gid is None else gid
+ current_uid, current_gid = current_process_ids()
+ probe_uid = current_uid if uid is None else uid
+ probe_gid = current_gid if gid is None else gid
+ fork = getattr(os, "fork", None)
+ setuid = getattr(os, "setuid", None)
+ setgid = getattr(os, "setgid", None)
drop_privs = (
- os.geteuid() == 0
+ current_uid == 0
and probe_uid != 0
- and (probe_uid != os.geteuid() or probe_gid != os.getegid())
- and hasattr(os, "fork")
- and hasattr(os, "setuid")
+ and (probe_uid != current_uid or probe_gid != current_gid)
+ and callable(fork)
+ and callable(setuid)
+ and callable(setgid)
)
if drop_privs:
_ensure_writable_as(target, probe_uid, probe_gid)
@@ -151,15 +173,25 @@ def _try_write_or_raise(path: Path, uid: int, gid: int) -> None:
def _ensure_writable_as(path: Path, uid: int, gid: int) -> None:
"""Fork, drop to *uid*/*gid*, and probe. Parent raises on child failure."""
- pid = os.fork()
+ fork = getattr(os, "fork", None)
+ setgid = getattr(os, "setgid", None)
+ setuid = getattr(os, "setuid", None)
+ waitpid = getattr(os, "waitpid", None)
+ wifexited = getattr(os, "WIFEXITED", None)
+ wexitstatus = getattr(os, "WEXITSTATUS", None)
+
+ if not all(callable(func) for func in (fork, setgid, setuid, waitpid, wifexited, wexitstatus)):
+ raise RuntimeError("POSIX privilege-drop APIs are unavailable on this platform")
+
+ pid = fork()
if pid == 0: # pragma: no cover - child process
try:
- os.setgid(gid)
- os.setuid(uid)
+ setgid(gid)
+ setuid(uid)
_try_write_or_raise(path, uid, gid)
os._exit(0)
except OSError:
os._exit(1)
- _, status = os.waitpid(pid, 0)
- if not os.WIFEXITED(status) or os.WEXITSTATUS(status) != 0:
+ _, status = waitpid(pid, 0)
+ if not wifexited(status) or wexitstatus(status) != 0:
raise DataVolumePermissionError(format_data_volume_permission_error(path, uid=uid, gid=gid))
diff --git a/tests/services/setup/test_data_volume.py b/tests/services/setup/test_data_volume.py
index 2201ac3b3..5afde99b4 100644
--- a/tests/services/setup/test_data_volume.py
+++ b/tests/services/setup/test_data_volume.py
@@ -10,6 +10,7 @@ import pytest
from deeptutor.services.setup.data_volume import (
DataVolumePermissionError,
check_container_data_volume,
+ current_process_ids,
ensure_data_volume_writable,
format_data_volume_permission_error,
parse_id,
@@ -26,6 +27,38 @@ def test_parse_id_rejects_root_and_non_integers() -> None:
parse_id("alan", default=1000, name="PUID")
+def test_process_ids_and_writable_probe_work_without_posix_uid_apis(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.delattr(os, "geteuid", raising=False)
+ monkeypatch.delattr(os, "getegid", raising=False)
+
+ assert current_process_ids() == (-1, -1)
+ assert resolve_runtime_ids() == (-1, -1)
+
+ target = tmp_path / "windows-style-data"
+ ensure_data_volume_writable(target)
+
+ assert target.is_dir()
+ assert not any(target.glob(".deeptutor-write-probe-*"))
+
+
+def test_permission_error_formats_without_posix_uid_apis(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.delattr(os, "geteuid", raising=False)
+ monkeypatch.delattr(os, "getegid", raising=False)
+
+ message = format_data_volume_permission_error(
+ tmp_path / "knowledge_bases",
+ cause=PermissionError("denied"),
+ )
+
+ assert "euid=-1" in message
+ assert "egid=-1" in message
+ assert "denied" in message
+
+
def test_resolve_runtime_ids_keeps_rootless_identity() -> None:
uid, gid = resolve_runtime_ids(puid="99", pgid="100", euid=501, egid=20)
assert (uid, gid) == (501, 20)
@@ -61,9 +94,37 @@ def test_root_probe_defers_mkdir_until_after_identity_drop(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
target = tmp_path / "mounted" / "knowledge_bases"
- monkeypatch.setattr("deeptutor.services.setup.data_volume.os.geteuid", lambda: 0)
+ monkeypatch.setattr(
+ "deeptutor.services.setup.data_volume.os.geteuid",
+ lambda: 0,
+ raising=False,
+ )
+ monkeypatch.setattr(
+ "deeptutor.services.setup.data_volume.os.getegid",
+ lambda: 0,
+ raising=False,
+ )
+ monkeypatch.setattr(
+ "deeptutor.services.setup.data_volume.os.fork",
+ lambda: 1,
+ raising=False,
+ )
+ monkeypatch.setattr(
+ "deeptutor.services.setup.data_volume.os.setuid",
+ lambda _uid: None,
+ raising=False,
+ )
+ monkeypatch.setattr(
+ "deeptutor.services.setup.data_volume.os.setgid",
+ lambda _gid: None,
+ raising=False,
+ )
+
+ called = False
def create_as_runtime_user(path: Path, uid: int, gid: int) -> None:
+ nonlocal called
+ called = True
assert (uid, gid) == (1234, 1234)
assert not path.exists()
path.mkdir(parents=True)
@@ -72,20 +133,28 @@ def test_root_probe_defers_mkdir_until_after_identity_drop(
"deeptutor.services.setup.data_volume._ensure_writable_as", create_as_runtime_user
)
ensure_data_volume_writable(target, uid=1234, gid=1234)
+
+ assert called is True
assert target.is_dir()
-def test_ensure_unwritable_directory_fail_fasts(tmp_path: Path) -> None:
- if hasattr(os, "geteuid") and os.geteuid() == 0:
- pytest.skip("root bypasses directory mode bits")
+def test_ensure_unwritable_directory_fail_fasts(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
target = tmp_path / "locked"
target.mkdir()
- target.chmod(0o555)
- try:
- with pytest.raises(DataVolumePermissionError, match="not writable"):
- ensure_data_volume_writable(target)
- finally:
- target.chmod(0o755)
+
+ real_write_text = Path.write_text
+
+ def deny_probe_write(self: Path, *args, **kwargs):
+ if self.parent == target and self.name.startswith(".deeptutor-write-probe-"):
+ raise PermissionError("permission denied")
+ return real_write_text(self, *args, **kwargs)
+
+ monkeypatch.setattr(Path, "write_text", deny_probe_write)
+
+ with pytest.raises(DataVolumePermissionError, match="not writable"):
+ ensure_data_volume_writable(target)
def test_check_container_data_volume_probes_knowledge_bases(tmp_path: Path, monkeypatch) -> None: