Files
OpenShell/e2e/python/test_sandbox_providers.py
Drew Newberry c1f2e7189f feat(isolation): implement the RFC 0012 sandbox architecture (#2942)
* feat(isolation): add RFC 0012 backend contract

Signed-off-by: Drew Newberry <385+drew@users.noreply.github.com>

* refactor(isolation): name the interface crate explicitly

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(isolation): expose trusted host gateway

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* docs(agents): inventory the MXC driver

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(isolation): add mediated DNS transport

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(isolation): tighten interface error and digest contracts

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* docs(isolation): remove unrelated driver inventory

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(isolation): define capability-free launch contract

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(isolation): seal confirmed boundary state

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(isolation): validate confirmation for external backend implementations

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(isolation): clarify mediated DNS identity

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(isolation): generalize loopback connector

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(isolation): unify typed network mediation

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(isolation): bind launches to sandbox sessions

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(mxc): initialize extended sandbox status

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(isolation): add boundary protocol and Linux primitives

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(isolation): harden signals and separate process status from transport

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(isolation): validate remote confirmation through public contract

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(isolation): validate wire state and propagate snapshot failures

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* test(isolation): import owned agent specification explicitly

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* docs(isolation): describe mediated DNS channel

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(isolation): bound mediation attach without nested retries

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(isolation): generalize loopback protocol

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(isolation): add transport-neutral session authentication

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(isolation): separate sandbox backend protocol

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(isolation): harden runtime boundary controls

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(isolation): add terminal boundary operation

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(isolation): split supervisor and sandbox runtimes

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(sandbox): harden boundary isolation and lifecycle ownership

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(sandbox): reject private root redirects and adopt typed errors

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(sandbox): preserve accept thread ownership on musl

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* test(sandbox): isolate credential probes from filtered threads

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(sandbox): return retained exec exit status to independent waiters

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(sandbox): bound network mediation and preserve socket authorization

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(sandbox): bound control admission and retire stale mediation

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* ci(e2e): select migrated drivers per stack layer

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(sandbox): implement loopback connector

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(isolation): authenticate the Sandbox Protocol

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(supervisor): rotate launch-scoped authentication

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(sandbox): consume dedicated backend crate

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* test(sandbox): align topology session fixture

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(sandbox): align projected bootstrap bundle

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(auth): validate refreshed credentials before rotation

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(sandbox): fail closed across supervisor disconnects

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(isolation): repair rebased sandbox CI

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* build(runtime): publish separate sandbox and supervisor images

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(config): configure the sandbox runtime image

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(ci): validate sandbox binary linkage

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(isolation): use backend and runtime terminology

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(sandbox): use a scratch runtime image

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(ci): refresh schema and dependency policy

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(sandbox): bind reconnects to supervisor process

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* docs: align runtime split operational guidance

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* chore(security): document Kubernetes runtime RBAC

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(isolation): enforce runtime lifecycle invariants

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(compute): identify sandbox start generations

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(server): restore sandbox launch sessions

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(isolation): support authenticated runtime replacement

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(auth): bind sandbox session successors

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(auth): retry pending sandbox successors

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(vm): run the supervisor outside the guest workload

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(vm): use sandbox backend protocol

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): repair rebase integration

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): use unified build toolchain

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(vm): use sandbox runtime terminology

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(vm): own guest network bootstrap

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): expose guest init version

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): select native supervisor artifacts

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): guard guest init Linux symbols

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): scope Linux test imports

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): avoid guest interface casts

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): reconcile admitted sandbox identity

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): share resolved sandbox identity

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): surface host supervisor failures

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): include guest logs on supervisor exit

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): rotate and clean runtime generations

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): make sandbox starts generation-aware

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): rotate restored sandbox sessions

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(vm): keep shared paths in the base layer

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): bind sandbox session lineage

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(docker): isolate workloads behind the host supervisor

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(docker): rotate launch-scoped authentication

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(docker): use sandbox backend protocol

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(docker): use host networking for supervisor

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(docker): preserve host gateway alias resolution

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(docker): use separate sandbox and supervisor images

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(docker): restore startup validation after rebase

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(docker): name the sandbox runtime directly

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(docker): narrow supervisor CA runtime storage

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(docker): close companion isolation gaps

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* test(docker): align mediated network expectations

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* test(docker): exercise mediated network paths

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(docker): attach supervisor to managed network

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(docker): defer supervisor recovery until gateway is ready

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(docker): make sandbox starts generation-aware

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(docker): rotate restored sandbox sessions

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(docker): preserve workloads during session rotation

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(docker): remove unrelated configuration RFC changes

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(docker): bind sandbox session lineage

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(kubernetes): add proxy-pod isolation topology

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(kubernetes): use sandbox backend protocol

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(kubernetes): use stable sandbox service authority

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(kubernetes): split sandbox and supervisor images

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(kubernetes): adapt proxy pods to current runtime APIs

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(kubernetes): describe the single runtime placement

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(kubernetes): simplify sandbox orchestration

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(kubernetes): validate deployment prerequisites

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(kubernetes): update Trivy Helm profile inventory

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* test(kubernetes): update Trivy scan inventory count

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(kubernetes): reuse preloaded runtime images in e2e

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(kubernetes): type and clean runtime resources

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(kubernetes): make sandbox restarts recoverable

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(kubernetes): rotate restored sandbox sessions

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(kubernetes): preserve supervisor egress

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(kubernetes): bind sandbox session lineage

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(podman): adopt isolated sandbox and supervisor containers

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(podman): stage bootstrap archives at named volume destinations

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(podman): rotate launch-scoped authentication

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(podman): use sandbox backend protocol

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(podman): use host networking for supervisor

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(podman): split sandbox and supervisor images

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(podman): repair rebase integration

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(podman): name the sandbox runtime directly

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(podman): provision supervisor CA runtime storage

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(podman): address isolation review findings

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(podman): inspect Debian supervisor provenance

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(podman): use libpod-compatible tmpfs options

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(podman): bind verified sandbox runtime binary

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(podman): provide external driver data directory

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(podman): start sandbox before joining user namespace

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(podman): separate supervisor user namespace

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(podman): make sandbox starts generation-aware

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(podman): rotate restored sandbox sessions

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(podman): bind sandbox session lineage

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* perf(isolation): add TCP and DNS benchmark harnesses

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(perf): align benchmark timing and supported protocols

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(perf): report TCP benchmark metrics accurately

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(perf): cancel failed worker startup

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(docker): build matching local supervisor image

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(podman): make local sandbox smoke test runnable

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(kubernetes): wire local sandbox runtime image

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(kubernetes): narrow sandbox service RBAC

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(ci): validate split runtime artifacts

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(isolation): harden runtime session handling

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* feat(supervisor): add standalone network proxy role

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* docs(rfc): remove implementation companion notes

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* refactor(vm): standardize runtime release name

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(vm): pin renamed runtime artifacts

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(auth): persist sandbox runtime identity

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(runtime): restore branch validation

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(isolation): reconcile main after rebase

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(network): close unframed HTTP 1.0 responses

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* chore(isolation): preserve upstream OCSF updates

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(security): close credential and TLS replay paths

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

