|
56 | 56 | WorkspaceReadNotFoundError, |
57 | 57 | ) |
58 | 58 | from agents.sandbox.files import EntryKind, FileEntry |
59 | | -from agents.sandbox.manifest import Manifest |
| 59 | +from agents.sandbox.manifest import Environment, Manifest |
60 | 60 | from agents.sandbox.materialization import MaterializedFile |
61 | 61 | from agents.sandbox.sandboxes.docker import ( |
62 | 62 | DockerSandboxClient, |
@@ -2011,6 +2011,158 @@ async def test_docker_create_container_mounts_explicit_host_path( |
2011 | 2011 | ] |
2012 | 2012 |
|
2013 | 2013 |
|
| 2014 | +@pytest.mark.asyncio |
| 2015 | +@pytest.mark.parametrize("operation", ["create", "resume"]) |
| 2016 | +@pytest.mark.parametrize( |
| 2017 | + "storage_mount", |
| 2018 | + [ |
| 2019 | + AzureBlobMount( |
| 2020 | + account="account", |
| 2021 | + container="container", |
| 2022 | + mount_strategy=InContainerMountStrategy(pattern=FuseMountPattern()), |
| 2023 | + ), |
| 2024 | + S3Mount( |
| 2025 | + bucket="bucket", |
| 2026 | + mount_strategy=InContainerMountStrategy(pattern=MountpointMountPattern()), |
| 2027 | + ), |
| 2028 | + S3Mount( |
| 2029 | + bucket="bucket", mount_strategy=InContainerMountStrategy(pattern=RcloneMountPattern()) |
| 2030 | + ), |
| 2031 | + S3Mount( |
| 2032 | + bucket="bucket", |
| 2033 | + mount_strategy=InContainerMountStrategy(pattern=RcloneMountPattern(mode="nfs")), |
| 2034 | + ), |
| 2035 | + S3FilesMount( |
| 2036 | + file_system_id="fs-1234567890abcdef0", |
| 2037 | + mount_strategy=InContainerMountStrategy(pattern=S3FilesMountPattern()), |
| 2038 | + ), |
| 2039 | + ], |
| 2040 | + ids=["fuse", "mountpoint", "rclone-fuse", "rclone-nfs", "s3-files"], |
| 2041 | +) |
| 2042 | +async def test_docker_rejects_read_only_host_grant_with_privileged_storage_before_side_effects( |
| 2043 | + tmp_path: Path, |
| 2044 | + monkeypatch: pytest.MonkeyPatch, |
| 2045 | + operation: str, |
| 2046 | + storage_mount: Mount, |
| 2047 | +) -> None: |
| 2048 | + container = _StartedContainer() |
| 2049 | + docker_client = _FakeCreateDockerClient(container) |
| 2050 | + client = DockerSandboxClient(docker_client=cast(object, docker_client)) |
| 2051 | + manifest = Manifest( |
| 2052 | + entries={"nested": Dir(children={"data": storage_mount})}, |
| 2053 | + extra_path_grants=( |
| 2054 | + SandboxPathGrant(path="/mnt/shared-data", host_path=str(tmp_path), read_only=True), |
| 2055 | + ), |
| 2056 | + ) |
| 2057 | + if isinstance(storage_mount.mount_strategy.pattern, MountpointMountPattern): |
| 2058 | + manifest = manifest.with_in_container_mount_credential_exposure_acknowledged("nested/data") |
| 2059 | + else: |
| 2060 | + manifest = manifest.with_in_container_mount_broad_credential_exposure_acknowledged( |
| 2061 | + "nested/data" |
| 2062 | + ) |
| 2063 | + state = DockerSandboxSessionState( |
| 2064 | + manifest=manifest, |
| 2065 | + snapshot=NoopSnapshot(id="snapshot"), |
| 2066 | + image=DEFAULT_PYTHON_SANDBOX_IMAGE, |
| 2067 | + container_id="existing-container", |
| 2068 | + workspace_root_ready=True, |
| 2069 | + ) |
| 2070 | + original_session_id = state.session_id |
| 2071 | + |
| 2072 | + def unexpected_provider_access(*args: object, **kwargs: object) -> None: |
| 2073 | + pytest.fail("incompatible grants must fail before Docker access or cleanup") |
| 2074 | + |
| 2075 | + async def unexpected_environment_resolution(self: Environment) -> dict[str, str]: |
| 2076 | + pytest.fail("incompatible grants must fail before environment resolution") |
| 2077 | + |
| 2078 | + monkeypatch.setattr(client, "image_exists", unexpected_provider_access) |
| 2079 | + monkeypatch.setattr(client, "get_container", unexpected_provider_access) |
| 2080 | + monkeypatch.setattr(client, "_cleanup_failed_create_resources", unexpected_provider_access) |
| 2081 | + monkeypatch.setattr(Environment, "resolve", unexpected_environment_resolution) |
| 2082 | + |
| 2083 | + with pytest.raises(MountConfigError, match="read-only host_path grants.*SYS_ADMIN"): |
| 2084 | + if operation == "create": |
| 2085 | + await client.create( |
| 2086 | + manifest=manifest, |
| 2087 | + options=DockerSandboxClientOptions(image=DEFAULT_PYTHON_SANDBOX_IMAGE), |
| 2088 | + ) |
| 2089 | + else: |
| 2090 | + await client.resume(state) |
| 2091 | + |
| 2092 | + assert docker_client.containers.calls == [] |
| 2093 | + assert container.start_calls == 0 |
| 2094 | + assert state.session_id == original_session_id |
| 2095 | + assert state.container_id == "existing-container" |
| 2096 | + assert state.workspace_root_ready is True |
| 2097 | + |
| 2098 | + |
| 2099 | +@pytest.mark.asyncio |
| 2100 | +@pytest.mark.parametrize( |
| 2101 | + ("storage", "explicit_host_path", "read_only"), |
| 2102 | + [ |
| 2103 | + ("none", True, True), |
| 2104 | + ("in-container", True, False), |
| 2105 | + ("in-container", False, True), |
| 2106 | + ("docker-volume", True, True), |
| 2107 | + ], |
| 2108 | + ids=["ordinary-read-only-bind", "writable-bind", "path-only-grant", "external-storage"], |
| 2109 | +) |
| 2110 | +async def test_docker_create_preserves_compatible_host_grants_and_storage( |
| 2111 | + tmp_path: Path, |
| 2112 | + monkeypatch: pytest.MonkeyPatch, |
| 2113 | + storage: str, |
| 2114 | + explicit_host_path: bool, |
| 2115 | + read_only: bool, |
| 2116 | +) -> None: |
| 2117 | + container = _StartedContainer() |
| 2118 | + docker_client = _FakeCreateDockerClient(container) |
| 2119 | + client = DockerSandboxClient(docker_client=cast(object, docker_client)) |
| 2120 | + manifest = Manifest( |
| 2121 | + extra_path_grants=( |
| 2122 | + SandboxPathGrant( |
| 2123 | + path="/mnt/shared-data", |
| 2124 | + host_path=str(tmp_path) if explicit_host_path else None, |
| 2125 | + read_only=read_only, |
| 2126 | + ), |
| 2127 | + ), |
| 2128 | + ) |
| 2129 | + if storage != "none": |
| 2130 | + manifest.entries["data"] = S3Mount( |
| 2131 | + bucket="bucket", |
| 2132 | + mount_strategy=( |
| 2133 | + InContainerMountStrategy(pattern=RcloneMountPattern()) |
| 2134 | + if storage == "in-container" |
| 2135 | + else DockerVolumeMountStrategy(driver="rclone") |
| 2136 | + ), |
| 2137 | + ) |
| 2138 | + monkeypatch.setattr(client, "image_exists", lambda _image: True) |
| 2139 | + |
| 2140 | + session = await client.create( |
| 2141 | + manifest=manifest, |
| 2142 | + options=DockerSandboxClientOptions(image=DEFAULT_PYTHON_SANDBOX_IMAGE), |
| 2143 | + ) |
| 2144 | + |
| 2145 | + assert isinstance(session._inner, DockerSandboxSession) |
| 2146 | + assert container.start_calls == 1 |
| 2147 | + kwargs = docker_client.containers.calls[0] |
| 2148 | + mounts = cast(list[dict[str, object]], kwargs.get("mounts", [])) |
| 2149 | + binds = [mount for mount in mounts if mount["Type"] == "bind"] |
| 2150 | + assert binds == ( |
| 2151 | + [ |
| 2152 | + { |
| 2153 | + "Target": "/mnt/shared-data", |
| 2154 | + "Source": str(tmp_path), |
| 2155 | + "Type": "bind", |
| 2156 | + "ReadOnly": read_only, |
| 2157 | + } |
| 2158 | + ] |
| 2159 | + if explicit_host_path |
| 2160 | + else [] |
| 2161 | + ) |
| 2162 | + assert kwargs.get("cap_add") == (["SYS_ADMIN"] if storage == "in-container" else None) |
| 2163 | + assert any(mount["Type"] == "volume" for mount in mounts) == (storage == "docker-volume") |
| 2164 | + |
| 2165 | + |
2014 | 2166 | @pytest.mark.asyncio |
2015 | 2167 | async def test_docker_create_container_keeps_path_only_grant_unmounted( |
2016 | 2168 | monkeypatch: pytest.MonkeyPatch, |
|
0 commit comments