Skip to content
Python
13.0 KB
Raw Blame
"""Secret store — one canonical, file-backed store for connector/MCP credentials.

Design (from OpenClaw): secrets **never enter the model's context, prompts, or traces**.
The store holds profiles keyed by `connector[:account]`; values may be literals OR
`${ENV_VAR}` references resolved at read time from the process env / `~/.config/coworker/.env`.

v1 is a `0600` JSON file behind this interface; the interface is what callers depend on, so
a Keychain / age-encrypted backend can swap in later without touching them.
"""

from __future__ import annotations

import contextlib
import json
import logging
import os
import re
import subprocess
import tempfile
import sys
import threading
import time
from pathlib import Path
from typing import Any, Optional

_REF = re.compile(r"\$\{([A-Za-z_][A-Za-z0-9_]*)\}")
_IS_WINDOWS = sys.platform == "win32"


def state_dir() -> Path:
    """Where coworker keeps its state — the one cross-platform source of truth.

    Resolution order:
    1. `$COWORKER_STATE_DIR` — explicit override on any OS (used by tests/sidecars).
    2. Windows: `%APPDATA%\\coworker` (e.g. `C:\\Users\\You\\AppData\\Roaming\\coworker`),
       the native per-user app-data location.
    3. macOS / Linux: `~/.config/coworker` (XDG-style, unchanged from prior behavior).
    """
    base = os.environ.get("COWORKER_STATE_DIR")
    if base:
        return Path(base).expanduser()
    # A box confined to OPENWORKER_BASE_DIR keeps its state there too
    # (coworker/basedir.py) — nothing of the machine's lives outside it.
    confined = os.environ.get("OPENWORKER_BASE_DIR", "").strip()
    if confined:
        return Path(confined).expanduser() / "state"
    if sys.platform == "win32":
        appdata = os.environ.get("APPDATA")
        if appdata:
            return Path(appdata) / "coworker"
    return Path.home() / ".config" / "coworker"


def _load_dotenv(path: Path) -> dict[str, str]:
    env: dict[str, str] = {}
    if not path.is_file():
        return env
    for line in path.read_text(encoding="utf-8").splitlines():
        line = line.strip()
        if not line or line.startswith("#") or "=" not in line:
            continue
        key, value = line.split("=", 1)
        env[key.strip()] = value.strip().strip('"').strip("'")
    return env


def _restrict_to_user(path: Path, *, is_dir: bool) -> None:
    """Restrict a path so only the current user can access it.

    POSIX expresses this with mode bits (0700 dir / 0600 file). Windows has no such bits —
    `os.chmod` there only toggles the read-only flag, so a 0600 chmod is a silent no-op and
    the file inherits broad ACLs (SYSTEM, Administrators, …). Use an ACL instead: strip
    inherited entries and grant the current user alone. Best-effort on Windows so a transient
    icacls failure never blocks saving a key."""
    if _IS_WINDOWS:
        user = os.environ.get("USERNAME")
        if not user:
            return
        domain = os.environ.get("USERDOMAIN")
        account = f"{domain}\\{user}" if domain else user
        # A directory grant MUST be inheritable — (OI) object-inherit for files, (CI)
        # container-inherit for subdirs — so everything created inside (the SQLite stores,
        # conversations, …) inherits the user's access. Without these flags, /inheritance:r
        # leaves the directory with a non-inheritable ACE and any child file ends up with an
        # empty DACL → sqlite3 "unable to open database file", crashing the server on launch.
        grant = f"{account}:(OI)(CI)F" if is_dir else f"{account}:F"
        try:
            subprocess.run(
                ["icacls", str(path), "/inheritance:r", "/grant:r", grant],
                capture_output=True,
                check=False,
            )
        except OSError:
            pass
        return
    os.chmod(path, 0o700 if is_dir else 0o600)


