Skip to content

Commit e18f72e

Browse files
committed
fix: preserve OpenSandbox connection compatibility
Signed-off-by: Hemil Desai <hemild@nvidia.com>
1 parent 6bf85fe commit e18f72e

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

nemo_gym/sandbox/providers/opensandbox/provider.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ def _connection_config(
642642
kwargs["request_timeout"] = timedelta(seconds=request_timeout_s)
643643
if self._connection.use_server_proxy:
644644
kwargs["use_server_proxy"] = True
645+
if self._connection.api_key is not None:
646+
kwargs["headers"] = {"OPEN-SANDBOX-API-KEY": self._connection.api_key}
645647
if self._connection.keepalive_expiry_s is not None or self._connection.disable_connection_pooling:
646648
kwargs["transport"] = self._get_transport()
647649
config = ConnectionConfig(**kwargs)
@@ -1114,12 +1116,12 @@ async def endpoint(self, handle: SandboxHandle, port: int) -> SandboxEndpoint:
11141116
# defaults match the connection that produced this endpoint.
11151117
scheme = connection.get_base_url().split("://", 1)[0]
11161118
endpoint_url = f"{scheme}://{endpoint_url.lstrip('/')}"
1117-
headers = dict(getattr(resolved, "headers", None) or {})
1118-
api_key = connection.headers.get("OPEN-SANDBOX-API-KEY")
1119-
if connection.use_server_proxy and api_key:
1120-
# Direct endpoints terminate at untrusted sandbox code; only proxy
1121-
# endpoints may receive server credentials.
1122-
headers.setdefault("OPEN-SANDBOX-API-KEY", api_key)
1119+
headers = dict(getattr(connection, "headers", None) or {})
1120+
headers.update(getattr(resolved, "headers", None) or {})
1121+
if not getattr(connection, "use_server_proxy", self._connection.use_server_proxy):
1122+
# Direct endpoints terminate at untrusted sandbox code and must
1123+
# never receive the management credential.
1124+
headers.pop("OPEN-SANDBOX-API-KEY", None)
11231125
return SandboxEndpoint(endpoint=endpoint_url, headers=headers)
11241126

11251127
def _command_retry_count(self) -> int:

tests/unit_tests/test_opensandbox_endpoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_schemeless_endpoint_uses_the_sdk_resolved_url_and_key() -> None:
116116
def test_direct_mode_endpoint_does_not_inject_the_key() -> None:
117117
raw = _RawWithEndpoint(
118118
endpoint="http://pod.example:6000",
119-
headers={},
119+
headers={"OPEN-SANDBOX-API-KEY": "secret"}, # pragma: allowlist secret
120120
connection=_Connection(api_key="secret"), # pragma: allowlist secret
121121
)
122122
resolved = asyncio.run(_provider().endpoint(_handle(raw), 6000))

tests/unit_tests/test_opensandbox_provider.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ def test_connection_config_and_image_policy(
365365
"protocol": "https",
366366
"request_timeout": timedelta(seconds=10),
367367
"use_server_proxy": True,
368+
"headers": {"OPEN-SANDBOX-API-KEY": "key"}, # pragma: allowlist secret
368369
}
369370
assert config.headers == {"OPEN-SANDBOX-API-KEY": "key"} # pragma: allowlist secret
370371
short_timeout_config = provider._connection_config(request_timeout_s=3)

0 commit comments

Comments
 (0)