From 7ef10ff035f56f8e9c11a02b6d5dc7639730ca59 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Sat, 7 Nov 2026 23:02:23 -0800 Subject: [PATCH 1/5] feat(computer): every runtime in a computer routing sites trusts the deploy's CA, however it was started The trust variables lived only in /etc/profile.d, which only login shells read, so apps launched at boot or from the menu and computer-exec commands never saw them. The local provider's trust convention is now agentenv's for the sites CA too: /etc/agentenv/ca.pem holds every CA agentenv adds and ca-bundle.pem the public roots plus them, a computer routing sites boots with the same SSL_CERT_FILE, REQUESTS_CA_BUNDLE and NODE_EXTRA_CA_CERTS, and routing appends the CA to both files. Every JDK trusts it, apt ones through the system store's hook and the rest by keytool, and routing checks Python through SSL_CERT_FILE alone, Node and curl too, failing the deploy on any runtime or JDK store that doesn't trust it. Co-Authored-By: Claude Opus 5.5 --- .../env/envs/computer/utils/site_proxy.py | 172 ++++++++++++++++- .../env/envs/computer/utils/sites.py | 38 ++-- .../env_providers/env_computer_provider.py | 2 +- tst/integration/computer/ca_trust_test.py | 55 ++++++ tst/integration/computer/conftest.py | 11 +- .../computer/tls/envs/clients/Fetch.java | 6 + .../computer/tls/envs/clients/app.py | 22 +++ .../computer/tls/envs/clients/env.toml | 4 + .../computer/tls/envs/clients/fetch.py | 22 +++ .../computer/tls/envs/clients/setup.sh | 8 + .../computer/tls/envs/desktop/env.toml | 4 + .../computer/tls/tasks/desktop.json | 3 + tst/unit/env/envs/test_computer_env.py | 4 +- tst/unit/env/envs/test_computer_site_proxy.py | 175 +++++++++++++++++- tst/unit/env/envs/test_computer_sites.py | 25 +++ 15 files changed, 518 insertions(+), 33 deletions(-) create mode 100644 tst/integration/computer/ca_trust_test.py create mode 100644 tst/integration/computer/tls/envs/clients/Fetch.java create mode 100644 tst/integration/computer/tls/envs/clients/app.py create mode 100644 tst/integration/computer/tls/envs/clients/env.toml create mode 100644 tst/integration/computer/tls/envs/clients/fetch.py create mode 100755 tst/integration/computer/tls/envs/clients/setup.sh create mode 100644 tst/integration/computer/tls/envs/desktop/env.toml create mode 100644 tst/integration/computer/tls/tasks/desktop.json 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..80f47cc9f 100644 --- a/src/agent_env/env/envs/computer/utils/site_proxy.py +++ b/src/agent_env/env/envs/computer/utils/site_proxy.py @@ -19,6 +19,8 @@ from __future__ import annotations import asyncio +import base64 +import hashlib import json import os import pwd @@ -59,6 +61,17 @@ # 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 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"),) @@ -665,6 +678,18 @@ def profile(self) -> Path: def system_bundle(self) -> Path: return self.root / "etc/ssl/certs/ca-certificates.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: """``current``, a hosts file, with its block of the sites' names replaced by one mapping ``names`` to loopback.""" @@ -731,6 +756,87 @@ def trust_system(paths: Paths, ca_pem: str) -> Optional[str]: return None +def add_trust_files(paths: Paths, files: dict, ca_pem: str) -> list[str]: + """The CA among those agentenv adds, ``files["ca"]``, and in the bundle of the public roots plus them, + ``files["bundle"]``: each kept as a provider made it, with its own CA in it, the bundle else started from the system + store's, which ``trust_system`` added the CA to where it could. Both, and their folder, readable by everyone.""" + ca, bundle = paths.rooted(files["ca"]), paths.rooted(files["bundle"]) + ca.parent.mkdir(parents=True, exist_ok=True) + ca.parent.chmod(0o755) + system = paths.system_bundle.read_text() if paths.system_bundle.exists() else "" + for path, current in ((ca, ca.read_text() if ca.exists() else ""), + (bundle, bundle.read_text() if bundle.exists() else system)): + write_in_place(path, current if ca_pem.strip() in current else + (current.rstrip("\n") + "\n" if current.strip() else "") + ca_pem.strip() + "\n") + 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 +908,15 @@ 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"]) + if added := given.get("trust_files"): + trust["added"] = add_trust_files(paths, added, given["ca_pem"]) + variables = {"SSL_CERT_FILE": added["bundle"], "REQUESTS_CA_BUNDLE": added["bundle"], + "NODE_EXTRA_CA_CERTS": added["ca"]} + else: + bundle = paths.system_bundle if paths.system_bundle.exists() else None + variables = {"NODE_EXTRA_CA_CERTS": ca, **({"REQUESTS_CA_BUNDLE": str(bundle)} if bundle else {})} + write_in_place(paths.profile, "".join(f"export {name}={value}\n" for name, value in variables.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 +927,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 +968,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 +1001,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..42abcf1d1 100644 --- a/src/agent_env/env/envs/computer/utils/sites.py +++ b/src/agent_env/env/envs/computer/utils/sites.py @@ -7,10 +7,12 @@ 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 to a container, the local provider's +convention (``LOCAL_TRUST_ENV``): ``/etc/agentenv/ca.pem``, every CA agentenv adds, at a fixed path 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 (``TRUST_ENVIRONMENT``), so every program in it trusts the sites however it was started.""" from __future__ import annotations @@ -37,6 +39,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 +51,10 @@ FOLDER = PurePosixPath("/etc/agentenv/sites") PROXY = FOLDER / "site_proxy.py" CONFIG = FOLDER / "config.json" -CA_PATH = FOLDER / "ca.pem" +#: The variables naming the trust agentenv adds, which a computer routing sites boots with, and the files they name. +TRUST_ENVIRONMENT = dict(LOCAL_TRUST_ENV) +CA_PATH = PurePosixPath(TRUST_ENVIRONMENT["NODE_EXTRA_CA_CERTS"]) +BUNDLE_PATH = PurePosixPath(TRUST_ENVIRONMENT["SSL_CERT_FILE"]) LOG = FOLDER / "site-proxy.log" SYNTHETIC, ALLOWLIST = "synthetic", "allowlist" RANKS = (SYNTHETIC, ALLOWLIST) @@ -278,7 +284,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": {"ca": str(CA_PATH), "bundle": str(BUNDLE_PATH)}} def reachable_problem(policy: Optional[NetworkPolicy], routes: list[Route]) -> Optional[str]: @@ -407,12 +414,21 @@ async def route(sandbox: Sandbox, sites: list[Site], *, user: Optional[str], net 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())) + if failed := routing_problems(report): + raise RuntimeError("site routing: " + "; ".join(failed)) return {"names": table.to_dict(), "ca": issued.ca_pem, "ca_path": str(CA_PATH), "trust": report["trust"], - "checks": report["checks"]} + "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..541edd0b8 100644 --- a/src/agent_env/providers/env_providers/env_computer_provider.py +++ b/src/agent_env/providers/env_providers/env_computer_provider.py @@ -311,7 +311,7 @@ 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} 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..a8634f49e --- /dev/null +++ b/tst/integration/computer/ca_trust_test.py @@ -0,0 +1,55 @@ +"""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 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"] + 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 {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..82899a1bd 100644 --- a/tst/unit/env/envs/test_computer_env.py +++ b/tst/unit/env/envs/test_computer_env.py @@ -986,10 +986,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_ENVIRONMENT) & 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 +1016,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_ENVIRONMENT.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..7ae97a01c 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 @@ -580,7 +583,8 @@ def test_install_writes_the_sites_files_names_and_trust_then_starts_the_proxy_on (folder / "config.json").write_text(json.dumps(sites.proxy_config(table, issued, user=None))) 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,9 +604,16 @@ 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}} + for added in ("etc/agentenv/ca.pem", "etc/agentenv/ca-bundle.pem"): + assert (root / added).read_text() == issued.ca_pem.strip() + "\n" + 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() == ( + "export SSL_CERT_FILE=/etc/agentenv/ca-bundle.pem\nexport REQUESTS_CA_BUNDLE=/etc/agentenv/ca-bundle.pem\n" + "export NODE_EXTRA_CA_CERTS=/etc/agentenv/ca.pem\n") def test_install_keeps_the_upstream_headers_in_a_file_root_alone_reads_and_serve_json_names_it(tmp_path, monkeypatch): @@ -616,7 +627,7 @@ def test_install_keeps_the_upstream_headers_in_a_file_root_alone_reads_and_serve (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,6 +666,152 @@ 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 + + +# 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" @@ -667,7 +824,7 @@ def test_a_wildcard_name_starts_dns_even_when_resolv_conf_names_no_other_nameser (folder / "config.json").write_text(json.dumps(sites.proxy_config(table, issued, user=None))) 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)) @@ -727,5 +884,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", "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..566b12fa9 100644 --- a/tst/unit/env/envs/test_computer_sites.py +++ b/tst/unit/env/envs/test_computer_sites.py @@ -545,3 +545,28 @@ 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_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_ENVIRONMENT == 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 (str(sites.CA_PATH), str(sites.BUNDLE_PATH)) == ("/etc/agentenv/ca.pem", "/etc/agentenv/ca-bundle.pem") From b0673c136b559eb3bc3b9f0cceeba40828a71d67 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Sat, 7 Nov 2026 23:22:01 -0800 Subject: [PATCH 2/5] fix(computer): a computer routing only plain-HTTP sites boots without the trust variables With every routed name a .localhost one, no CA is made and the install writes no trust files, so SSL_CERT_FILE and REQUESTS_CA_BUNDLE named files that didn't exist and every public HTTPS request through them failed. The computer now boots with them only when a site it routes is served over HTTPS, as issue() decides a CA is needed. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/env/envs/computer/utils/sites.py | 6 ++++++ .../providers/env_providers/env_computer_provider.py | 2 +- tst/unit/env/envs/test_computer_sites.py | 9 +++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/agent_env/env/envs/computer/utils/sites.py b/src/agent_env/env/envs/computer/utils/sites.py index 42abcf1d1..3f3ba1414 100644 --- a/src/agent_env/env/envs/computer/utils/sites.py +++ b/src/agent_env/env/envs/computer/utils/sites.py @@ -271,6 +271,12 @@ def _key_usage(**granted: bool) -> x509.KeyUsage: return x509.KeyUsage(**{usage: granted.get(usage, False) for usage in usages}) +def trust_environment(sites: list[Site]) -> dict[str, str]: + """The variables naming the trust agentenv adds, for a 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_ENVIRONMENT) if any(route.tls_names for route in site_table(sites).routes) else {} + + def proxy_config(table: SiteTable, issued: Issued, *, user: Optional[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 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 541edd0b8..4b3a0d9c1 100644 --- a/src/agent_env/providers/env_providers/env_computer_provider.py +++ b/src/agent_env/providers/env_providers/env_computer_provider.py @@ -311,7 +311,7 @@ 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", **sites.TRUST_ENVIRONMENT} if params.site_routing else {})} + **({HOLD_BOOT_LAUNCH_ENV: "1", **sites.trust_environment(params.sites)} if params.site_routing else {})} def _require_name(env: ComputerEnv, card: dict) -> None: diff --git a/tst/unit/env/envs/test_computer_sites.py b/tst/unit/env/envs/test_computer_sites.py index 566b12fa9..281171712 100644 --- a/tst/unit/env/envs/test_computer_sites.py +++ b/tst/unit/env/envs/test_computer_sites.py @@ -570,3 +570,12 @@ def test_the_trust_a_routing_computer_boots_with_is_the_local_providers_so_the_t "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 (str(sites.CA_PATH), str(sites.BUNDLE_PATH)) == ("/etc/agentenv/ca.pem", "/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(local) == {} + assert sites.trust_environment(secure) == sites.TRUST_ENVIRONMENT From f47f064931e514a47fbf1645e5680e3c26f8c051 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Sun, 8 Nov 2026 05:17:10 -0800 Subject: [PATCH 3/5] fix(computer): site routing finds public roots on Red Hat too, ships the trust files in the Ubuntu bases, and is refused off Linux - The bundle of public roots plus the CAs agentenv adds starts from Debian's or Red Hat's file of public roots where neither the image nor a provider made it, and routing is refused on a Linux with neither rather than leave it the CA alone. - The ubuntu-24.04 and ubuntu-24.04-headless images ship /etc/agentenv/ca.pem (empty) and ca-bundle.pem (the system bundle), so services started at boot, before routing, find the files the trust variables name. - The trust files and variables are kept per OS type (sites.TRUST_ENVIRONMENTS, Linux's the local provider's), and site routing is refused at save and at deploy on a computer that isn't Linux, which a macOS computer would have half-applied. Co-Authored-By: Claude Opus 5.5 --- .../ubuntu-24.04-headless/Dockerfile | 5 +- .../artifacts/ubuntu-24.04/Dockerfile | 5 +- .../env/envs/computer/utils/params.py | 3 + .../env/envs/computer/utils/site_proxy.py | 29 +++++++--- .../env/envs/computer/utils/sites.py | 55 +++++++++++-------- .../env_providers/env_computer_provider.py | 6 +- tst/integration/computer/ca_trust_test.py | 7 ++- tst/unit/env/envs/test_computer_env.py | 7 ++- tst/unit/env/envs/test_computer_site_proxy.py | 35 +++++++++--- tst/unit/env/envs/test_computer_sites.py | 30 +++++----- tst/unit/env/envs/test_computer_vm_bundle.py | 14 +++++ 11 files changed, 132 insertions(+), 64 deletions(-) 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 80f47cc9f..2783829c8 100644 --- a/src/agent_env/env/envs/computer/utils/site_proxy.py +++ b/src/agent_env/env/envs/computer/utils/site_proxy.py @@ -675,8 +675,9 @@ 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: @@ -756,16 +757,26 @@ 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 among those agentenv adds, ``files["ca"]``, and in the bundle of the public roots plus them, - ``files["bundle"]``: each kept as a provider made it, with its own CA in it, the bundle else started from the system - store's, which ``trust_system`` added the CA to where it could. Both, and their folder, readable by everyone.""" + """The CA added to ``files["ca"]``, the CAs agentenv adds, and to ``files["bundle"]``, the public roots plus them, + each kept as the image or a provider made it. A missing or empty bundle starts from the system store's public + roots, which ``trust_system`` added the CA to where it could; a Linux with none of them is refused rather than left + with a bundle of the CA alone. Both, and their folder, readable by everyone.""" ca, bundle = paths.rooted(files["ca"]), paths.rooted(files["bundle"]) + roots = bundle.read_text() if bundle.exists() else "" + if not roots.strip(): + if (system := public_roots(paths)) is None: + 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") + roots = system.read_text() ca.parent.mkdir(parents=True, exist_ok=True) ca.parent.chmod(0o755) - system = paths.system_bundle.read_text() if paths.system_bundle.exists() else "" - for path, current in ((ca, ca.read_text() if ca.exists() else ""), - (bundle, bundle.read_text() if bundle.exists() else system)): + for path, current in ((ca, ca.read_text() if ca.exists() else ""), (bundle, roots)): write_in_place(path, current if ca_pem.strip() in current else (current.rstrip("\n") + "\n" if current.strip() else "") + ca_pem.strip() + "\n") path.chmod(0o644) @@ -914,7 +925,7 @@ def install(config_path: str, paths: Paths = Paths()) -> dict: variables = {"SSL_CERT_FILE": added["bundle"], "REQUESTS_CA_BUNDLE": added["bundle"], "NODE_EXTRA_CA_CERTS": added["ca"]} else: - bundle = paths.system_bundle if paths.system_bundle.exists() else None + bundle = public_roots(paths) variables = {"NODE_EXTRA_CA_CERTS": ca, **({"REQUESTS_CA_BUNDLE": str(bundle)} if bundle else {})} write_in_place(paths.profile, "".join(f"export {name}={value}\n" for name, value in variables.items())) trust["environment"] = str(paths.profile) diff --git a/src/agent_env/env/envs/computer/utils/sites.py b/src/agent_env/env/envs/computer/utils/sites.py index 3f3ba1414..00ff70872 100644 --- a/src/agent_env/env/envs/computer/utils/sites.py +++ b/src/agent_env/env/envs/computer/utils/sites.py @@ -9,10 +9,11 @@ started (``route``), which a reset does again on its fresh computer. In the computer they live in ``/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 to a container, the local provider's -convention (``LOCAL_TRUST_ENV``): ``/etc/agentenv/ca.pem``, every CA agentenv adds, at a fixed path 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 (``TRUST_ENVIRONMENT``), so every program in it trusts the sites however it was started.""" +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 @@ -51,10 +52,9 @@ FOLDER = PurePosixPath("/etc/agentenv/sites") PROXY = FOLDER / "site_proxy.py" CONFIG = FOLDER / "config.json" -#: The variables naming the trust agentenv adds, which a computer routing sites boots with, and the files they name. -TRUST_ENVIRONMENT = dict(LOCAL_TRUST_ENV) -CA_PATH = PurePosixPath(TRUST_ENVIRONMENT["NODE_EXTRA_CA_CERTS"]) -BUNDLE_PATH = PurePosixPath(TRUST_ENVIRONMENT["SSL_CERT_FILE"]) +#: 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) @@ -271,17 +271,24 @@ def _key_usage(**granted: bool) -> x509.KeyUsage: return x509.KeyUsage(**{usage: granted.get(usage, False) for usage in usages}) -def trust_environment(sites: list[Site]) -> dict[str, str]: - """The variables naming the trust agentenv adds, for a 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_ENVIRONMENT) if any(route.tls_names for route in site_table(sites).routes) else {} +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 proxy_config(table: SiteTable, issued: Issued, *, user: Optional[str], +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 + 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)} @@ -291,7 +298,7 @@ def proxy_config(table: SiteTable, issued: Issued, *, user: Optional[str], site["upstream_headers"] = dict(upstream_headers[route.site]) sites.append(site) return {"user": user, "ca_pem": issued.ca_pem, "sites": sites, "log": str(LOG), - "trust_files": {"ca": str(CA_PATH), "bundle": str(BUNDLE_PATH)}} + "trust_files": trust_files(os_type)} def reachable_problem(policy: Optional[NetworkPolicy], routes: list[Route]) -> Optional[str]: @@ -396,10 +403,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] @@ -415,15 +422,15 @@ 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]) if failed := routing_problems(report): raise RuntimeError("site routing: " + "; ".join(failed)) - return {"names": table.to_dict(), "ca": issued.ca_pem, "ca_path": str(CA_PATH), "trust": report["trust"], - "checks": report["checks"], "runtimes": report.get("runtimes", {})} + 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]: 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 4b3a0d9c1..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", **sites.trust_environment(params.sites)} 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 index a8634f49e..5e525e091 100644 --- a/tst/integration/computer/ca_trust_test.py +++ b/tst/integration/computer/ca_trust_test.py @@ -1,8 +1,9 @@ """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 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.""" +/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 @@ -40,6 +41,7 @@ async def test_every_runtime_of_a_computer_routing_a_site_trusts_its_ca_launched 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) @@ -48,6 +50,7 @@ async def test_every_runtime_of_a_computer_routing_a_site_trusts_its_ca_launched 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"] diff --git a/tst/unit/env/envs/test_computer_env.py b/tst/unit/env/envs/test_computer_env.py index 82899a1bd..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,7 +987,7 @@ 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_ENVIRONMENT) & set(boots.boots[0]["environment"]) + assert not set(env_computer_provider.sites.TRUST_ENVIRONMENTS["linux"]) & set(boots.boots[0]["environment"]) @pytest.mark.asyncio @@ -1016,7 +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_ENVIRONMENT.items() <= boots.boots[0]["environment"].items() + 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 7ae97a01c..df8f4e773 100644 --- a/tst/unit/env/envs/test_computer_site_proxy.py +++ b/tst/unit/env/envs/test_computer_site_proxy.py @@ -575,12 +575,14 @@ 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: {"sites": {"notes.test": {"status": 200}}, @@ -607,8 +609,9 @@ def test_install_writes_the_sites_files_names_and_trust_then_starts_the_proxy_on 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}} 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).read_text() == issued.ca_pem.strip() + "\n" 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() == ( @@ -618,12 +621,14 @@ def test_install_writes_the_sites_files_names_and_trust_then_starts_the_proxy_on 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))) @@ -688,6 +693,20 @@ def test_the_sites_ca_joins_the_trust_a_provider_added_and_a_bundle_made_here_st assert (tmp_path / "other/etc/agentenv/ca-bundle.pem").read_text() == "SYSTEM ROOTS\n" + ca_pem +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. @@ -815,13 +834,14 @@ def open(self, url, timeout): @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: {"sites": {}, "runtimes": {}}) @@ -868,7 +888,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) diff --git a/tst/unit/env/envs/test_computer_sites.py b/tst/unit/env/envs/test_computer_sites.py index 281171712..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 == {} @@ -563,13 +563,13 @@ def test_routing_names_each_site_that_didnt_load_runtime_that_didnt_trust_the_ca assert sites.routing_problems({"checks": {}, "trust": {}}) == [] -def test_the_trust_a_routing_computer_boots_with_is_the_local_providers_so_the_two_always_agree(): +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_ENVIRONMENT == 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 (str(sites.CA_PATH), str(sites.BUNDLE_PATH)) == ("/etc/agentenv/ca.pem", "/etc/agentenv/ca-bundle.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(): @@ -577,5 +577,5 @@ def test_a_computer_boots_with_the_trust_variables_only_when_a_site_it_routes_is 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(local) == {} - assert sites.trust_environment(secure) == sites.TRUST_ENVIRONMENT + 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)) From ece919c6b34f4e8f36fc9469856670c53a6aff96 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Sun, 8 Nov 2026 05:38:58 -0800 Subject: [PATCH 4/5] fix(computer): routing makes the trust bundle again from the system store, so it holds CAs apps added after the image was built The Ubuntu bases copy the system bundle into /etc/agentenv/ca-bundle.pem when they're built, before a composed app's install runs. An app that added a CA with update-ca-certificates would have been left out of the bundle routing kept, so clients reading SSL_CERT_FILE rejected it. Routing now makes the bundle from the system store's public roots as they are then, plus every CA in ca.pem; a Linux with no file of public roots keeps the bundle it has. Co-Authored-By: Claude Opus 5.5 --- .../env/envs/computer/utils/site_proxy.py | 32 ++++++++++++------- tst/unit/env/envs/test_computer_site_proxy.py | 19 ++++++++++- 2 files changed, 38 insertions(+), 13 deletions(-) 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 2783829c8..6347568a8 100644 --- a/src/agent_env/env/envs/computer/utils/site_proxy.py +++ b/src/agent_env/env/envs/computer/utils/site_proxy.py @@ -24,6 +24,7 @@ import json import os import pwd +import re import shutil import socket import ssl @@ -69,6 +70,8 @@ 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); })") @@ -763,22 +766,27 @@ def public_roots(paths: Paths) -> Optional[Path]: def add_trust_files(paths: Paths, files: dict, ca_pem: str) -> list[str]: - """The CA added to ``files["ca"]``, the CAs agentenv adds, and to ``files["bundle"]``, the public roots plus them, - each kept as the image or a provider made it. A missing or empty bundle starts from the system store's public - roots, which ``trust_system`` added the CA to where it could; a Linux with none of them is refused rather than left - with a bundle of the CA alone. Both, and their folder, readable by everyone.""" + """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"]) - roots = bundle.read_text() if bundle.exists() else "" - if not roots.strip(): - if (system := public_roots(paths)) is None: - 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") + 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, current in ((ca, ca.read_text() if ca.exists() else ""), (bundle, roots)): - write_in_place(path, current if ca_pem.strip() in current else - (current.rstrip("\n") + "\n" if current.strip() else "") + ca_pem.strip() + "\n") + 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)] diff --git a/tst/unit/env/envs/test_computer_site_proxy.py b/tst/unit/env/envs/test_computer_site_proxy.py index df8f4e773..c4e16413c 100644 --- a/tst/unit/env/envs/test_computer_site_proxy.py +++ b/tst/unit/env/envs/test_computer_site_proxy.py @@ -693,6 +693,23 @@ def test_the_sites_ca_joins_the_trust_a_provider_added_and_a_bundle_made_here_st 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"} @@ -905,5 +922,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", "base64", "hashlib", "json", "os", "pwd", "shutil", "socket", "ssl", "struct", + assert imported <= {"asyncio", "base64", "hashlib", "json", "os", "pwd", "re", "shutil", "socket", "ssl", "struct", "subprocess", "sys", "time", "urllib", "dataclasses", "pathlib", "typing"} From 9e842152fc7f8d84253c9dde094951dfd32fb92a Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Sun, 8 Nov 2026 06:26:00 -0800 Subject: [PATCH 5/5] refactor(computer): the profile script sets the variables the host's per-OS table names, with no fallback The host now sends the OS's trust variables with the trust files, and install writes exactly those to the profile script, so login shells and the boot environment can't disagree when a variable is added. The fallback for a config without trust files, which nothing sends since the proxy is copied with the host on every route, is gone. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/env/envs/computer/utils/site_proxy.py | 11 +++-------- src/agent_env/env/envs/computer/utils/sites.py | 7 ++++--- tst/unit/env/envs/test_computer_site_proxy.py | 5 ++--- 3 files changed, 9 insertions(+), 14 deletions(-) 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 6347568a8..76c97c566 100644 --- a/src/agent_env/env/envs/computer/utils/site_proxy.py +++ b/src/agent_env/env/envs/computer/utils/site_proxy.py @@ -928,14 +928,9 @@ def install(config_path: str, paths: Paths = Paths()) -> dict: trust["chromium"] = [str(policy) for policy in paths.chromium_policies] trust["nss"] = trust_nss(user, ca) trust["java"] = trust_java(paths, ca, given["ca_pem"]) - if added := given.get("trust_files"): - trust["added"] = add_trust_files(paths, added, given["ca_pem"]) - variables = {"SSL_CERT_FILE": added["bundle"], "REQUESTS_CA_BUNDLE": added["bundle"], - "NODE_EXTRA_CA_CERTS": added["ca"]} - else: - bundle = public_roots(paths) - variables = {"NODE_EXTRA_CA_CERTS": ca, **({"REQUESTS_CA_BUNDLE": str(bundle)} if bundle else {})} - write_in_place(paths.profile, "".join(f"export {name}={value}\n" for name, value in variables.items())) + 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}} diff --git a/src/agent_env/env/envs/computer/utils/sites.py b/src/agent_env/env/envs/computer/utils/sites.py index 00ff70872..e8c3d5ca8 100644 --- a/src/agent_env/env/envs/computer/utils/sites.py +++ b/src/agent_env/env/envs/computer/utils/sites.py @@ -286,8 +286,9 @@ def trust_environment(os_type: str, sites: list[Site]) -> dict[str, str]: 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, the trust files on an ``os_type`` computer, and - each route with its certificate and key, and the headers its gateway's sandbox provider asks requests to it to carry + """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: @@ -298,7 +299,7 @@ def proxy_config(table: SiteTable, issued: Issued, *, user: Optional[str], os_ty site["upstream_headers"] = dict(upstream_headers[route.site]) sites.append(site) return {"user": user, "ca_pem": issued.ca_pem, "sites": sites, "log": str(LOG), - "trust_files": trust_files(os_type)} + "trust_files": trust_files(os_type), "trust_environment": dict(TRUST_ENVIRONMENTS[os_type])} def reachable_problem(policy: Optional[NetworkPolicy], routes: list[Route]) -> Optional[str]: diff --git a/tst/unit/env/envs/test_computer_site_proxy.py b/tst/unit/env/envs/test_computer_site_proxy.py index c4e16413c..e520b8447 100644 --- a/tst/unit/env/envs/test_computer_site_proxy.py +++ b/tst/unit/env/envs/test_computer_site_proxy.py @@ -614,9 +614,8 @@ def test_install_writes_the_sites_files_names_and_trust_then_starts_the_proxy_on 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() == ( - "export SSL_CERT_FILE=/etc/agentenv/ca-bundle.pem\nexport REQUESTS_CA_BUNDLE=/etc/agentenv/ca-bundle.pem\n" - "export NODE_EXTRA_CA_CERTS=/etc/agentenv/ca.pem\n") + 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):