def _atomic_private_write(target: Path, content: str) -> Path:
    """Write `content` to `target` atomically, never exposing it through a readable temp.

    The temp file used to be created by `Path.write_text` and only chmod-ed afterwards, so
    the plaintext sat on disk at the umask default (0644 on a normal box) for the length of
    the write — readable by every local process and by anything backing the directory up.
    That is issue #143; the same pattern was in both writers here.

    `tempfile.mkstemp` creates with 0600 and O_EXCL before a byte is written, which also
    removes the fixed `<name>.tmp` filename. That name was predictable, so a local attacker
    could pre-create it as a symlink and have the write land wherever the link pointed.

    Windows gets no mode bits from mkstemp, so the ACL is applied to the still-empty file
    before the content goes in.
    """
    target.parent.mkdir(parents=True, exist_ok=True)
    try:
        _restrict_to_user(target.parent, is_dir=True)
    except OSError:
        pass

    fd, tmp_name = tempfile.mkstemp(
        dir=str(target.parent), prefix=f".{target.name}.", suffix=".tmp"
    )
    tmp = Path(tmp_name)
    try:
        _restrict_to_user(tmp, is_dir=False)
        with os.fdopen(fd, "w", encoding="utf-8") as fh:
            fh.write(content)
        os.replace(tmp, target)
    except BaseException:
        try:
            tmp.unlink()
        except OSError:
            pass
        raise
    return target


def write_private_text(path: str | Path, content: str) -> Path:
    """Atomically write a user-only text file using the SecretStore's OS protections."""
    return _atomic_private_write(Path(path).expanduser(), content)


class _StoreLock:
    """One lock per secrets file, across threads AND processes.

    Several OpenWorker processes can share a state folder: the app's server, and each
    `openworker run`. A change to the file is read, modify, write of the WHOLE file, so
    without this two processes can each write and one change is lost. A sign-in renewal
    is worse: two processes renewing the same sign-in at once can leave it cancelled at a
    vendor that hands out a new refresh token each time.

    Inside the process it is a re-entrant lock shared by every SecretStore on the same
    file. Across processes it is an advisory lock on `<file>.lock` (flock; byte-range
    locking on Windows), taken with a deadline: if another process holds it for too long,
    this one goes ahead without it rather than stall a turn on a stuck peer."""

    def __init__(self, path: Path) -> None:
        self.path = path.with_name(path.name + ".lock")
        self._thread = threading.RLock()
        self._depth = 0
        self._fh: Any = None

    def acquire(self, timeout: float) -> None:
        self._thread.acquire()
        if self._depth == 0:
            self._fh = self._lock_file(timeout)
        self._depth += 1

    def release(self) -> None:
        self._depth -= 1
        if self._depth == 0 and self._fh is not None:
            fh, self._fh = self._fh, None
            try:
                if _IS_WINDOWS:
                    import msvcrt

                    fh.seek(0)
                    msvcrt.locking(fh.fileno(), msvcrt.LK_UNLCK, 1)
                else:
                    import fcntl

                    fcntl.flock(fh.fileno(), fcntl.LOCK_UN)
            except (OSError, ImportError):
                pass
            fh.close()
        self._thread.release()

    def _lock_file(self, timeout: float) -> Any:
        from .statelock import _try_lock

        try:
            self.path.parent.mkdir(parents=True, exist_ok=True)
            # Made private from the first moment, like every file beside the secrets. It
            # is empty and stays: it is the lock, not a temporary file.
            fh = os.fdopen(os.open(self.path, os.O_RDWR | os.O_CREAT, 0o600), "r+")
        except OSError:
            return None  # a read-only folder: nothing could be written there anyway
        try:
            _restrict_to_user(self.path, is_dir=False)
        except OSError:
            pass
        deadline = time.monotonic() + max(0.0, timeout)
        while True:
            if _try_lock(fh):
                return fh
            if time.monotonic() >= deadline:
                logging.getLogger(__name__).warning(
                    "the stored sign-ins stayed locked by another process for %.0fs; going ahead without the lock",
                    timeout,
                )
                fh.close()
                return None
            time.sleep(0.05)


_STORE_LOCKS: dict[str, _StoreLock] = {}
_STORE_LOCKS_GUARD = threading.Lock()


