diff --git a/packages/agentenv-protocol/tests/test_browser_recording.py b/packages/agentenv-protocol/tests/test_browser_recording.py index e3376c88f..2da359a0a 100644 --- a/packages/agentenv-protocol/tests/test_browser_recording.py +++ b/packages/agentenv-protocol/tests/test_browser_recording.py @@ -417,10 +417,11 @@ async def test_a_tab_behind_the_active_one_is_framed_once_a_second_capture_nudge pipe.emit("Target.targetCreated", {"targetInfo": {"targetId": "T1", "type": "page", "url": "about:blank"}}) pipe.emit("Target.attachedToTarget", {"sessionId": "s-T1", "targetInfo": {"targetId": "T1", "type": "page"}}) + asked = asyncio.get_running_loop().time() data, _, tab, _ = await control.frame("T1", load_wait_s=0) - (_, _, first), (_, _, second) = pipe.captured - assert (base64.b64decode(data), tab) == (b"behind", "T1") and second - first >= NUDGE_S + (_, _, _), (_, _, second) = pipe.captured + assert (base64.b64decode(data), tab) == (b"behind", "T1") and second - asked >= NUDGE_S @pytest.mark.asyncio diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/Dockerfile b/src/agent_env/computers/artifacts/ubuntu-24.04/Dockerfile index 8124c124b..7ac094a1b 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/Dockerfile +++ b/src/agent_env/computers/artifacts/ubuntu-24.04/Dockerfile @@ -56,6 +56,12 @@ RUN sed -i -e "s/^AGENTENV_SESSION_UID=.*/AGENTENV_SESSION_UID=$(id -u user)/" \ > /etc/ld.so.preload COPY --from=egress /out/egress /usr/local/lib/agentenv-egress/egress COPY egress/agentenv-egress.sh /etc/profile.d/agentenv-egress.sh +# Every Chromium build an app brings goes out through the egress proxy by a managed policy, however it's launched. +COPY egress/chromium-policy.json /usr/local/lib/agentenv-egress/chromium-policy.json +RUN for build in /etc/opt/chrome_for_testing /etc/chromium /etc/chromium-browser /etc/opt/chrome; do \ + install -D -m 0644 /usr/local/lib/agentenv-egress/chromium-policy.json \ + "$build/policies/managed/agentenv-egress.json"; \ + done COPY root.conf /etc/supervisor/root.conf 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/computers/artifacts/ubuntu-24.04/egress/chromium-policy.json b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/chromium-policy.json new file mode 100644 index 000000000..ef2d43a7e --- /dev/null +++ b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/chromium-policy.json @@ -0,0 +1,8 @@ +{ + "ProxySettings": { + "ProxyMode": "fixed_servers", + "ProxyServer": "http://127.0.0.1:3128", + "ProxyBypassList": "localhost;127.0.0.1;[::1];host.docker.internal" + }, + "WebRtcIPHandling": "disable_non_proxied_udp" +} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/start-desktop b/src/agent_env/computers/artifacts/ubuntu-24.04/start-desktop index 423f143ac..321f69cd1 100755 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/start-desktop +++ b/src/agent_env/computers/artifacts/ubuntu-24.04/start-desktop @@ -26,7 +26,8 @@ case "${1:-}" in # supervisord runs one X at a time, so a lock here is one a killed X left, and X refuses to start while the pid # it names is taken, which after a restart it often is. rm -f "/tmp/.X${DISPLAY#:}-lock" - exec Xvfb "$DISPLAY" -screen 0 "${resolution}x24" -nolisten tcp -ac + # No screensaver and no DPMS: their timeouts run on real time, and would blank an unattended screen. + exec Xvfb "$DISPLAY" -screen 0 "${resolution}x24" -nolisten tcp -ac -s 0 -dpms ;; with-x) shift diff --git a/tst/integration/computer/chromium_universe_test.py b/tst/integration/computer/chromium_universe_test.py new file mode 100644 index 000000000..ee127de05 --- /dev/null +++ b/tst/integration/computer/chromium_universe_test.py @@ -0,0 +1,87 @@ +"""agentenv's chromium on the ubuntu-24.04 desktop, on the universe clock: its boot browser, never launched again, goes +out through the computer's egress proxy by the base's policy and loads live sites it hasn't visited with the universe +182 days back, 730 days on and six years on, and right after a jump back; and X runs no screensaver. Needs Docker, the +local registry on localhost:5000, and the internet.""" + +import asyncio +import json +import re +import subprocess +import time + +import pytest +from agentenv_protocol import client as protocol_v1 +from agentenv_protocol.types import WELL_KNOWN_PATH +from agentenv_protocol.website_browser import BROWSER + +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.browser_state import _client +from tst.integration.computer.conftest import UniverseClock, computer_container +from tst.integration.computer.website_browser_test import _app, _browser_process + +pytestmark = [pytest.mark.integration, pytest.mark.int_test_slow] + +DAY = 86400 +#: Live sites the browser hasn't visited, each with its page's title, opened in turn with the universe that many days +#: from now: the last right after the jump back. +STOPS = [(-182, "https://example.com/", "Example Domain"), (730, "https://example.org/", "Example Domain"), + (6 * 365, "https://example.net/", "Example Domain"), (-182, "https://www.wikipedia.org/", "Wikipedia")] +#: How long a page may take to load before its tab counts as not loaded. +LOAD_S = 45 +POLICY = "/etc/opt/chrome_for_testing/policies/managed/agentenv-egress.json" + + +async def _loaded(ok, url: str, title: str) -> str: + """The title of the active tab once it's ``title`` after navigating to ``url``, else the tabs it last saw.""" + await ok("navigate", {"url": url}) + tabs, deadline = [], time.monotonic() + LOAD_S + while time.monotonic() < deadline: + tabs = (await ok("tabs/list", method="GET"))["tabs"] + if (active := next((tab for tab in tabs if tab["active"]), None)) and active["title"] == title: + return title + await asyncio.sleep(0.5) + return json.dumps(tabs) + + +def _egress_rows(deployed) -> list[dict]: + log = subprocess.run(["docker", "exec", computer_container(deployed.sandbox_id), "cat", + "/var/log/agentenv-root/egress.log"], capture_output=True, text=True).stdout + return [json.loads(line) for line in log.splitlines() if line.startswith("{")] + + +@pytest.mark.asyncio +async def test_its_boot_browser_loads_live_sites_at_any_universe_date_and_x_never_blanks( + chromium_desktop): + with namespace_routing(): + deployed = await Env.get(chromium_desktop.id).deploy() + context = TaskStepContext(deployed_envs=[deployed]) + try: + card = await _app(deployed, WELL_KNOWN_PATH, "chromium") + _, ok = _client(deployed, lambda op: f"{deployed.environment_url}/svc/chromium" + f"{protocol_v1.find_extension_method(card, BROWSER, op)['endpoint']}") + booted = await _browser_process(deployed) + clock, loaded = UniverseClock(deployed), [] + for days, url, title in STOPS: + clock.set(time.time() + days * DAY, 1) + await asyncio.sleep(2) # each program maps the page the next time it reads the clock, within a second + loaded.append(await _loaded(ok, url, title)) + still = await _browser_process(deployed) + rows = _egress_rows(deployed) + xset = (await inside.run(deployed, ["xset", "q"], timeout_s=30))["stdout"] + policy = json.loads((await inside.run(deployed, ["cat", POLICY], timeout_s=30))["stdout"]) + finally: + report = await teardown_run(context) + + assert loaded == [title for _, _, title in STOPS], loaded + assert still["pid"] == booted["pid"] + hosts = {url.split("/")[2] for _, url, _ in STOPS} + intercepted = {row["host"] for row in rows if row.get("host") in hosts and row.get("mode") == "intercept"} + assert intercepted == hosts, [row for row in rows if row.get("host") in hosts][-8:] + assert re.search(r"Screen Saver:.*?timeout:\s+0\b", xset, re.S), xset + assert "DPMS is Disabled" in xset or "does not have the DPMS Extension" in xset, xset + assert policy["ProxySettings"]["ProxyServer"] == "http://127.0.0.1:3128" + assert not report.failed diff --git a/tst/integration/computer/website_browser_test.py b/tst/integration/computer/website_browser_test.py index a2da177f0..5a80b0d4f 100644 --- a/tst/integration/computer/website_browser_test.py +++ b/tst/integration/computer/website_browser_test.py @@ -73,7 +73,7 @@ def log_message(self, *args): # The browser's pid and argv, and how many TCP sockets anything in its session listens on: its oldest process with # agentenv's pipe flag, whose title Chromium rewrites as one line. BROWSER_PROCESS = """import glob, json, os, subprocess -pid = int(subprocess.run(["pgrep", "-o", "-f", "--", "--remote-debugging-pipe"], capture_output=True, text=True, +pid = int(subprocess.run(["pgrep", "-o", "-f", "--", "[-]-remote-debugging-pipe"], capture_output=True, text=True, check=True).stdout) listening = set() for table in ("/proc/net/tcp", "/proc/net/tcp6"): diff --git a/tst/unit/bundle/shipped_computers_test.py b/tst/unit/bundle/shipped_computers_test.py index 47860103e..7df4b7f05 100644 --- a/tst/unit/bundle/shipped_computers_test.py +++ b/tst/unit/bundle/shipped_computers_test.py @@ -48,9 +48,9 @@ def test_a_task_naming_ubuntu_24_04_writes_the_shipped_base_over_its_folders_ima assert built[1] == ["Dockerfile", "artifact.toml", *(f"clock/{name}" for name in ( "COPYING", "README.md", "agentenv_clock_page.c", "agentenv_clock_waits.c", "build.sh", "clock-follower", "clock.conf", "clockpage.py", "libfaketime.patch", "tls_realtime.c")), *(f"egress/{name}" for name in ( - "README.md", "agentenv-egress.sh", "boot.go", "boot_test.go", "clock.go", "clock_test.go", "go.mod", "go.sum", - "hello.go", "hello_test.go", "leaves.go", "main.go", "proxy.go", "proxy_test.go", "testdata/page-armed.hex", - "testdata/page-unarmed.hex", "upstream.go")), "root.conf", + "README.md", "agentenv-egress.sh", "boot.go", "boot_test.go", "chromium-policy.json", "clock.go", + "clock_test.go", "go.mod", "go.sum", "hello.go", "hello_test.go", "leaves.go", "main.go", "proxy.go", + "proxy_test.go", "testdata/page-armed.hex", "testdata/page-unarmed.hex", "upstream.go")), "root.conf", "start-desktop", "supervisord.conf", "xfwm4.xml"] assert computer.metadata["validated_images"]["container"]["arm64"]["status"] == "uninspected" diff --git a/tst/unit/env/envs/test_computer_egress_base.py b/tst/unit/env/envs/test_computer_egress_base.py index 2678fbe34..d3ead3027 100644 --- a/tst/unit/env/envs/test_computer_egress_base.py +++ b/tst/unit/env/envs/test_computer_egress_base.py @@ -7,6 +7,7 @@ import configparser import importlib.util +import json import re import shlex from datetime import UTC, datetime @@ -14,7 +15,7 @@ from urllib.parse import urlsplit import agent_env -from agent_env.env.envs.computer.utils import sites +from agent_env.env.envs.computer.utils import site_proxy, sites from agent_env.providers.sandbox_providers.local_docker.provider import LOCAL_TRUST_ENV BASE = Path(agent_env.__file__).parent / "computers" / "artifacts" / "ubuntu-24.04" @@ -87,3 +88,21 @@ def test_the_pages_the_proxy_is_tested_on_are_the_ones_the_followers_writer_writ assert golden == { "armed": page.encode(4, 1577836800_000000000, 1794000000_123456789, 2.0, 1, 7, 5_000_000_000, 5_000_000_000), "unarmed": page.encode(2, 0, 0, 1.0, 0, 3, 0, 0)} + + +def test_every_chromium_build_goes_out_through_the_proxy_by_a_policy_that_bypasses_what_the_session_does(): + policy = json.loads((EGRESS / "chromium-policy.json").read_text()) + session = _session_environment() + loop = re.search(r"for build in (.+?); do", (BASE / "Dockerfile").read_text()).group(1).split() + + assert policy["ProxySettings"] == { + "ProxyMode": "fixed_servers", "ProxyServer": session["HTTPS_PROXY"], + "ProxyBypassList": ";".join(f"[{host}]" if ":" in host else host for host in session["NO_PROXY"].split(","))} + assert policy["WebRtcIPHandling"] == "disable_non_proxied_udp" + assert {str(path.parents[2]) for path in site_proxy.Paths().chromium_policies} == set(loop) + + +def test_x_has_no_screensaver_or_dpms_whose_timeouts_run_on_real_time(): + xvfb = shlex.split(next(line for line in (BASE / "start-desktop").read_text().splitlines() if "exec Xvfb" in line)) + + assert xvfb[xvfb.index("-s") + 1] == "0" and "-dpms" in xvfb