Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

6.4 KB
Raw Blame
"""ODS managed Hermes login; never writes an operator's config or credentials.

Also mounted read-only as the Hermes entrypoint so s6's dashboard inherits the
same defaults as the gateway. Explicit upstream auth configuration wins.
"""
from __future__ import annotations

import hashlib
import hmac
import json
import os
from pathlib import Path

import yaml


def settings(env=None, config_path=None):
    env = os.environ if env is None else env
    if config_path is None:
        # The API must not need access to another UID's private Hermes home.
        policy_path = Path(env.get("HERMES_AUTH_POLICY_PATH", "/data/hermes-auth/policy.json"))
        try:
            policy = json.loads(policy_path.read_text())
        except (OSError, ValueError):
            policy = {}
        if not env.get("HERMES_DASHBOARD_BASIC_AUTH_PASSWORD"):
            seed_digest = hashlib.sha256(env.get("HERMES_DASHBOARD_SESSION_TOKEN", "").encode()).hexdigest()
            if (policy.get("schemaVersion") != 1 or policy.get("managed") is not True
                    or not hmac.compare_digest(str(policy.get("seedDigest", "")), seed_digest)):
                return None
        config_path = "/nonexistent-ods-hermes-config"
    try:
        config = yaml.safe_load(Path(config_path).read_text()) or {}
    except FileNotFoundError:
        config = {}
    dashboard = config.get("dashboard", {}) or {}
    basic = dashboard.get("basic_auth", {}) or {}
    username = env.get("HERMES_DASHBOARD_BASIC_AUTH_USERNAME") or basic.get("username")
    explicit_password = env.get("HERMES_DASHBOARD_BASIC_AUTH_PASSWORD")
    password_hash = env.get("HERMES_DASHBOARD_BASIC_AUTH_PASSWORD_HASH") or basic.get("password_hash")
    password = explicit_password or (basic.get("password") if not password_hash else None)
    if password:
        return {"username": username or "ods", "password": password, "managed": False}
    # A hash cannot be turned into a bridge password. Nor can ODS silently
    # replace an operator's OAuth or other dashboard provider with basic auth.
    oauth = dashboard.get("oauth", {}) or {}
    oidc = oauth.get("self_hosted", {}) or dashboard.get("self_hosted", {}) or {}
    # Upstream config migration fills empty auth sections. Empty provider
    # defaults are not an operator override and must not disable the next boot.
    custom_auth = bool(basic.get("username") or oauth.get("client_id")
                       or oidc.get("issuer") or oidc.get("client_id") or dashboard.get("auth"))
    if (password_hash or custom_auth or env.get("HERMES_DASHBOARD_OAUTH_CLIENT_ID")
            or env.get("HERMES_DASHBOARD_OIDC_CLIENT_ID")
            or env.get("HERMES_ODS_MANAGED_AUTH", "true").lower() == "false"):
        return None
    seed = env.get("HERMES_DASHBOARD_SESSION_TOKEN", "")
    if not seed:
        return None
    def derive(purpose):
        return hmac.new(seed.encode(), ("ods/hermes/" + purpose + "/v1").encode(), hashlib.sha256).hexdigest()
    return {"username": username or "ods", "password": derive("password"),
            "secret": env.get("HERMES_DASHBOARD_BASIC_AUTH_SECRET") or basic.get("secret") or derive("signing"), "managed": True}


def _prepare_scratch_volume():
    """Assign only the scratch volume root to the configured runtime user.

    Upstream can skip ownership setup on an already initialized Hermes home.
    A newly mounted volume still needs ownership before the UID-remapped agent
    can use it. Descriptor-relative traversal refuses symlinked components.
    """
    if os.name != "posix" or os.getuid() != 0:
        return
    raw_ids = [os.environ.get(key) for key in ("HERMES_UID", "HERMES_GID")]
    if any(value is None for value in raw_ids):
        return  # Compatibility with custom images without UID remapping.
    if any(not value.isascii() or not value.isdecimal() or int(value) >= 2**32 - 1 for value in raw_ids):
        raise ValueError("Hermes scratch requires valid HERMES_UID and HERMES_GID")
    uid, gid = map(int, raw_ids)
    home = Path(os.environ.get("HERMES_HOME", "/opt/data"))
    if not home.is_absolute() or ".." in home.parts:
        raise ValueError("Hermes home must be an absolute directory without parent traversal")
    flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW
    fd = os.open("/", flags)
    try:
        for part in (*home.parts[1:], "cache", "scratch"):
            next_fd = os.open(part, flags, dir_fd=fd)
            os.close(fd)
            fd = next_fd
        os.fchown(fd, uid, gid)
        os.fchmod(fd, 0o700)
    except FileNotFoundError:
        return  # An older custom compose has no scratch volume to prepare.
    finally:
        os.close(fd)


def bootstrap():
    config_path = Path(os.environ.get("HERMES_HOME", "/opt/data")) / "config.yaml"
    if not config_path.exists():
        config_path = Path("/opt/hermes/cli-config.yaml.example")
    auth = settings(config_path=config_path)
    if auth and not os.environ.get("HERMES_DASHBOARD_BASIC_AUTH_USERNAME"):
        os.environ["HERMES_DASHBOARD_BASIC_AUTH_USERNAME"] = auth["username"]
    policy_dir = Path(os.environ.get("HERMES_AUTH_POLICY_DIR", "/run/ods-hermes-auth"))
    if policy_dir.is_symlink():
        raise RuntimeError("Hermes auth policy directory must not be a symlink")
    policy_dir.mkdir(parents=True, exist_ok=True)
    if os.getuid() == 0:
        os.chown(policy_dir, 0, 0)
    policy_dir.chmod(0o755)
    policy_tmp = policy_dir / "policy.json.tmp"
    if policy_tmp.is_symlink():
        raise RuntimeError("Hermes auth policy file must not be a symlink")
    policy_tmp.write_text(json.dumps({"schemaVersion": 1, "managed": bool(auth and auth["managed"]),
                                     "seedDigest": hashlib.sha256(os.environ.get("HERMES_DASHBOARD_SESSION_TOKEN", "").encode()).hexdigest()}))
    policy_tmp.chmod(0o644)
    policy_tmp.replace(policy_dir / "policy.json")
    if auth and auth["managed"]:
        for key, value in (("USERNAME", auth["username"]), ("PASSWORD", auth["password"]), ("SECRET", auth["secret"])):
            name = "HERMES_DASHBOARD_BASIC_AUTH_" + key
            if not os.environ.get(name):
                os.environ[name] = value
    _prepare_scratch_volume()
    # Keep the actual upstream bootstrap/UID remapping/seed-once behavior.
    import sys
    dispatcher = "/opt/hermes/docker/entrypoint-dispatch.sh"
    argv = [dispatcher] if Path(dispatcher).is_file() else ["/init", "/opt/hermes/docker/main-wrapper.sh"]
    os.execv(argv[0], argv + sys.argv[1:])


if __name__ == "__main__":
    bootstrap()