def _store_lock(path: Path) -> _StoreLock:
    key = os.path.realpath(str(path))
    with _STORE_LOCKS_GUARD:
        lock = _STORE_LOCKS.get(key)
        if lock is None:
            lock = _STORE_LOCKS[key] = _StoreLock(Path(key))
        return lock


class SecretStore:
    """File-backed secret store. Reads resolve `${VAR}` refs; status never leaks values."""

    def __init__(self, path: Optional[str | Path] = None) -> None:
        self.path = Path(path).expanduser() if path else state_dir() / "secrets.json"
        self._dotenv_path = self.path.parent / ".env"
        self._lock = _store_lock(self.path)

    @contextlib.contextmanager
    def exclusive(self, timeout: float = 30.0):
        """Hold the store for a read-then-write that no other thread or process may
        interleave with. A sign-in renewal takes it, reads the profile AGAIN inside, and
        renews only if nobody else just did: the first process renews, the others read
        its result. Re-entrant: `put` and `delete` inside it are fine."""
        self._lock.acquire(timeout)
        try:
            yield self
        finally:
            self._lock.release()

    # -- reads ------------------------------------------------------------------
    def get(self, profile: str) -> Optional[dict[str, Any]]:
        """Return a profile with `${VAR}` refs resolved, or None if absent."""
        data = self._read().get(profile)
        if data is None:
            return None
        return self.resolve(data)

    def resolve(self, value: Any) -> Any:
        """Resolve `${VAR}` refs in a value (recursively) from env + the local `.env`."""
        env = _load_dotenv(self._dotenv_path)

        def _walk(v: Any) -> Any:
            if isinstance(v, str):
                return _REF.sub(
                    lambda m: os.environ.get(m.group(1))
                    or env.get(m.group(1))
                    or m.group(0),
                    v,
                )
            if isinstance(v, dict):
                return {k: _walk(x) for k, x in v.items()}
            if isinstance(v, list):
                return [_walk(x) for x in v]
            return v

        return _walk(value)

    def status(self) -> list[dict[str, Any]]:
        """Profile metadata only — **never** the secret values themselves."""
        out: list[dict[str, Any]] = []
        for profile, data in self._read().items():
            data = data if isinstance(data, dict) else {}
            expires = data.get("expires")
            expired = isinstance(expires, (int, float)) and expires < time.time()
            out.append(
                {
                    "profile": profile,
                    "type": data.get("type"),
                    "account": data.get("account_id"),
                    "expired": bool(expired),
                }
            )
        return out

    # -- writes -----------------------------------------------------------------
    def put(self, profile: str, data: dict[str, Any]) -> None:
        with self.exclusive():
            store = self._read()
            store[profile] = data
            self._write(store)

    def delete(self, profile: str) -> bool:
        with self.exclusive():
            store = self._read()
            if profile not in store:
                return False
            del store[profile]
            self._write(store)
            return True

    # -- internals --------------------------------------------------------------
    def _read(self) -> dict[str, Any]:
        if not self.path.is_file():
            return {}
        try:
            return json.loads(self.path.read_text(encoding="utf-8"))
        except (OSError, json.JSONDecodeError):
            return {}

    def _write(self, store: dict[str, Any]) -> None:
        _atomic_private_write(self.path, json.dumps(store, indent=2))


class EphemeralSecretStore(SecretStore):
    """A SecretStore that never touches disk.

    Used to stage a connector grant that is destined for ANOTHER machine
    (machines spec §Remote OAuth): the normal storage layers run against this
    store, producing exactly the profile keys a fresh connect would write, and
    the result is sealed and shipped — the grant never lands in the local
    secrets file.
    """

    def __init__(self) -> None:
        super().__init__(path="/dev/null/ephemeral")  # never read or written
        self._data: dict[str, Any] = {}

    def _read(self) -> dict[str, Any]:
        return dict(self._data)

    def _write(self, store: dict[str, Any]) -> None:
        self._data = dict(store)

    def profiles(self) -> dict[str, Any]:
        """Every staged profile, by key — the payload a sealed deploy ships."""
        return dict(self._data)