diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/Dockerfile b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/Dockerfile index f859aed21..2314fdd2b 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/Dockerfile +++ b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/Dockerfile @@ -2,7 +2,8 @@ # for one website browser, which runs headless there, and a page driver whose screen is the browser's page. supervisord # runs every program as the session user, and the browser goes out only through the site proxy on 127.0.0.1:3128. # Files a page's file chooser may be given go in ~/Uploads. Composing a driver or an app onto it adds its services as -# programs in /etc/supervisor/conf.d. +# programs in /etc/supervisor/conf.d. The trust files agentenv adds are in /etc/agentenv from boot: ca.pem, empty until +# a deploy adds a CA, and ca-bundle.pem, the public roots and those CAs. FROM public.ecr.aws/docker/library/ubuntu:24.04@sha256:24737cef17f4e22b1977df7d9a05e77c4e28b7da5b8911f8d8ccda136bf3573c ENV DEBIAN_FRONTEND=noninteractive RUN apt-get update && apt-get install -y --no-install-recommends \ @@ -12,6 +13,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ && useradd --create-home --uid 1000 --shell /bin/bash user \ && runuser -u user -- mkdir -p /home/user/Desktop /home/user/Documents /home/user/Downloads /home/user/Uploads \ && mkdir -p /etc/supervisor/conf.d /var/log/agentenv /run/agentenv \ + && mkdir -p /etc/agentenv && touch /etc/agentenv/ca.pem \ + && cp /etc/ssl/certs/ca-certificates.crt /etc/agentenv/ca-bundle.pem \ && chown user:user /var/log/agentenv /run/agentenv COPY supervisord.conf /etc/supervisor/supervisord.conf COPY start-computer /usr/local/bin/start-computer diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/Dockerfile b/src/agent_env/computers/artifacts/ubuntu-24.04/Dockerfile index d46579c51..c8f53a2c3 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/Dockerfile +++ b/src/agent_env/computers/artifacts/ubuntu-24.04/Dockerfile @@ -1,6 +1,7 @@ # The ubuntu-24.04 base computer: Ubuntu 24.04 with XFCE on Xvfb, D-Bus and AT-SPI, every program run by supervisord # as the session user, whose home has a desktop's standard folders (Documents, Downloads, ...). Composing a driver or an -# app onto it adds its services as programs in /etc/supervisor/conf.d. +# app onto it adds its services as programs in /etc/supervisor/conf.d. The trust files agentenv adds are in +# /etc/agentenv from boot: ca.pem, empty until a deploy adds a CA, and ca-bundle.pem, the public roots and those CAs. FROM public.ecr.aws/docker/library/ubuntu:24.04@sha256:24737cef17f4e22b1977df7d9a05e77c4e28b7da5b8911f8d8ccda136bf3573c ENV DEBIAN_FRONTEND=noninteractive RUN apt-get update && apt-get install -y --no-install-recommends \ @@ -16,6 +17,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ && useradd --create-home --uid 1000 --shell /bin/bash user \ && runuser -u user -- xdg-user-dirs-update \ && mkdir -p /etc/supervisor/conf.d /var/log/agentenv /run/agentenv \ + && mkdir -p /etc/agentenv && touch /etc/agentenv/ca.pem \ + && cp /etc/ssl/certs/ca-certificates.crt /etc/agentenv/ca-bundle.pem \ && chown user:user /var/log/agentenv /run/agentenv COPY supervisord.conf /etc/supervisor/supervisord.conf COPY xfwm4.xml /etc/xdg/xfce4/xfconf/xfce-perchannel-xml/xfwm4.xml diff --git a/src/agent_env/env/envs/computer/utils/params.py b/src/agent_env/env/envs/computer/utils/params.py index 1836a031b..3d978b412 100644 --- a/src/agent_env/env/envs/computer/utils/params.py +++ b/src/agent_env/env/envs/computer/utils/params.py @@ -83,6 +83,9 @@ def checked(env: ComputerEnv, env_params: Optional[dict]) -> tuple[ComputerParam problems += gate.check_app_settings(app, merged[name], f"env_params.app_settings.{name}", env.computer.os.type) if params.sites and not params.site_routing: problems.append("env_params.sites: a computer reaches sites only with site_routing") + if params.site_routing and (os_type := env.computer.os.type) not in sites.TRUST_ENVIRONMENTS: + problems.append(f"env_params.site_routing: site routing reaches Linux computers alone, and this computer runs " + f"{os_type}") allow_hosts = (env.network or {}).get("allow_hosts") or () problems += sites.table_problems(params.sites, allow_hosts) if params.sites else [] return params, values, problems diff --git a/src/agent_env/env/envs/computer/utils/site_proxy.py b/src/agent_env/env/envs/computer/utils/site_proxy.py index 0c14c9cf0..76c97c566 100644 --- a/src/agent_env/env/envs/computer/utils/site_proxy.py +++ b/src/agent_env/env/envs/computer/utils/site_proxy.py @@ -19,9 +19,12 @@ from __future__ import annotations import asyncio +import base64 +import hashlib import json import os import pwd +import re import shutil import socket import ssl @@ -59,6 +62,19 @@ # log the failure shows. LISTEN_PROBE_S, LISTEN_POLL_S, LOG_TAIL_CHARS = 1.0, 0.2, 2000 CHECK_TIMEOUT_S = 15.0 +#: The checks after each site's: Python through ``SSL_CERT_FILE``, Node and curl. +RUNTIME_CHECKS = 3 +#: What a JDK's store is opened with unless it says otherwise, how deep a JDK's store is looked for under its folders, +#: the folders not looked in, and how long a ``keytool`` call may take. +JAVA_STORE_PASSWORD = "changeit" +JDK_SEARCH_DEPTH = 8 +JDK_SKIPPED = frozenset({"node_modules", ".git", "__pycache__", "site-packages"}) +KEYTOOL_TIMEOUT_S = 60.0 +#: A PEM certificate, from its BEGIN line to its END line. +PEM_CERTIFICATE = re.compile(r"-----BEGIN CERTIFICATE-----.+?-----END CERTIFICATE-----", re.S) +#: A Node program loading the URL it's given, printing the status it's answered, and failing with the error it meets. +NODE_GET = ("require('https').get(process.argv[1], r => { console.log(r.statusCode); r.resume(); })" + ".on('error', e => { console.error(e.message); process.exit(1); })") RESTART_DELAY_S = 1.0 SERVE_USER = "nobody" FAILED = (("X-Agentenv-Site-Proxy", "failed"),) @@ -662,8 +678,21 @@ def profile(self) -> Path: return self.root / f"etc/profile.d/{CA_NAME}.sh" @property - def system_bundle(self) -> Path: - return self.root / "etc/ssl/certs/ca-certificates.crt" + def system_bundles(self) -> tuple[Path, ...]: + """Where the system store keeps its public roots in one file, Debian's then Red Hat's.""" + return self.root / "etc/ssl/certs/ca-certificates.crt", self.root / "etc/pki/tls/certs/ca-bundle.crt" + + @property + def java_hook_store(self) -> Path: + """The JDK store the system store's hook (``ca-certificates-java``) builds from it, which apt JDKs link to.""" + return self.root / "etc/ssl/certs/java/cacerts" + + @property + def jdk_folders(self) -> tuple[Path, ...]: + return self.root / "usr/lib/jvm", self.root / "opt" + + def rooted(self, path: str) -> Path: + return self.root / path.lstrip("/") def hosts_text(current: str, names: list[str]) -> str: @@ -731,6 +760,102 @@ def trust_system(paths: Paths, ca_pem: str) -> Optional[str]: return None +def public_roots(paths: Paths) -> Optional[Path]: + """The system store's file of public roots, or None on a Linux with none of them.""" + return next((bundle for bundle in paths.system_bundles if bundle.exists()), None) + + +def add_trust_files(paths: Paths, files: dict, ca_pem: str) -> list[str]: + """The CA added to ``files["ca"]``, the CAs agentenv adds, and ``files["bundle"]`` made again from the system + store's public roots as they are now, which ``trust_system`` added the CA to where it could, and every CA in + ``files["ca"]``, so it also holds what apps added to the system store after the image or a provider made it. A + Linux with no file of public roots keeps the bundle the image or a provider made, and one with neither is refused + rather than given a bundle of the CA alone. Both, and their folder, readable by everyone.""" + ca, bundle = paths.rooted(files["ca"]), paths.rooted(files["bundle"]) + if (system := public_roots(paths)) is not None: + roots = system.read_text() + elif bundle.exists() and bundle.read_text().strip(): + roots = bundle.read_text() + else: + raise SystemExit(f"there are no public roots to make {files['bundle']} from: none of " + f"{', '.join(str(path) for path in paths.system_bundles)} is there") + added = ca.read_text() if ca.exists() else "" + if ca_pem.strip() not in added: + added = (added.rstrip("\n") + "\n" if added.strip() else "") + ca_pem.strip() + "\n" + missing = "".join(pem + "\n" for pem in PEM_CERTIFICATE.findall(added) if pem not in roots) + ca.parent.mkdir(parents=True, exist_ok=True) + ca.parent.chmod(0o755) + for path, text in ((ca, added), (bundle, roots.rstrip("\n") + "\n" + missing)): + write_in_place(path, text) + path.chmod(0o644) + return [str(ca), str(bundle)] + + +def trust_java(paths: Paths, ca: str, ca_pem: str) -> dict: + """Each JDK's store trusting the CA, by its path, as ``keytool -list`` finds it there: the store the system store's + hook builds as ``trust_system`` updated it, and any other given the CA with ``keytool``, that JDK's own else any; + none on a computer with no JDK.""" + fingerprint = sha256_fingerprint(ca_pem) + hook = paths.java_hook_store.resolve() if paths.java_hook_store.exists() else None + results: dict = {} + for store in java_stores(paths): + keytool = home_keytool(store) or shutil.which("keytool") + if keytool is None: + results[str(store)] = {"error": "there's no keytool to add the CA with"} + continue + opened = ["-keystore", str(store), "-storepass", JAVA_STORE_PASSWORD] + try: + if store.resolve() != hook: + subprocess.run([keytool, "-delete", "-alias", CA_NAME, *opened], capture_output=True, text=True, + timeout=KEYTOOL_TIMEOUT_S) + subprocess.run([keytool, "-importcert", "-noprompt", "-trustcacerts", "-alias", CA_NAME, "-file", ca, + *opened], check=True, capture_output=True, text=True, timeout=KEYTOOL_TIMEOUT_S) + listed = subprocess.run([keytool, "-list", "-v", *opened], check=True, capture_output=True, text=True, + timeout=KEYTOOL_TIMEOUT_S).stdout + except (OSError, subprocess.SubprocessError) as e: + results[str(store)] = {"error": _why(e)} + continue + results[str(store)] = {"trusted": True} if fingerprint in listed else { + "error": "keytool -list doesn't list the CA in it"} + return results + + +def java_stores(paths: Paths) -> list[Path]: + """Each JDK's store, ``lib/security/cacerts``, a Java 8 JRE's among them, under the JDK folders and ``/opt`` to + ``JDK_SEARCH_DEPTH``: a store two paths lead to once.""" + found, seen = [], set() + for top in paths.jdk_folders: + for folder, folders, files in os.walk(top): + here = Path(folder) + folders[:] = [] if len(here.relative_to(top).parts) >= JDK_SEARCH_DEPTH else [ + name for name in folders if name not in JDK_SKIPPED] + if here.parts[-2:] == ("lib", "security") and "cacerts" in files: + store = here / "cacerts" + if (key := store.resolve()) not in seen: + seen.add(key) + found.append(store) + return found + + +def home_keytool(store: Path) -> Optional[str]: + keytool = store.parents[2] / "bin" / "keytool" + return str(keytool) if keytool.exists() else None + + +def sha256_fingerprint(pem: str) -> str: + """``pem``'s SHA-256 fingerprint as ``keytool -list -v`` prints it: hex pairs, upper case, joined by colons.""" + body = "".join(line.strip() for line in pem.splitlines() if line.strip() and not line.startswith("-----")) + return ":".join(f"{byte:02X}" for byte in hashlib.sha256(base64.b64decode(body)).digest()) + + +def _why(error: Exception) -> str: + """What a program run with text output said it failed on, its last line; else the error.""" + if isinstance(error, subprocess.CalledProcessError): + lines = (error.stderr or error.stdout or "").strip().splitlines() + return lines[-1] if lines else f"exited {error.returncode}" + return f"{type(error).__name__}: {error}" + + def trust_nss(user: Optional[str], ca: str) -> list[str]: """Add the CA to the session user's NSS databases, Chromium's (and a snap Chromium's when there is one), as that user, for a Chromium too old to read its ``CACertificates`` policy; none without ``certutil``.""" @@ -802,9 +927,10 @@ def install(config_path: str, paths: Paths = Paths()) -> dict: write_in_place(policy, chromium_policies(given["ca_pem"])) trust["chromium"] = [str(policy) for policy in paths.chromium_policies] trust["nss"] = trust_nss(user, ca) - bundle = paths.system_bundle if paths.system_bundle.exists() else None - write_in_place(paths.profile, f"export NODE_EXTRA_CA_CERTS={ca}\n" - + (f"export REQUESTS_CA_BUNDLE={bundle}\n" if bundle else "")) + trust["java"] = trust_java(paths, ca, given["ca_pem"]) + trust["added"] = add_trust_files(paths, given["trust_files"], given["ca_pem"]) + write_in_place(paths.profile, "".join(f"export {name}={value}\n" + for name, value in given["trust_environment"].items())) trust["environment"] = str(paths.profile) serving = {"user": user, "serve_user": serve_user(user), "sites": sites, **{key: given[key] for key in ("listen", "https_port", "http_port") if key in given}} @@ -815,7 +941,8 @@ def install(config_path: str, paths: Paths = Paths()) -> dict: serve_path = folder / "serve.json" serve_path.write_text(json.dumps(serving)) start(str(serve_path), given.get("log")) - return {"trust": trust, "names": names, "checks": checks_as(user, str(serve_path))} + checks = checks_as(user, str(serve_path)) + return {"trust": trust, "names": names, "checks": checks["sites"], "runtimes": checks["runtimes"]} def start(serve_path: str, log: Optional[str]) -> None: @@ -855,19 +982,22 @@ def keep(serve_path: str) -> None: def checks_as(user: Optional[str], serve_path: str) -> dict: - """``check`` run as ``user``, given each site's timeout one after another and a margin for its start.""" + """``check`` run as ``user``, with the environment it's given, each site's timeout and each runtime's given one + after another and a margin for its start.""" sites = len(Config.load(serve_path).sites) done = subprocess.run(as_user(user, [sys.executable, os.path.abspath(__file__), "check", serve_path]), - capture_output=True, text=True, timeout=CHECK_TIMEOUT_S * (sites + 2)) + capture_output=True, text=True, timeout=CHECK_TIMEOUT_S * (sites + RUNTIME_CHECKS + 2)) if done.returncode != 0: - return {"*": {"error": (done.stderr.strip().splitlines() or ["the check failed"])[-1]}} + return {"sites": {"*": {"error": (done.stderr.strip().splitlines() or ["the check failed"])[-1]}}, + "runtimes": {}} return json.loads(done.stdout) def check(config: Config) -> dict: - """Each site's first name loaded, over HTTPS but for a ``.localhost`` name, through the system's resolver and - trust store: its status, or why it didn't load, which an answer the proxy made itself (it marks them ``FAILED``), - such as its 502 for a gateway it couldn't reach, is too.""" + """``sites``: each site's first name loaded, over HTTPS but for a ``.localhost`` name, through the system's + resolver and trust store: its status, or why it didn't load, which an answer the proxy made itself (it marks them + ``FAILED``), such as its 502 for a gateway it couldn't reach, is too. ``runtimes``: the first of them over HTTPS + loaded through each other runtime's own trust (``check_runtimes``).""" opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) results = {} for site in config.sites: @@ -885,9 +1015,45 @@ def check(config: Config) -> dict: results[name] = {"url": url, "status": e.code} except (urllib.error.URLError, OSError, ssl.SSLError) as e: results[name] = {"url": url, "error": str(getattr(e, "reason", e))} + secure = next((result["url"] for result in results.values() if result["url"].startswith("https:")), None) + return {"sites": results, "runtimes": check_runtimes(secure) if secure else {}} + + +def check_runtimes(url: str) -> dict: + """``url`` loaded through each runtime's own trust: Python through ``SSL_CERT_FILE`` alone, and Node and curl where + the computer has them, each by name with the status it was answered, any answer meaning the CA was trusted, or + why it couldn't.""" + results = {"python SSL_CERT_FILE": _python_through_cert_file(url)} + for runtime, argv in (("node", ["-e", NODE_GET, url]), + ("curl", ["-sS", "-o", "/dev/null", "-w", "%{http_code}", "--max-time", + str(int(CHECK_TIMEOUT_S)), url])): + if (program := shutil.which(runtime)) is None: + continue + try: + done = subprocess.run([program, *argv], check=True, capture_output=True, text=True, + timeout=CHECK_TIMEOUT_S + 5) + results[runtime] = {"url": url, "status": int(done.stdout.strip().splitlines()[-1])} + except (OSError, ValueError, IndexError, subprocess.SubprocessError) as e: + results[runtime] = {"url": url, "error": _why(e)} return results +def _python_through_cert_file(url: str) -> dict: + cert_file = os.environ.get("SSL_CERT_FILE") + if not cert_file: + return {"url": url, "error": "SSL_CERT_FILE isn't set in the computer's environment"} + try: + context = ssl.create_default_context(cafile=cert_file) + opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), + urllib.request.HTTPSHandler(context=context)) + with opener.open(url, timeout=CHECK_TIMEOUT_S) as response: + return {"url": url, "status": response.status} + except urllib.error.HTTPError as e: + return {"url": url, "status": e.code} + except (urllib.error.URLError, OSError, ssl.SSLError) as e: + return {"url": url, "error": str(getattr(e, "reason", e))} + + def main(argv: list[str]) -> None: command, path = argv[0], argv[1] if command == "install": diff --git a/src/agent_env/env/envs/computer/utils/sites.py b/src/agent_env/env/envs/computer/utils/sites.py index fd236b291..e8c3d5ca8 100644 --- a/src/agent_env/env/envs/computer/utils/sites.py +++ b/src/agent_env/env/envs/computer/utils/sites.py @@ -7,10 +7,13 @@ a site's name winning over the allowlist; two sites claiming a name are refused. Once the computer is booted, a CA made for the deploy signs a certificate for each site's names (``issue``), and the site proxy (``site_proxy.py``) is installed through the sandbox's root channel and started (``route``), which a reset does again on its fresh computer. In the computer they live in -``/etc/agentenv/sites``, readable by root alone but the CA, ``ca.pem``, at a fixed path for an app with a certificate -list of its own: the folder is root's alone (0700) before the config and its keys are written into it, until the -install has made the keys and the config private; the CA and certificates are valid for the deploy's ttl and a day more, from a few minutes back for a -clock that's behind.""" +``/etc/agentenv/sites``, root's alone (0700) before the config and its keys are written into it, until the install has +made the keys and the config private; the CA and certificates are valid for the deploy's ttl and a day more, from a few +minutes back for a clock that's behind. The CA also joins the trust agentenv adds, two files at fixed paths for each OS +site routing reaches (``TRUST_ENVIRONMENTS``), which on Linux are the local provider's (``LOCAL_TRUST_ENV``): +``/etc/agentenv/ca.pem``, every CA agentenv adds, for an app with a certificate list of its own, and +``/etc/agentenv/ca-bundle.pem``, the public roots and them. The computer's boot environment names them, so every program +in it trusts the sites however it was started. Site routing reaches Linux computers alone.""" from __future__ import annotations @@ -37,6 +40,7 @@ from agent_env.env.env import Env, EnvCapabilityUnsupported, gateway_url_of from agent_env.env.envs.computer.utils.site_proxy import is_local, matches +from agent_env.providers.sandbox_providers.local_docker.provider import LOCAL_TRUST_ENV from agent_env.providers.sandbox_providers.sandbox import NetworkPolicy, VmSandbox from agent_env.store.base import NotFoundError @@ -48,7 +52,9 @@ FOLDER = PurePosixPath("/etc/agentenv/sites") PROXY = FOLDER / "site_proxy.py" CONFIG = FOLDER / "config.json" -CA_PATH = FOLDER / "ca.pem" +#: By the OS type of each computer site routing reaches, the variables naming the trust files agentenv adds, which a +#: computer routing sites boots with. +TRUST_ENVIRONMENTS = {"linux": dict(LOCAL_TRUST_ENV)} LOG = FOLDER / "site-proxy.log" SYNTHETIC, ALLOWLIST = "synthetic", "allowlist" RANKS = (SYNTHETIC, ALLOWLIST) @@ -265,11 +271,25 @@ def _key_usage(**granted: bool) -> x509.KeyUsage: return x509.KeyUsage(**{usage: granted.get(usage, False) for usage in usages}) -def proxy_config(table: SiteTable, issued: Issued, *, user: Optional[str], +def trust_files(os_type: str) -> dict[str, str]: + """The trust files agentenv adds on an ``os_type`` computer, as its variables name them: every CA it adds + (``ca``), and the public roots and them (``bundle``).""" + environment = TRUST_ENVIRONMENTS[os_type] + return {"ca": environment["NODE_EXTRA_CA_CERTS"], "bundle": environment["SSL_CERT_FILE"]} + + +def trust_environment(os_type: str, sites: list[Site]) -> dict[str, str]: + """The variables naming the trust agentenv adds, for an ``os_type`` computer routing ``sites``: none when every name + is served over plain HTTP, as a ``.localhost`` one is, since no CA is made then to write the files they'd name.""" + return dict(TRUST_ENVIRONMENTS[os_type]) if any(route.tls_names for route in site_table(sites).routes) else {} + + +def proxy_config(table: SiteTable, issued: Issued, *, user: Optional[str], os_type: str, upstream_headers: Optional[dict[str, dict[str, str]]] = None) -> dict: - """What ``site_proxy.py install`` reads: the session user, the CA, and each route with its certificate and key, and - the headers its gateway's sandbox provider asks requests to it to carry (``upstream_headers``, by site), which the - proxy sends upstream alone.""" + """What ``site_proxy.py install`` reads: the session user, the CA, the trust files on an ``os_type`` computer and + the variables naming them, which its profile script sets as the computer's boot environment does, each route with + its certificate and key, and the headers its gateway's sandbox provider asks requests to it to carry + (``upstream_headers``, by site), which the proxy sends upstream alone.""" sites = [] for route in table.routes: site = {"name": route.site, "upstream": route.url, "domains": list(route.domains)} @@ -278,7 +298,8 @@ def proxy_config(table: SiteTable, issued: Issued, *, user: Optional[str], if (upstream_headers or {}).get(route.site): site["upstream_headers"] = dict(upstream_headers[route.site]) sites.append(site) - return {"user": user, "ca_pem": issued.ca_pem, "sites": sites, "log": str(LOG)} + return {"user": user, "ca_pem": issued.ca_pem, "sites": sites, "log": str(LOG), + "trust_files": trust_files(os_type), "trust_environment": dict(TRUST_ENVIRONMENTS[os_type])} def reachable_problem(policy: Optional[NetworkPolicy], routes: list[Route]) -> Optional[str]: @@ -383,10 +404,10 @@ async def _exec(self, argv: list[str]) -> str: async def route(sandbox: Sandbox, sites: list[Site], *, user: Optional[str], network: Optional[dict], - valid_for: timedelta, label: str) -> dict: - """Have the computer on ``sandbox`` reach each of ``sites`` by its names: the table made, the CA and certificates - issued, the proxy installed and started as root, and each name checked as ``user``; what the deploy record keeps of - it. Raises naming a name that didn't load.""" + valid_for: timedelta, label: str, os_type: str) -> dict: + """Have the ``os_type`` computer on ``sandbox`` reach each of ``sites`` by its names: the table made, the CA and + certificates issued, the proxy installed and started as root, and each name checked as ``user``; what the deploy + record keeps of it. Raises naming a name that didn't load.""" requested = NetworkPolicy.from_dict(network) if network else None table = site_table(sites, requested.allow_hosts if requested else ()) table.routes = [Route(r.site, sandbox.url_from_sandbox(r.url), r.domains) for r in table.routes] @@ -402,17 +423,26 @@ async def route(sandbox: Sandbox, sites: list[Site], *, user: Optional[str], net channel = RootChannel(sandbox) await channel.private_folder(FOLDER) await channel.write(PROXY, _PROXY_SOURCE.read_text()) - await channel.write_secret(CONFIG, json.dumps(proxy_config(table, issued, user=user, + await channel.write_secret(CONFIG, json.dumps(proxy_config(table, issued, user=user, os_type=os_type, upstream_headers=upstream_headers))) await channel.run(["chmod", "0600", str(CONFIG)]) output = await channel.run(["python3", str(PROXY), "install", str(CONFIG)]) report = json.loads(output.strip().splitlines()[-1]) - failed = {name: result for name, result in report["checks"].items() if "error" in result} - if failed: - raise RuntimeError("site routing: " + "; ".join(f"{result.get('url', name)} didn't load in the computer: " - f"{result['error']}" for name, result in failed.items())) - return {"names": table.to_dict(), "ca": issued.ca_pem, "ca_path": str(CA_PATH), "trust": report["trust"], - "checks": report["checks"]} + if failed := routing_problems(report): + raise RuntimeError("site routing: " + "; ".join(failed)) + return {"names": table.to_dict(), "ca": issued.ca_pem, "ca_path": trust_files(os_type)["ca"], + "trust": report["trust"], "checks": report["checks"], "runtimes": report.get("runtimes", {})} + + +def routing_problems(report: dict) -> list[str]: + """What the install's ``report`` says failed: a site that didn't load, a runtime that didn't trust the CA, and a + JDK's store that didn't take it.""" + return ([f"{result.get('url', name)} didn't load in the computer: {result['error']}" + for name, result in report["checks"].items() if "error" in result] + + [f"{runtime} in the computer didn't trust the deploy's CA loading {result.get('url')}: {result['error']}" + for runtime, result in report.get("runtimes", {}).items() if "error" in result] + + [f"the JDK store {store} didn't take the deploy's CA: {result['error']}" + for store, result in report["trust"].get("java", {}).items() if "error" in result]) async def deployed_sites(deployed_envs: list[DeployedEnv], only: Optional[list[str]] = None) -> list[dict]: diff --git a/src/agent_env/providers/env_providers/env_computer_provider.py b/src/agent_env/providers/env_providers/env_computer_provider.py index da886c5c6..e5a11e637 100644 --- a/src/agent_env/providers/env_providers/env_computer_provider.py +++ b/src/agent_env/providers/env_providers/env_computer_provider.py @@ -221,7 +221,8 @@ async def _route_sites(env: ComputerEnv, sandbox: Sandbox, params: computer_para """The computer on ``sandbox`` routed to ``params.sites`` under a CA of its own, valid ``valid_for_s``; what its record keeps of it.""" routed = await sites.route(sandbox, params.sites, user=env.computer.session.user, network=env.network, - valid_for=timedelta(seconds=valid_for_s), label=env.environment_name) + valid_for=timedelta(seconds=valid_for_s), label=env.environment_name, + os_type=env.computer.os.type) return {**routed, "valid_for_s": valid_for_s} @@ -311,7 +312,8 @@ def _environment(env: ComputerEnv, computer: ComputerArtifact, values: dict, driver_settings=params.driver_settings), **app_settings_environment(env.composed_apps(), computer_params.app_settings(env, params.app_settings), env.computer.os.type), - **({HOLD_BOOT_LAUNCH_ENV: "1"} if params.site_routing else {})} + **({HOLD_BOOT_LAUNCH_ENV: "1", **sites.trust_environment(env.computer.os.type, params.sites)} + if params.site_routing else {})} def _require_name(env: ComputerEnv, card: dict) -> None: diff --git a/tst/integration/computer/ca_trust_test.py b/tst/integration/computer/ca_trust_test.py new file mode 100644 index 000000000..5e525e091 --- /dev/null +++ b/tst/integration/computer/ca_trust_test.py @@ -0,0 +1,58 @@ +"""A computer routing a site trusts the deploy's CA in every runtime it has, however a program started: an app launched +at boot loads the site through Node, an apt JDK, a JDK copied under /opt with a store of its own, and curl; a command +the computer runs sees the trust variables and loads it through Python with SSL_CERT_FILE alone; the CA is at +/etc/agentenv/ca.pem, among the CAs agentenv adds, and in /etc/agentenv/ca-bundle.pem beside the public roots; and the +deploy's record keeps each runtime's check and each JDK store it trusted. Needs Docker, the local registry on +localhost:5000, and Ubuntu's archive for the app's packages.""" + +import json + +import pytest + +from agent_env.env import Env +from agent_env.env.envs.computer.utils import inside +from agent_env.store.routing import namespace_routing +from agent_env.task.teardown import teardown_run +from agent_env.task_step.context import TaskStepContext +from tst.integration.computer.start_tabs import host_pages + +pytestmark = [pytest.mark.integration, pytest.mark.int_test_slow] + +URL = "https://shop.test/landed" +# Loads the URL it's given through SSL_CERT_FILE alone, printing the status it's answered. +THROUGH_CERT_FILE = ("import os, ssl, sys, urllib.request\n" + "context = ssl.create_default_context(cafile=os.environ['SSL_CERT_FILE'])\n" + "print(urllib.request.urlopen(sys.argv[1], context=context).status)") + + +@pytest.mark.asyncio +async def test_every_runtime_of_a_computer_routing_a_site_trusts_its_ca_launched_at_boot_or_run_by_the_computer( + tls_desktop): + with host_pages() as port, namespace_routing(): + params = {"site_routing": True, "sites": [{"name": "shop", "url": f"http://127.0.0.1:{port}/website/shop/", + "domains": ["shop.test"]}], + "app_settings": {"clients": {"url": URL}}} + deployed = await Env.get(tls_desktop.id).deploy(env_params=params) + context = TaskStepContext(deployed_envs=[deployed]) + try: + at_boot = json.loads((await inside.run(deployed, ["cat", ".tls.json"], timeout_s=30))["stdout"]) + variables = (await inside.run(deployed, ["sh", "-c", "echo $SSL_CERT_FILE $REQUESTS_CA_BUNDLE " + "$NODE_EXTRA_CA_CERTS"], timeout_s=30))["stdout"] + through_cert_file = await inside.run(deployed, ["python3", "-c", THROUGH_CERT_FILE, URL], timeout_s=60) + added = (await inside.run(deployed, ["cat", "/etc/agentenv/ca.pem"], timeout_s=30))["stdout"] + mode = (await inside.run(deployed, ["stat", "-c", "%a", "/etc/agentenv/ca.pem"], timeout_s=30))["stdout"] + bundle = (await inside.run(deployed, ["cat", "/etc/agentenv/ca-bundle.pem"], timeout_s=30))["stdout"] + finally: + report = await teardown_run(context) + + routed = deployed.metadata["computer"]["sites"] + assert at_boot == {"node": "200", "java": "200", "copied java": "200", "curl": "200"}, at_boot + assert variables.split() == ["/etc/agentenv/ca-bundle.pem", "/etc/agentenv/ca-bundle.pem", "/etc/agentenv/ca.pem"] + assert through_cert_file["exit_code"] == 0 and through_cert_file["stdout"].strip() == "200", through_cert_file + assert routed["ca_path"] == "/etc/agentenv/ca.pem" and routed["ca"].strip() in added and mode.strip() == "644" + assert routed["ca"].strip() in bundle and bundle.count("BEGIN CERTIFICATE") > 100 + assert {runtime: "status" in result for runtime, result in routed["runtimes"].items()} == { + "python SSL_CERT_FILE": True, "node": True, "curl": True}, routed["runtimes"] + stores = routed["trust"]["java"] + assert len(stores) == 2 and all(result == {"trusted": True} for result in stores.values()), stores + assert any(store.startswith("/opt/copied-jdk/") for store in stores) and not report.failed diff --git a/tst/integration/computer/conftest.py b/tst/integration/computer/conftest.py index ac448aa70..a4360da4e 100644 --- a/tst/integration/computer/conftest.py +++ b/tst/integration/computer/conftest.py @@ -1,6 +1,7 @@ """The ubuntu-24.04 base computer, built from its folder once for the tests here and tagged for the local registry; the shipped desktop example's computer env, the cua-driver test bundle's, the chromium test bundle's, the website-browser -test bundle's and agentenv's chromium deployed alone, each composed once for a module's tests; +test bundle's, the TLS clients test bundle's and agentenv's chromium deployed alone, each composed once for a module's +tests; ``state``, local stores for a test that runs a bundle; ``docker``, which runs the docker CLI for them; and ``cdp_clients``, Playwright and Puppeteer installed to connect to a CDP address.""" @@ -31,6 +32,7 @@ CHROMIUM = Path(__file__).parent / "chromium" WEBSITE_BROWSER = Path(__file__).parent / "website-browser" BROWSER_COMPUTER = Path(__file__).parent / "browser-computer" +TLS = Path(__file__).parent / "tls" #: Playwright and Puppeteer, which connect to a cdp address from Node. CDP_CLIENTS = Path(__file__).parent / "cdp_clients" CHROMIUM_ALONE = "@local/agentenv-framework/browsers/chromium" @@ -81,6 +83,13 @@ def browser_computer(tmp_path_factory): yield from _composed(tmp_path_factory.mktemp("browser-computer"), parse_bundle(BROWSER_COMPUTER), CHROMIUM_ALONE) +@pytest.fixture(scope="module") +def tls_desktop(tmp_path_factory): + """The TLS clients test bundle's desktop, the ubuntu-24.04 base and the reference driver with Node, an apt JDK, a + copy of it under /opt and curl, composed as ``desktop`` is.""" + yield from _composed(tmp_path_factory.mktemp("tls-desktop"), parse_bundle(TLS)) + + @pytest.fixture(scope="module") def cdp_clients(tmp_path_factory) -> Path: """The CDP clients installed in a folder of their own, or the test skipped without Node 20 or later and npm.""" diff --git a/tst/integration/computer/tls/envs/clients/Fetch.java b/tst/integration/computer/tls/envs/clients/Fetch.java new file mode 100644 index 000000000..79863178e --- /dev/null +++ b/tst/integration/computer/tls/envs/clients/Fetch.java @@ -0,0 +1,6 @@ +public class Fetch { + public static void main(String[] args) throws Exception { + var connection = (javax.net.ssl.HttpsURLConnection) java.net.URI.create(args[0]).toURL().openConnection(); + System.out.println(connection.getResponseCode()); + } +} diff --git a/tst/integration/computer/tls/envs/clients/app.py b/tst/integration/computer/tls/envs/clients/app.py new file mode 100644 index 000000000..77cc3be3f --- /dev/null +++ b/tst/integration/computer/tls/envs/clients/app.py @@ -0,0 +1,22 @@ +"""TLS clients for the CA trust tests: Node, a JDK and curl from Ubuntu's archive, and a copy of that JDK under /opt +with a store of its own, as a JDK not from apt has. Launched at boot, it loads its ``url`` setting through Node, each +JDK and curl, each through its own trust, and writes what each was answered to ~/.tls.json; it's ready once it has.""" +from pathlib import Path + +from pydantic import BaseModel + +from agentenv_protocol.computer import (ComputerAppEnvironment, Launch, Packages, RunScript, computer_app, linux, + ready_check) + + +@computer_app(supports=[{"os": {"type": "linux", "display": ["x11"]}, "arch": ["amd64", "arm64"]}]) +@linux(packages=Packages(apt=["openjdk-21-jdk-headless", "nodejs", "curl"]), run=[RunScript(script="setup.sh")], + launch=Launch(at="boot", command=["venv/bin/python", "fetch.py"])) +class Clients(ComputerAppEnvironment): + + class Settings(BaseModel): + url: str = "" + + @ready_check(os="linux") + def fetched(self) -> bool: + return (Path.home() / ".tls.json").exists() diff --git a/tst/integration/computer/tls/envs/clients/env.toml b/tst/integration/computer/tls/envs/clients/env.toml new file mode 100644 index 000000000..d6af807c4 --- /dev/null +++ b/tst/integration/computer/tls/envs/clients/env.toml @@ -0,0 +1,4 @@ +type = "computer_app" +module = "app.py:Clients" +computer = "ubuntu-24.04" +driver = "reference" diff --git a/tst/integration/computer/tls/envs/clients/fetch.py b/tst/integration/computer/tls/envs/clients/fetch.py new file mode 100644 index 000000000..3b6396b48 --- /dev/null +++ b/tst/integration/computer/tls/envs/clients/fetch.py @@ -0,0 +1,22 @@ +import json +import subprocess +import time +from pathlib import Path + +from app import Clients + +NODE = ("require('https').get(process.argv[1], r => { console.log(r.statusCode); r.resume(); })" + ".on('error', e => { console.error(e.message); process.exit(1); })") +FETCH = str(Path(__file__).with_name("Fetch.java")) + +url = Clients().settings.url +results = {} +for name, argv in (("node", ["node", "-e", NODE, url]), ("java", ["java", FETCH, url]), + ("copied java", ["/opt/copied-jdk/bin/java", FETCH, url]), + ("curl", ["curl", "-sS", "-o", "/dev/null", "-w", "%{http_code}", url])): + done = subprocess.run(argv, capture_output=True, text=True, timeout=180) + said = (done.stdout if done.returncode == 0 else done.stderr).strip().splitlines() + results[name] = said[-1] if said else f"exited {done.returncode}" +Path.home().joinpath(".tls.json").write_text(json.dumps(results)) +while True: + time.sleep(3600) diff --git a/tst/integration/computer/tls/envs/clients/setup.sh b/tst/integration/computer/tls/envs/clients/setup.sh new file mode 100755 index 000000000..d22748621 --- /dev/null +++ b/tst/integration/computer/tls/envs/clients/setup.sh @@ -0,0 +1,8 @@ +#!/bin/sh +# Run as root once the app's packages are in: a copy of the apt JDK under /opt, its links kept but its store a file of +# its own rather than the link to the one the system store's hook builds, as a JDK not from apt has. +set -e +cp -a /usr/lib/jvm/java-21-openjdk-*/. /opt/copied-jdk/ +rm /opt/copied-jdk/lib/security/cacerts +cp /etc/ssl/certs/java/cacerts /opt/copied-jdk/lib/security/cacerts +test ! -L /opt/copied-jdk/lib/security/cacerts diff --git a/tst/integration/computer/tls/envs/desktop/env.toml b/tst/integration/computer/tls/envs/desktop/env.toml new file mode 100644 index 000000000..b6181e472 --- /dev/null +++ b/tst/integration/computer/tls/envs/desktop/env.toml @@ -0,0 +1,4 @@ +type = "computer" +computer = "ubuntu-24.04" +driver = "reference" +apps = ["clients"] diff --git a/tst/integration/computer/tls/tasks/desktop.json b/tst/integration/computer/tls/tasks/desktop.json new file mode 100644 index 000000000..67f3b3154 --- /dev/null +++ b/tst/integration/computer/tls/tasks/desktop.json @@ -0,0 +1,3 @@ +[ + {"id": "env", "type": "deploy_env", "env_id": "desktop"} +] diff --git a/tst/unit/env/envs/test_computer_env.py b/tst/unit/env/envs/test_computer_env.py index 3b3cdc7b0..3c3f3d584 100644 --- a/tst/unit/env/envs/test_computer_env.py +++ b/tst/unit/env/envs/test_computer_env.py @@ -960,7 +960,8 @@ async def test_a_computer_with_site_routing_reaches_the_websites_the_run_deploye assert sandbox is boots.sandboxes[0] assert [site.model_dump() for site in sites] == [{"name": "notes", "url": "http://localhost:8080/website/notes/", "domains": ["notes.test"]}] - assert options == {"user": "user", "network": None, "valid_for": timedelta(seconds=3600), "label": "desktop"} + assert options == {"user": "user", "network": None, "valid_for": timedelta(seconds=3600), "label": "desktop", + "os_type": "linux"} assert boots.boots[0]["environment"][HOLD_BOOT_LAUNCH_ENV] == "1" record = context.deployed_envs[-1].metadata["computer"] assert record["sites"] == {"names": {"routes": {"notes": ["notes.test"]}}, "ca": "CA PEM", "valid_for_s": 3600} @@ -986,10 +987,11 @@ async def test_a_computer_without_site_routing_routes_nothing_whatever_websites_ assert routes == [] and "sites" not in context.deployed_envs[-1].metadata["computer"] assert HOLD_BOOT_LAUNCH_ENV not in boots.boots[0]["environment"] and boots.boots[0]["held"] is False + assert not set(env_computer_provider.sites.TRUST_ENVIRONMENTS["linux"]) & set(boots.boots[0]["environment"]) @pytest.mark.asyncio -async def test_a_computer_routing_sites_boots_until_its_driver_answers_and_checks_its_display_once_ready( +async def test_a_computer_routing_sites_boots_with_its_trust_until_its_driver_answers_and_checks_its_display( base, driver, docker, boots, routes, monkeypatch): """A page driver's screen is its browser's page, whose launch a deploy routing sites holds: the boot waits only for the driver to answer, and the display is checked once the sites are routed and the computer is ready.""" @@ -1015,6 +1017,7 @@ async def require_display(record, resolution): env_params={"site_routing": True}).execute(TaskStepContext(deployed_envs=[_sites_gateway()])) assert boots.boots[0]["held"] is True and order == ["route", ("ready", {}), "display"] + assert env_computer_provider.sites.TRUST_ENVIRONMENTS["linux"].items() <= boots.boots[0]["environment"].items() @pytest.mark.asyncio diff --git a/tst/unit/env/envs/test_computer_site_proxy.py b/tst/unit/env/envs/test_computer_site_proxy.py index ebb500398..e520b8447 100644 --- a/tst/unit/env/envs/test_computer_site_proxy.py +++ b/tst/unit/env/envs/test_computer_site_proxy.py @@ -14,6 +14,7 @@ import socket import ssl import struct +import sys import urllib.error from datetime import timedelta from pathlib import Path @@ -396,12 +397,14 @@ def open(self, url, timeout): raise urllib.error.HTTPError(url, 502, "Bad Gateway", headers, io.BytesIO(b"the site can't be reached\n")) monkeypatch.setattr(site_proxy.urllib.request, "build_opener", lambda *handlers: Opener()) + monkeypatch.setattr(site_proxy, "check_runtimes", lambda url: {"node": {"url": url, "status": 502}}) config = Config(user=None, sites=[Site("notes", "http://gw/website/notes/", ["notes.test"]), Site("chat", "http://gw/website/chat/", ["chat.test"])]) - assert site_proxy.check(config) == { + assert site_proxy.check(config) == {"sites": { "notes.test": {"url": "https://notes.test/", "error": "502 Bad Gateway: the site can't be reached"}, - "chat.test": {"url": "https://chat.test/", "status": 502}} + "chat.test": {"url": "https://chat.test/", "status": 502}}, + "runtimes": {"node": {"url": "https://notes.test/", "status": 502}}} @pytest.mark.asyncio @@ -572,15 +575,18 @@ def test_install_writes_the_sites_files_names_and_trust_then_starts_the_proxy_on folder.mkdir() (root / "etc/hosts").write_text("127.0.0.1 localhost\n") (root / "etc/resolv.conf").write_text("nameserver 127.0.0.11\n") + (root / "etc/pki/tls/certs").mkdir(parents=True) + (root / "etc/pki/tls/certs/ca-bundle.crt").write_text("RED HAT ROOTS\n") folder.chmod(0o700) table = sites.site_table([SiteParams(name="notes", url="http://gw/website/notes/", domains=["notes.test"]), SiteParams(name="chat", url="http://gw/website/chat/", domains=["*.chat.test"]), SiteParams(name="bare", url="http://gw/website/bare/")]) issued = sites.issue(table.routes, valid_for=timedelta(hours=1), label="t") - (folder / "config.json").write_text(json.dumps(sites.proxy_config(table, issued, user=None))) + (folder / "config.json").write_text(json.dumps(sites.proxy_config(table, issued, user=None, os_type="linux"))) started = [] monkeypatch.setattr(site_proxy, "start", lambda path, log: started.append(Config.load(path))) - monkeypatch.setattr(site_proxy, "checks_as", lambda user, path: {"notes.test": {"status": 200}}) + monkeypatch.setattr(site_proxy, "checks_as", lambda user, path: {"sites": {"notes.test": {"status": 200}}, + "runtimes": {"node": {"status": 200}}}) monkeypatch.setattr(site_proxy.shutil, "which", lambda tool: None) report = site_proxy.install(str(folder / "config.json"), site_proxy.Paths(root)) @@ -600,23 +606,32 @@ def test_install_writes_the_sites_files_names_and_trust_then_starts_the_proxy_on for policy in ("etc/opt/chrome_for_testing", "etc/chromium", "etc/chromium-browser", "etc/opt/chrome"): (der,) = json.loads((root / policy / "policies/managed/agentenv-sites.json").read_text())["CACertificates"] assert base64.b64decode(der) == ssl.PEM_cert_to_DER_cert(issued.ca_pem) - assert report["trust"]["system"] is None and report["trust"]["nss"] == [] + assert report["trust"]["system"] is None and report["trust"]["nss"] == [] and report["trust"]["java"] == {} assert len(report["trust"]["chromium"]) == 4 - assert report["checks"] == {"notes.test": {"status": 200}} + assert report["checks"] == {"notes.test": {"status": 200}} and report["runtimes"] == {"node": {"status": 200}} + assert (root / "etc/agentenv/ca.pem").read_text() == issued.ca_pem.strip() + "\n" + assert (root / "etc/agentenv/ca-bundle.pem").read_text() == "RED HAT ROOTS\n" + issued.ca_pem.strip() + "\n" + for added in ("etc/agentenv/ca.pem", "etc/agentenv/ca-bundle.pem"): + assert (root / added).stat().st_mode & 0o777 == 0o644 + assert report["trust"]["added"] == [str(root / "etc/agentenv/ca.pem"), str(root / "etc/agentenv/ca-bundle.pem")] + assert (root / "etc/profile.d/agentenv-sites.sh").read_text() == "".join( + f"export {name}={value}\n" for name, value in sites.TRUST_ENVIRONMENTS["linux"].items()) def test_install_keeps_the_upstream_headers_in_a_file_root_alone_reads_and_serve_json_names_it(tmp_path, monkeypatch): root, folder = tmp_path / "root", tmp_path / "sites" - (root / "etc").mkdir(parents=True) + (root / "etc/ssl/certs").mkdir(parents=True) + (root / "etc/ssl/certs/ca-certificates.crt").write_text("PUBLIC ROOTS\n") folder.mkdir() folder.chmod(0o700) table = sites.site_table([SiteParams(name="notes", url="http://gw/website/notes/", domains=["notes.test"])]) issued = sites.issue(table.routes, valid_for=timedelta(hours=1), label="t") - given = sites.proxy_config(table, issued, user=None, upstream_headers={"notes": {"CF-Access-Client-Secret": "s3cret"}}) + given = sites.proxy_config(table, issued, user=None, os_type="linux", + upstream_headers={"notes": {"CF-Access-Client-Secret": "s3cret"}}) (folder / "config.json").write_text(json.dumps(given)) started = [] monkeypatch.setattr(site_proxy, "start", lambda path, log: started.append(Config.load(path))) - monkeypatch.setattr(site_proxy, "checks_as", lambda user, path: {}) + monkeypatch.setattr(site_proxy, "checks_as", lambda user, path: {"sites": {}, "runtimes": {}}) monkeypatch.setattr(site_proxy.shutil, "which", lambda tool: None) site_proxy.install(str(folder / "config.json"), site_proxy.Paths(root)) @@ -655,19 +670,197 @@ def run(argv, **kwargs): assert given[0] >= site_proxy.CHECK_TIMEOUT_S * 7 +def test_the_sites_ca_joins_the_trust_a_provider_added_and_a_bundle_made_here_starts_from_the_systems(tmp_path): + paths, ca_pem = site_proxy.Paths(tmp_path), "-----BEGIN CERTIFICATE-----\nU0lURVM=\n-----END CERTIFICATE-----\n" + files = {"ca": "/etc/agentenv/ca.pem", "bundle": "/etc/agentenv/ca-bundle.pem"} + (tmp_path / "etc/agentenv").mkdir(parents=True) + (tmp_path / "etc/agentenv/ca.pem").write_text("LOCAL CA\n") + (tmp_path / "etc/agentenv/ca-bundle.pem").write_text("PUBLIC ROOTS\nLOCAL CA\n") + (tmp_path / "etc/agentenv").chmod(0o700) + other = site_proxy.Paths(tmp_path / "other") + (tmp_path / "other/etc/ssl/certs").mkdir(parents=True) + (tmp_path / "other/etc/ssl/certs/ca-certificates.crt").write_text("SYSTEM ROOTS\n") + + site_proxy.add_trust_files(paths, files, ca_pem) + site_proxy.add_trust_files(paths, files, ca_pem) + site_proxy.add_trust_files(other, files, ca_pem) + + assert (tmp_path / "etc/agentenv/ca.pem").read_text() == "LOCAL CA\n" + ca_pem + assert (tmp_path / "etc/agentenv/ca-bundle.pem").read_text() == "PUBLIC ROOTS\nLOCAL CA\n" + ca_pem + assert (tmp_path / "etc/agentenv").stat().st_mode & 0o777 == 0o755 + assert (tmp_path / "other/etc/agentenv/ca.pem").read_text() == ca_pem + assert (tmp_path / "other/etc/agentenv/ca-bundle.pem").read_text() == "SYSTEM ROOTS\n" + ca_pem + + +def test_the_bundle_is_made_again_from_the_system_store_with_a_ca_an_app_added_after_the_image_was_built(tmp_path): + paths, files = site_proxy.Paths(tmp_path), {"ca": "/etc/agentenv/ca.pem", "bundle": "/etc/agentenv/ca-bundle.pem"} + app, local, sites_ca = (f"-----BEGIN CERTIFICATE-----\n{name}\n-----END CERTIFICATE-----\n" + for name in ("QVBQ", "TE9DQUw=", "U0lURVM=")) + (tmp_path / "etc/agentenv").mkdir(parents=True) + (tmp_path / "etc/agentenv/ca.pem").write_text(local) + (tmp_path / "etc/agentenv/ca-bundle.pem").write_text("ROOTS AT BUILD\n") + (tmp_path / "etc/ssl/certs").mkdir(parents=True) + (tmp_path / "etc/ssl/certs/ca-certificates.crt").write_text("ROOTS AT BUILD\n" + app + sites_ca) + + site_proxy.add_trust_files(paths, files, sites_ca) + site_proxy.add_trust_files(paths, files, sites_ca) + + assert (tmp_path / "etc/agentenv/ca.pem").read_text() == local + sites_ca + assert (tmp_path / "etc/agentenv/ca-bundle.pem").read_text() == "ROOTS AT BUILD\n" + app + sites_ca + local + + +def test_a_bundle_with_no_public_roots_to_start_from_is_refused_before_either_file_is_written(tmp_path): + paths, ca_pem = site_proxy.Paths(tmp_path), "-----BEGIN CERTIFICATE-----\nU0lURVM=\n-----END CERTIFICATE-----\n" + files = {"ca": "/etc/agentenv/ca.pem", "bundle": "/etc/agentenv/ca-bundle.pem"} + (tmp_path / "etc/agentenv").mkdir(parents=True) + (tmp_path / "etc/agentenv/ca-bundle.pem").write_text("\n") + + with pytest.raises(SystemExit, match="there are no public roots to make /etc/agentenv/ca-bundle.pem from: none " + "of .*etc/ssl/certs/ca-certificates.crt, .*etc/pki/tls/certs/ca-bundle.crt"): + site_proxy.add_trust_files(paths, files, ca_pem) + + assert not (tmp_path / "etc/agentenv/ca.pem").exists() + assert (tmp_path / "etc/agentenv/ca-bundle.pem").read_text() == "\n" + + +# A keytool whose store is a text file of the SHA-256 fingerprints it holds: -importcert adds the file's, -list -v +# prints them, -delete does nothing; each call's operation is logged beside the store. With FAIL in the store, +# -importcert fails as a store with another password does. +FAKE_KEYTOOL = """#!{python} +import base64, hashlib, sys +args = sys.argv[1:] +store = args[args.index("-keystore") + 1] +with open(store + ".calls", "a") as calls: + calls.write(args[0] + "\\n") +if args[0] == "-importcert": + if "FAIL" in open(store).read(): + sys.exit("keytool error: java.io.IOException: keystore password was incorrect") + pem = open(args[args.index("-file") + 1]).read() + body = "".join(line.strip() for line in pem.splitlines() if line.strip() and not line.startswith("-----")) + digest = hashlib.sha256(base64.b64decode(body)).digest() + with open(store, "a") as f: + f.write("SHA256: " + ":".join(f"{{byte:02X}}" for byte in digest) + "\\n") +elif args[0] == "-list": + print(open(store).read()) +""" + + +def _jdk(home: Path, store_text: str = "", keytool: bool = True) -> Path: + """A JDK at ``home``: its store, holding ``store_text``, and with ``keytool``, a fake one in its bin.""" + store = home / "lib/security/cacerts" + store.parent.mkdir(parents=True) + store.write_text(store_text) + if keytool: + (home / "bin").mkdir() + (home / "bin/keytool").write_text(FAKE_KEYTOOL.format(python=sys.executable)) + (home / "bin/keytool").chmod(0o755) + return store + + +def _calls(store: Path) -> list[str]: + calls = Path(f"{store}.calls") + return calls.read_text().split() if calls.exists() else [] + + +def test_every_jdk_trusts_the_ca_an_apt_one_through_the_system_hook_the_rest_by_keytool(tmp_path, monkeypatch): + issued = sites.issue(sites.site_table([SiteParams(name="notes", url="http://gw/website/notes/", + domains=["notes.test"])]).routes, valid_for=timedelta(hours=1), + label="t") + fingerprint = site_proxy.sha256_fingerprint(issued.ca_pem) + ca = tmp_path / "ca.pem" + ca.write_text(issued.ca_pem) + root = tmp_path / "root" + hook = root / "etc/ssl/certs/java/cacerts" + hook.parent.mkdir(parents=True) + hook.write_text(f"SHA256: {fingerprint}\n") + temurin = _jdk(root / "usr/lib/jvm/temurin-21") + apt = _jdk(root / "usr/lib/jvm/java-21-openjdk-amd64") + apt.unlink() + apt.symlink_to(hook) + (root / "usr/lib/jvm/default-java").symlink_to(root / "usr/lib/jvm/java-21-openjdk-amd64") + bundled = _jdk(root / "opt/agentenv/apps/ide/idea/jbr", keytool=False) + locked = _jdk(root / "opt/locked/jdk", "FAIL\n") + skipped = _jdk(root / "opt/app/node_modules/jre") + monkeypatch.setattr(site_proxy.shutil, "which", + lambda tool: str(root / "usr/lib/jvm/temurin-21/bin/keytool") if tool == "keytool" else None) + + trusted = site_proxy.trust_java(site_proxy.Paths(root), str(ca), issued.ca_pem) + + assert trusted == {str(temurin): {"trusted": True}, str(apt): {"trusted": True}, + str(bundled): {"trusted": True}, + str(locked): {"error": "keytool error: java.io.IOException: keystore password was incorrect"}} + assert _calls(temurin) == _calls(bundled) == ["-delete", "-importcert", "-list"] + assert _calls(apt) == ["-list"] and _calls(skipped) == [] + assert fingerprint in temurin.read_text() and fingerprint in bundled.read_text() + assert site_proxy.trust_java(site_proxy.Paths(tmp_path / "bare"), str(ca), issued.ca_pem) == {} + + +def test_a_jdk_store_the_ca_isnt_listed_in_after_routing_is_named(tmp_path, monkeypatch): + root = tmp_path / "root" + hook = root / "etc/ssl/certs/java/cacerts" + hook.parent.mkdir(parents=True) + hook.write_text("SHA256: 00:11\n") + apt = _jdk(root / "usr/lib/jvm/java-21-openjdk-amd64") + apt.unlink() + apt.symlink_to(hook) + issued = sites.issue(sites.site_table([SiteParams(name="notes", url="http://gw/website/notes/", + domains=["notes.test"])]).routes, valid_for=timedelta(hours=1), + label="t") + + trusted = site_proxy.trust_java(site_proxy.Paths(root), str(tmp_path / "ca.pem"), issued.ca_pem) + + assert trusted == {str(apt): {"error": "keytool -list doesn't list the CA in it"}} + + +def test_each_runtime_loads_the_first_https_site_through_its_own_trust_and_says_why_one_couldnt(tmp_path, monkeypatch): + programs = {"node": "#!/bin/sh\necho 200\n", + "curl": "#!/bin/sh\necho 'curl: (60) SSL certificate problem: unable to get local issuer " + "certificate' >&2\nexit 60\n"} + for name, text in programs.items(): + (tmp_path / name).write_text(text) + (tmp_path / name).chmod(0o755) + monkeypatch.setattr(site_proxy.shutil, "which", lambda tool: str(tmp_path / tool) if tool in programs else None) + monkeypatch.delenv("SSL_CERT_FILE", raising=False) + unset = site_proxy.check_runtimes("https://notes.test/") + issued = sites.issue(sites.site_table([SiteParams(name="notes", url="http://gw/website/notes/", + domains=["notes.test"])]).routes, valid_for=timedelta(hours=1), + label="t") + (tmp_path / "bundle.pem").write_text(issued.ca_pem) + monkeypatch.setenv("SSL_CERT_FILE", str(tmp_path / "bundle.pem")) + opened = [] + + class Opener: + def open(self, url, timeout): + opened.append(url) + return contextlib.nullcontext(SimpleNamespace(status=200)) + + monkeypatch.setattr(site_proxy.urllib.request, "build_opener", lambda *handlers: Opener()) + set_ = site_proxy.check_runtimes("https://notes.test/") + + assert unset == { + "python SSL_CERT_FILE": {"url": "https://notes.test/", + "error": "SSL_CERT_FILE isn't set in the computer's environment"}, + "node": {"url": "https://notes.test/", "status": 200}, + "curl": {"url": "https://notes.test/", + "error": "curl: (60) SSL certificate problem: unable to get local issuer certificate"}} + assert set_["python SSL_CERT_FILE"] == {"url": "https://notes.test/", "status": 200} + assert opened == ["https://notes.test/"] + + @pytest.mark.parametrize("resolv", [None, "", "nameserver 127.0.0.1\n"], ids=["missing", "empty", "loopback-only"]) def test_a_wildcard_name_starts_dns_even_when_resolv_conf_names_no_other_nameserver(tmp_path, monkeypatch, resolv): root, folder = tmp_path / "root", tmp_path / "sites" - (root / "etc").mkdir(parents=True) + (root / "etc/ssl/certs").mkdir(parents=True) + (root / "etc/ssl/certs/ca-certificates.crt").write_text("PUBLIC ROOTS\n") folder.mkdir() if resolv is not None: (root / "etc/resolv.conf").write_text(resolv) table = sites.site_table([SiteParams(name="chat", url="http://gw/website/chat/", domains=["*.chat.test"])]) issued = sites.issue(table.routes, valid_for=timedelta(hours=1), label="t") - (folder / "config.json").write_text(json.dumps(sites.proxy_config(table, issued, user=None))) + (folder / "config.json").write_text(json.dumps(sites.proxy_config(table, issued, user=None, os_type="linux"))) started = [] monkeypatch.setattr(site_proxy, "start", lambda path, log: started.append(Config.load(path))) - monkeypatch.setattr(site_proxy, "checks_as", lambda user, path: {}) + monkeypatch.setattr(site_proxy, "checks_as", lambda user, path: {"sites": {}, "runtimes": {}}) monkeypatch.setattr(site_proxy.shutil, "which", lambda tool: None) site_proxy.install(str(folder / "config.json"), site_proxy.Paths(root)) @@ -711,7 +904,8 @@ def sleep(seconds): def test_the_log_is_root_owned_beside_the_config_and_a_link_at_its_path_is_refused(tmp_path): - assert sites.proxy_config(sites.site_table([]), SimpleNamespace(ca_pem=None, leaves={}), user=None)["log"] == str(sites.FOLDER / "site-proxy.log") + assert sites.proxy_config(sites.site_table([]), SimpleNamespace(ca_pem=None, leaves={}), user=None, + os_type="linux")["log"] == str(sites.FOLDER / "site-proxy.log") target, log = tmp_path / "elsewhere", tmp_path / "site-proxy.log" target.write_text("kept") log.symlink_to(target) @@ -727,5 +921,5 @@ def test_the_proxy_is_standard_library_alone_so_the_bases_own_python_runs_it(): imported = {line.split()[1].split(".")[0] for line in source.splitlines() if line.startswith(("import ", "from ")) and not line.startswith("from __future__")} - assert imported <= {"asyncio", "json", "os", "pwd", "shutil", "socket", "ssl", "struct", "subprocess", "sys", - "time", "urllib", "dataclasses", "pathlib", "typing"} + assert imported <= {"asyncio", "base64", "hashlib", "json", "os", "pwd", "re", "shutil", "socket", "ssl", "struct", + "subprocess", "sys", "time", "urllib", "dataclasses", "pathlib", "typing"} diff --git a/tst/unit/env/envs/test_computer_sites.py b/tst/unit/env/envs/test_computer_sites.py index cb4fa285e..c151b467b 100644 --- a/tst/unit/env/envs/test_computer_sites.py +++ b/tst/unit/env/envs/test_computer_sites.py @@ -185,7 +185,7 @@ async def test_route_installs_the_proxy_as_root_with_each_sites_route_as_the_com sandbox = FakeVm(_REPORT) routed = await sites.route(sandbox, [_site("notes", "notes.test")], user="user", network=None, - valid_for=timedelta(hours=1), label="desktop") + valid_for=timedelta(hours=1), label="desktop", os_type="linux") assert set(sandbox.written) == {"/etc/agentenv/sites/site_proxy.py", "/etc/agentenv/sites/config.json"} config = json.loads(sandbox.written["/etc/agentenv/sites/config.json"]) @@ -209,7 +209,7 @@ async def test_the_config_holding_the_keys_is_written_over_stdin_into_a_private_ sandbox = FakeVm(_REPORT) await sites.route(sandbox, [_site("notes", "notes.test")], user="user", network=None, - valid_for=timedelta(hours=1), label="desktop") + valid_for=timedelta(hours=1), label="desktop", os_type="linux") assert ("write", "/etc/agentenv/sites/config.json") not in sandbox.events secret = sandbox.events.index(("secret", "/tmp/secret-1/config.json")) @@ -227,7 +227,7 @@ async def test_the_headers_the_gateways_provider_asks_for_go_in_the_private_conf "CF-Access-Client-Secret": "s3cret"} if url.startswith("http://host.docker.internal:8080/") else {}) routed = await sites.route(sandbox, [_site("notes", "notes.test")], user="user", network=None, - valid_for=timedelta(hours=1), label="desktop") + valid_for=timedelta(hours=1), label="desktop", os_type="linux") (site,) = json.loads(sandbox.written["/etc/agentenv/sites/config.json"])["sites"] assert site["upstream_headers"] == {"CF-Access-Client-Secret": "s3cret"} @@ -242,7 +242,7 @@ async def test_the_sites_keys_land_in_a_folder_root_alone_can_enter(): sandbox = FakeVm(_REPORT) await sites.route(sandbox, [_site("notes", "notes.test")], user="user", network=None, - valid_for=timedelta(hours=1), label="desktop") + valid_for=timedelta(hours=1), label="desktop", os_type="linux") private = sandbox.events.index("docker exec -u 0 computer-1 chmod 0700 /etc/agentenv/sites") assert private < sandbox.events.index(("cp", "/tmp/secret-1/config.json", "computer-1:/etc/agentenv/sites/config.json")) @@ -255,7 +255,7 @@ async def test_a_name_that_doesnt_load_in_the_computer_fails_the_route(): with pytest.raises(RuntimeError, match="https://notes.test/ didn't load in the computer: certificate verify"): await sites.route(FakeVm(report), [_site("notes", "notes.test")], user="user", network=None, - valid_for=timedelta(hours=1), label="desktop") + valid_for=timedelta(hours=1), label="desktop", os_type="linux") @pytest.mark.asyncio @@ -266,7 +266,7 @@ async def test_a_network_that_keeps_the_computer_from_the_gateway_is_refused_bef "host.docker.internal"): await sites.route(sandbox, [_site("notes", "notes.test")], user="user", network={"mode": "allowlist", "allow_hosts": ["api.example"]}, valid_for=timedelta(hours=1), - label="desktop") + label="desktop", os_type="linux") assert sandbox.scripts == [] and sandbox.written == {} @@ -280,7 +280,7 @@ async def test_a_gateway_the_sandboxs_applied_network_allows_by_its_address_rang routed = await sites.route(sandbox, [_site("notes", "notes.test")], user="user", network={"mode": "allowlist", "allow_hosts": ["api.example"]}, - valid_for=timedelta(hours=1), label="desktop") + valid_for=timedelta(hours=1), label="desktop", os_type="linux") assert routed["checks"] == _REPORT["checks"] routes = [sites.Route("notes", "http://10.0.0.7:8080/website/notes/", ("notes.test",))] @@ -305,7 +305,7 @@ async def write_file_from_text(content, path): write_file_from_text=write_file_from_text) await sites.route(sandbox, [_site("notes", "notes.test")], user="user", network=None, - valid_for=timedelta(hours=1), label="desktop") + valid_for=timedelta(hours=1), label="desktop", os_type="linux") assert calls == [("id", "-u"), ("mkdir", "-p", "/etc/agentenv/sites"), ("chmod", "0700", "/etc/agentenv/sites"), ("mkdir", "-p", "/etc/agentenv/sites"), ("write", "/etc/agentenv/sites/site_proxy.py"), @@ -330,7 +330,7 @@ async def write_file_from_text(content, path): with pytest.raises(RuntimeError, match="the coreweave sandbox runs the computer's commands as uid 1000"): await sites.route(sandbox, [_site("notes", "notes.test")], user="user", network=None, - valid_for=timedelta(hours=1), label="desktop") + valid_for=timedelta(hours=1), label="desktop", os_type="linux") assert calls == [("id", "-u")] @@ -343,7 +343,7 @@ async def test_a_site_naming_the_gateway_its_routes_are_on_is_refused_before_any with pytest.raises(RuntimeError, match=r"the gateway at gw.sites.test \(named by notes's .*sites.test\) would"): await sites.route(sandbox, [_site("notes", *domains)], user="user", network=None, - valid_for=timedelta(hours=1), label="desktop") + valid_for=timedelta(hours=1), label="desktop", os_type="linux") assert sandbox.scripts == [] and sandbox.written == {} @@ -545,3 +545,37 @@ async def test_the_websites_a_computer_routes_are_waited_on_till_ready_one_never assert (on_its_card.asked, mcp_only.asked) == (1, 0) assert str(never.value).startswith("site routing: the run's websites weren't ready after 0.05 s: ") assert str(never.value).endswith("broken (shop: its database is down); unreachable (ConnectError: refused)") + + +def test_routing_names_each_site_that_didnt_load_runtime_that_didnt_trust_the_ca_and_jdk_store_that_didnt_take_it(): + report = {"checks": {"notes.test": {"url": "https://notes.test/", "status": 200}, + "chat.test": {"url": "https://chat.test/", "error": "502 Bad Gateway: no route"}}, + "runtimes": {"node": {"url": "https://notes.test/", "status": 200}, + "curl": {"url": "https://notes.test/", "error": "curl: (60) SSL certificate problem"}}, + "trust": {"java": {"/usr/lib/jvm/a/lib/security/cacerts": {"trusted": True}, + "/opt/b/lib/security/cacerts": {"error": "keystore password was incorrect"}}}} + + assert sites.routing_problems(report) == [ + "https://chat.test/ didn't load in the computer: 502 Bad Gateway: no route", + "curl in the computer didn't trust the deploy's CA loading https://notes.test/: curl: (60) SSL certificate " + "problem", + "the JDK store /opt/b/lib/security/cacerts didn't take the deploy's CA: keystore password was incorrect"] + assert sites.routing_problems({"checks": {}, "trust": {}}) == [] + + +def test_the_trust_a_routing_linux_computer_boots_with_is_the_local_providers_so_the_two_always_agree(): + from agent_env.providers.sandbox_providers.local_docker.provider import LOCAL_TRUST_ENV + + assert sites.TRUST_ENVIRONMENTS == {"linux": LOCAL_TRUST_ENV} and LOCAL_TRUST_ENV == { + "SSL_CERT_FILE": "/etc/agentenv/ca-bundle.pem", "REQUESTS_CA_BUNDLE": "/etc/agentenv/ca-bundle.pem", + "NODE_EXTRA_CA_CERTS": "/etc/agentenv/ca.pem"} + assert sites.trust_files("linux") == {"ca": "/etc/agentenv/ca.pem", "bundle": "/etc/agentenv/ca-bundle.pem"} + + +def test_a_computer_boots_with_the_trust_variables_only_when_a_site_it_routes_is_served_over_https(): + local = [Site(name="notes", url="http://gw/website/notes/", domains=["notes.localhost"]), + Site(name="bare", url="http://gw/website/bare/")] + secure = [*local, Site(name="shop", url="http://gw/website/shop/", domains=["shop.test"])] + + assert sites.trust_environment("linux", local) == {} + assert sites.trust_environment("linux", secure) == sites.TRUST_ENVIRONMENTS["linux"] diff --git a/tst/unit/env/envs/test_computer_vm_bundle.py b/tst/unit/env/envs/test_computer_vm_bundle.py index 5ffb8f83a..26b03cd62 100644 --- a/tst/unit/env/envs/test_computer_vm_bundle.py +++ b/tst/unit/env/envs/test_computer_vm_bundle.py @@ -30,9 +30,11 @@ from agent_env.env.envs.computer.driver_env import ComputerDriverEnv from agent_env.env.envs.computer.env import ComputerEnv from agent_env.env.envs.computer.utils import composer, install, macos_vm_bundle +from agent_env.env.envs.computer.utils import params as computer_params from agent_env.env.envs.computer.utils.composer import Composing from agent_env.env.envs.computer.utils.install import Grant, MacInstall, MacService from agent_env.providers.sandbox_providers.sandbox_provider import SandboxProvider +from agent_env.task_step.task_steps.deploy_env import DeployEnvTaskStep BASE_IMAGE = "ghcr.io/trycua/macos:26-x@sha256:" + "7c" * 32 SERVE = "venv/bin/python -m agentenv_protocol.computer.serve driver.py:Desk" @@ -369,6 +371,18 @@ def test_a_macos_base_refuses_a_driver_that_declares_no_install_for_it(mac, tmp_ _put(driver, mac) +def test_site_routing_into_a_macos_computer_is_refused_at_save_and_at_deploy(mac, tmp_path): + env = _put(_driver(tmp_path / "desk"), mac) + params = {"site_routing": True, "sites": [{"name": "a", "url": "http://gw/website/a/", "domains": ["a.test"]}]} + + saved = DeployEnvTaskStep(id="d", version=None, env_id=env.id, env_params=params).preflight() + _, _, problems = computer_params.checked(env, params) + + assert problems == ["env_params.site_routing: site routing reaches Linux computers alone, and this computer runs " + "macos"] + assert len(saved) == 1 and problems[0] in saved[0] + + def test_the_service_runner_reads_the_protocols_seed_byte_for_byte(tmp_path): loader = importlib.machinery.SourceFileLoader("agentenv_service", str(macos_vm_bundle.PREPARE / "agentenv-service")) module = importlib.util.module_from_spec(importlib.util.spec_from_loader(loader.name, loader))