* fix(auth): make sandbox refresh retries idempotent

Signed-off-by: Drew Newberry <anewberry@nvidia.com>

---------

Signed-off-by: Drew Newberry <385+drew@users.noreply.github.com>
Signed-off-by: Drew Newberry <anewberry@nvidia.com>
2026-09-16 00:49:14 +00:00

1178 lines
41 KiB
Python

# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""E2E tests for supervisor-managed provider placeholders in sandboxes.
Provider credentials are fetched at runtime by the sandbox supervisor via the
GetSandboxProviderEnvironment gRPC call. Sandboxed child processes should see
placeholder values (not raw secrets). Credentials must never be present in the
persisted sandbox spec environment map.
"""
from __future__ import annotations
import json
import socket
import subprocess
import sys
import textwrap
import time
from contextlib import contextmanager
from typing import TYPE_CHECKING
import grpc
import pytest
from openshell._proto import datamodel_pb2, openshell_pb2, sandbox_pb2
if TYPE_CHECKING:
from collections.abc import Callable, Iterator
from openshell import Sandbox, SandboxClient, WorkspaceClient
# ---------------------------------------------------------------------------
# Policy helpers
# ---------------------------------------------------------------------------
def _is_placeholder_for_env_key(value: str, key: str) -> bool:
"""Return true when value is an OpenShell credential placeholder for key."""
prefix = "openshell:resolve:env:"
if value == f"{prefix}{key}":
return True
token = value.removeprefix(prefix)
if token == value:
return False
return token.startswith(("v", "s")) and token.endswith(f"_{key}")
def _default_policy() -> sandbox_pb2.SandboxPolicy:
"""Build a sandbox policy with standard filesystem/process/landlock settings."""
return sandbox_pb2.SandboxPolicy(
version=1,
filesystem=sandbox_pb2.FilesystemPolicy(
include_workdir=True,
read_only=["/usr", "/lib", "/etc", "/app", "/dev/urandom"],
read_write=["/sandbox", "/tmp"],
),
landlock=sandbox_pb2.LandlockPolicy(compatibility="best_effort"),
process=sandbox_pb2.ProcessPolicy(
run_as_user="sandbox", run_as_group="sandbox"
),
)
# ---------------------------------------------------------------------------
# Provider lifecycle helper
# ---------------------------------------------------------------------------
@contextmanager
def provider(
stub: object,
*,
name: str,
provider_type: str,
credentials: dict[str, str],
profile_workspace: str = "",
) -> Iterator[str]:
"""Create a provider for the duration of the block, then delete it."""
_delete_provider(stub, name)
stub.CreateProvider(
openshell_pb2.CreateProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
provider=datamodel_pb2.Provider(
metadata=datamodel_pb2.ObjectMeta(name=name),
type=provider_type,
credentials=credentials,
profile_workspace=profile_workspace,
),
)
)
try:
yield name
finally:
_delete_provider(stub, name)
def _delete_provider(stub: object, name: str) -> None:
"""Delete a provider, ignoring not-found errors."""
try:
stub.DeleteProvider(
openshell_pb2.DeleteProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
name=name,
)
)
except grpc.RpcError as exc:
if hasattr(exc, "code") and exc.code() == grpc.StatusCode.NOT_FOUND:
pass
else:
raise
def _delete_provider_profile(stub: object, profile_id: str) -> None:
"""Delete a provider profile, ignoring not-found errors."""
try:
stub.DeleteProviderProfile(
openshell_pb2.DeleteProviderProfileRequest(
id=profile_id,
workspace="default",
)
)
except grpc.RpcError as exc:
if hasattr(exc, "code") and exc.code() == grpc.StatusCode.NOT_FOUND:
pass
else:
raise
@contextmanager
def imported_provider_profile(
stub: object,
*,
profile: openshell_pb2.ProviderProfile,
source: str,
) -> Iterator[str]:
"""Import a workspace-scoped provider profile for the duration of the block."""
_delete_provider_profile(stub, profile.id)
response = stub.ImportProviderProfiles(
openshell_pb2.ImportProviderProfilesRequest(
profiles=[
openshell_pb2.ProviderProfileImportItem(
profile=profile,
source=source,
)
],
workspace="default",
)
)
assert response.imported, f"profile import failed: {response.diagnostics!r}"
try:
yield profile.id
finally:
_delete_provider_profile(stub, profile.id)
def _native_inference_profile(
*,
profile_id: str,
env_var: str,
port: int,
rules: list[sandbox_pb2.L7Rule],
auth_style: str = "bearer",
header_name: str = "authorization",
) -> openshell_pb2.ProviderProfile:
return openshell_pb2.ProviderProfile(
id=profile_id,
display_name=f"{profile_id} display",
description="E2E imported inference profile fixture",
category=openshell_pb2.PROVIDER_PROFILE_CATEGORY_INFERENCE,
inference_capable=True,
credentials=[
openshell_pb2.ProviderProfileCredential(
name="api_key",
description="API key",
env_vars=[env_var],
required=True,
auth_style=auth_style,
header_name=header_name,
)
],
endpoints=[
sandbox_pb2.NetworkEndpoint(
host="host.openshell.internal",
port=port,
protocol="rest",
tls="none",
enforcement="enforce",
rules=rules,
allowed_ips=[
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
"fc00::/7",
],
)
],
binaries=[
sandbox_pb2.NetworkBinary(path="/usr/bin/python*"),
sandbox_pb2.NetworkBinary(path="/usr/local/bin/python*"),
sandbox_pb2.NetworkBinary(path="/sandbox/.uv/python/**/python*"),
],
)
@contextmanager
def native_endpoint_server() -> Iterator[int]:
"""Start a small host-side HTTP fixture that echoes auth and request data."""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listener:
listener.bind(("127.0.0.1", 0))
port = listener.getsockname()[1]
script = textwrap.dedent(
"""
import json
import sys
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
PORT = int(sys.argv[1])
class Handler(BaseHTTPRequestHandler):
def _reply(self):
content_length = int(self.headers.get("Content-Length", "0"))
body = self.rfile.read(content_length) if content_length else b""
payload = {
"method": self.command,
"path": self.path,
"authorization": self.headers.get("Authorization"),
"x_api_key": self.headers.get("x-api-key"),
"body": body.decode("utf-8"),
}
encoded = json.dumps(payload).encode("utf-8")
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(encoded)))
self.end_headers()
self.wfile.write(encoded)
def do_GET(self):
self._reply()
def do_POST(self):
self._reply()
def log_message(self, fmt, *args):
pass
ThreadingHTTPServer(("0.0.0.0", PORT), Handler).serve_forever()
"""
)
proc = subprocess.Popen(
[sys.executable, "-c", script, str(port)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
deadline = time.monotonic() + 20
while time.monotonic() < deadline:
if proc.poll() is not None:
stdout, stderr = proc.communicate(timeout=5)
raise RuntimeError(
"native endpoint fixture exited early: "
f"stdout={stdout!r} stderr={stderr!r}"
)
try:
with socket.create_connection(("127.0.0.1", port), timeout=1):
break
except OSError:
time.sleep(0.2)
else:
proc.kill()
stdout, stderr = proc.communicate(timeout=5)
raise RuntimeError(
"native endpoint fixture did not become ready: "
f"stdout={stdout!r} stderr={stderr!r}"
)
try:
yield port
finally:
proc.kill()
proc.communicate(timeout=5)
def _tcp_connect_errno():
"""Return a closure that reports the errno from a normal TCP connection."""
def fn(host, port):
import socket
try:
with socket.create_connection((host, port), timeout=10):
return 0
except OSError as error:
return error.errno or -1
return fn
# ===========================================================================
# Tests: placeholder visibility
# ===========================================================================
def test_provider_credentials_available_as_env_vars(
sandbox: Callable[..., Sandbox],
sandbox_client: SandboxClient,
) -> None:
"""Sandbox child processes see provider env vars as placeholders."""
with provider(
sandbox_client._stub,
name="e2e-test-provider-env",
provider_type="claude",
credentials={"ANTHROPIC_API_KEY": "sk-e2e-test-key-12345"},
) as provider_name:
spec = datamodel_pb2.SandboxSpec(
policy=_default_policy(),
providers=[provider_name],
)
def read_env_var() -> str:
import os
return os.environ.get("ANTHROPIC_API_KEY", "NOT_SET")
with sandbox(spec=spec, delete_on_exit=True) as sb:
result = sb.exec_python(read_env_var)
assert result.exit_code == 0, result.stderr
value = result.stdout.strip()
assert _is_placeholder_for_env_key(value, "ANTHROPIC_API_KEY")
assert value != "sk-e2e-test-key-12345"
def test_profileless_provider_creation_is_rejected(
sandbox_client: SandboxClient,
) -> None:
"""New providers must reference a built-in or imported profile."""
with pytest.raises(grpc.RpcError) as exc_info:
sandbox_client._stub.CreateProvider(
openshell_pb2.CreateProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
provider=datamodel_pb2.Provider(
metadata=datamodel_pb2.ObjectMeta(
name="e2e-test-profileless-provider"
),
type="generic",
credentials={"CUSTOM_SERVICE_TOKEN": "token-generic-123"},
),
)
)
assert exc_info.value.code() == grpc.StatusCode.INVALID_ARGUMENT
assert "provider profile 'generic' was not found" in exc_info.value.details()
def test_endpointless_profile_credentials_fail_closed_without_policy_binding(
sandbox: Callable[..., Sandbox],
sandbox_client: SandboxClient,
) -> None:
"""Endpointless profile credentials are withheld without an explicit binding."""
with provider(
sandbox_client._stub,
name="e2e-test-google-cloud-without-policy-binding",
provider_type="google-cloud",
credentials={"GCP_ADC_ACCESS_TOKEN": "gcp-e2e-token"},
) as provider_name:
spec = datamodel_pb2.SandboxSpec(
policy=_default_policy(),
providers=[provider_name],
)
def read_gcp_token() -> str:
import os
return os.environ.get("GCP_ADC_ACCESS_TOKEN", "NOT_SET")
with sandbox(spec=spec, delete_on_exit=True) as sb:
result = sb.exec_python(read_gcp_token)
assert result.exit_code == 0, result.stderr
assert result.stdout.strip() == "NOT_SET"
def test_endpointless_profile_credentials_use_explicit_policy_binding(
sandbox: Callable[..., Sandbox],
sandbox_client: SandboxClient,
) -> None:
"""An endpointless profile emits credentials only with an explicit binding."""
with provider(
sandbox_client._stub,
name="e2e-test-google-cloud-policy-binding",
provider_type="google-cloud",
credentials={"GCP_ADC_ACCESS_TOKEN": "gcp-e2e-token"},
) as provider_name:
policy = _default_policy()
policy.network_policies["gcp_storage"].CopyFrom(
sandbox_pb2.NetworkPolicyRule(
name="gcp_storage",
endpoints=[
sandbox_pb2.NetworkEndpoint(
host="storage.googleapis.com",
port=443,
protocol="rest",
access="full",
credential_binding=sandbox_pb2.NetworkCredentialBinding(
provider=provider_name
),
)
],
)
)
spec = datamodel_pb2.SandboxSpec(
policy=policy,
providers=[provider_name],
)
def read_gcp_token() -> str:
import os
return os.environ.get("GCP_ADC_ACCESS_TOKEN", "NOT_SET")
with sandbox(spec=spec, delete_on_exit=True) as sb:
result = sb.exec_python(read_gcp_token)
assert result.exit_code == 0, result.stderr
assert _is_placeholder_for_env_key(
result.stdout.strip(), "GCP_ADC_ACCESS_TOKEN"
)
def test_nvidia_provider_injects_nvidia_api_key_env_var(
sandbox: Callable[..., Sandbox],
sandbox_client: SandboxClient,
) -> None:
"""NVIDIA provider projects a placeholder env value into child processes."""
with provider(
sandbox_client._stub,
name="e2e-test-nvidia-provider-env",
provider_type="nvidia",
credentials={"NVIDIA_API_KEY": "nvapi-e2e-test-key"},
) as provider_name:
spec = datamodel_pb2.SandboxSpec(
policy=_default_policy(),
providers=[provider_name],
)
def read_nvidia_key() -> str:
import os
return os.environ.get("NVIDIA_API_KEY", "NOT_SET")
with sandbox(spec=spec, delete_on_exit=True) as sb:
result = sb.exec_python(read_nvidia_key)
assert result.exit_code == 0, result.stderr
assert _is_placeholder_for_env_key(result.stdout.strip(), "NVIDIA_API_KEY")
def test_attach_detach_updates_credentials_for_later_exec_launches(
sandbox: Callable[..., Sandbox],
sandbox_client: SandboxClient,
) -> None:
"""Later exec launches see provider attach/detach credential changes."""
stub = sandbox_client._stub
provider_name = "e2e-test-attach-detach-env"
with provider(
stub,
name=provider_name,
provider_type="nvidia",
credentials={"NVIDIA_API_KEY": "token-attach-detach"},
):
spec = datamodel_pb2.SandboxSpec(policy=_default_policy(), providers=[])
def read_attach_token() -> str:
import os
return os.environ.get("NVIDIA_API_KEY", "NOT_SET")
def exec_token(sb: Sandbox) -> str:
result = sb.exec_python(read_attach_token)
assert result.exit_code == 0, result.stderr
return result.stdout.strip()
def wait_for_token(sb: Sandbox, expected: str) -> None:
deadline = time.monotonic() + 35
last = None
while time.monotonic() < deadline:
last = exec_token(sb)
if expected == "NOT_SET":
matched = last == expected
else:
matched = _is_placeholder_for_env_key(last, "NVIDIA_API_KEY")
if matched:
return
time.sleep(2)
pytest.fail(f"expected {expected!r}, last exec saw {last!r}")
with sandbox(spec=spec, delete_on_exit=True) as sb:
assert exec_token(sb) == "NOT_SET"
try:
stub.AttachSandboxProvider(
openshell_pb2.AttachSandboxProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(
workspace="default"
),
sandbox_name=sb.sandbox.name,
provider_name=provider_name,
)
)
wait_for_token(
sb,
"openshell:resolve:env:NVIDIA_API_KEY",
)
stub.DetachSandboxProvider(
openshell_pb2.DetachSandboxProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(
workspace="default"
),
sandbox_name=sb.sandbox.name,
provider_name=provider_name,
)
)
wait_for_token(sb, "NOT_SET")
finally:
try:
stub.DetachSandboxProvider(
openshell_pb2.DetachSandboxProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(
workspace="default"
),
sandbox_name=sb.sandbox.name,
provider_name=provider_name,
)
)
except grpc.RpcError as exc:
if exc.code() != grpc.StatusCode.NOT_FOUND:
raise
def test_imported_openai_profile_allows_native_endpoint_with_attached_provider(
sandbox: Callable[..., Sandbox],
sandbox_client: SandboxClient,
) -> None:
"""Imported fixture profiles should support native OpenAI-style access."""
stub = sandbox_client._stub
profile_id = f"e2e-native-openai-{int(time.time() * 1000)}"
provider_name = f"{profile_id}-provider"
secret = "sk-native-openai-secret"
profile = _native_inference_profile(
profile_id=profile_id,
env_var="OPENAI_API_KEY",
port=0,
rules=[
sandbox_pb2.L7Rule(
allow=sandbox_pb2.L7Allow(
method="POST",
path="/v1/chat/completions",
)
)
],
)
def call_native_openai(host: str, port: int) -> str:
import json
import os
import urllib.error
import urllib.request
body = json.dumps(
{
"model": "fixture-openai-model",
"messages": [{"role": "user", "content": "hello"}],
}
).encode()
request = urllib.request.Request(
f"http://{host}:{port}/v1/chat/completions",
data=body,
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ['OPENAI_API_KEY']}",
},
method="POST",
)
try:
with urllib.request.urlopen(request, timeout=30) as response:
return response.read().decode()
except urllib.error.HTTPError as exc:
raise RuntimeError(
f"native OpenAI request failed with {exc.code}: "
f"{exc.read().decode(errors='replace')}"
) from exc
with native_endpoint_server() as port:
profile.endpoints[0].port = port
with imported_provider_profile(
stub,
profile=profile,
source=f"{profile_id}.yaml",
):
with provider(
stub,
name=provider_name,
provider_type=profile_id,
credentials={"OPENAI_API_KEY": secret},
profile_workspace="default",
) as attached_provider:
spec = datamodel_pb2.SandboxSpec(
policy=_default_policy(),
providers=[attached_provider],
)
with sandbox(spec=spec, delete_on_exit=True) as sb:
result = sb.exec_python(
call_native_openai,
args=("host.openshell.internal", port),
timeout_seconds=60,
)
assert result.exit_code == 0, result.stderr
payload = json.loads(result.stdout)
body = json.loads(payload["body"])
assert payload["method"] == "POST"
assert payload["path"] == "/v1/chat/completions"
assert payload["authorization"] == f"Bearer {secret}"
assert body["model"] == "fixture-openai-model"
def test_imported_anthropic_profile_uses_native_endpoint_and_inference_local_is_not_privileged(
sandbox: Callable[..., Sandbox],
sandbox_client: SandboxClient,
) -> None:
"""Attached imported profiles should not resurrect `inference.local` routing."""
stub = sandbox_client._stub
profile_id = f"e2e-native-anthropic-{int(time.time() * 1000)}"
provider_name = f"{profile_id}-provider"
secret = "sk-native-anthropic-secret"
profile = _native_inference_profile(
profile_id=profile_id,
env_var="ANTHROPIC_API_KEY",
port=0,
rules=[
sandbox_pb2.L7Rule(
allow=sandbox_pb2.L7Allow(
method="POST",
path="/v1/messages",
)
)
],
auth_style="header",
header_name="x-api-key",
)
def call_native_anthropic(host: str, port: int) -> str:
import json
import os
import urllib.error
import urllib.request
body = json.dumps(
{
"model": "fixture-anthropic-model",
"messages": [{"role": "user", "content": "hello"}],
}
).encode()
request = urllib.request.Request(
f"http://{host}:{port}/v1/messages",
data=body,
headers={
"Content-Type": "application/json",
"x-api-key": os.environ["ANTHROPIC_API_KEY"],
"anthropic-version": "2023-06-01",
},
method="POST",
)
try:
with urllib.request.urlopen(request, timeout=30) as response:
return response.read().decode()
except urllib.error.HTTPError as exc:
raise RuntimeError(
f"native Anthropic request failed with {exc.code}: "
f"{exc.read().decode(errors='replace')}"
) from exc
with native_endpoint_server() as port:
profile.endpoints[0].port = port
with imported_provider_profile(
stub,
profile=profile,
source=f"{profile_id}.yaml",
):
with provider(
stub,
name=provider_name,
provider_type=profile_id,
credentials={"ANTHROPIC_API_KEY": secret},
profile_workspace="default",
) as attached_provider:
spec = datamodel_pb2.SandboxSpec(
policy=_default_policy(),
providers=[attached_provider],
)
with sandbox(spec=spec, delete_on_exit=True) as sb:
native_result = sb.exec_python(
call_native_anthropic,
args=("host.openshell.internal", port),
timeout_seconds=60,
)
assert native_result.exit_code == 0, native_result.stderr
payload = json.loads(native_result.stdout)
body = json.loads(payload["body"])
assert payload["method"] == "POST"
assert payload["path"] == "/v1/messages"
assert payload["x_api_key"] == secret
assert body["model"] == "fixture-anthropic-model"
denied = sb.exec_python(
_tcp_connect_errno(),
args=("inference.local", 443),
timeout_seconds=30,
)
assert denied.exit_code == 0, denied.stderr
assert int(denied.stdout.strip()) != 0
# ===========================================================================
# Tests: security & edge cases
# ===========================================================================
def test_create_sandbox_rejects_unknown_provider(
sandbox_client: SandboxClient,
) -> None:
"""CreateSandbox fails fast when a provider name does not exist."""
spec = datamodel_pb2.SandboxSpec(
policy=_default_policy(),
providers=["nonexistent-provider-xyz"],
)
with pytest.raises(grpc.RpcError) as exc_info:
sandbox_client.create(workspace="default", spec=spec)
assert exc_info.value.code() == grpc.StatusCode.FAILED_PRECONDITION
assert "nonexistent-provider-xyz" in (exc_info.value.details() or "")
def test_credentials_not_in_persisted_spec_environment(
sandbox: Callable[..., Sandbox],
sandbox_client: SandboxClient,
) -> None:
"""Provider credentials should NOT appear in the sandbox spec's environment map."""
with provider(
sandbox_client._stub,
name="e2e-test-no-persist",
provider_type="claude",
credentials={"ANTHROPIC_API_KEY": "sk-should-not-persist"},
) as provider_name:
spec = datamodel_pb2.SandboxSpec(
policy=_default_policy(),
providers=[provider_name],
)
with sandbox(spec=spec, delete_on_exit=True) as sb:
fetched = sandbox_client._stub.GetSandbox(
openshell_pb2.GetSandboxRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(
workspace="default"
),
name=sb.sandbox.name,
)
)
persisted_env = dict(fetched.sandbox.spec.environment)
assert "ANTHROPIC_API_KEY" not in persisted_env, (
"credentials should not be persisted in sandbox spec environment"
)
# ===========================================================================
# Tests: provider update merge semantics
# ===========================================================================
def test_update_provider_preserves_unset_credentials_and_config(
sandbox_client: SandboxClient,
) -> None:
"""Updating one credential must not clobber other credentials or config."""
stub = sandbox_client._stub
name = "merge-test-preserve"
_delete_provider(stub, name)
try:
stub.CreateProvider(
openshell_pb2.CreateProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
provider=datamodel_pb2.Provider(
metadata=datamodel_pb2.ObjectMeta(name=name),
type="codex",
credentials={
"CODEX_AUTH_ACCESS_TOKEN": "val-a",
"CODEX_AUTH_REFRESH_TOKEN": "val-b",
"CODEX_AUTH_ACCOUNT_ID": "account-id",
},
config={"BASE_URL": "https://example.com"},
),
)
)
stub.UpdateProvider(
openshell_pb2.UpdateProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
provider=datamodel_pb2.Provider(
metadata=datamodel_pb2.ObjectMeta(name=name),
type="",
credentials={"CODEX_AUTH_ACCESS_TOKEN": "rotated-a"},
),
)
)
got = stub.GetProvider(
openshell_pb2.GetProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
name=name,
)
)
p = got.provider
# Credential keys are preserved but values are redacted.
assert len(p.credentials) > 0, "credential keys should be preserved"
for key, val in p.credentials.items():
assert val == "REDACTED", (
f"credential '{key}' should be REDACTED, got '{val}'"
)
assert p.config["BASE_URL"] == "https://example.com", (
"config should be preserved"
)
finally:
_delete_provider(stub, name)
def test_update_provider_empty_maps_preserves_all(
sandbox_client: SandboxClient,
) -> None:
"""Sending empty credential and config maps should be a no-op."""
stub = sandbox_client._stub
name = "merge-test-noop"
_delete_provider(stub, name)
try:
stub.CreateProvider(
openshell_pb2.CreateProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
provider=datamodel_pb2.Provider(
metadata=datamodel_pb2.ObjectMeta(name=name),
type="openai",
credentials={"OPENAI_API_KEY": "secret"},
config={"URL": "https://api.example.com"},
),
)
)
stub.UpdateProvider(
openshell_pb2.UpdateProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
provider=datamodel_pb2.Provider(
metadata=datamodel_pb2.ObjectMeta(name=name),
type="",
),
)
)
got = stub.GetProvider(
openshell_pb2.GetProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
name=name,
)
)
p = got.provider
# Credential keys are preserved but values are redacted.
assert len(p.credentials) > 0, "credential keys should be preserved"
for key, val in p.credentials.items():
assert val == "REDACTED", (
f"credential '{key}' should be REDACTED, got '{val}'"
)
assert p.config["URL"] == "https://api.example.com"
finally:
_delete_provider(stub, name)
def test_update_provider_merges_config_preserves_credentials(
sandbox_client: SandboxClient,
) -> None:
"""Updating only config should not touch credentials."""
stub = sandbox_client._stub
name = "merge-test-config-only"
_delete_provider(stub, name)
try:
stub.CreateProvider(
openshell_pb2.CreateProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
provider=datamodel_pb2.Provider(
metadata=datamodel_pb2.ObjectMeta(name=name),
type="openai",
credentials={"OPENAI_API_KEY": "original-key"},
config={"ENDPOINT": "https://old.example.com"},
),
)
)
stub.UpdateProvider(
openshell_pb2.UpdateProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
provider=datamodel_pb2.Provider(
metadata=datamodel_pb2.ObjectMeta(name=name),
type="",
config={"ENDPOINT": "https://new.example.com"},
),
)
)
got = stub.GetProvider(
openshell_pb2.GetProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
name=name,
)
)
p = got.provider
# Credential keys are preserved but values are redacted.
assert len(p.credentials) > 0, "credential keys should be preserved"
for key, val in p.credentials.items():
assert val == "REDACTED", (
f"credential '{key}' should be REDACTED, got '{val}'"
)
assert p.config["ENDPOINT"] == "https://new.example.com"
finally:
_delete_provider(stub, name)
def test_update_provider_rejects_type_change(
sandbox_client: SandboxClient,
) -> None:
"""Attempting to change a provider's type must be rejected."""
stub = sandbox_client._stub
name = "merge-test-type-reject"
_delete_provider(stub, name)
try:
stub.CreateProvider(
openshell_pb2.CreateProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"),
provider=datamodel_pb2.Provider(
metadata=datamodel_pb2.ObjectMeta(name=name),
type="openai",
credentials={"OPENAI_API_KEY": "val"},
),
)
)
with pytest.raises(grpc.RpcError) as exc_info:
stub.UpdateProvider(
openshell_pb2.UpdateProviderRequest(
workspace_scope=datamodel_pb2.WorkspaceSelector(
workspace="default"
),
provider=datamodel_pb2.Provider(
metadata=datamodel_pb2.ObjectMeta(name=name),
type="nvidia",
),
)
)
assert exc_info.value.code() == grpc.StatusCode.INVALID_ARGUMENT
assert "type cannot be changed" in exc_info.value.details()
finally:
_delete_provider(stub, name)
# ===========================================================================
# Tests: git transport network policy
# ===========================================================================
def test_github_provider_allows_https_git_clone(
sandbox: Callable[..., Sandbox],
sandbox_client: SandboxClient,
) -> None:
"""Built-in github provider permits anonymous HTTPS clone/fetch (#1769).
Git smart HTTP clone/fetch issues a POST to ``*/git-upload-pack``. The
read-only preset (GET/HEAD/OPTIONS) denied that POST, so ``git clone`` over
HTTPS failed. Attaching the github provider composes its network policy onto
the sandbox, exercising provider attachment, effective-policy composition,
TLS interception, and real git behavior end to end. git delegates HTTPS to a
``git-remote-https`` helper whose ancestor is ``/usr/bin/git``, so the
profile's git binary covers it via ancestor matching.
"""
with provider(
sandbox_client._stub,
name="e2e-test-github-clone",
provider_type="github",
# A required credential value is needed to create the provider, but an
# anonymous clone of a public repo never uses it: git only sends the
# token when a credential helper is configured.
credentials={"GITHUB_TOKEN": "e2e-placeholder-unused"},
) as provider_name:
# git opens /dev/null O_RDWR, so it must be read-write; the shared
# _default_policy only grants /dev/urandom. Everything else (binaries,
# CA bundle, clone target) is covered by the standard allowlist.
policy = _default_policy()
policy.filesystem.read_write.append("/dev/null")
spec = datamodel_pb2.SandboxSpec(
policy=policy,
providers=[provider_name],
)
with sandbox(spec=spec, delete_on_exit=True) as sb:
clone = sb.exec(
[
"git",
"clone",
"--depth",
"1",
"https://github.com/octocat/Hello-World.git",
"/tmp/hello-world",
],
timeout_seconds=120,
)
assert clone.exit_code == 0, (
"git clone over HTTPS should succeed with the github provider "
f"attached; stdout={clone.stdout!r} stderr={clone.stderr!r}"
)
# A completed clone materializes .git/HEAD, proving ref discovery
# (GET) and upload-pack (POST) both succeeded, not just a handshake.
head = sb.exec(["cat", "/tmp/hello-world/.git/HEAD"])
assert head.exit_code == 0, (
f"cloned repo is missing .git/HEAD; stderr={head.stderr!r}"
)
# ===========================================================================
# Tests: provider profile platform vs workspace scope isolation
# ===========================================================================
def test_provider_profile_platform_vs_workspace_isolation(
sandbox_client: "SandboxClient",
) -> None:
"""Platform-scoped profiles are visible in workspace listings; workspace profiles are not visible in platform listings."""
stub = sandbox_client._stub
platform_id = "e2e-platform-profile"
workspace_id = "e2e-workspace-profile"
def _make_profile(profile_id: str) -> openshell_pb2.ProviderProfileImportItem:
return openshell_pb2.ProviderProfileImportItem(
profile=openshell_pb2.ProviderProfile(
id=profile_id,
display_name=f"{profile_id} display",
category=openshell_pb2.PROVIDER_PROFILE_CATEGORY_OTHER,
),
source=f"{profile_id}.yaml",
)
def _cleanup() -> None:
for pid, ws in [(platform_id, ""), (workspace_id, "default")]:
try:
stub.DeleteProviderProfile(
openshell_pb2.DeleteProviderProfileRequest(id=pid, workspace=ws)
)
except grpc.RpcError:
pass
_cleanup()
try:
resp = stub.ImportProviderProfiles(
openshell_pb2.ImportProviderProfilesRequest(
profiles=[_make_profile(platform_id)],
workspace="",
)
)
assert resp.imported, "platform-scoped import should succeed"
resp = stub.ImportProviderProfiles(
openshell_pb2.ImportProviderProfilesRequest(
profiles=[_make_profile(workspace_id)],
workspace="default",
)
)
assert resp.imported, "workspace-scoped import should succeed"
platform_list = stub.ListProviderProfiles(
openshell_pb2.ListProviderProfilesRequest(page_size=200, workspace="")
)
platform_ids = [p.id for p in platform_list.profiles]
assert platform_id in platform_ids, (
"platform profile should appear in platform list"
)
assert workspace_id not in platform_ids, (
"workspace profile should NOT appear in platform list"
)
workspace_list = stub.ListProviderProfiles(
openshell_pb2.ListProviderProfilesRequest(page_size=200, workspace="default")
)
workspace_ids = [p.id for p in workspace_list.profiles]
assert workspace_id in workspace_ids, (
"workspace profile should appear in workspace list"
)
assert platform_id in workspace_ids, (
"platform profile should appear in workspace list (visible as fallback)"
)
finally:
_cleanup()
def test_cross_workspace_profile_ids_do_not_collide(
sandbox_client: "SandboxClient",
workspace_client: "WorkspaceClient",
) -> None:
"""Same profile ID in two workspaces must not cause a catalog collision."""
import contextlib
import uuid
stub = sandbox_client._stub
profile_id = f"e2e-xws-{uuid.uuid4().hex[:8]}"
ws_a = f"ws-a-{uuid.uuid4().hex[:8]}"
ws_b = f"ws-b-{uuid.uuid4().hex[:8]}"
def _make_profile() -> openshell_pb2.ProviderProfileImportItem:
return openshell_pb2.ProviderProfileImportItem(
profile=openshell_pb2.ProviderProfile(
id=profile_id,
display_name=f"{profile_id} display",
category=openshell_pb2.PROVIDER_PROFILE_CATEGORY_OTHER,
),
source=f"{profile_id}.yaml",
)
workspace_client.create(ws_a)
workspace_client.create(ws_b)
try:
resp_a = stub.ImportProviderProfiles(
openshell_pb2.ImportProviderProfilesRequest(
profiles=[_make_profile()],
workspace=ws_a,
)
)
assert resp_a.imported, "import into ws-a should succeed"
resp_b = stub.ImportProviderProfiles(
openshell_pb2.ImportProviderProfilesRequest(
profiles=[_make_profile()],
workspace=ws_b,
)
)
assert resp_b.imported, "import into ws-b should succeed"
list_a = stub.ListProviderProfiles(
openshell_pb2.ListProviderProfilesRequest(page_size=200, workspace=ws_a)
)
assert any(p.id == profile_id for p in list_a.profiles), (
"profile should appear in ws-a"
)
list_b = stub.ListProviderProfiles(
openshell_pb2.ListProviderProfilesRequest(page_size=200, workspace=ws_b)
)
assert any(p.id == profile_id for p in list_b.profiles), (
"profile should appear in ws-b"
)
finally:
for ws in [ws_a, ws_b]:
with contextlib.suppress(Exception):
stub.DeleteProviderProfile(
openshell_pb2.DeleteProviderProfileRequest(
id=profile_id, workspace=ws
)
)
with contextlib.suppress(Exception):
workspace_client.delete(ws)