diff --git a/.github/workflows/e2e-docker-test.yml b/.github/workflows/e2e-docker-test.yml index e0f6d5b7eb..39fc71b142 100644 --- a/.github/workflows/e2e-docker-test.yml +++ b/.github/workflows/e2e-docker-test.yml @@ -29,7 +29,7 @@ on: required: false type: string default: >- - [{"suite":"python","cmd":"mise run --no-deps --skip-deps e2e:python","apt_packages":"","python_proto":true,"mcp":false},{"suite":"oidc-python","cmd":"mise run --no-deps --skip-deps e2e:oidc-python:docker","apt_packages":"","python_proto":true,"mcp":false},{"suite":"oidc-pkce-docker","cmd":"mise run --no-deps --skip-deps e2e:oidc-pkce:docker","apt_packages":"openssh-client","python_proto":false,"mcp":false},{"suite":"rust-docker","cmd":"mise run --no-deps --skip-deps e2e:rust","apt_packages":"openssh-client","python_proto":false,"mcp":false},{"suite":"rust-docker-push","cmd":"mise run --no-deps --skip-deps e2e:rust:push","apt_packages":"openssh-client","python_proto":false,"mcp":false},{"suite":"oci-image","cmd":"mise run --no-deps --skip-deps e2e:oci-image","apt_packages":"openssh-client","python_proto":false,"mcp":false},{"suite":"additional-ca","cmd":"OPENSHELL_E2E_DOCKER_TEST=additional_ca mise run --no-deps --skip-deps e2e:docker","apt_packages":"openssh-client","python_proto":false,"mcp":false},{"suite":"middleware-http-hooks","cmd":"mise run --no-deps --skip-deps e2e:middleware-http-hooks","apt_packages":"","python_proto":false,"mcp":false},{"suite":"mcp","cmd":"mise run --no-deps --skip-deps e2e:mcp","apt_packages":"","python_proto":false,"mcp":true}] + [{"suite":"oidc-pkce-docker","cmd":"mise run --no-deps --skip-deps e2e:oidc-pkce:docker","apt_packages":"openssh-client","mcp":false},{"suite":"rust-docker","cmd":"mise run --no-deps --skip-deps e2e:rust","apt_packages":"openssh-client","mcp":false},{"suite":"gateway-mtls","cmd":"mise run --no-deps --skip-deps e2e:gateway-mtls","apt_packages":"","mcp":false},{"suite":"rust-docker-push","cmd":"mise run --no-deps --skip-deps e2e:rust:push","apt_packages":"openssh-client","mcp":false},{"suite":"oci-image","cmd":"mise run --no-deps --skip-deps e2e:oci-image","apt_packages":"openssh-client","mcp":false},{"suite":"additional-ca","cmd":"OPENSHELL_E2E_DOCKER_TEST=additional_ca mise run --no-deps --skip-deps e2e:docker","apt_packages":"openssh-client","mcp":false},{"suite":"middleware-http-hooks","cmd":"mise run --no-deps --skip-deps e2e:middleware-http-hooks","apt_packages":"","mcp":false},{"suite":"mcp","cmd":"mise run --no-deps --skip-deps e2e:mcp","apt_packages":"","mcp":true}] permissions: actions: read @@ -97,10 +97,6 @@ jobs: - name: Log in to GHCR run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${GITHUB_ACTOR}" --password-stdin - - name: Install Python dependencies and generate protobuf stubs - if: matrix.python_proto - run: uv sync --frozen && mise run --no-deps python:proto - - name: Run tests env: OPENSHELL_MCP_CONFORMANCE_CLIENT_IMAGE: ${{ format('openshell-mcp-conformance-client:{0}', inputs.image-tag) }} diff --git a/.github/workflows/e2e-suites.yml b/.github/workflows/e2e-suites.yml index 6496d910af..a7fc3dc981 100644 --- a/.github/workflows/e2e-suites.yml +++ b/.github/workflows/e2e-suites.yml @@ -91,6 +91,7 @@ jobs: {"environment":"fedora-podman-rootful-cdi","installer":"rpm","family":"features","package":"cdi","gateway":"openshell","runner":"ubuntu-24.04"}, {"environment":"fedora-podman-rootless-cdi","installer":"rpm","family":"features","package":"cdi","gateway":"openshell","runner":"ubuntu-24.04"}, {"environment":"ubuntu-docker-rootful-keycloak","installer":"deb","family":"features","package":"keycloak-provider-refresh","gateway":"openshell","runner":"ubuntu-24.04"}, + {"environment":"ubuntu-docker-rootful","installer":"deb","family":"features","package":"gateway-mtls","gateway":"openshell","runner":"ubuntu-24.04"}, {"environment":"ubuntu-docker-rootful","installer":"deb","family":"features","package":"oci-image","gateway":"openshell","runner":"ubuntu-24.04"}, {"environment":"fedora-podman-rootful","installer":"rpm","family":"features","package":"oci-image","gateway":"openshell","runner":"ubuntu-24.04"}, {"environment":"fedora-podman-rootless","installer":"rpm","family":"features","package":"oci-image","gateway":"openshell","runner":"ubuntu-24.04"}, diff --git a/TESTING.md b/TESTING.md index 084535630a..91868384a0 100644 --- a/TESTING.md +++ b/TESTING.md @@ -14,7 +14,7 @@ mise run ci # Everything: lint, compile checks, and tests crates/*/src/ # Inline #[cfg(test)] modules crates/*/tests/ # Rust integration tests python/openshell/ # Python unit tests (*_test.py suffix) -e2e/python/ # Python E2E tests (test_*.py prefix) +e2e/workload/ # Workload image the E2E sandboxes run python3, curl, and git from e2e/rust/ # Rust CLI E2E tests tests/testkit/ # Support library for SDK-driven E2E suites tests/suites/ # E2E suites, grouped by test family @@ -345,73 +345,23 @@ can run the suite. The expectations characterize the sandbox's behavior today, not a specification, and the `echo-socket` check needs `perl` in the workload image, which the default image has. -### Python E2E (`e2e/python/`) +### E2E workload image (`e2e/workload/`) -`mise run e2e:python` builds `openshell/e2e-python:dev` from -`e2e/python/Dockerfile.workload` and selects it only for the test gateway. -This Noble-based fixture supplies the `sandbox` user, Python tooling, Git, -and a writable `/sandbox/.venv`. Its Python version comes from `.python-version` -so cloudpickle code objects match the test runner. The production workload +`mise run e2e:workload:build` builds `openshell/e2e-python:dev` from +`e2e/workload/Dockerfile`; `testkit::python_image()` and the Rust Docker +harness select it for tests that need Python, Git, curl, openssl, or the +`sandbox` user. This Noble-based fixture also has a writable `/sandbox/.venv`. +Its Python version comes from `.python-version`. The production workload default remains the unmodified NVIDIA Ubuntu image. -The Rust Docker harness also selects this fixture for tests that need tools -or the named user. Explicit `--from` or `--template` arguments and the default-image -tests retain their own image selection. Docker sandbox and support-container -fixtures use the fixed image name; no workload-image override is needed. -Podman, VM, and Kubernetes retain their existing pinned, pullable fixture and -image setup. Conformance-only runs do not need the Docker fixture. -Build the Docker fixture separately with `mise run e2e:workload:build`. +Explicit `--from` or `--template` arguments and the default-image tests retain +their own image selection. Docker sandbox and support-container fixtures use +the fixed image name; no workload-image override is needed. Podman, VM, and +Kubernetes retain their existing pinned, pullable fixture and image setup. -Tests use the `sandbox` fixture from `conftest.py` to create real sandboxes: - -```python -def test_exec_returns_stdout(sandbox): - with sandbox(delete_on_exit=True) as sb: - result = sb.exec(["echo", "hello"]) - assert result.exit_code == 0 - assert "hello" in result.stdout -``` - -#### `Sandbox.exec_python` - -`exec_python` serializes a Python callable with `cloudpickle`, sends it to the -sandbox, and returns the result. Because cloudpickle serializes module-level -functions by reference (which fails inside the sandbox), use one of these -patterns: - -**Closures from factory functions:** - -```python -def _make_adder(): - def add(a, b): - return a + b - return add - -def test_addition(sandbox): - with sandbox(delete_on_exit=True) as sb: - result = sb.exec_python(_make_adder(), args=(2, 3)) - assert result.stdout.strip() == "5" -``` - -**Bound methods on local classes:** - -```python -def test_multiply(sandbox): - class Calculator: - def multiply(self, a, b): - return a * b - - with sandbox(delete_on_exit=True) as sb: - result = sb.exec_python(Calculator().multiply, args=(6, 7)) - assert result.stdout.strip() == "42" -``` - -#### Shared Fixtures (`e2e/python/conftest.py`) - -| Fixture | Scope | Purpose | -|---|---|---| -| `sandbox_client` | session | gRPC client connected to the active gateway | -| `sandbox` | function | Factory returning a `Sandbox` context manager | +Core has no Python E2E suite. The Python SDK's own E2E suite lives in +[ai-openshell/sdk-python](https://github.com/ai-openshell/sdk-python) and runs +against published core gateways. ### Rust CLI E2E (`e2e/rust/`) @@ -453,7 +403,8 @@ Suites: - Common suite (`--features e2e`) - driver-neutral CLI behavior, sandbox lifecycle, sync, port forwarding, policy, and provider tests. - Conformance (`tests/suites/conformance`) - the testkit `conformance` crate: lifecycle, the CLI, persistent workspaces, file transfer, the policy - advisor, and credential gating. Driver E2E runs it with + advisor, credential gating, policy validation and enforcement (Landlock and + denied connections), and provider credential placement. Driver E2E runs it with `openshell-conformance` against the lane's gateway and writes a JUnit report under `results/`; set `OPENSHELL_CONFORMANCE_BIN` to use a prebuilt program, as Branch E2E does with the static build it makes once per architecture. @@ -650,6 +601,17 @@ rootful and rootless, and the `k3s` and `k3s-ha` installers. The tests run on the K3s node, so the K3s installers set the chart's `server.hostGatewayIP` to the node's address. +The `gateway-mtls` feature suite (`tests/suites/features/gateway-mtls`) checks +an mTLS gateway's user boundary: a client certificate from the gateway's CA +authenticates a user, a CA-only client reaches only Health, and a foreign +certificate or plaintext connection is refused. CI runs it against the deb +gateway on Docker rootful and in the Docker E2E workflow. Run it against a +local Docker gateway: + +```shell +mise run e2e:gateway-mtls +``` + Run the VM-backed Rust CLI e2e suite: ```shell diff --git a/e2e/python/conftest.py b/e2e/python/conftest.py deleted file mode 100644 index fb27b1e810..0000000000 --- a/e2e/python/conftest.py +++ /dev/null @@ -1,132 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -from __future__ import annotations - -import fcntl -import os -import time -from typing import TYPE_CHECKING - -import grpc -import pytest - -from openshell import Sandbox, SandboxClient, WorkspaceClient - -if TYPE_CHECKING: - from collections.abc import Callable, Iterator - - -def pytest_configure(config: pytest.Config) -> None: - config.addinivalue_line( - "markers", - "exclusive_gateway_config: hold an exclusive lock on gateway-global " - "config so no other xdist worker observes a transient global setting", - ) - - -@pytest.fixture(autouse=True) -def _gateway_config_guard( - request: pytest.FixtureRequest, - tmp_path_factory: pytest.TempPathFactory, -) -> Iterator[None]: - """Readers-writer guard over gateway-global config mutations. - - The e2e gateway is shared across xdist workers, so a test that flips a - gateway-global setting can leak that transient value into another worker's - sandbox creation. Every test holds a shared lock by default; a test marked - ``exclusive_gateway_config`` holds an exclusive lock, so while it mutates and - restores the setting no other worker is mid-test and none can observe the - transient value. The lock file lives in the run's shared base temp dir - (``getbasetemp().parent``), which is common to all xdist workers. - """ - lock_path = tmp_path_factory.getbasetemp().parent / "gateway-config.lock" - exclusive = ( - request.node.get_closest_marker("exclusive_gateway_config") is not None - ) - with lock_path.open("w") as lock_file: - fcntl.flock(lock_file, fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH) - yield - - -@pytest.fixture(scope="session") -def cluster_name() -> str | None: - return os.environ.get("OPENSHELL_GATEWAY") - - -@pytest.fixture(scope="session") -def sandbox_client(cluster_name: str | None) -> Iterator[SandboxClient]: - # Direct deletes can wait out the Kubernetes pod grace period. - with SandboxClient.from_active_cluster( - cluster=cluster_name, timeout=120.0 - ) as client: - yield client - - -@pytest.fixture(scope="session", autouse=True) -def ensure_sandbox_persistence_ready(sandbox_client: SandboxClient) -> None: - for _ in range(60): - try: - sandbox_client.list_ids(workspace="default", page_size=1) - return - except grpc.RpcError as exc: - details = exc.details() or "" - if exc.code() == grpc.StatusCode.UNAVAILABLE: - time.sleep(2) - continue - if ( - exc.code() == grpc.StatusCode.INTERNAL - and "no such table: objects" in details - ): - time.sleep(1) - continue - raise - - pytest.fail( - "openshell-server persistence is not initialized (missing sqlite objects table); " - "redeploy the active cluster and rerun e2e sandbox tests" - ) - - -@pytest.fixture -def sandbox(cluster_name: str | None) -> Callable[..., Sandbox]: - def _create(*, spec: object | None = None, delete_on_exit: bool = True) -> Sandbox: - return Sandbox( - workspace="default", - cluster=cluster_name, - spec=spec, - delete_on_exit=delete_on_exit, - # Allow time to pull an explicitly supplied workload fixture. - ready_timeout_seconds=300.0, - # Kubernetes holds DeleteSandbox open until the pod terminates - # (30s grace plus API headroom), which can exceed the 30s default. - delete_timeout_seconds=120.0, - ) - - return _create - - -@pytest.fixture(scope="session") -def workspace_client(sandbox_client: SandboxClient) -> WorkspaceClient: - return WorkspaceClient.from_sandbox_client(sandbox_client) - - -@pytest.fixture(scope="session") -def _worker_suffix(worker_id: str) -> str: - """Return a suffix for worker-unique resource names. - - Uses the built-in ``worker_id`` fixture from pytest-xdist which returns - ``"gw0"``, ``"gw1"``, etc. for workers, or ``"master"`` for non-xdist runs. - """ - if worker_id == "master": - return "" - return f"-{worker_id}" - - -@pytest.fixture -def run_python() -> Callable[[Sandbox, str], tuple[int, str, str]]: - def _run(sandbox: Sandbox, code: str) -> tuple[int, str, str]: - result = sandbox.exec(["python", "-c", code], timeout_seconds=20) - return result.exit_code, result.stdout, result.stderr - - return _run diff --git a/e2e/python/oidc/__init__.py b/e2e/python/oidc/__init__.py deleted file mode 100644 index e5725ea5a4..0000000000 --- a/e2e/python/oidc/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 diff --git a/e2e/python/oidc/conftest.py b/e2e/python/oidc/conftest.py deleted file mode 100644 index 8890357992..0000000000 --- a/e2e/python/oidc/conftest.py +++ /dev/null @@ -1,21 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -"""OIDC e2e test fixtures. - -Overrides the parent conftest's session fixtures that assume unauthenticated -gRPC access, since the OIDC-enabled gateway requires Bearer tokens. -""" - -import pytest - - -@pytest.fixture(scope="session") -def sandbox_client(): - """Stub — OIDC tests manage their own authenticated gRPC connections.""" - pytest.skip("OIDC tests do not use the shared sandbox_client fixture") - - -@pytest.fixture(scope="session", autouse=True) -def ensure_sandbox_persistence_ready(): - """No-op — OIDC tests skip the unauthenticated persistence check.""" diff --git a/e2e/python/oidc/helpers.py b/e2e/python/oidc/helpers.py deleted file mode 100644 index 7f441644b2..0000000000 --- a/e2e/python/oidc/helpers.py +++ /dev/null @@ -1,163 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -"""Shared helpers for OIDC e2e tests. - -Provides Keycloak token acquisition, gRPC channel setup, and JWT utilities. -""" - -from __future__ import annotations - -import base64 -import json -import os -import urllib.parse -import urllib.request -from pathlib import Path - -import grpc - -from openshell._proto import openshell_pb2_grpc - -KEYCLOAK_REALM = "openshell" - - -def _xdg_config_home() -> Path: - return Path(os.environ.get("XDG_CONFIG_HOME", Path.home() / ".config")) - - -def keycloak_url() -> str: - """Derive the Keycloak URL from the gateway's stored OIDC issuer. - - The server validates the issuer claim in JWTs, so the token must be - requested from the same base URL the server was configured with - (typically the host IP, not localhost). - """ - if url := os.environ.get("OPENSHELL_KEYCLOAK_URL"): - return url - if issuer := os.environ.get("OPENSHELL_E2E_OIDC_ISSUER"): - idx = issuer.find("/realms/") - if idx > 0: - return issuer[:idx] - cluster_name = os.environ.get("OPENSHELL_GATEWAY", "openshell") - metadata_path = ( - _xdg_config_home() / "openshell" / "gateways" / cluster_name / "metadata.json" - ) - if metadata_path.exists(): - metadata = json.loads(metadata_path.read_text()) - issuer = metadata.get("oidc_issuer", "") - if issuer: - idx = issuer.find("/realms/") - if idx > 0: - return issuer[:idx] - return "http://localhost:8180" - - -TOKEN_ENDPOINT = ( - f"{keycloak_url()}/realms/{KEYCLOAK_REALM}/protocol/openid-connect/token" -) - - -def _gateway_endpoint() -> tuple[str, bool]: - """Read the active gateway endpoint from metadata.""" - if endpoint := os.environ.get("OPENSHELL_E2E_OIDC_GATEWAY_ENDPOINT"): - return endpoint, endpoint.startswith("https://") - cluster_name = os.environ.get("OPENSHELL_GATEWAY", "openshell") - metadata_path = ( - _xdg_config_home() / "openshell" / "gateways" / cluster_name / "metadata.json" - ) - metadata = json.loads(metadata_path.read_text()) - endpoint = metadata["gateway_endpoint"] - is_tls = endpoint.startswith("https://") - return endpoint, is_tls - - -def _mtls_dir() -> Path: - cluster_name = os.environ.get("OPENSHELL_GATEWAY", "openshell") - return _xdg_config_home() / "openshell" / "gateways" / cluster_name / "mtls" - - -def _token_request(data: dict[str, str]) -> str: - """POST to the Keycloak token endpoint and return the access token.""" - encoded = urllib.parse.urlencode(data).encode() - req = urllib.request.Request(TOKEN_ENDPOINT, data=encoded) - with urllib.request.urlopen(req, timeout=10) as resp: - body = json.loads(resp.read()) - return body["access_token"] - - -def get_token( - username: str, - password: str, - *, - client_id: str = "openshell-cli", - scopes: str | None = None, -) -> str: - """Get an access token from Keycloak via password grant.""" - data = { - "grant_type": "password", - "client_id": client_id, - "username": username, - "password": password, - } - if scopes: - data["scope"] = scopes - return _token_request(data) - - -def get_ci_token( - *, - client_id: str = "openshell-ci", - client_secret: str = "ci-test-secret", -) -> str: - """Get an access token via client credentials grant.""" - return _token_request( - { - "grant_type": "client_credentials", - "client_id": client_id, - "client_secret": client_secret, - } - ) - - -def grpc_channel() -> grpc.Channel: - """Create a gRPC channel to the gateway over its configured TLS transport.""" - endpoint, is_tls = _gateway_endpoint() - parsed = urllib.parse.urlparse(endpoint) - host = parsed.hostname or "127.0.0.1" - port = parsed.port or (443 if is_tls else 80) - target = f"{host}:{port}" - - if is_tls: - if ca_path := os.environ.get("OPENSHELL_E2E_GATEWAY_CA_CERT"): - creds = grpc.ssl_channel_credentials( - root_certificates=Path(ca_path).read_bytes() - ) - else: - mtls = _mtls_dir() - creds = grpc.ssl_channel_credentials( - root_certificates=(mtls / "ca.crt").read_bytes(), - private_key=(mtls / "tls.key").read_bytes(), - certificate_chain=(mtls / "tls.crt").read_bytes(), - ) - return grpc.secure_channel(target, creds) - return grpc.insecure_channel(target) - - -def stub_with_token( - token: str, -) -> tuple[openshell_pb2_grpc.OpenShellStub, list[tuple[str, str]]]: - """Create a gRPC stub that injects a Bearer token.""" - channel = grpc_channel() - return openshell_pb2_grpc.OpenShellStub(channel), [ - ("authorization", f"Bearer {token}") - ] - - -def extract_sub(token: str) -> str: - """Extract the 'sub' claim from a JWT access token.""" - payload = token.split(".")[1] - padded = payload + "=" * (4 - len(payload) % 4) - decoded = base64.urlsafe_b64decode(padded) - claims = json.loads(decoded) - return claims["sub"] diff --git a/e2e/python/oidc/oidc_auth_test.py b/e2e/python/oidc/oidc_auth_test.py deleted file mode 100644 index 785a8ebdfb..0000000000 --- a/e2e/python/oidc/oidc_auth_test.py +++ /dev/null @@ -1,280 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -"""End-to-end tests for OIDC authentication, RBAC, and scope enforcement. - -These tests require: -- A running K3s cluster with OIDC enabled (OPENSHELL_OIDC_ISSUER set) -- A running Keycloak instance with the openshell realm -- The cluster started with OPENSHELL_OIDC_SCOPES_CLAIM=scope - -Skip condition: set OPENSHELL_E2E_OIDC=1 to enable these tests. -""" - -from __future__ import annotations - -import contextlib -import os -import urllib.parse -from pathlib import Path - -import grpc -import pytest - -from openshell import ClientCredentialsAuth, SandboxClient, TlsConfig -from openshell._proto import datamodel_pb2, openshell_pb2, openshell_pb2_grpc - -from .helpers import ( - KEYCLOAK_REALM, - _gateway_endpoint, - _mtls_dir, - extract_sub, - get_token, - grpc_channel, - keycloak_url, - stub_with_token, -) - -pytestmark = pytest.mark.skipif( - os.environ.get("OPENSHELL_E2E_OIDC") != "1", - reason="OIDC e2e tests disabled (set OPENSHELL_E2E_OIDC=1)", -) - - -# ── RBAC Tests ──────────────────────────────────────────────────────── - - -class TestRbac: - """Test role-based access control.""" - - def test_admin_can_create_provider(self) -> None: - token = get_token("admin@test", "admin", scopes="openid openshell:all") - stub, metadata = stub_with_token(token) - req = openshell_pb2.CreateProviderRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"), - provider=datamodel_pb2.Provider( - metadata=datamodel_pb2.ObjectMeta(name="e2e-oidc-admin-test"), - type="claude-code", - credentials={"ANTHROPIC_API_KEY": "test-value"}, - ), - ) - try: - stub.CreateProvider(req, metadata=metadata) - except grpc.RpcError as e: - if e.code() == grpc.StatusCode.ALREADY_EXISTS: - pass # fine, provider exists from a previous run - else: - raise - finally: - with contextlib.suppress(grpc.RpcError): - stub.DeleteProvider( - openshell_pb2.DeleteProviderRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector( - workspace="default" - ), - name="e2e-oidc-admin-test", - ), - metadata=metadata, - ) - - def test_user_cannot_create_provider(self) -> None: - token = get_token("user@test", "user", scopes="openid openshell:all") - stub, metadata = stub_with_token(token) - req = openshell_pb2.CreateProviderRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"), - provider=datamodel_pb2.Provider( - metadata=datamodel_pb2.ObjectMeta(name="e2e-oidc-user-blocked"), - type="claude-code", - credentials={"ANTHROPIC_API_KEY": "test-value"}, - ), - ) - with pytest.raises(grpc.RpcError) as exc_info: - stub.CreateProvider(req, metadata=metadata) - assert exc_info.value.code() == grpc.StatusCode.PERMISSION_DENIED - - def test_user_can_list_sandboxes(self) -> None: - admin_token = get_token("admin@test", "admin", scopes="openid openshell:all") - admin_stub, admin_md = stub_with_token(admin_token) - user_token = get_token("user@test", "user", scopes="openid openshell:all") - user_sub = extract_sub(user_token) - user_stub, user_md = stub_with_token(user_token) - - with contextlib.suppress(grpc.RpcError): - admin_stub.AddWorkspaceMember( - openshell_pb2.AddWorkspaceMemberRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector( - workspace="default" - ), - principal_subject=user_sub, - role=openshell_pb2.WORKSPACE_ROLE_USER, - ), - metadata=admin_md, - ) - try: - user_stub.ListSandboxes( - openshell_pb2.ListSandboxesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default") - ), - metadata=user_md, - ) - finally: - with contextlib.suppress(grpc.RpcError): - admin_stub.RemoveWorkspaceMember( - openshell_pb2.RemoveWorkspaceMemberRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector( - workspace="default" - ), - principal_subject=user_sub, - ), - metadata=admin_md, - ) - - def test_request_without_bearer_token_rejected(self) -> None: - channel = grpc_channel() - stub = openshell_pb2_grpc.OpenShellStub(channel) - with pytest.raises(grpc.RpcError) as exc_info: - stub.ListSandboxes( - openshell_pb2.ListSandboxesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default") - ) - ) - assert exc_info.value.code() in ( - grpc.StatusCode.UNAUTHENTICATED, - grpc.StatusCode.PERMISSION_DENIED, - ) - - def test_health_does_not_require_auth(self) -> None: - channel = grpc_channel() - stub = openshell_pb2_grpc.OpenShellStub(channel) - resp = stub.Health(openshell_pb2.HealthRequest()) - assert resp.status == openshell_pb2.SERVICE_STATUS_HEALTHY - - -# ── Scope Enforcement Tests ────────────────────────────────────────── - - -class TestScopes: - """Test scope-based fine-grained permissions. - - These tests require the server to be started with - OPENSHELL_OIDC_SCOPES_CLAIM=scope. - """ - - pytestmark = pytest.mark.skipif( - os.environ.get("OPENSHELL_E2E_OIDC_SCOPES") != "1", - reason="Scope e2e tests disabled (set OPENSHELL_E2E_OIDC_SCOPES=1)", - ) - - def test_sandbox_scoped_token_can_list_sandboxes(self) -> None: - token = get_token( - "admin@test", "admin", scopes="openid sandbox:read sandbox:write" - ) - stub, metadata = stub_with_token(token) - stub.ListSandboxes( - openshell_pb2.ListSandboxesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default") - ), - metadata=metadata, - ) - - def test_sandbox_scoped_token_cannot_list_providers(self) -> None: - token = get_token( - "admin@test", "admin", scopes="openid sandbox:read sandbox:write" - ) - stub, metadata = stub_with_token(token) - with pytest.raises(grpc.RpcError) as exc_info: - stub.ListProviders( - openshell_pb2.ListProvidersRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default") - ), - metadata=metadata, - ) - assert exc_info.value.code() == grpc.StatusCode.PERMISSION_DENIED - assert "provider:read" in exc_info.value.details() - - def test_openshell_all_grants_full_access(self) -> None: - token = get_token("admin@test", "admin", scopes="openid openshell:all") - stub, metadata = stub_with_token(token) - stub.ListSandboxes( - openshell_pb2.ListSandboxesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default") - ), - metadata=metadata, - ) - stub.ListProviders( - openshell_pb2.ListProvidersRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default") - ), - metadata=metadata, - ) - - def test_no_openshell_scopes_denied(self) -> None: - token = get_token("admin@test", "admin") - stub, metadata = stub_with_token(token) - with pytest.raises(grpc.RpcError) as exc_info: - stub.ListSandboxes( - openshell_pb2.ListSandboxesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default") - ), - metadata=metadata, - ) - assert exc_info.value.code() == grpc.StatusCode.PERMISSION_DENIED - - -# ── Client Credentials Tests ───────────────────────────────────────── - - -class TestClientCredentials: - """Test CI/automation client credentials flow.""" - - def test_ci_token_can_list_sandboxes(self) -> None: - admin_token = get_token("admin@test", "admin", scopes="openid openshell:all") - admin_stub, admin_md = stub_with_token(admin_token) - auth = ClientCredentialsAuth( - issuer=f"{keycloak_url()}/realms/{KEYCLOAK_REALM}", - client_id="openshell-ci", - client_secret="ci-test-secret", - ) - ci_token = auth() - ci_sub = extract_sub(ci_token) - gateway_endpoint, is_tls = _gateway_endpoint() - parsed = urllib.parse.urlparse(gateway_endpoint) - target = f"{parsed.hostname}:{parsed.port or (443 if is_tls else 80)}" - tls = None - if is_tls: - if ca_path := os.environ.get("OPENSHELL_E2E_GATEWAY_CA_CERT"): - tls = TlsConfig(ca_path=Path(ca_path)) - else: - mtls = _mtls_dir() - tls = TlsConfig( - ca_path=mtls / "ca.crt", - cert_path=mtls / "tls.crt", - key_path=mtls / "tls.key", - ) - ci_client = SandboxClient(target, tls=tls, client_credentials=auth) - - with contextlib.suppress(grpc.RpcError): - admin_stub.AddWorkspaceMember( - openshell_pb2.AddWorkspaceMemberRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector( - workspace="default" - ), - principal_subject=ci_sub, - role=openshell_pb2.WORKSPACE_ROLE_USER, - ), - metadata=admin_md, - ) - try: - ci_client.list_all(workspace="default") - finally: - ci_client.close() - with contextlib.suppress(grpc.RpcError): - admin_stub.RemoveWorkspaceMember( - openshell_pb2.RemoveWorkspaceMemberRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector( - workspace="default" - ), - principal_subject=ci_sub, - ), - metadata=admin_md, - ) diff --git a/e2e/python/oidc/workspace_authz_test.py b/e2e/python/oidc/workspace_authz_test.py deleted file mode 100644 index ad64fbe900..0000000000 --- a/e2e/python/oidc/workspace_authz_test.py +++ /dev/null @@ -1,1365 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -"""End-to-end tests for workspace-scoped authorization enforcement. - -Validates that every workspace-scoped RPC enforces membership and role -checks when OIDC is configured. Uses two Keycloak users: - -- admin@test — openshell-admin role → Platform Admin (bypasses membership) -- user@test — openshell-user role → must be an explicit workspace member - -Skip condition: set OPENSHELL_E2E_OIDC=1 to enable these tests. -""" - -from __future__ import annotations - -import contextlib -import os -from typing import TYPE_CHECKING, Any - -import grpc -import pytest - -if TYPE_CHECKING: - from collections.abc import Callable - -from openshell._proto import ( - datamodel_pb2, - openshell_pb2, - openshell_pb2_grpc, -) - -from .helpers import extract_sub, get_token, stub_with_token - -WS = "e2e-authz-test" - -pytestmark = pytest.mark.skipif( - os.environ.get("OPENSHELL_E2E_OIDC") != "1", - reason="OIDC e2e tests disabled (set OPENSHELL_E2E_OIDC=1)", -) - - -# ── Helpers ────────────────────────────────────────────────────────────── - - -def _admin_token() -> str: - return get_token("admin@test", "admin", scopes="openid openshell:all") - - -def _user_token() -> str: - return get_token("user@test", "user", scopes="openid openshell:all") - - -def _add_member( - stub: openshell_pb2_grpc.OpenShellStub, - metadata: list[tuple[str, str]], - workspace: str, - subject: str, - role: int, -) -> None: - stub.AddWorkspaceMember( - openshell_pb2.AddWorkspaceMemberRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=workspace), - principal_subject=subject, - role=role, - ), - metadata=metadata, - ) - - -def _remove_member( - stub: openshell_pb2_grpc.OpenShellStub, - metadata: list[tuple[str, str]], - workspace: str, - subject: str, -) -> None: - with contextlib.suppress(grpc.RpcError): - stub.RemoveWorkspaceMember( - openshell_pb2.RemoveWorkspaceMemberRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=workspace), - principal_subject=subject, - ), - metadata=metadata, - ) - - -# ── RPC call builders for parametrized non-member rejection tests ──────── -# -# Each entry is (test_id, callable(stub, metadata) -> response). -# The callable constructs a minimal valid request for the given RPC. - - -def _assert_non_member_denial( - error: grpc.RpcError, - workspace: str, - subject: str, - rpc_name: str, -) -> None: - assert error.code() == grpc.StatusCode.PERMISSION_DENIED, ( - f"{rpc_name}: expected PERMISSION_DENIED, got {error.code()}" - ) - details = error.details() - assert f"not a member of workspace '{workspace}'" in details, ( - f"{rpc_name}: denial came from the wrong authorization layer: {details}" - ) - command = ( - "openshell workspace member add " - f"--workspace '{workspace}' --subject '{subject}' --role user" - ) - assert command in details, ( - f"{rpc_name}: denial omitted the actionable membership command: {details}" - ) - - -def _assert_hidden_sandbox(error: grpc.RpcError, rpc_name: str) -> None: - assert error.code() == grpc.StatusCode.NOT_FOUND, ( - f"{rpc_name}: expected NOT_FOUND, got {error.code()}" - ) - - -def _assert_workspace_admin_denial( - error: grpc.RpcError, - workspace: str, - subject: str, - rpc_name: str, -) -> None: - assert error.code() == grpc.StatusCode.PERMISSION_DENIED, ( - f"{rpc_name}: expected PERMISSION_DENIED, got {error.code()}" - ) - details = error.details() - assert f"workspace role 'admin' required in workspace '{workspace}'" in details, ( - f"{rpc_name}: denial came from the wrong authorization layer: {details}" - ) - command = ( - "openshell workspace member add " - f"--workspace '{workspace}' --subject '{subject}' --role admin" - ) - assert command in details, ( - f"{rpc_name}: denial omitted the admin remediation command: {details}" - ) - - -def _workspace_rpcs() -> list[tuple[str, Callable]]: - """All workspace-scoped RPCs that accept a workspace field.""" - return [ - # ── Workspace domain ── - ( - "GetWorkspace", - lambda s, m: s.GetWorkspace( - openshell_pb2.GetWorkspaceRequest(name=WS), metadata=m - ), - ), - ( - "ListWorkspaceMembers", - lambda s, m: s.ListWorkspaceMembers( - openshell_pb2.ListWorkspaceMembersRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS) - ), - metadata=m, - ), - ), - ( - "AddWorkspaceMember", - lambda s, m: s.AddWorkspaceMember( - openshell_pb2.AddWorkspaceMemberRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - principal_subject="fake", - role=openshell_pb2.WORKSPACE_ROLE_USER, - ), - metadata=m, - ), - ), - ( - "RemoveWorkspaceMember", - lambda s, m: s.RemoveWorkspaceMember( - openshell_pb2.RemoveWorkspaceMemberRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - principal_subject="fake", - ), - metadata=m, - ), - ), - # ── Sandbox domain ── - ( - "CreateSandbox", - lambda s, m: s.CreateSandbox( - openshell_pb2.CreateSandboxRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - spec=openshell_pb2.SandboxSpec( - template=openshell_pb2.SandboxTemplate(image="ubuntu:24.04") - ), - ), - metadata=m, - ), - ), - ( - "GetSandbox", - lambda s, m: s.GetSandbox( - openshell_pb2.GetSandboxRequest( - name="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "ListSandboxes", - lambda s, m: s.ListSandboxes( - openshell_pb2.ListSandboxesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS) - ), - metadata=m, - ), - ), - ( - "DeleteSandbox", - lambda s, m: s.DeleteSandbox( - openshell_pb2.DeleteSandboxRequest( - name="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "ListSandboxProviders", - lambda s, m: s.ListSandboxProviders( - openshell_pb2.ListSandboxProvidersRequest( - sandbox="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "AttachSandboxProvider", - lambda s, m: s.AttachSandboxProvider( - openshell_pb2.AttachSandboxProviderRequest( - sandbox="nonexistent", - provider="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "DetachSandboxProvider", - lambda s, m: s.DetachSandboxProvider( - openshell_pb2.DetachSandboxProviderRequest( - sandbox="nonexistent", - provider="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - # ── Provider domain ── - ( - "CreateProvider", - lambda s, m: s.CreateProvider( - openshell_pb2.CreateProviderRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - provider=datamodel_pb2.Provider( - metadata=datamodel_pb2.ObjectMeta( - name="authz-test", workspace=WS - ), - type="claude-code", - credentials={"ANTHROPIC_API_KEY": "v"}, - ), - ), - metadata=m, - ), - ), - ( - "GetProvider", - lambda s, m: s.GetProvider( - openshell_pb2.GetProviderRequest( - name="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "ListProviders", - lambda s, m: s.ListProviders( - openshell_pb2.ListProvidersRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS) - ), - metadata=m, - ), - ), - ( - "UpdateProvider", - lambda s, m: s.UpdateProvider( - openshell_pb2.UpdateProviderRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - provider=datamodel_pb2.Provider( - metadata=datamodel_pb2.ObjectMeta( - name="nonexistent", workspace=WS - ), - type="claude-code", - credentials={"ANTHROPIC_API_KEY": "v"}, - ), - ), - metadata=m, - ), - ), - ( - "DeleteProvider", - lambda s, m: s.DeleteProvider( - openshell_pb2.DeleteProviderRequest( - name="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "ListProviderProfiles", - lambda s, m: s.ListProviderProfiles( - openshell_pb2.ListProviderProfilesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS) - ), - metadata=m, - ), - ), - ( - "GetProviderProfile", - lambda s, m: s.GetProviderProfile( - openshell_pb2.GetProviderProfileRequest( - id="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "ImportProviderProfiles", - lambda s, m: s.ImportProviderProfiles( - openshell_pb2.ImportProviderProfilesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - profiles=[], - ), - metadata=m, - ), - ), - ( - "UpdateProviderProfiles", - lambda s, m: s.UpdateProviderProfiles( - openshell_pb2.UpdateProviderProfilesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - id="nonexistent", - ), - metadata=m, - ), - ), - ( - "LintProviderProfiles", - lambda s, m: s.LintProviderProfiles( - openshell_pb2.LintProviderProfilesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - profiles=[], - ), - metadata=m, - ), - ), - ( - "DeleteProviderProfile", - lambda s, m: s.DeleteProviderProfile( - openshell_pb2.DeleteProviderProfileRequest( - id="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "GetProviderRefreshStatus", - lambda s, m: s.GetProviderRefreshStatus( - openshell_pb2.GetProviderRefreshStatusRequest( - provider="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "ConfigureProviderRefresh", - lambda s, m: s.ConfigureProviderRefresh( - openshell_pb2.ConfigureProviderRefreshRequest( - provider="nonexistent", - credential_key="k", - strategy=openshell_pb2.PROVIDER_CREDENTIAL_REFRESH_STRATEGY_STATIC, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "RotateProviderCredential", - lambda s, m: s.RotateProviderCredential( - openshell_pb2.RotateProviderCredentialRequest( - provider="nonexistent", - credential_key="k", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "DeleteProviderRefresh", - lambda s, m: s.DeleteProviderRefresh( - openshell_pb2.DeleteProviderRefreshRequest( - provider="nonexistent", - credential_key="k", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - # ── Service domain ── - ( - "ExposeService", - lambda s, m: s.ExposeService( - openshell_pb2.ExposeServiceRequest( - sandbox="nonexistent", - name="svc", - target_port=8080, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "GetService", - lambda s, m: s.GetService( - openshell_pb2.GetServiceRequest( - sandbox="nonexistent", - name="svc", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "ListServices", - lambda s, m: s.ListServices( - openshell_pb2.ListServicesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS) - ), - metadata=m, - ), - ), - ( - "DeleteService", - lambda s, m: s.DeleteService( - openshell_pb2.DeleteServiceRequest( - sandbox="nonexistent", - name="svc", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - # ── Policy domain ── - ( - "GetSandboxPolicyStatus", - lambda s, m: s.GetSandboxPolicyStatus( - openshell_pb2.GetSandboxPolicyStatusRequest( - sandbox="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "ListSandboxPolicies", - lambda s, m: s.ListSandboxPolicies( - openshell_pb2.ListSandboxPoliciesRequest( - sandbox="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "GetDraftPolicy", - lambda s, m: s.GetDraftPolicy( - openshell_pb2.GetDraftPolicyRequest( - sandbox="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "ApproveDraftChunk", - lambda s, m: s.ApproveDraftChunk( - openshell_pb2.ApproveDraftChunkRequest( - sandbox="nonexistent", - chunk_id="x", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "RejectDraftChunk", - lambda s, m: s.RejectDraftChunk( - openshell_pb2.RejectDraftChunkRequest( - sandbox="nonexistent", - chunk_id="x", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "ApproveAllDraftChunks", - lambda s, m: s.ApproveAllDraftChunks( - openshell_pb2.ApproveAllDraftChunksRequest( - sandbox="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "EditDraftChunk", - lambda s, m: s.EditDraftChunk( - openshell_pb2.EditDraftChunkRequest( - sandbox="nonexistent", - chunk_id="x", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "UndoDraftChunk", - lambda s, m: s.UndoDraftChunk( - openshell_pb2.UndoDraftChunkRequest( - sandbox="nonexistent", - chunk_id="x", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "ClearDraftChunks", - lambda s, m: s.ClearDraftChunks( - openshell_pb2.ClearDraftChunksRequest( - sandbox="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ( - "GetDraftHistory", - lambda s, m: s.GetDraftHistory( - openshell_pb2.GetDraftHistoryRequest( - sandbox="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=m, - ), - ), - ] - - -def _platform_profile_rpcs() -> list[tuple[str, Callable]]: - """Provider-profile RPCs targeting the explicit platform scope.""" - return [ - ( - "ListProviderProfiles", - lambda s, m: s.ListProviderProfiles( - openshell_pb2.ListProviderProfilesRequest(), metadata=m - ), - ), - ( - "GetProviderProfile", - lambda s, m: s.GetProviderProfile( - openshell_pb2.GetProviderProfileRequest(id="nonexistent"), - metadata=m, - ), - ), - ( - "ImportProviderProfiles", - lambda s, m: s.ImportProviderProfiles( - openshell_pb2.ImportProviderProfilesRequest(profiles=[]), - metadata=m, - ), - ), - ( - "UpdateProviderProfiles", - lambda s, m: s.UpdateProviderProfiles( - openshell_pb2.UpdateProviderProfilesRequest(id="nonexistent"), - metadata=m, - ), - ), - ( - "LintProviderProfiles", - lambda s, m: s.LintProviderProfiles( - openshell_pb2.LintProviderProfilesRequest(profiles=[]), - metadata=m, - ), - ), - ( - "DeleteProviderProfile", - lambda s, m: s.DeleteProviderProfile( - openshell_pb2.DeleteProviderProfileRequest(id="nonexistent"), - metadata=m, - ), - ), - ] - - -def _global_policy_read_rpcs() -> list[tuple[str, Callable]]: - """Policy history reads targeting the explicit global scope.""" - return [ - ( - "GetSandboxPolicyStatus", - lambda s, m: s.GetSandboxPolicyStatus( - openshell_pb2.GetSandboxPolicyStatusRequest(**{"global": True}), - metadata=m, - ), - ), - ( - "ListSandboxPolicies", - lambda s, m: s.ListSandboxPolicies( - openshell_pb2.ListSandboxPoliciesRequest(**{"global": True}), - metadata=m, - ), - ), - ] - - -# ── Test class ─────────────────────────────────────────────────────────── - - -class TestWorkspaceAuthorization: - """Workspace-scoped authorization enforcement tests.""" - - @pytest.fixture(autouse=True, scope="class") - def workspace(self) -> Any: - """Create a test workspace and tear it down after all tests.""" - token = _admin_token() - stub, metadata = stub_with_token(token) - - with contextlib.suppress(grpc.RpcError): - stub.CreateWorkspace( - openshell_pb2.CreateWorkspaceRequest(name=WS), - metadata=metadata, - ) - - yield WS - - with contextlib.suppress(grpc.RpcError): - stub.DeleteWorkspace( - openshell_pb2.DeleteWorkspaceRequest(name=WS), - metadata=metadata, - ) - - @pytest.fixture(scope="class") - def admin_ctx( - self, - ) -> tuple[openshell_pb2_grpc.OpenShellStub, list[tuple[str, str]]]: - token = _admin_token() - return stub_with_token(token) - - @pytest.fixture(scope="class") - def user_ctx( - self, - ) -> tuple[openshell_pb2_grpc.OpenShellStub, list[tuple[str, str]], str]: - token = _user_token() - stub, metadata = stub_with_token(token) - sub = extract_sub(token) - return stub, metadata, sub - - @pytest.fixture(scope="class") - def seed_provider(self, admin_ctx: Any, workspace: str) -> Any: - """Create a provider so read RPCs have data to return.""" - stub, metadata = admin_ctx - prov_name = "e2e-authz-provider" - with contextlib.suppress(grpc.RpcError): - stub.CreateProvider( - openshell_pb2.CreateProviderRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector( - workspace=workspace - ), - provider=datamodel_pb2.Provider( - metadata=datamodel_pb2.ObjectMeta( - name=prov_name, workspace=workspace - ), - type="claude-code", - credentials={"ANTHROPIC_API_KEY": "test"}, - ), - ), - metadata=metadata, - ) - yield prov_name - with contextlib.suppress(grpc.RpcError): - stub.DeleteProvider( - openshell_pb2.DeleteProviderRequest( - name=prov_name, - workspace_scope=datamodel_pb2.WorkspaceSelector( - workspace=workspace - ), - ), - metadata=metadata, - ) - - # ── Test 1: Non-member rejection — workspace-field RPCs ────────── - - @pytest.mark.parametrize( - "rpc_name,call", - _workspace_rpcs(), - ids=[r[0] for r in _workspace_rpcs()], - ) - def test_non_member_rejected( - self, - rpc_name: str, - call: Callable, - user_ctx: Any, - ) -> None: - stub, metadata, user_sub = user_ctx - with pytest.raises(grpc.RpcError) as exc_info: - call(stub, metadata) - if rpc_name in { - "GetSandbox", - "DeleteSandbox", - "ListSandboxProviders", - "AttachSandboxProvider", - "DetachSandboxProvider", - "ExposeService", - "GetService", - "DeleteService", - "GetSandboxPolicyStatus", - "ListSandboxPolicies", - "GetDraftPolicy", - "ApproveDraftChunk", - "RejectDraftChunk", - "ApproveAllDraftChunks", - "EditDraftChunk", - "UndoDraftChunk", - "ClearDraftChunks", - "GetDraftHistory", - }: - _assert_hidden_sandbox(exc_info.value, rpc_name) - else: - _assert_non_member_denial(exc_info.value, WS, user_sub, rpc_name) - - # ── Test 2: Non-member rejection — dual-mode RPCs ──────────────── - - def test_non_member_rejected_update_config( - self, - user_ctx: Any, - ) -> None: - stub, metadata, _user_sub = user_ctx - with pytest.raises(grpc.RpcError) as exc_info: - stub.UpdateConfig( - openshell_pb2.UpdateConfigRequest( - sandbox="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=metadata, - ) - _assert_hidden_sandbox(exc_info.value, "UpdateConfig") - - # ── Test 3: Sandbox log authorization uses persisted workspace ─── - - def test_get_sandbox_logs_rejects_spoofed_workspace( - self, - admin_ctx: Any, - user_ctx: Any, - ) -> None: - admin_stub, admin_md = admin_ctx - user_stub, user_md, user_sub = user_ctx - other_workspace = "e2e-authz-log-b" - sandbox_name = "e2e-log-target" - - with contextlib.suppress(grpc.RpcError): - admin_stub.DeleteWorkspace( - openshell_pb2.DeleteWorkspaceRequest(name=other_workspace), - metadata=admin_md, - ) - admin_stub.CreateWorkspace( - openshell_pb2.CreateWorkspaceRequest(name=other_workspace), - metadata=admin_md, - ) - _add_member( - admin_stub, admin_md, WS, user_sub, openshell_pb2.WORKSPACE_ROLE_USER - ) - - sandbox_created = False - try: - admin_stub.CreateSandbox( - openshell_pb2.CreateSandboxRequest( - name=sandbox_name, - workspace_scope=datamodel_pb2.WorkspaceSelector( - workspace=other_workspace - ), - spec=openshell_pb2.SandboxSpec(), - ), - metadata=admin_md, - ) - sandbox_created = True - - with pytest.raises(grpc.RpcError) as exc_info: - user_stub.GetSandboxLogs( - openshell_pb2.GetSandboxLogsRequest( - sandbox=sandbox_name, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=user_md, - ) - # Name-based handlers normalize unauthorized responses to NOT_FOUND - # so cross-workspace sandbox existence cannot be inferred (CWE-203). - assert exc_info.value.code() == grpc.StatusCode.NOT_FOUND, ( - "GetSandboxLogs: expected NOT_FOUND for cross-workspace sandbox, " - f"got {exc_info.value.code()}" - ) - finally: - if sandbox_created: - with contextlib.suppress(grpc.RpcError): - admin_stub.DeleteSandbox( - openshell_pb2.DeleteSandboxRequest( - name=sandbox_name, - workspace_scope=datamodel_pb2.WorkspaceSelector( - workspace=other_workspace - ), - ), - metadata=admin_md, - ) - _remove_member(admin_stub, admin_md, WS, user_sub) - with contextlib.suppress(grpc.RpcError): - admin_stub.DeleteWorkspace( - openshell_pb2.DeleteWorkspaceRequest(name=other_workspace), - metadata=admin_md, - ) - - # ── Test 4: Global config requires Platform Admin ──────────────── - - def test_global_update_rejected_for_default_workspace_admin( - self, - admin_ctx: Any, - user_ctx: Any, - ) -> None: - admin_stub, admin_md = admin_ctx - user_stub, user_md, user_sub = user_ctx - - _remove_member(admin_stub, admin_md, "default", user_sub) - _add_member( - admin_stub, - admin_md, - "default", - user_sub, - openshell_pb2.WORKSPACE_ROLE_ADMIN, - ) - try: - with pytest.raises(grpc.RpcError) as exc_info: - user_stub.UpdateConfig( - openshell_pb2.UpdateConfigRequest( - setting_key="log_level", - delete_setting=True, - **{"global": True}, - ), - metadata=user_md, - ) - assert exc_info.value.code() == grpc.StatusCode.PERMISSION_DENIED - assert "platform admin role required" in exc_info.value.details(), ( - "UpdateConfig: denial came from the wrong authorization layer: " - f"{exc_info.value.details()}" - ) - finally: - _remove_member(admin_stub, admin_md, "default", user_sub) - - # ── Test 5: Non-member ListWorkspaces returns filtered results ─── - - def test_non_member_list_workspaces_filtered( - self, - user_ctx: Any, - ) -> None: - stub, metadata, _ = user_ctx - resp = stub.ListWorkspaces( - openshell_pb2.ListWorkspacesRequest(), - metadata=metadata, - ) - ws_names = [w.metadata.name for w in resp.workspaces] - assert WS not in ws_names, ( - f"non-member should not see workspace {WS} in ListWorkspaces" - ) - - # ── Test 6: Platform Admin bypass ──────────────────────────────── - - @pytest.mark.usefixtures("seed_provider") - def test_platform_admin_get_workspace( - self, - admin_ctx: Any, - ) -> None: - stub, metadata = admin_ctx - resp = stub.GetWorkspace( - openshell_pb2.GetWorkspaceRequest(name=WS), - metadata=metadata, - ) - assert resp.workspace.metadata.name == WS - - def test_platform_admin_list_sandboxes( - self, - admin_ctx: Any, - ) -> None: - stub, metadata = admin_ctx - stub.ListSandboxes( - openshell_pb2.ListSandboxesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS) - ), - metadata=metadata, - ) - - def test_platform_admin_get_provider( - self, - admin_ctx: Any, - seed_provider: str, - ) -> None: - stub, metadata = admin_ctx - resp = stub.GetProvider( - openshell_pb2.GetProviderRequest( - name=seed_provider, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=metadata, - ) - assert resp.provider.metadata.name == seed_provider - - def test_platform_admin_list_services( - self, - admin_ctx: Any, - ) -> None: - stub, metadata = admin_ctx - stub.ListServices( - openshell_pb2.ListServicesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS) - ), - metadata=metadata, - ) - - def test_platform_admin_get_draft_history( - self, - admin_ctx: Any, - ) -> None: - stub, metadata = admin_ctx - # May fail with NOT_FOUND for the sandbox name, but should not fail with PERMISSION_DENIED - try: - stub.GetDraftHistory( - openshell_pb2.GetDraftHistoryRequest( - sandbox="nonexistent", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=metadata, - ) - except grpc.RpcError as e: - assert e.code() != grpc.StatusCode.PERMISSION_DENIED - - # ── Test 7: User member — read operations succeed ──────────────── - - def test_user_member_read_operations( - self, - admin_ctx: Any, - user_ctx: Any, - seed_provider: str, - ) -> None: - admin_stub, admin_md = admin_ctx - user_stub, user_md, user_sub = user_ctx - - _add_member( - admin_stub, admin_md, WS, user_sub, openshell_pb2.WORKSPACE_ROLE_USER - ) - try: - # GetWorkspace - resp = user_stub.GetWorkspace( - openshell_pb2.GetWorkspaceRequest(name=WS), - metadata=user_md, - ) - assert resp.workspace.metadata.name == WS - - # ListSandboxes - user_stub.ListSandboxes( - openshell_pb2.ListSandboxesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS) - ), - metadata=user_md, - ) - - # GetProvider - resp = user_stub.GetProvider( - openshell_pb2.GetProviderRequest( - name=seed_provider, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=user_md, - ) - assert resp.provider.metadata.name == seed_provider - - # ListProviders - user_stub.ListProviders( - openshell_pb2.ListProvidersRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS) - ), - metadata=user_md, - ) - - # ListServices - user_stub.ListServices( - openshell_pb2.ListServicesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS) - ), - metadata=user_md, - ) - - # ListWorkspaceMembers - user_stub.ListWorkspaceMembers( - openshell_pb2.ListWorkspaceMembersRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS) - ), - metadata=user_md, - ) - finally: - _remove_member(admin_stub, admin_md, WS, user_sub) - - # ── Test 8: User member — admin operations denied ──────────────── - - def test_user_member_admin_operations_denied( - self, - admin_ctx: Any, - user_ctx: Any, - ) -> None: - admin_stub, admin_md = admin_ctx - user_stub, user_md, user_sub = user_ctx - - _add_member( - admin_stub, admin_md, WS, user_sub, openshell_pb2.WORKSPACE_ROLE_USER - ) - try: - # CreateProvider requires workspace admin - with pytest.raises(grpc.RpcError) as exc_info: - user_stub.CreateProvider( - openshell_pb2.CreateProviderRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - provider=datamodel_pb2.Provider( - metadata=datamodel_pb2.ObjectMeta( - name="user-blocked", workspace=WS - ), - type="claude-code", - credentials={"ANTHROPIC_API_KEY": "v"}, - ), - ), - metadata=user_md, - ) - _assert_workspace_admin_denial( - exc_info.value, - WS, - user_sub, - "CreateProvider", - ) - - # AddWorkspaceMember requires workspace admin - with pytest.raises(grpc.RpcError) as exc_info: - user_stub.AddWorkspaceMember( - openshell_pb2.AddWorkspaceMemberRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - principal_subject="fake-subject", - role=openshell_pb2.WORKSPACE_ROLE_USER, - ), - metadata=user_md, - ) - _assert_workspace_admin_denial( - exc_info.value, - WS, - user_sub, - "AddWorkspaceMember", - ) - - # ApproveDraftChunk requires workspace admin - with pytest.raises(grpc.RpcError) as exc_info: - user_stub.ApproveDraftChunk( - openshell_pb2.ApproveDraftChunkRequest( - sandbox="nonexistent", - chunk_id="x", - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=user_md, - ) - # Name-scoped sandbox operations hide whether an unknown sandbox - # exists before evaluating workspace-admin authorization. - _assert_hidden_sandbox(exc_info.value, "ApproveDraftChunk") - finally: - _remove_member(admin_stub, admin_md, WS, user_sub) - - # ── Test 9: Workspace Admin — admin operations succeed ─────────── - - def test_workspace_admin_can_create_provider( - self, - admin_ctx: Any, - user_ctx: Any, - ) -> None: - admin_stub, admin_md = admin_ctx - user_stub, user_md, user_sub = user_ctx - - _add_member( - admin_stub, admin_md, WS, user_sub, openshell_pb2.WORKSPACE_ROLE_ADMIN - ) - prov_name = "e2e-authz-ws-admin-prov" - try: - user_stub.CreateProvider( - openshell_pb2.CreateProviderRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - provider=datamodel_pb2.Provider( - metadata=datamodel_pb2.ObjectMeta(name=prov_name, workspace=WS), - type="claude-code", - credentials={"ANTHROPIC_API_KEY": "v"}, - ), - ), - metadata=user_md, - ) - - # Also test AddWorkspaceMember with User role - user_stub.AddWorkspaceMember( - openshell_pb2.AddWorkspaceMemberRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - principal_subject="fake-member-subject", - role=openshell_pb2.WORKSPACE_ROLE_USER, - ), - metadata=user_md, - ) - _remove_member(admin_stub, admin_md, WS, "fake-member-subject") - finally: - with contextlib.suppress(grpc.RpcError): - admin_stub.DeleteProvider( - openshell_pb2.DeleteProviderRequest( - name=prov_name, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - ), - metadata=admin_md, - ) - _remove_member(admin_stub, admin_md, WS, user_sub) - - # ── Test 10: all_workspaces rejected for non-Platform-Admin ────── - - def test_all_workspaces_rejected_for_workspace_admin( - self, - admin_ctx: Any, - user_ctx: Any, - ) -> None: - admin_stub, admin_md = admin_ctx - user_stub, user_md, user_sub = user_ctx - - _add_member( - admin_stub, admin_md, WS, user_sub, openshell_pb2.WORKSPACE_ROLE_ADMIN - ) - try: - with pytest.raises(grpc.RpcError) as exc_info: - user_stub.ListSandboxes( - openshell_pb2.ListSandboxesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector( - all_workspaces=datamodel_pb2.AllWorkspaces() - ) - ), - metadata=user_md, - ) - assert exc_info.value.code() == grpc.StatusCode.PERMISSION_DENIED - - with pytest.raises(grpc.RpcError) as exc_info: - user_stub.ListProviders( - openshell_pb2.ListProvidersRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector( - all_workspaces=datamodel_pb2.AllWorkspaces() - ) - ), - metadata=user_md, - ) - assert exc_info.value.code() == grpc.StatusCode.PERMISSION_DENIED - - with pytest.raises(grpc.RpcError) as exc_info: - user_stub.ListServices( - openshell_pb2.ListServicesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector( - all_workspaces=datamodel_pb2.AllWorkspaces() - ) - ), - metadata=user_md, - ) - assert exc_info.value.code() == grpc.StatusCode.PERMISSION_DENIED - finally: - _remove_member(admin_stub, admin_md, WS, user_sub) - - # ── Test 11: Workspace Admin cannot assign Admin role ──────────── - - def test_workspace_admin_cannot_assign_admin_role( - self, - admin_ctx: Any, - user_ctx: Any, - ) -> None: - admin_stub, admin_md = admin_ctx - user_stub, user_md, user_sub = user_ctx - - _add_member( - admin_stub, admin_md, WS, user_sub, openshell_pb2.WORKSPACE_ROLE_ADMIN - ) - try: - # Workspace Admin cannot assign Admin role - with pytest.raises(grpc.RpcError) as exc_info: - user_stub.AddWorkspaceMember( - openshell_pb2.AddWorkspaceMemberRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - principal_subject="another-subject", - role=openshell_pb2.WORKSPACE_ROLE_ADMIN, - ), - metadata=user_md, - ) - assert exc_info.value.code() == grpc.StatusCode.PERMISSION_DENIED - - # But User role assignment succeeds - user_stub.AddWorkspaceMember( - openshell_pb2.AddWorkspaceMemberRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=WS), - principal_subject="another-subject", - role=openshell_pb2.WORKSPACE_ROLE_USER, - ), - metadata=user_md, - ) - _remove_member(admin_stub, admin_md, WS, "another-subject") - finally: - _remove_member(admin_stub, admin_md, WS, user_sub) - - # ── Test 12: ListWorkspaces filtered by membership ─────────────── - - def test_list_workspaces_filtered_by_membership( - self, - admin_ctx: Any, - user_ctx: Any, - ) -> None: - admin_stub, admin_md = admin_ctx - user_stub, user_md, user_sub = user_ctx - - ws2 = "e2e-authz-test-2" - with contextlib.suppress(grpc.RpcError): - admin_stub.CreateWorkspace( - openshell_pb2.CreateWorkspaceRequest(name=ws2), - metadata=admin_md, - ) - - _add_member( - admin_stub, admin_md, WS, user_sub, openshell_pb2.WORKSPACE_ROLE_USER - ) - try: - resp = user_stub.ListWorkspaces( - openshell_pb2.ListWorkspacesRequest(), - metadata=user_md, - ) - ws_names = [w.metadata.name for w in resp.workspaces] - assert WS in ws_names, f"member should see {WS}" - assert ws2 not in ws_names, f"non-member should not see {ws2}" - assert "default" not in ws_names, "non-member should not see default" - finally: - _remove_member(admin_stub, admin_md, WS, user_sub) - with contextlib.suppress(grpc.RpcError): - admin_stub.DeleteWorkspace( - openshell_pb2.DeleteWorkspaceRequest(name=ws2), - metadata=admin_md, - ) - - # ── Test 13: Platform provider profiles require Platform Admin ─── - - @pytest.mark.parametrize( - "rpc_name,call", - _platform_profile_rpcs(), - ids=[r[0] for r in _platform_profile_rpcs()], - ) - def test_platform_provider_profile_operations_require_platform_admin( - self, - rpc_name: str, - call: Callable, - user_ctx: Any, - ) -> None: - stub, metadata, _ = user_ctx - - with pytest.raises(grpc.RpcError) as exc_info: - call(stub, metadata) - - assert exc_info.value.code() == grpc.StatusCode.PERMISSION_DENIED, ( - f"{rpc_name}: expected PERMISSION_DENIED, got {exc_info.value.code()}" - ) - assert "platform admin role required" in exc_info.value.details(), ( - f"{rpc_name}: denial came from the wrong authorization layer: " - f"{exc_info.value.details()}" - ) - - @pytest.mark.parametrize( - "rpc_name,call", - _platform_profile_rpcs(), - ids=[r[0] for r in _platform_profile_rpcs()], - ) - def test_platform_admin_can_access_platform_provider_profile_operations( - self, - rpc_name: str, - call: Callable, - admin_ctx: Any, - ) -> None: - stub, metadata = admin_ctx - - try: - call(stub, metadata) - except grpc.RpcError as error: - assert error.code() != grpc.StatusCode.PERMISSION_DENIED, ( - f"{rpc_name}: Platform Admin was denied: {error.details()}" - ) - - # ── Test 14: Global policy reads require Platform Admin ────────── - - @pytest.mark.parametrize( - "rpc_name,call", - _global_policy_read_rpcs(), - ids=[r[0] for r in _global_policy_read_rpcs()], - ) - def test_global_policy_reads_require_platform_admin( - self, - rpc_name: str, - call: Callable, - user_ctx: Any, - ) -> None: - stub, metadata, _ = user_ctx - - with pytest.raises(grpc.RpcError) as exc_info: - call(stub, metadata) - - assert exc_info.value.code() == grpc.StatusCode.PERMISSION_DENIED, ( - f"{rpc_name}: expected PERMISSION_DENIED, got {exc_info.value.code()}" - ) - assert "platform admin role required" in exc_info.value.details(), ( - f"{rpc_name}: denial came from the wrong authorization layer: " - f"{exc_info.value.details()}" - ) - - @pytest.mark.parametrize( - "rpc_name,call", - _global_policy_read_rpcs(), - ids=[r[0] for r in _global_policy_read_rpcs()], - ) - def test_platform_admin_can_access_global_policy_reads( - self, - rpc_name: str, - call: Callable, - admin_ctx: Any, - ) -> None: - stub, metadata = admin_ctx - - try: - call(stub, metadata) - except grpc.RpcError as error: - assert error.code() != grpc.StatusCode.PERMISSION_DENIED, ( - f"{rpc_name}: Platform Admin was denied: {error.details()}" - ) diff --git a/e2e/python/test_exec_admission.py b/e2e/python/test_exec_admission.py deleted file mode 100644 index dd5c8a9b72..0000000000 --- a/e2e/python/test_exec_admission.py +++ /dev/null @@ -1,157 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -from __future__ import annotations - -import threading -import time -import uuid -from typing import TYPE_CHECKING - -import grpc -import pytest -from google.protobuf import duration_pb2 - -from openshell._proto import datamodel_pb2, openshell_pb2 -from openshell.errors import from_grpc_error - -if TYPE_CHECKING: - from collections.abc import Callable - - from openshell import Sandbox, SandboxClient - - -def assert_blocked(stream, reason: str) -> None: - with pytest.raises(grpc.RpcError) as caught: - list(stream) - error = from_grpc_error(caught.value) - assert error.code() == grpc.StatusCode.FAILED_PRECONDITION - assert error.error_info is not None - assert error.error_info.reason == reason - - -@pytest.mark.parametrize("interactive", [False, True]) -def test_exec_request_id_never_relaunches_or_replays_output( - sandbox: Callable[..., Sandbox], sandbox_client: SandboxClient, interactive: bool -) -> None: - with sandbox(delete_on_exit=True) as sb: - path = f"/sandbox/exec-admission-{uuid.uuid4().hex}" - request = openshell_pb2.ExecSandboxRequest( - sandbox=sb.sandbox.name, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"), - command=[ - "/bin/sh", - "-c", - f"printf x >> {path}; printf started; " - f"while [ ! -e {path}.release ]; do sleep 0.1; done; " - "printf finished; exit 7", - ], - request_id=str(uuid.uuid4()), - ) - done = threading.Event() - - def invoke(value): - if not interactive: - return sandbox_client._stub.ExecSandbox(value, timeout=30) - - def inputs(): - yield openshell_pb2.ExecSandboxInput(start=value) - done.wait(timeout=30) - - return sandbox_client._stub.ExecSandboxInteractive(inputs(), timeout=30) - - stream = invoke(request) - try: - first = next(stream) - assert first.WhichOneof("payload") == "stdout" - assert b"started" in first.stdout.data - assert_blocked(invoke(request), "REQUEST_OUTCOME_UNCERTAIN") - changed = openshell_pb2.ExecSandboxRequest() - changed.CopyFrom(request) - changed.command.append("changed-payload") - assert_blocked(invoke(changed), "REQUEST_ID_PAYLOAD_MISMATCH") - assert sb.exec(["touch", f"{path}.release"]).exit_code == 0 - events = list(stream) - assert events[-1].WhichOneof("payload") == "exit" - assert events[-1].exit.exit_code == 7 - assert_blocked(invoke(request), "REQUEST_STREAM_UNAVAILABLE") - result = sb.exec(["cat", path]) - assert result.exit_code == 0 - assert result.stdout == "x" - finally: - done.set() - stream.cancel() - - -@pytest.mark.parametrize("interactive", [False, True]) -def test_exec_request_timeout_keeps_launch_unresolved( - sandbox: Callable[..., Sandbox], sandbox_client: SandboxClient, interactive: bool -) -> None: - with sandbox(delete_on_exit=True) as sb: - path = f"/sandbox/exec-timeout-{uuid.uuid4().hex}" - request = openshell_pb2.ExecSandboxRequest( - sandbox=sb.sandbox.name, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"), - command=["/bin/sh", "-c", f"printf x >> {path}; sleep 5"], - execution_timeout=duration_pb2.Duration(seconds=1), - request_id=str(uuid.uuid4()), - ) - done = threading.Event() - - def invoke(): - if not interactive: - return sandbox_client._stub.ExecSandbox(request, timeout=30) - - def inputs(): - yield openshell_pb2.ExecSandboxInput(start=request) - done.wait(timeout=30) - - return sandbox_client._stub.ExecSandboxInteractive(inputs(), timeout=30) - - stream = invoke() - try: - events = [] - for event in stream: - events.append(event) - if event.WhichOneof("payload") == "exit": - # A synthetic timeout leaves the interactive input task - # alive until the client closes its side of the stream. - done.set() - assert events[-1].exit.exit_code == 124 - assert_blocked(invoke(), "REQUEST_OUTCOME_UNCERTAIN") - assert sb.exec(["cat", path]).stdout == "x" - finally: - done.set() - stream.cancel() - - -def test_exec_client_cancellation_does_not_clear_launch_admission( - sandbox: Callable[..., Sandbox], sandbox_client: SandboxClient -) -> None: - with sandbox(delete_on_exit=True) as sb: - path = f"/sandbox/exec-cancel-{uuid.uuid4().hex}" - request = openshell_pb2.ExecSandboxRequest( - sandbox=sb.sandbox.name, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"), - command=[ - "/bin/sh", - "-c", - f"printf x >> {path}; printf started; sleep 1; printf finished", - ], - request_id=str(uuid.uuid4()), - ) - stream = sandbox_client._stub.ExecSandbox(request, timeout=30) - assert b"started" in next(stream).stdout.data - stream.cancel() - deadline = time.monotonic() + 15 - while True: - with pytest.raises(grpc.RpcError) as caught: - list(sandbox_client._stub.ExecSandbox(request, timeout=30)) - error = from_grpc_error(caught.value) - assert error.error_info is not None - if error.error_info.reason == "REQUEST_STREAM_UNAVAILABLE": - break - assert error.error_info.reason == "REQUEST_OUTCOME_UNCERTAIN" - assert time.monotonic() < deadline - time.sleep(0.1) - assert sb.exec(["cat", path]).stdout == "x" diff --git a/e2e/python/test_policy_validation.py b/e2e/python/test_policy_validation.py deleted file mode 100644 index 7a117d27ff..0000000000 --- a/e2e/python/test_policy_validation.py +++ /dev/null @@ -1,243 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -"""E2E tests for server-side policy safety validation. - -These tests verify that the gRPC server rejects sandbox creation and policy -updates that contain unsafe content (root process identity, path traversal, -overly broad filesystem paths). -""" - -from __future__ import annotations - -import contextlib -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 - - from openshell import Sandbox, SandboxClient - - -# ============================================================================= -# Policy helpers -# ============================================================================= - -_SAFE_FILESYSTEM = sandbox_pb2.FilesystemPolicy( - include_workdir=True, - read_only=["/usr", "/lib", "/etc", "/app", "/var/log"], - read_write=["/sandbox", "/tmp"], -) -_SAFE_LANDLOCK = sandbox_pb2.LandlockPolicy(compatibility="best_effort") -_SAFE_PROCESS = sandbox_pb2.ProcessPolicy(run_as_user="sandbox", run_as_group="sandbox") - - -def _safe_policy() -> sandbox_pb2.SandboxPolicy: - """Build a safe baseline policy for testing.""" - return sandbox_pb2.SandboxPolicy( - version=1, - filesystem=_SAFE_FILESYSTEM, - landlock=_SAFE_LANDLOCK, - process=_SAFE_PROCESS, - ) - - -# ============================================================================= -# Tests -# ============================================================================= - - -def test_create_sandbox_rejects_root_user( - sandbox_client: SandboxClient, -) -> None: - """Server rejects CreateSandbox with run_as_user='root'.""" - policy = sandbox_pb2.SandboxPolicy( - version=1, - filesystem=_SAFE_FILESYSTEM, - landlock=_SAFE_LANDLOCK, - process=sandbox_pb2.ProcessPolicy( - run_as_user="root", - run_as_group="sandbox", - ), - ) - spec = datamodel_pb2.SandboxSpec(policy=policy) - - stub = sandbox_client._stub - with pytest.raises(grpc.RpcError) as exc_info: - stub.CreateSandbox( - openshell_pb2.CreateSandboxRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"), - name="", - spec=spec, - ) - ) - - assert exc_info.value.code() == grpc.StatusCode.INVALID_ARGUMENT - assert "root" in exc_info.value.details().lower() - - -def test_create_sandbox_rejects_path_traversal( - sandbox_client: SandboxClient, -) -> None: - """Server rejects CreateSandbox with '..' in filesystem paths.""" - policy = sandbox_pb2.SandboxPolicy( - version=1, - filesystem=sandbox_pb2.FilesystemPolicy( - include_workdir=True, - read_only=["/usr/../etc/shadow"], - read_write=["/tmp"], - ), - landlock=_SAFE_LANDLOCK, - process=_SAFE_PROCESS, - ) - spec = datamodel_pb2.SandboxSpec(policy=policy) - - stub = sandbox_client._stub - with pytest.raises(grpc.RpcError) as exc_info: - stub.CreateSandbox( - openshell_pb2.CreateSandboxRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"), - name="", - spec=spec, - ) - ) - - assert exc_info.value.code() == grpc.StatusCode.INVALID_ARGUMENT - assert "traversal" in exc_info.value.details().lower() - - -def test_create_sandbox_rejects_overly_broad_paths( - sandbox_client: SandboxClient, -) -> None: - """Server rejects CreateSandbox with read_write=['/'].""" - policy = sandbox_pb2.SandboxPolicy( - version=1, - filesystem=sandbox_pb2.FilesystemPolicy( - include_workdir=True, - read_only=["/usr"], - read_write=["/"], - ), - landlock=_SAFE_LANDLOCK, - process=_SAFE_PROCESS, - ) - spec = datamodel_pb2.SandboxSpec(policy=policy) - - stub = sandbox_client._stub - with pytest.raises(grpc.RpcError) as exc_info: - stub.CreateSandbox( - openshell_pb2.CreateSandboxRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"), - name="", - spec=spec, - ) - ) - - assert exc_info.value.code() == grpc.StatusCode.INVALID_ARGUMENT - assert "broad" in exc_info.value.details().lower() - - -def test_create_sandbox_materializes_default_mcp_version( - sandbox_client: SandboxClient, -) -> None: - """An omitted MCP options stanza is stored with the pinned default version.""" - policy = _safe_policy() - policy.network_policies["mcp_default"].CopyFrom( - sandbox_pb2.NetworkPolicyRule( - name="mcp_default", - endpoints=[ - sandbox_pb2.NetworkEndpoint( - host="mcp.example.com", - port=443, - protocol="mcp", - rules=[ - sandbox_pb2.L7Rule( - allow=sandbox_pb2.L7Allow(method="initialize") - ) - ], - ) - ], - ) - ) - assert not policy.network_policies["mcp_default"].endpoints[0].HasField("mcp") - spec = datamodel_pb2.SandboxSpec(policy=policy) - - created = sandbox_client.create(workspace="default", spec=spec) - try: - stored = sandbox_client._stub.GetSandbox( - openshell_pb2.GetSandboxRequest( - name=created.name, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"), - ) - ) - stored_endpoint = stored.sandbox.spec.policy.network_policies[ - "mcp_default" - ].endpoints[0] - - assert stored_endpoint.HasField("mcp") - assert list(stored_endpoint.mcp.versions) == ["2025-11-25"] - - config = sandbox_client._stub.GetSandboxConfig( - sandbox_pb2.GetSandboxConfigRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"), - name=created.name, - ) - ) - endpoint = config.policy.network_policies["mcp_default"].endpoints[0] - - assert endpoint.HasField("mcp") - assert list(endpoint.mcp.versions) == ["2025-11-25"] - finally: - # Creation can complete before the compute driver reaches Ready, so - # cleanup targets the gateway resource directly by its canonical name. - with contextlib.suppress(grpc.RpcError): - sandbox_client.delete(created.name, workspace="default") - - -def test_update_policy_rejects_immutable_fields( - sandbox: Callable[..., Sandbox], - sandbox_client: SandboxClient, -) -> None: - """UpdateConfig rejects removal of filesystem paths on a live sandbox. - - Filesystem paths are enforced by Landlock at sandbox startup and cannot be - removed after the fact. This test verifies that the server rejects updates - that remove existing read_only paths, which also prevents unsafe content - from being introduced via policy updates. - """ - safe_policy = _safe_policy() - spec = datamodel_pb2.SandboxSpec(policy=safe_policy) - - with sandbox(spec=spec, delete_on_exit=True) as sb: - sandbox_name = sb.sandbox.name - stub = sandbox_client._stub - - # Try to update with a modified filesystem policy (immutable field) - unsafe_policy = sandbox_pb2.SandboxPolicy( - version=1, - filesystem=sandbox_pb2.FilesystemPolicy( - include_workdir=True, - read_only=["/usr/../etc/shadow"], - read_write=["/tmp"], - ), - landlock=_SAFE_LANDLOCK, - process=_SAFE_PROCESS, - ) - - with pytest.raises(grpc.RpcError) as exc_info: - stub.UpdateConfig( - openshell_pb2.UpdateConfigRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector( - workspace="default" - ), - sandbox=sandbox_name, - policy=unsafe_policy, - ) - ) - - assert exc_info.value.code() == grpc.StatusCode.INVALID_ARGUMENT - assert "cannot be removed" in exc_info.value.details().lower() diff --git a/e2e/python/test_sandbox_api.py b/e2e/python/test_sandbox_api.py deleted file mode 100644 index 5544981e3f..0000000000 --- a/e2e/python/test_sandbox_api.py +++ /dev/null @@ -1,385 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -from __future__ import annotations - -import contextlib -import threading -import uuid -from typing import TYPE_CHECKING - -from google.protobuf import duration_pb2 - -from openshell._proto import datamodel_pb2, openshell_pb2, sandbox_pb2 - -if TYPE_CHECKING: - from collections.abc import Callable - - from openshell import Sandbox, SandboxClient, WorkspaceClient - - -def test_mutation_replay_preserves_sandbox_lifecycle_and_replacement( - sandbox_client: SandboxClient, -) -> None: - name = f"replay-{uuid.uuid4().hex[:8]}" - scope = "default" - stub = sandbox_client._stub - create = openshell_pb2.CreateSandboxRequest( - name=name, - spec=openshell_pb2.SandboxSpec(), - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=scope), - request_id=str(uuid.uuid4()), - ) - - def replay(method, request): - result, call = method.with_call(request, timeout=60) - assert dict(call.initial_metadata())["openshell-replayed"] == "true" - return result - - try: - original = stub.CreateSandbox(create, timeout=60).sandbox.metadata.id - sandbox_client.wait_ready(name, workspace="default", timeout_seconds=300) - assert replay(stub.CreateSandbox, create).sandbox.metadata.id == original - stop = openshell_pb2.StopSandboxRequest( - name=name, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=scope), - request_id=str(uuid.uuid4()), - ) - stub.StopSandbox(stop, timeout=60) - sandbox_client.wait_stopped(name, workspace="default", timeout_seconds=120) - assert replay(stub.StopSandbox, stop).sandbox.metadata.id == original - start = openshell_pb2.StartSandboxRequest( - name=name, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=scope), - request_id=str(uuid.uuid4()), - ) - stub.StartSandbox(start, timeout=60) - sandbox_client.wait_ready(name, workspace="default", timeout_seconds=300) - assert replay(stub.StartSandbox, start).sandbox.metadata.id == original - update = openshell_pb2.UpdateConfigRequest( - sandbox=name, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=scope), - setting_key="ocsf_json_enabled", - setting_value=sandbox_pb2.SettingValue(bool_value=True), - request_id=str(uuid.uuid4()), - ) - updated = stub.UpdateConfig(update, timeout=30) - assert replay(stub.UpdateConfig, update) == updated - delete = openshell_pb2.DeleteSandboxRequest( - name=name, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=scope), - request_id=str(uuid.uuid4()), - ) - deleted = stub.DeleteSandbox(delete, timeout=60) - assert deleted.sandbox_id == original - sandbox_client.wait_deleted(name, workspace="default", timeout_seconds=120) - replacement = sandbox_client.create(workspace="default", name=name) - assert replacement.id != original - assert replay(stub.DeleteSandbox, delete) == deleted - assert sandbox_client.get(name, workspace="default").id == replacement.id - finally: - with contextlib.suppress(Exception): - sandbox_client.delete(name, workspace="default", allow_missing=True) - sandbox_client.wait_deleted(name, workspace="default", timeout_seconds=120) - - -def test_sandbox_api_crud_and_exec( - sandbox: Callable[..., Sandbox], - sandbox_client: SandboxClient, -) -> None: - class _FileOps: - def write(self, path: str, content: str) -> None: - from pathlib import Path - - Path(path).write_text(content) - - def read(self, path: str) -> str: - from pathlib import Path - - return Path(path).read_text() - - with sandbox(delete_on_exit=True) as sb: - assert sb.id - # Server auto-generates a petname (e.g. "feasible-retriever") - assert sb.sandbox.name - parts = sb.sandbox.name.split("-") - assert len(parts) == 2, ( - f"expected petname with 2 parts, got {sb.sandbox.name!r}" - ) - assert all(p.isalpha() and p.islower() for p in parts) - - fetched = sandbox_client.get(sb.sandbox.name, workspace="default") - assert fetched.id == sb.id - - ids = set(sandbox_client.list_ids(workspace="default", page_size=100)) - assert sb.id in ids - - result = sb.exec(["python", "-c", "print('sandbox-ok')"]) - assert result.exit_code == 0 - assert "sandbox-ok" in result.stdout - - file_ops = _FileOps() - create_file = sb.exec_python( - file_ops.write, - args=("/sandbox/exec-persistence.txt", "ok"), - ) - assert create_file.exit_code == 0 - - verify_file = sb.exec_python( - file_ops.read, args=("/sandbox/exec-persistence.txt",) - ) - assert verify_file.exit_code == 0 - assert verify_file.stdout.strip() == "ok" - - -def test_sandbox_interactive_exec_honors_tty( - sandbox: Callable[..., Sandbox], - sandbox_client: SandboxClient, -) -> None: - ready_marker = b"tty-ready\n" - stdin_sentinel = b"streamed-stdin-sentinel" - stdout_sentinel = b"stdout-sentinel" - stderr_sentinel = b"stderr-sentinel" - - def exec_interactive(sandbox_name: str, *, tty: bool) -> tuple[bytes, bytes]: - request = openshell_pb2.ExecSandboxInput( - start=openshell_pb2.ExecSandboxRequest( - sandbox=sandbox_name, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"), - command=[ - "/bin/sh", - "-c", - "[ -t 0 ] && printf T || printf N; " - "[ -t 1 ] && printf T || printf N; " - "[ -t 2 ] && printf T || printf N; " - f"printf '\\n%s' '{ready_marker.decode()}'; " - "IFS= read -r stdin_value; " - "printf 'stdin:%s\\n' \"$stdin_value\"; " - "printf 'stdout-sentinel\\n'; " - "printf 'stderr-sentinel\\n' >&2", - ], - tty=tty, - execution_timeout=duration_pb2.Duration(seconds=20), - ) - ) - - ready = threading.Event() - done = threading.Event() - - def requests(): - yield request - # PTY input echo can split the separate TTY flag writes. Wait for - # the complete marker, including its newline, before sending input. - if not ready.wait(timeout=20) or done.is_set(): - return - yield openshell_pb2.ExecSandboxInput(stdin=stdin_sentinel + b"\n") - done.wait(timeout=30) - - stdout = bytearray() - stderr = bytearray() - exit_code: int | None = None - try: - events = sandbox_client._stub.ExecSandboxInteractive(requests(), timeout=30) - for event in events: - payload = event.WhichOneof("payload") - if payload == "stdout": - stdout.extend(event.stdout.data) - if ready_marker in stdout.replace(b"\r\n", b"\n"): - ready.set() - elif payload == "stderr": - stderr.extend(event.stderr.data) - elif payload == "exit": - exit_code = int(event.exit.exit_code) - finally: - done.set() - # Unblock a request iterator waiting for readiness on early exit - # or RPC failure without sending input after the call has ended. - ready.set() - - assert exit_code == 0 - return bytes(stdout), bytes(stderr) - - with sandbox(delete_on_exit=True) as sb: - stdout, stderr = exec_interactive(sb.sandbox.name, tty=False) - assert b"NNN" in stdout.splitlines() - assert b"stdin:" + stdin_sentinel in stdout - assert stdout_sentinel in stdout - assert stdout_sentinel not in stderr - assert stderr_sentinel in stderr - assert stderr_sentinel not in stdout - - stdout, stderr = exec_interactive(sb.sandbox.name, tty=True) - terminal_output = stdout + stderr - assert b"TTT" in terminal_output.splitlines() - assert b"stdin:" + stdin_sentinel in terminal_output - assert stdout_sentinel in terminal_output - assert stderr_sentinel in terminal_output - - -def test_interactive_exec_drains_output_after_request_eof( - sandbox: Callable[..., Sandbox], - sandbox_client: SandboxClient, -) -> None: - with sandbox(delete_on_exit=True) as sb: - - def requests(): - yield openshell_pb2.ExecSandboxInput( - start=openshell_pb2.ExecSandboxRequest( - sandbox=sb.sandbox.name, - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"), - command=[ - "/bin/sh", - "-c", - "input=$(cat); sleep 0.2; " - "printf '%s' \"$input\"; printf 'drained-stderr' >&2; exit 7", - ], - tty=False, - execution_timeout=duration_pb2.Duration(seconds=20), - ) - ) - yield openshell_pb2.ExecSandboxInput(stdin=b"drained-stdout") - # End requests before the command emits output. The receive - # direction must survive long enough to drain both output streams. - - stdout = bytearray() - stderr = bytearray() - exit_codes: list[int] = [] - for event in sandbox_client._stub.ExecSandboxInteractive( - requests(), timeout=30 - ): - assert not exit_codes, "received another event after terminal exit" - payload = event.WhichOneof("payload") - if payload == "stdout": - stdout.extend(event.stdout.data) - elif payload == "stderr": - stderr.extend(event.stderr.data) - elif payload == "exit": - exit_codes.append(event.exit.exit_code) - assert stdout == b"drained-stdout" - assert stderr == b"drained-stderr" - assert exit_codes == [7] - - -def test_list_scoped_and_for_all_workspaces( - sandbox_client: SandboxClient, - workspace_client: WorkspaceClient, -) -> None: - import contextlib - import uuid - - suffix = uuid.uuid4().hex[:8] - other_ws = f"list-ws-{suffix}" - created_default: list[str] = [] - created_other: list[str] = [] - - try: - workspace_client.create(other_ws) - - ref_default = sandbox_client.create( - workspace="default", name=f"ls-def-{suffix}" - ) - created_default.append(ref_default.name) - - ref_other = sandbox_client.create(workspace=other_ws, name=f"ls-oth-{suffix}") - created_other.append(ref_other.name) - - default_ids = set(sandbox_client.list_ids(workspace="default")) - assert ref_default.id in default_ids - assert ref_other.id not in default_ids - - other_ids = set(sandbox_client.list_ids(workspace=other_ws)) - assert ref_other.id in other_ids - assert ref_default.id not in other_ids - - all_ids = set(sandbox_client.list_ids_for_all_workspaces()) - assert ref_default.id in all_ids - assert ref_other.id in all_ids - finally: - for name in created_default: - with contextlib.suppress(Exception): - sandbox_client.delete(name, workspace="default") - sandbox_client.wait_deleted(name, workspace="default") - for name in created_other: - with contextlib.suppress(Exception): - sandbox_client.delete(name, workspace=other_ws) - sandbox_client.wait_deleted(name, workspace=other_ws) - with contextlib.suppress(Exception): - workspace_client.delete(other_ws) - - -def test_sandbox_labels_and_selectors(sandbox_client: SandboxClient) -> None: - import contextlib - import uuid - - suffix = uuid.uuid4().hex[:8] - job_a = f"lbl-a-{suffix}" - job_b = f"lbl-b-{suffix}" - group_selector = f"aiq-test={suffix}" - primary_selector = f"aiq-test={suffix},role=primary" - - created: list[str] = [] - try: - ref_a = sandbox_client.create( - workspace="default", - name=job_a, - labels={"aiq-test": suffix, "role": "primary"}, - ) - created.append(ref_a.name) - ref_b = sandbox_client.create( - workspace="default", - name=job_b, - labels={"aiq-test": suffix, "role": "secondary"}, - ) - created.append(ref_b.name) - - # Labels round-trip through create and get. - assert ref_a.labels["role"] == "primary" - assert ( - dict(sandbox_client.get(job_a, workspace="default").labels)["role"] - == "primary" - ) - assert ( - dict(sandbox_client.get(job_b, workspace="default").labels)["role"] - == "secondary" - ) - - # A specific selector filters to exactly the primary sandbox. - assert { - s.name - for s in sandbox_client.list_all( - workspace="default", label_selector=primary_selector - ) - } == {job_a} - # The shared group label returns both. - assert { - s.name - for s in sandbox_client.list_all( - workspace="default", label_selector=group_selector - ) - } == { - job_a, - job_b, - } - - # Deleting one removes only it from selector results. - assert sandbox_client.delete(job_a, workspace="default") - sandbox_client.wait_deleted(job_a, workspace="default") - created.remove(job_a) - assert { - s.name - for s in sandbox_client.list_all( - workspace="default", label_selector=group_selector - ) - } == {job_b} - - # Final deletion leaves no matching sandboxes. - assert sandbox_client.delete(job_b, workspace="default") - sandbox_client.wait_deleted(job_b, workspace="default") - created.remove(job_b) - assert not sandbox_client.list_all( - workspace="default", label_selector=group_selector - ) - finally: - for name in created: - with contextlib.suppress(Exception): - sandbox_client.delete(name, workspace="default") diff --git a/e2e/python/test_sandbox_exec_python.py b/e2e/python/test_sandbox_exec_python.py deleted file mode 100644 index 4c61d7d333..0000000000 --- a/e2e/python/test_sandbox_exec_python.py +++ /dev/null @@ -1,42 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -from __future__ import annotations - -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - from collections.abc import Callable - - from openshell import Sandbox - - -def test_exec_python_accepts_bound_methods( - sandbox: Callable[..., Sandbox], -) -> None: - class _Calculator: - def multiply(self, left: int, right: int) -> int: - return left * right - - with sandbox(delete_on_exit=True) as sb: - calculator = _Calculator() - result = sb.exec_python(calculator.multiply, args=(6, 7), timeout_seconds=20) - - assert result.exit_code == 0 - assert result.stdout.strip() == "42" - - -def test_exec_python_surfaces_callable_errors( - sandbox: Callable[..., Sandbox], -) -> None: - class _Boom: - def fail(self) -> None: - raise RuntimeError("expected-failure") - - with sandbox(delete_on_exit=True) as sb: - result = sb.exec_python(_Boom().fail, timeout_seconds=20) - - assert result.exit_code != 0 - output = f"{result.stdout}\n{result.stderr}" - assert "expected-failure" in output - assert "RuntimeError" in output diff --git a/e2e/python/test_sandbox_landlock.py b/e2e/python/test_sandbox_landlock.py deleted file mode 100644 index d0c63fc2c4..0000000000 --- a/e2e/python/test_sandbox_landlock.py +++ /dev/null @@ -1,207 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -"""Tests for Landlock filesystem sandboxing. - -Verifies that: -- Landlock availability is logged via OCSF in sandbox logs -- Read-only paths block writes but allow reads -- Read-write paths allow both reads and writes -- Paths outside the policy are blocked entirely -- Paths the sandbox user owns but are not in the policy are still blocked -- best_effort mode skips inaccessible paths without crashing - -These tests require a Linux host with Landlock support (kernel 5.13+). -GitHub Actions Linux runners satisfy this requirement. Docker Desktop -linuxkit kernels also support Landlock (ABI v5+). - -Related: https://github.com/NVIDIA/OpenShell/issues/803 -""" - -from __future__ import annotations - -from typing import TYPE_CHECKING - -from openshell._proto import datamodel_pb2, sandbox_pb2 - -if TYPE_CHECKING: - from collections.abc import Callable - - from openshell import Sandbox - - -# ============================================================================= -# Policy helpers -# ============================================================================= - -_LANDLOCK_FILESYSTEM = sandbox_pb2.FilesystemPolicy( - include_workdir=True, - read_only=["/usr", "/lib", "/etc", "/proc", "/dev/urandom"], - read_write=["/sandbox", "/tmp"], -) -_LANDLOCK_BEST_EFFORT = sandbox_pb2.LandlockPolicy(compatibility="best_effort") -_LANDLOCK_PROCESS = sandbox_pb2.ProcessPolicy( - run_as_user="sandbox", run_as_group="sandbox" -) - - -def _landlock_policy( - *, - filesystem: sandbox_pb2.FilesystemPolicy | None = None, - landlock: sandbox_pb2.LandlockPolicy | None = None, -) -> sandbox_pb2.SandboxPolicy: - return sandbox_pb2.SandboxPolicy( - version=1, - filesystem=filesystem or _LANDLOCK_FILESYSTEM, - landlock=landlock or _LANDLOCK_BEST_EFFORT, - process=_LANDLOCK_PROCESS, - network_policies={}, - ) - - -# ============================================================================= -# Closures for exec_python (serialized into the sandbox by cloudpickle) -# ============================================================================= - - -def _try_write(): - """Return a closure that attempts to write a file and returns the result.""" - - def fn(path): - import os - - try: - with open(os.path.join(path, ".landlock-test"), "w") as f: - f.write("test") - return "OK" - except PermissionError: - return "EPERM" - except OSError as e: - return f"ERROR:{e.errno}" - - return fn - - -def _try_read(): - """Return a closure that attempts to read a directory listing.""" - - def fn(path): - import os - - try: - entries = os.listdir(path) - return f"OK:{len(entries)}" - except PermissionError: - return "EPERM" - except OSError as e: - return f"ERROR:{e.errno}" - - return fn - - -def _check_user_owns_path(): - """Return a closure that checks if the current user owns a path.""" - - def fn(path): - import os - - try: - st = os.stat(path) - uid = os.getuid() - return f"owner:{st.st_uid} me:{uid} match:{st.st_uid == uid}" - except OSError as e: - return f"ERROR:{e}" - - return fn - - -# ============================================================================= -# Landlock enforcement tests -# ============================================================================= - - -def test_landlock_blocks_write_to_read_only_path( - sandbox: Callable[..., Sandbox], -) -> None: - """Writes to read-only paths (/usr) are blocked by Landlock.""" - spec = datamodel_pb2.SandboxSpec(policy=_landlock_policy()) - with sandbox(spec=spec, delete_on_exit=True) as sb: - result = sb.exec_python(_try_write(), args=("/usr",)) - assert result.exit_code == 0, result.stderr - assert result.stdout.strip() == "EPERM", ( - f"Expected write to /usr to be denied, got: {result.stdout.strip()}" - ) - - -def test_landlock_allows_write_to_read_write_path( - sandbox: Callable[..., Sandbox], -) -> None: - """Writes to read-write paths (/tmp, /sandbox) are allowed.""" - spec = datamodel_pb2.SandboxSpec(policy=_landlock_policy()) - with sandbox(spec=spec, delete_on_exit=True) as sb: - for path in ["/tmp", "/sandbox"]: - result = sb.exec_python(_try_write(), args=(path,)) - assert result.exit_code == 0, result.stderr - assert result.stdout.strip() == "OK", ( - f"Expected write to {path} to succeed, got: {result.stdout.strip()}" - ) - - -def test_landlock_allows_read_on_read_only_path( - sandbox: Callable[..., Sandbox], -) -> None: - """Reads from read-only paths (/usr, /etc) are allowed.""" - spec = datamodel_pb2.SandboxSpec(policy=_landlock_policy()) - with sandbox(spec=spec, delete_on_exit=True) as sb: - for path in ["/usr", "/etc"]: - result = sb.exec_python(_try_read(), args=(path,)) - assert result.exit_code == 0, result.stderr - assert result.stdout.strip().startswith("OK:"), ( - f"Expected read from {path} to succeed, got: {result.stdout.strip()}" - ) - - -def test_landlock_blocks_access_outside_policy( - sandbox: Callable[..., Sandbox], -) -> None: - """Paths not listed in the policy (/opt, /root) are blocked entirely. - - When Landlock is enforced, any path not covered by a rule is denied - by default. This is the fundamental allowlist property. - """ - spec = datamodel_pb2.SandboxSpec(policy=_landlock_policy()) - with sandbox(spec=spec, delete_on_exit=True) as sb: - for path in ["/opt", "/root"]: - result = sb.exec_python(_try_read(), args=(path,)) - assert result.exit_code == 0, result.stderr - assert ( - "EPERM" in result.stdout.strip() or "ERROR:" in result.stdout.strip() - ), ( - f"Expected access to {path} (outside policy) to be denied, " - f"got: {result.stdout.strip()}" - ) - - -def test_landlock_blocks_user_owned_path_outside_policy( - sandbox: Callable[..., Sandbox], -) -> None: - """Landlock blocks access to /home/sandbox even though the sandbox user owns it. - - This is the key distinction between Landlock and Unix DAC permissions: - the sandbox user has filesystem ownership of /home/sandbox, but because - /home is not in the Landlock policy, access is denied. This confirms - Landlock is enforcing independently of Unix permissions. - """ - spec = datamodel_pb2.SandboxSpec(policy=_landlock_policy()) - with sandbox(spec=spec, delete_on_exit=True) as sb: - # Verify the sandbox user owns /home/sandbox - own_result = sb.exec_python(_check_user_owns_path(), args=("/home/sandbox",)) - # The path might not exist in all images, so only assert Landlock - # enforcement if the path is present and owned by us. - if own_result.exit_code == 0 and "match:True" in own_result.stdout: - write_result = sb.exec_python(_try_write(), args=("/home/sandbox",)) - assert write_result.exit_code == 0, write_result.stderr - assert write_result.stdout.strip() == "EPERM", ( - "Expected Landlock to block write to /home/sandbox despite user ownership. " - f"Got: {write_result.stdout.strip()}" - ) diff --git a/e2e/python/test_sandbox_policy.py b/e2e/python/test_sandbox_policy.py deleted file mode 100644 index 80d9963e02..0000000000 --- a/e2e/python/test_sandbox_policy.py +++ /dev/null @@ -1,183 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -"""Python SDK policy integration tests. - -The Rust E2E suites cover the complete transparent mediation pipeline. These -SDK-level checks retain the most important policy denials through normal -workload sockets, without relying on a workload-visible proxy endpoint. -""" - -from __future__ import annotations - -from typing import TYPE_CHECKING - -import grpc -import pytest - -from openshell._proto import datamodel_pb2, sandbox_pb2 - -if TYPE_CHECKING: - from collections.abc import Callable - - from openshell import Sandbox - - -_BASE_FILESYSTEM = sandbox_pb2.FilesystemPolicy( - include_workdir=True, - read_only=["/usr", "/lib", "/etc", "/app", "/var/log", "/proc", "/dev/urandom"], - read_write=["/sandbox", "/tmp"], -) -_BASE_LANDLOCK = sandbox_pb2.LandlockPolicy(compatibility="best_effort") -_BASE_PROCESS = sandbox_pb2.ProcessPolicy(run_as_user="sandbox", run_as_group="sandbox") - - -def _base_policy( - network_policies: dict[str, sandbox_pb2.NetworkPolicyRule] | None = None, -) -> sandbox_pb2.SandboxPolicy: - return sandbox_pb2.SandboxPolicy( - version=1, - filesystem=_BASE_FILESYSTEM, - landlock=_BASE_LANDLOCK, - process=_BASE_PROCESS, - network_policies=network_policies or {}, - ) - - -def _tcp_connect_errno(): - def connect(host: str, port: int) -> int: - import socket - - try: - with socket.create_connection((host, port), timeout=5): - return 0 - except OSError as error: - return error.errno or -1 - - return connect - - -def _network_rule( - host: str, - port: int, - *, - binary: str = "/**", - allowed_ips: list[str] | None = None, -) -> sandbox_pb2.NetworkPolicyRule: - return sandbox_pb2.NetworkPolicyRule( - name="test_rule", - endpoints=[ - sandbox_pb2.NetworkEndpoint( - host=host, - port=port, - allowed_ips=allowed_ips or [], - ) - ], - binaries=[sandbox_pb2.NetworkBinary(path=binary)], - ) - - -def test_policy_applies_to_exec_commands( - sandbox: Callable[..., Sandbox], -) -> None: - def current_user() -> str: - import os - import pwd - - return pwd.getpwuid(os.getuid()).pw_name - - def write_allowed_files() -> str: - from pathlib import Path - - Path("/sandbox/allowed.txt").write_text("ok") - Path("/tmp/allowed.txt").write_text("ok") - return "ok" - - spec = datamodel_pb2.SandboxSpec(policy=_base_policy()) - with sandbox(spec=spec, delete_on_exit=True) as policy_sandbox: - user_result = policy_sandbox.exec_python(current_user) - assert user_result.exit_code == 0, user_result.stderr - assert user_result.stdout.strip() == "sandbox" - - file_result = policy_sandbox.exec_python(write_allowed_files) - assert file_result.exit_code == 0, file_result.stderr - assert file_result.stdout.strip() == "ok" - - -@pytest.mark.parametrize( - ("policy", "host", "port"), - [ - (_base_policy(), "1.1.1.1", 443), - ( - _base_policy( - {"test_rule": _network_rule("1.1.1.1", 80)}, - ), - "1.1.1.1", - 443, - ), - ( - _base_policy( - {"test_rule": _network_rule("1.1.1.1", 443, binary="/bin/false")}, - ), - "1.1.1.1", - 443, - ), - ], - ids=["no-policy", "wrong-port", "wrong-binary"], -) -def test_transparent_tcp_policy_denies_unauthorized_connections( - sandbox: Callable[..., Sandbox], - policy: sandbox_pb2.SandboxPolicy, - host: str, - port: int, -) -> None: - import errno - - spec = datamodel_pb2.SandboxSpec(policy=policy) - with sandbox(spec=spec, delete_on_exit=True) as policy_sandbox: - result = policy_sandbox.exec_python( - _tcp_connect_errno(), - args=(host, port), - ) - - assert result.exit_code == 0, result.stderr - assert int(result.stdout.strip()) in {errno.EACCES, errno.EPERM} - - -def test_conflicting_destination_metadata_is_rejected( - sandbox: Callable[..., Sandbox], -) -> None: - """The gateway rejects ambiguous endpoint pinning before launch.""" - target = "10.200.0.2" - port = 19876 - policy = _base_policy( - network_policies={ - "user_rule": sandbox_pb2.NetworkPolicyRule( - name="user_rule", - endpoints=[sandbox_pb2.NetworkEndpoint(host=target, port=port)], - binaries=[sandbox_pb2.NetworkBinary(path="/**")], - ), - "approved_rule": sandbox_pb2.NetworkPolicyRule( - name="approved_rule", - endpoints=[ - sandbox_pb2.NetworkEndpoint( - host=target, - port=port, - allowed_ips=["10.200.0.0/24"], - ) - ], - binaries=[sandbox_pb2.NetworkBinary(path="/**")], - ), - } - ) - spec = datamodel_pb2.SandboxSpec(policy=policy) - with ( - pytest.raises(grpc.RpcError) as exc_info, - sandbox(spec=spec, delete_on_exit=True), - ): - pytest.fail("ambiguous policy unexpectedly created a sandbox") - - assert exc_info.value.code() == grpc.StatusCode.FAILED_PRECONDITION - details = exc_info.value.details() or "" - assert "network endpoint ambiguity validation failed" in details - assert "allowed_ips" in details diff --git a/e2e/python/test_sandbox_providers.py b/e2e/python/test_sandbox_providers.py deleted file mode 100644 index 6619c4b465..0000000000 --- a/e2e/python/test_sandbox_providers.py +++ /dev/null @@ -1,1437 +0,0 @@ -# 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 = "", - config: dict[str, str] | None = None, -) -> 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, - config=config or {}, - 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_scope=datamodel_pb2.WorkspaceSelector(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_scope=datamodel_pb2.WorkspaceSelector(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=sandbox_pb2.NETWORK_TLS_MODE_UNSPECIFIED, - enforcement=sandbox_pb2.NETWORK_ENFORCEMENT_MODE_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))) - separator = "&" if "?" in self.path else "?" - self.send_header("Link", f"<{self.path}{separator}page=2>; rel=next") - 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) - - -# =========================================================================== -# 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-code", - 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 read_metadata_token_status() -> int: - import os - import urllib.error - import urllib.request - - request = urllib.request.Request( - f"http://{os.environ['GCE_METADATA_HOST']}/computeMetadata/v1/instance/service-accounts/default/token", - headers={"Metadata-Flavor": "Google"}, - ) - opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) - try: - with opener.open(request, timeout=5) as response: - return response.status - except urllib.error.HTTPError as error: - return error.code - - result = sb.exec_python(read_metadata_token_status) - assert result.exit_code == 0, result.stderr - assert result.stdout.strip() == "503" - - -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=sandbox_pb2.NETWORK_ACCESS_PRESET_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" - ) - - -@pytest.mark.parametrize( - ("service_account_email", "provider_suffix"), - [ - (None, "no-email"), - ("", "empty-email"), - ("sdk@metadata-test-project.iam.gserviceaccount.com", "configured-email"), - ], - ids=["no-email", "empty-email", "configured-email"], -) -def test_google_metadata_sdk_discovery( - sandbox: Callable[..., Sandbox], - sandbox_client: SandboxClient, - service_account_email: str | None, - provider_suffix: str, -) -> None: - """Google's SDK discovers project/account metadata and refreshes a placeholder.""" - config = {"project_id": "metadata-test-project"} - if service_account_email is not None: - config["service_account_email"] = service_account_email - with provider( - sandbox_client._stub, - name=f"e2e-google-metadata-sdk-{provider_suffix}", - provider_type="google-cloud", - credentials={"GCP_ADC_ACCESS_TOKEN": "gcp-metadata-real-secret"}, - config=config, - ) as provider_name: - policy = _default_policy() - policy.network_policies["gcp_api"].CopyFrom( - sandbox_pb2.NetworkPolicyRule( - name="gcp_api", - endpoints=[ - sandbox_pb2.NetworkEndpoint( - host="storage.googleapis.com", - port=443, - protocol="rest", - access=sandbox_pb2.NETWORK_ACCESS_PRESET_FULL, - credential_binding=sandbox_pb2.NetworkCredentialBinding( - provider=provider_name - ), - ) - ], - ) - ) - spec = datamodel_pb2.SandboxSpec(policy=policy, providers=[provider_name]) - - def discover_metadata() -> str: - import json - import os - - import google.auth - import requests - from google.auth.compute_engine import _metadata - from google.auth.transport.requests import Request - - # Force ADC to use SDK metadata detection rather than a local key file. - for key in ( - "GOOGLE_APPLICATION_CREDENTIALS", - "GOOGLE_CLOUD_PROJECT", - "GCLOUD_PROJECT", - ): - os.environ.pop(key, None) - session = requests.Session() - # Exercise the direct TCP path used by metadata clients. - session.trust_env = False - request = Request(session=session) - if not _metadata.ping(request): - raise RuntimeError("Google SDK could not detect the metadata endpoint") - credentials, project = google.auth.default(request=request) - credentials.refresh(request) - credentials.refresh(request) - return json.dumps( - { - "project": project, - "account": credentials.service_account_email, - "token": credentials.token, - "expiry_present": credentials.expiry is not None, - "metadata_host": os.environ["GCE_METADATA_HOST"], - "metadata_ip": os.environ["GCE_METADATA_IP"], - } - ) - - with sandbox(spec=spec, delete_on_exit=True) as sb: - result = sb.exec_python(discover_metadata) - assert result.exit_code == 0, result.stderr - data = json.loads(result.stdout) - assert data["project"] == "metadata-test-project" - assert data["account"] == (service_account_email or "default") - assert data["metadata_host"] == data["metadata_ip"] == "127.0.0.1:8174" - assert data["expiry_present"] - assert _is_placeholder_for_env_key(data["token"], "GCP_ADC_ACCESS_TOKEN") - assert "gcp-metadata-real-secret" not in result.stdout - - -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=sb.sandbox.name, - provider=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=sb.sandbox.name, - provider=provider_name, - ) - ) - wait_for_token(sb, "NOT_SET") - finally: - try: - stub.DetachSandboxProvider( - openshell_pb2.DetachSandboxProviderRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector( - workspace="default" - ), - sandbox=sb.sandbox.name, - provider=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" - - -@pytest.mark.parametrize( - "placement", - ["authorization", "missing", "header", "query", "path", "unused-fields"], -) -def test_static_header_credentials_reject_reflective_request_locations( - sandbox: Callable[..., Sandbox], - sandbox_client: SandboxClient, - placement: str, -) -> None: - """Missing metadata restricts substitution to Authorization and explicit body rewriting.""" - stub = sandbox_client._stub - profile_id = f"e2e-placement-{time.time_ns()}" - secret = "synthetic-placement-secret" - profile = _native_inference_profile( - profile_id=profile_id, - env_var="API_TOKEN", - port=0, - rules=[sandbox_pb2.L7Rule(allow=sandbox_pb2.L7Allow(method="*", path="/**"))], - ) - credential = profile.credentials[0] - if placement == "missing": - credential.auth_style = "" - credential.header_name = "" - elif placement == "unused-fields": - credential.auth_style = "bearer" - credential.header_name = "Authorization" - credential.query_param = "api_key" - credential.path_template = "/v1/{credential}/resources" - elif placement in {"header", "query", "path"}: - credential.auth_style = placement - credential.header_name = "x-api-key" if placement == "header" else "" - credential.query_param = "api_key" if placement == "query" else "" - credential.path_template = ( - "/credential/{credential}/search" if placement == "path" else "" - ) - profile.endpoints[0].request_body_credential_rewrite = True - - def probe_locations(host: str, port: int, placement: str) -> str: - import json - import os - import urllib.error - import urllib.parse - import urllib.request - - token = os.environ["API_TOKEN"] - root = f"http://{host}:{port}" - query_key = "api_key" if placement == "unused-fields" else "probe" - encoded_token = urllib.parse.quote(token, safe="") - token_path = ( - f"/v1/{encoded_token}/resources" - if placement == "unused-fields" - else f"/{encoded_token}/search" - ) - requests = [ - urllib.request.Request( - root - + "/search?" - + urllib.parse.urlencode( - {"q": "rust", "per_page": "1", query_key: token} - ) - ), - urllib.request.Request( - root + "/search", headers={"X-Other-Credential": token} - ), - urllib.request.Request(root + token_path), - urllib.request.Request( - root + "/search", - data=urllib.parse.urlencode({"token": token}).encode(), - headers={"Content-Type": "application/x-www-form-urlencoded"}, - ), - ] - results = [] - for request in requests: - try: - with urllib.request.urlopen(request, timeout=30) as response: - results.append( - { - "status": response.status, - "body": response.read().decode(), - "link": response.headers.get("Link"), - } - ) - except urllib.error.HTTPError as error: - results.append( - { - "status": error.code, - "body": error.read().decode(), - "link": error.headers.get("Link"), - } - ) - target = "/search?q=rust&per_page=1" - headers = {"Authorization": f"token {token}"} - if placement == "header": - headers = {"x-api-key": token} - elif placement == "query": - target += "&" + urllib.parse.urlencode({"api_key": token}) - headers = {} - elif placement == "path": - target = ( - "/credential/" - + urllib.parse.quote(token, safe="") - + "/search?q=rust&per_page=1" - ) - headers = {} - request = urllib.request.Request(root + target, headers=headers) - with urllib.request.urlopen(request, timeout=30) as response: - valid = { - "body": json.loads(response.read()), - "link": response.headers.get("Link"), - } - return json.dumps({"denied": results, "valid": valid}) - - with native_endpoint_server() as port: - profile.endpoints[0].port = port - with ( - imported_provider_profile( - stub, profile=profile, source=f"{profile_id}.yaml" - ), - provider( - stub, - name=f"{profile_id}-provider", - provider_type=profile_id, - credentials={"API_TOKEN": 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( - probe_locations, - args=("host.openshell.internal", port, placement), - timeout_seconds=180, - ) - assert result.exit_code == 0, result.stderr - payload = json.loads(result.stdout) - for result in payload["denied"][:3]: - assert result["status"] == 403, result - assert "credential_placement_mismatch" in result["body"] - assert secret not in result["body"] - assert result["link"] is None - body_result = payload["denied"][3] - assert body_result["status"] == 200, body_result - assert secret in body_result["body"] - valid = payload["valid"] - if placement in {"authorization", "missing", "unused-fields"}: - assert valid["body"]["authorization"] == f"token {secret}" - elif placement == "header": - assert valid["body"]["x_api_key"] == secret - else: - assert secret in valid["body"]["path"] - assert "page=2" in valid["link"] - if placement in {"authorization", "missing", "header", "unused-fields"}: - assert secret not in valid["link"] - - -def test_imported_anthropic_profile_allows_native_endpoint_with_attached_provider( - sandbox: Callable[..., Sandbox], - sandbox_client: SandboxClient, -) -> None: - 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" - - -# =========================================================================== -# 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-code", - 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: - request = openshell_pb2.DeleteProviderProfileRequest(id=pid) - if ws: - request.workspace_scope.workspace = ws - stub.DeleteProviderProfile(request) - except grpc.RpcError: - pass - - _cleanup() - try: - resp = stub.ImportProviderProfiles( - openshell_pb2.ImportProviderProfilesRequest( - profiles=[_make_profile(platform_id)], - ) - ) - assert resp.imported, "platform-scoped import should succeed" - - resp = stub.ImportProviderProfiles( - openshell_pb2.ImportProviderProfilesRequest( - profiles=[_make_profile(workspace_id)], - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default"), - ) - ) - assert resp.imported, "workspace-scoped import should succeed" - - platform_list = stub.ListProviderProfiles( - openshell_pb2.ListProviderProfilesRequest(page_size=200) - ) - 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_scope=datamodel_pb2.WorkspaceSelector(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_scope=datamodel_pb2.WorkspaceSelector(workspace=ws_a), - ) - ) - assert resp_a.imported, "import into ws-a should succeed" - - resp_b = stub.ImportProviderProfiles( - openshell_pb2.ImportProviderProfilesRequest( - profiles=[_make_profile()], - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace=ws_b), - ) - ) - assert resp_b.imported, "import into ws-b should succeed" - - list_a = stub.ListProviderProfiles( - openshell_pb2.ListProviderProfilesRequest( - page_size=200, - workspace_scope=datamodel_pb2.WorkspaceSelector(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_scope=datamodel_pb2.WorkspaceSelector(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_scope=datamodel_pb2.WorkspaceSelector(workspace=ws), - ) - ) - with contextlib.suppress(Exception): - workspace_client.delete(ws) diff --git a/e2e/python/test_sandbox_venv.py b/e2e/python/test_sandbox_venv.py deleted file mode 100644 index 0461542e0d..0000000000 --- a/e2e/python/test_sandbox_venv.py +++ /dev/null @@ -1,162 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -"""Tests for the writable sandbox venv, PATH, and package installation. - -Verifies that: -- /sandbox/.venv/bin is in PATH for both interactive and non-interactive sessions -- pip install works inside the sandbox with an explicit PyPI policy -- uv pip install works (validates Landlock V2 cross-directory rename support) -- uv run --with works for ephemeral dependency injection -- Installed packages are importable after installation - -Package-install tests pass their policy explicitly. In the split sandbox and -supervisor topology, trusted policy evaluation no longer discovers policy from -the untrusted workload image filesystem. -""" - -from __future__ import annotations - -from typing import TYPE_CHECKING - -from openshell._proto import datamodel_pb2, sandbox_pb2 - -if TYPE_CHECKING: - from collections.abc import Callable - - from openshell import Sandbox - - -def _pypi_spec() -> datamodel_pb2.SandboxSpec: - endpoints = [ - "pypi.org", - "files.pythonhosted.org", - "github.com", - "objects.githubusercontent.com", - "api.github.com", - "downloads.python.org", - ] - return datamodel_pb2.SandboxSpec( - policy=sandbox_pb2.SandboxPolicy( - version=1, - filesystem=sandbox_pb2.FilesystemPolicy( - include_workdir=True, - read_only=["/usr", "/lib", "/etc", "/app", "/proc"], - read_write=["/sandbox", "/tmp"], - ), - landlock=sandbox_pb2.LandlockPolicy(compatibility="best_effort"), - process=sandbox_pb2.ProcessPolicy( - run_as_user="sandbox", run_as_group="sandbox" - ), - network_policies={ - "pypi": sandbox_pb2.NetworkPolicyRule( - name="pypi", - endpoints=[ - sandbox_pb2.NetworkEndpoint(host=host, port=443) - for host in endpoints - ], - binaries=[sandbox_pb2.NetworkBinary(path="/**")], - ) - }, - ) - ) - - -def test_sandbox_venv_in_path( - sandbox: Callable[..., Sandbox], -) -> None: - """Non-interactive exec sees /sandbox/.venv/bin in PATH.""" - with sandbox(spec=_pypi_spec(), delete_on_exit=True) as sb: - result = sb.exec(["bash", "-c", "echo $PATH"], timeout_seconds=20) - assert result.exit_code == 0, result.stderr - path_dirs = result.stdout.strip().split(":") - assert "/sandbox/.venv/bin" in path_dirs, ( - f"Expected /sandbox/.venv/bin in PATH, got: {result.stdout.strip()}" - ) - - -def test_pip_install_in_sandbox( - sandbox: Callable[..., Sandbox], -) -> None: - """pip install works inside the sandbox and installed packages are importable.""" - with sandbox(spec=_pypi_spec(), delete_on_exit=True) as sb: - install = sb.exec( - ["pip", "install", "--quiet", "cowsay"], - timeout_seconds=60, - ) - assert install.exit_code == 0, ( - f"pip install failed:\nstdout: {install.stdout}\nstderr: {install.stderr}" - ) - - # Verify the package is importable - verify = sb.exec( - ["python", "-c", "import cowsay; print(cowsay.char_names[0])"], - timeout_seconds=20, - ) - assert verify.exit_code == 0, ( - f"import failed:\nstdout: {verify.stdout}\nstderr: {verify.stderr}" - ) - assert verify.stdout.strip(), "Expected non-empty output from cowsay" - - -def test_uv_pip_install_in_sandbox( - sandbox: Callable[..., Sandbox], -) -> None: - """uv pip install works inside the sandbox (validates Landlock V2 REFER support). - - Under Landlock V1 this would fail with EXDEV (cross-device link, os error 18) - because uv uses cross-directory rename() for cache population and installation. - Landlock V2 adds the REFER right which permits this. - """ - with sandbox(spec=_pypi_spec(), delete_on_exit=True) as sb: - install = sb.exec( - [ - "uv", - "pip", - "install", - "--python", - "/sandbox/.venv/bin/python", - "--quiet", - "cowsay", - ], - timeout_seconds=60, - ) - assert install.exit_code == 0, ( - f"uv pip install failed:\nstdout: {install.stdout}\nstderr: {install.stderr}" - ) - - # Verify the package is importable - verify = sb.exec( - ["python", "-c", "import cowsay; print(cowsay.char_names[0])"], - timeout_seconds=20, - ) - assert verify.exit_code == 0, ( - f"import failed after uv install:\n" - f"stdout: {verify.stdout}\nstderr: {verify.stderr}" - ) - assert verify.stdout.strip(), "Expected non-empty output from cowsay" - - -def test_uv_run_with_ephemeral_dependency( - sandbox: Callable[..., Sandbox], -) -> None: - """uv run --with installs a dependency on-the-fly and runs a script using it.""" - with sandbox(spec=_pypi_spec(), delete_on_exit=True) as sb: - result = sb.exec( - [ - "uv", - "run", - "--python", - "/sandbox/.venv/bin/python", - "--with", - "cowsay", - "python", - "-c", - "import cowsay; print(cowsay.char_names[0])", - ], - timeout_seconds=60, - ) - assert result.exit_code == 0, ( - f"uv run --with failed:\nstdout: {result.stdout}\nstderr: {result.stderr}" - ) - assert result.stdout.strip(), "Expected non-empty output from uv run" diff --git a/e2e/python/test_security_tls.py b/e2e/python/test_security_tls.py deleted file mode 100644 index 31e0fa4a0a..0000000000 --- a/e2e/python/test_security_tls.py +++ /dev/null @@ -1,270 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -"""E2e tests for gateway TLS and mTLS user authentication. - -TLS accepts CA-only clients so supervisors can use sandbox bearer tokens. -Health is public; user RPCs require an authenticated user. Presented client -certificates must be signed by the gateway CA. -""" - -from __future__ import annotations - -import json -import os -import pathlib -import subprocess -import tempfile -from urllib.parse import urlparse - -import grpc -import pytest - -from openshell._proto import datamodel_pb2, openshell_pb2, openshell_pb2_grpc - -# --------------------------------------------------------------------------- -# Helpers -# --------------------------------------------------------------------------- - - -def _xdg_config_home() -> pathlib.Path: - configured = os.environ.get("XDG_CONFIG_HOME") - if configured: - return pathlib.Path(configured) - return pathlib.Path.home() / ".config" - - -def _resolve_cluster_name() -> str: - if os.environ.get("OPENSHELL_GATEWAY_ENDPOINT"): - return os.environ.get("OPENSHELL_GATEWAY", "openshell-e2e-endpoint") - env_cluster = os.environ.get("OPENSHELL_GATEWAY") - if env_cluster: - return env_cluster - active_file = _xdg_config_home() / "openshell" / "active_gateway" - return active_file.read_text().strip() - - -def _cluster_metadata(cluster_name: str) -> dict: - endpoint = os.environ.get("OPENSHELL_GATEWAY_ENDPOINT") - if endpoint: - return { - "name": cluster_name, - "gateway_endpoint": endpoint, - "auth_mode": "plaintext", - } - metadata_path = ( - _xdg_config_home() / "openshell" / "gateways" / cluster_name / "metadata.json" - ) - return json.loads(metadata_path.read_text()) - - -def _mtls_dir(cluster_name: str) -> pathlib.Path: - return _xdg_config_home() / "openshell" / "gateways" / cluster_name / "mtls" - - -def _generate_self_signed_cert( - tmpdir: pathlib.Path, -) -> tuple[pathlib.Path, pathlib.Path]: - """Generate a self-signed cert+key pair that is NOT signed by the cluster CA.""" - cert_path = tmpdir / "rogue.crt" - key_path = tmpdir / "rogue.key" - subprocess.run( - [ - "openssl", - "req", - "-x509", - "-sha256", - "-nodes", - "-days", - "1", - "-newkey", - "rsa:2048", - "-subj", - "/O=rogue/CN=rogue-client", - "-keyout", - str(key_path), - "-out", - str(cert_path), - ], - check=True, - capture_output=True, - ) - return cert_path, key_path - - -# --------------------------------------------------------------------------- -# Fixtures -# --------------------------------------------------------------------------- - - -@pytest.fixture(scope="session") -def cluster_name() -> str: - name = _resolve_cluster_name() - if not name: - pytest.skip("no active cluster configured") - return name - - -@pytest.fixture(scope="session") -def server_endpoint(cluster_name: str) -> tuple[str, int, str]: - """Return (host, port, scheme) for the OpenShell server.""" - metadata = _cluster_metadata(cluster_name) - parsed = urlparse(metadata["gateway_endpoint"]) - host = parsed.hostname or "127.0.0.1" - port = parsed.port or (443 if parsed.scheme == "https" else 8080) - return host, port, parsed.scheme - - -@pytest.fixture(scope="session") -def mtls_certs( - cluster_name: str, server_endpoint: tuple[str, int, str] -) -> tuple[bytes, bytes, bytes]: - """Return (ca_pem, cert_pem, key_pem) for the provisioned mTLS client.""" - _, _, scheme = server_endpoint - if scheme != "https": - pytest.skip("server is not using TLS; mTLS tests require an HTTPS endpoint") - mtls = _mtls_dir(cluster_name) - ca = (mtls / "ca.crt").read_bytes() - cert = (mtls / "tls.crt").read_bytes() - key = (mtls / "tls.key").read_bytes() - return ca, cert, key - - -# --------------------------------------------------------------------------- -# Tests -# --------------------------------------------------------------------------- - - -class TestServerMtlsEnforcement: - """Verify TLS trust and the mTLS user authorization boundary.""" - - def test_authenticated_client_succeeds( - self, - server_endpoint: tuple[str, int, str], - mtls_certs: tuple[bytes, bytes, bytes], - ) -> None: - """A verified mTLS user can call Health and a protected user RPC.""" - host, port, _ = server_endpoint - ca, cert, key = mtls_certs - - credentials = grpc.ssl_channel_credentials( - root_certificates=ca, - private_key=key, - certificate_chain=cert, - ) - channel = grpc.secure_channel(f"{host}:{port}", credentials) - try: - stub = openshell_pb2_grpc.OpenShellStub(channel) - response = stub.Health(openshell_pb2.HealthRequest(), timeout=10) - assert response.status == openshell_pb2.SERVICE_STATUS_HEALTHY - stub.ListSandboxes( - openshell_pb2.ListSandboxesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector(workspace="default") - ), - timeout=10, - ) - finally: - channel.close() - - def test_ca_only_client_health_succeeds_but_user_rpc_rejected( - self, - server_endpoint: tuple[str, int, str], - mtls_certs: tuple[bytes, bytes, bytes], - ) -> None: - """CA-only TLS reaches Health but cannot acquire mTLS user identity.""" - host, port, _ = server_endpoint - ca, _, _ = mtls_certs - - # Only provide the CA for server verification -- no client cert/key. - credentials = grpc.ssl_channel_credentials(root_certificates=ca) - channel = grpc.secure_channel(f"{host}:{port}", credentials) - try: - stub = openshell_pb2_grpc.OpenShellStub(channel) - response = stub.Health(openshell_pb2.HealthRequest(), timeout=10) - assert response.status == openshell_pb2.SERVICE_STATUS_HEALTHY - with pytest.raises(grpc.RpcError) as exc_info: - stub.ListSandboxes( - openshell_pb2.ListSandboxesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector( - workspace="default" - ) - ), - timeout=10, - ) - assert exc_info.value.code() == grpc.StatusCode.UNAUTHENTICATED - - # An unverified bearer token must not promote the TLS connection - # to a user identity either. - with pytest.raises(grpc.RpcError) as exc_info: - stub.ListSandboxes( - openshell_pb2.ListSandboxesRequest( - workspace_scope=datamodel_pb2.WorkspaceSelector( - workspace="default" - ) - ), - metadata=(("authorization", "Bearer invalid-token"),), - timeout=10, - ) - assert exc_info.value.code() == grpc.StatusCode.UNAUTHENTICATED - finally: - channel.close() - - def test_wrong_client_cert_rejected( - self, - server_endpoint: tuple[str, int, str], - mtls_certs: tuple[bytes, bytes, bytes], - ) -> None: - """A client presenting a cert signed by a different CA is rejected.""" - host, port, _ = server_endpoint - ca, _, _ = mtls_certs - - with tempfile.TemporaryDirectory() as tmpdir: - rogue_cert_path, rogue_key_path = _generate_self_signed_cert( - pathlib.Path(tmpdir) - ) - rogue_cert = rogue_cert_path.read_bytes() - rogue_key = rogue_key_path.read_bytes() - - credentials = grpc.ssl_channel_credentials( - root_certificates=ca, - private_key=rogue_key, - certificate_chain=rogue_cert, - ) - channel = grpc.secure_channel(f"{host}:{port}", credentials) - try: - stub = openshell_pb2_grpc.OpenShellStub(channel) - with pytest.raises(grpc.RpcError) as exc_info: - stub.Health(openshell_pb2.HealthRequest(), timeout=10) - assert exc_info.value.code() in ( - grpc.StatusCode.UNAVAILABLE, - grpc.StatusCode.UNKNOWN, - ), f"expected UNAVAILABLE or UNKNOWN, got {exc_info.value.code()}" - finally: - channel.close() - - def test_plaintext_connection_rejected( - self, - server_endpoint: tuple[str, int, str], - mtls_certs: tuple[bytes, bytes, bytes], - ) -> None: - """A plaintext (non-TLS) connection to the server port is rejected.""" - host, port, _ = server_endpoint - # Ensure we have certs loaded (so the test isn't skipped for non-TLS). - _ = mtls_certs - - channel = grpc.insecure_channel(f"{host}:{port}") - try: - stub = openshell_pb2_grpc.OpenShellStub(channel) - with pytest.raises(grpc.RpcError) as exc_info: - stub.Health(openshell_pb2.HealthRequest(), timeout=10) - # The loopback listener may intentionally accept plaintext service - # HTTP. A gRPC request is still rejected, either at the transport - # boundary or as an unimplemented HTTP route. - assert exc_info.value.code() in ( - grpc.StatusCode.UNAVAILABLE, - grpc.StatusCode.UNKNOWN, - grpc.StatusCode.INTERNAL, - grpc.StatusCode.UNIMPLEMENTED, - ), f"expected plaintext gRPC rejection, got {exc_info.value.code()}" - finally: - channel.close() diff --git a/e2e/python/test_workspace_api.py b/e2e/python/test_workspace_api.py deleted file mode 100644 index a60597eb53..0000000000 --- a/e2e/python/test_workspace_api.py +++ /dev/null @@ -1,173 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -from __future__ import annotations - -import contextlib -import uuid -from typing import TYPE_CHECKING - -import grpc -import pytest - -from openshell._proto import openshell_pb2 - -if TYPE_CHECKING: - from openshell import WorkspaceClient - - -def test_workspace_crud(workspace_client: WorkspaceClient) -> None: - name = f"ws-crud-{uuid.uuid4().hex[:8]}" - - try: - ws = workspace_client.create(name) - assert ws.name == name - assert ws.phase == "WORKSPACE_PHASE_ACTIVE" - - fetched = workspace_client.get(name) - assert fetched.name == name - assert fetched.phase == "WORKSPACE_PHASE_ACTIVE" - finally: - with contextlib.suppress(Exception): - workspace_client.delete(name) - - -def test_workspace_create_with_labels(workspace_client: WorkspaceClient) -> None: - name = f"ws-lbl-{uuid.uuid4().hex[:8]}" - - try: - ws = workspace_client.create(name, labels={"env": "test", "team": "infra"}) - assert ws.labels["env"] == "test" - assert ws.labels["team"] == "infra" - - fetched = workspace_client.get(name) - assert fetched.labels["env"] == "test" - assert fetched.labels["team"] == "infra" - finally: - with contextlib.suppress(Exception): - workspace_client.delete(name) - - -def test_workspace_list_includes_created(workspace_client: WorkspaceClient) -> None: - name = f"ws-list-{uuid.uuid4().hex[:8]}" - - try: - workspace_client.create(name) - - names = {ws.name for ws in workspace_client.list_all()} - assert name in names - assert "default" in names - finally: - with contextlib.suppress(Exception): - workspace_client.delete(name) - - -def test_workspace_delete_nonexistent_raises_not_found( - workspace_client: WorkspaceClient, -) -> None: - with pytest.raises(grpc.RpcError) as exc_info: - workspace_client.delete(f"no-such-ws-{uuid.uuid4().hex[:8]}") - assert exc_info.value.code() == grpc.StatusCode.NOT_FOUND - - -def test_workspace_get_nonexistent_raises_not_found( - workspace_client: WorkspaceClient, -) -> None: - with pytest.raises(grpc.RpcError) as exc_info: - workspace_client.get(f"no-such-ws-{uuid.uuid4().hex[:8]}") - assert exc_info.value.code() == grpc.StatusCode.NOT_FOUND - - -def test_workspace_request_ids_are_scoped_to_each_target( - workspace_client: WorkspaceClient, -) -> None: - names = [f"ws-scope-{uuid.uuid4().hex[:8]}" for _ in range(2)] - create_id = str(uuid.uuid4()) - delete_id = str(uuid.uuid4()) - stub = workspace_client._stub - originals = [] - try: - for name in names: - request = openshell_pb2.CreateWorkspaceRequest( - name=name, request_id=create_id - ) - response, call = stub.CreateWorkspace.with_call(request, timeout=20) - assert "openshell-replayed" not in dict(call.initial_metadata()) - assert response.workspace.metadata.name == name - fetched = stub.GetWorkspace( - openshell_pb2.GetWorkspaceRequest(name=name), timeout=20 - ) - assert fetched.workspace.metadata.id == response.workspace.metadata.id - originals.append(response) - assert originals[0].workspace.metadata.id != originals[1].workspace.metadata.id - - for name, original in zip(names, originals, strict=True): - replay, call = stub.CreateWorkspace.with_call( - openshell_pb2.CreateWorkspaceRequest(name=name, request_id=create_id), - timeout=20, - ) - assert replay == original - assert dict(call.initial_metadata())["openshell-replayed"] == "true" - - for name in names: - request = openshell_pb2.DeleteWorkspaceRequest( - name=name, request_id=delete_id - ) - response, call = stub.DeleteWorkspace.with_call(request, timeout=20) - assert "openshell-replayed" not in dict(call.initial_metadata()) - assert response.outcome == openshell_pb2.DELETION_OUTCOME_COMPLETED - with pytest.raises(grpc.RpcError) as exc_info: - workspace_client.get(name) - assert exc_info.value.code() == grpc.StatusCode.NOT_FOUND - - for name in names: - replay, call = stub.DeleteWorkspace.with_call( - openshell_pb2.DeleteWorkspaceRequest(name=name, request_id=delete_id), - timeout=20, - ) - assert replay.outcome == openshell_pb2.DELETION_OUTCOME_COMPLETED - assert dict(call.initial_metadata())["openshell-replayed"] == "true" - finally: - for name in names: - with contextlib.suppress(Exception): - workspace_client.delete(name, allow_missing=True) - - -def test_workspace_request_id_replays_without_deleting_replacement( - workspace_client: WorkspaceClient, -) -> None: - name = f"ws-replay-{uuid.uuid4().hex[:8]}" - # Exercise the generated wire fields before curated request-ID helpers land. - stub = workspace_client._stub - create = openshell_pb2.CreateWorkspaceRequest( - name=name, request_id=str(uuid.uuid4()) - ) - delete = openshell_pb2.DeleteWorkspaceRequest( - name=name, request_id=str(uuid.uuid4()) - ) - try: - original = stub.CreateWorkspace(create, timeout=20) - replay, call = stub.CreateWorkspace.with_call(create, timeout=20) - assert original == replay - assert dict(call.initial_metadata())["openshell-replayed"] == "true" - - mismatch = openshell_pb2.CreateWorkspaceRequest( - name=name, request_id=create.request_id, labels={"changed": "true"} - ) - with pytest.raises(grpc.RpcError) as exc_info: - stub.CreateWorkspace(mismatch, timeout=20) - assert exc_info.value.code() == grpc.StatusCode.FAILED_PRECONDITION - - removed = stub.DeleteWorkspace(delete, timeout=20) - replacement = stub.CreateWorkspace( - openshell_pb2.CreateWorkspaceRequest(name=name), timeout=20 - ) - assert replacement.workspace.metadata.id != original.workspace.metadata.id - assert stub.DeleteWorkspace(delete, timeout=20) == removed - fetched = stub.GetWorkspace( - openshell_pb2.GetWorkspaceRequest(name=name), timeout=20 - ) - assert fetched.workspace.metadata.id == replacement.workspace.metadata.id - finally: - with contextlib.suppress(Exception): - workspace_client.delete(name) diff --git a/e2e/rust/Cargo.lock b/e2e/rust/Cargo.lock index 4782ce04bc..7787e56b6d 100644 --- a/e2e/rust/Cargo.lock +++ b/e2e/rust/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "addr2line" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e567177890eb1617b1f774005b66b26b2377afd138a2ca37aae7d8f0c81429d4" +dependencies = [ + "gimli", +] + [[package]] name = "adler2" version = "2.0.1" @@ -64,7 +73,7 @@ dependencies = [ "nom", "num-traits", "rusticata-macros", - "thiserror", + "thiserror 2.0.19", "time", ] @@ -77,7 +86,7 @@ dependencies = [ "proc-macro2", "quote", "syn 2.0.119", - "synstructure", + "synstructure 0.13.2", ] [[package]] @@ -235,6 +244,30 @@ dependencies = [ "tokio", ] +[[package]] +name = "backtrace" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f4220217f3288537b83684130f42fad43320b1c3b805f1910d683d52ded24f" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-link", +] + +[[package]] +name = "backtrace-ext" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "537beee3be4a18fb023b570f80e3ae28003db9167a751266b259926e25539d50" +dependencies = [ + "backtrace", +] + [[package]] name = "base64" version = "0.22.1" @@ -322,7 +355,7 @@ dependencies = [ "serde_derive", "serde_json", "serde_urlencoded", - "thiserror", + "thiserror 2.0.19", "tokio", "tokio-util", "tower-service", @@ -766,6 +799,12 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" +[[package]] +name = "fixedbitset" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" + [[package]] name = "flate2" version = "1.1.10" @@ -958,8 +997,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi", + "wasm-bindgen", ] [[package]] @@ -988,6 +1029,18 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "gimli" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1033caf0b349c518623b5396bfb2cf0bddf44f0306d543a250e5743297aafd10" + +[[package]] +name = "glob" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b" + [[package]] name = "gloo-timers" version = "0.3.0" @@ -1280,13 +1333,16 @@ version = "0.1.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" dependencies = [ + "base64 0.22.1", "bytes", "futures-channel", "futures-util", "http", "http-body", "hyper", + "ipnet", "libc", + "percent-encoding", "pin-project-lite", "socket2", "tokio", @@ -1335,9 +1391,9 @@ dependencies = [ [[package]] name = "icu_collections" -version = "2.2.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2984d1cd16c883d7935b9e07e44071dca8d917fd52ecc02c04d5fa0b5a3f191c" +checksum = "fe090a42a0d3140fed8c63263ed1fecc02422cb2f4bfe55cd8de2de1b8e105dd" dependencies = [ "displaydoc", "potential_utf", @@ -1349,22 +1405,43 @@ dependencies = [ [[package]] name = "icu_locale_core" -version = "2.2.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92219b62b3e2b4d88ac5119f8904c10f8f61bf7e95b640d25ba3075e6cac2c29" +checksum = "25d29b57c17c6528d0dc4c2141cfa2d08200465acb537a3909c8b9b8a4a984c0" dependencies = [ "displaydoc", "litemap", + "serde", "tinystr", "writeable", "zerovec", ] +[[package]] +name = "icu_locale_fallback" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "406e9ae5b95d8aa37db6293405931b766a6172d1848daee22d70eb44aa78a43c" +dependencies = [ + "icu_locale_core", + "icu_locale_fallback_data", + "icu_provider", + "potential_utf", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_locale_fallback_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3632ebbd0c5c3637ba1a40bdf8288f2cb9d2268e73441a69d5fc73d7cfbaa87c" + [[package]] name = "icu_normalizer" -version = "2.2.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c56e5ee99d6e3d33bd91c5d85458b6005a22140021cc324cea84dd0e72cff3b4" +checksum = "404a3b8c486f205c87fe7f23ab6894d84a4ad76d6e4671ae1981ba7def87c599" dependencies = [ "icu_collections", "icu_normalizer_data", @@ -1376,16 +1453,17 @@ dependencies = [ [[package]] name = "icu_normalizer_data" -version = "2.2.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da3be0ae77ea334f4da67c12f149704f19f81d1adf7c51cf482943e84a2bad38" +checksum = "20f0088bfb7441f7a37fa0c9e20cb1e9cba1cac6b9ce8cd2a6dc44294f8e8cbc" [[package]] name = "icu_properties" -version = "2.2.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bee3b67d0ea5c2cca5003417989af8996f8604e34fb9ddf96208a033901e70de" +checksum = "2ea83d991c56158f7cf898bf86436c1dfe7c07ee82fe88951f2695c9f37a379f" dependencies = [ + "displaydoc", "icu_collections", "icu_locale_core", "icu_properties_data", @@ -1396,18 +1474,20 @@ dependencies = [ [[package]] name = "icu_properties_data" -version = "2.2.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e2bbb201e0c04f7b4b3e14382af113e17ba4f63e2c9d2ee626b720cbce54a14" +checksum = "b98f29af82d02b163fe50161388350e743bb3107cc8e8c2f7314cb505faddfcf" [[package]] name = "icu_provider" -version = "2.2.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "139c4cf31c8b5f33d7e199446eff9c1e02decfc2f0eec2c8d71f65befa45b421" +checksum = "413099315636c588ee8f011190b329b971a09a8fae9d157e01dcae8379468514" dependencies = [ "displaydoc", "icu_locale_core", + "serde", + "stable_deref_trait", "writeable", "yoke", "zerofrom", @@ -1415,6 +1495,29 @@ dependencies = [ "zerovec", ] +[[package]] +name = "icu_segmenter" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61b195dc605f81aa181bec9b3bd14e26cc71b903e4644f6af578ca9772c7a710" +dependencies = [ + "icu_collections", + "icu_locale_fallback", + "icu_provider", + "icu_segmenter_data", + "potential_utf", + "smallvec", + "utf8_iter", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_segmenter_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "913a7d294ad056474749152107309fdee3ec17d2482d1ab70fdc61a81c64b9a3" + [[package]] name = "ident_case" version = "1.0.1" @@ -1454,6 +1557,18 @@ dependencies = [ "serde_core", ] +[[package]] +name = "ipnet" +version = "2.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0" + +[[package]] +name = "is_ci" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7655c9839580ee829dfacba1d1278c2b7883e50a277ff7541299489d6bdfdc45" + [[package]] name = "itertools" version = "0.14.0" @@ -1481,9 +1596,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.103" +version = "0.3.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53b44bfcdb3f8d5837a46dae1ca9660a837176eee74a28b229bc626816589102" +checksum = "ce57d20d1ea864ce2ac172ab472d409214f4fd359f0b2a2775abdf522e2af99e" dependencies = [ "cfg-if", "futures-util", @@ -1499,7 +1614,7 @@ dependencies = [ "jsonptr", "serde", "serde_json", - "thiserror", + "thiserror 2.0.19", ] [[package]] @@ -1512,7 +1627,7 @@ dependencies = [ "pest_derive", "regex", "serde_json", - "thiserror", + "thiserror 2.0.19", ] [[package]] @@ -1651,7 +1766,7 @@ dependencies = [ "serde", "serde_json", "serde_yaml", - "thiserror", + "thiserror 2.0.19", "tokio", "tokio-util", "tower", @@ -1674,7 +1789,7 @@ dependencies = [ "serde", "serde-value", "serde_json", - "thiserror", + "thiserror 2.0.19", ] [[package]] @@ -1713,7 +1828,7 @@ dependencies = [ "pin-project", "serde", "serde_json", - "thiserror", + "thiserror 2.0.19", "tokio", "tokio-util", "tracing", @@ -1822,6 +1937,36 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a86d3146ed3995b5913c414f6664344b9617457320782e64f0bb44afd49d74" +[[package]] +name = "miette" +version = "7.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f98efec8807c63c752b5bd61f862c165c115b0a35685bdcfd9238c7aeb592b7" +dependencies = [ + "backtrace", + "backtrace-ext", + "cfg-if", + "miette-derive", + "owo-colors", + "supports-color", + "supports-hyperlinks", + "supports-unicode", + "terminal_size", + "textwrap", + "unicode-width 0.1.14", +] + +[[package]] +name = "miette-derive" +version = "7.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db5b29714e950dbb20d5e6f74f9dcec4edbcc1067bb7f8ed198c097b8c1a818b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "mime" version = "0.3.17" @@ -1855,6 +2000,12 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "multimap" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084" + [[package]] name = "nix" version = "0.29.0" @@ -1985,6 +2136,35 @@ dependencies = [ "autocfg", ] +[[package]] +name = "oauth2" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51e219e79014df21a225b1860a479e2dcd7cbd9130f4defd4bd0e191ea31d67d" +dependencies = [ + "base64 0.22.1", + "chrono", + "getrandom 0.2.17", + "http", + "rand 0.8.8", + "reqwest", + "serde", + "serde_json", + "serde_path_to_error", + "sha2 0.10.9", + "thiserror 1.0.69", + "url", +] + +[[package]] +name = "object" +version = "0.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd229a0361b9d0d4396176e02d65897f487eebeab7caa6d443855ee152ca0b9c" +dependencies = [ + "memchr", +] + [[package]] name = "oid-registry" version = "0.8.1" @@ -2000,6 +2180,42 @@ version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +[[package]] +name = "openshell-core" +version = "0.0.0" +dependencies = [ + "async-trait", + "base64 0.22.1", + "chrono", + "glob", + "ipnet", + "libc", + "miette", + "nix", + "openshell-crypto", + "openshell-extension-core", + "openshell-policy-schema", + "prost", + "prost-types", + "protoc-bin-vendored", + "reqwest", + "rustix", + "rustls", + "serde", + "serde_json", + "sha2 0.10.9", + "thiserror 2.0.19", + "tokio", + "tokio-stream", + "tonic", + "tonic-prost", + "tonic-prost-build", + "tonic-types", + "tracing", + "url", + "uuid", +] + [[package]] name = "openshell-crypto" version = "0.0.0" @@ -2012,7 +2228,7 @@ dependencies = [ "rustls", "serde", "sqlx", - "thiserror", + "thiserror 2.0.19", "tonic", "x509-parser", ] @@ -2036,6 +2252,7 @@ dependencies = [ "openshell-crypto", "openshell-ocsf-validate", "openshell-otel-test-support", + "openshell-sdk", "opentelemetry-proto", "prost", "rand 0.9.5", @@ -2055,6 +2272,18 @@ dependencies = [ "url", ] +[[package]] +name = "openshell-extension-core" +version = "0.0.0" +dependencies = [ + "hyper-util", + "serde", + "thiserror 2.0.19", + "tokio", + "tonic", + "tower", +] + [[package]] name = "openshell-ocsf" version = "0.0.0" @@ -2074,7 +2303,7 @@ version = "0.0.0" dependencies = [ "jsonschema", "serde_json", - "thiserror", + "thiserror 2.0.19", ] [[package]] @@ -2098,7 +2327,7 @@ dependencies = [ "rustls", "rustls-native-certs", "serde_json", - "thiserror", + "thiserror 2.0.19", "tokio", "tonic", "tower-http", @@ -2131,6 +2360,43 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "openshell-policy-schema" +version = "0.0.0" +dependencies = [ + "miette", + "noyalib", + "serde", + "serde_json", + "serde_path_to_error", + "thiserror 2.0.19", +] + +[[package]] +name = "openshell-sdk" +version = "0.0.0" +dependencies = [ + "async-stream", + "async-trait", + "futures", + "hyper", + "hyper-util", + "miette", + "oauth2", + "openshell-core", + "openshell-crypto", + "reqwest", + "rustls", + "serde", + "thiserror 2.0.19", + "tokio", + "tokio-rustls", + "tokio-tungstenite", + "tonic", + "tower", + "tracing", +] + [[package]] name = "openssl-probe" version = "0.2.1" @@ -2147,7 +2413,7 @@ dependencies = [ "futures-sink", "js-sys", "pin-project-lite", - "thiserror", + "thiserror 2.0.19", "tracing", ] @@ -2177,7 +2443,7 @@ dependencies = [ "opentelemetry-proto", "opentelemetry_sdk", "prost", - "thiserror", + "thiserror 2.0.19", "tokio", "tonic", "tonic-types", @@ -2209,7 +2475,7 @@ dependencies = [ "percent-encoding", "portable-atomic", "rand 0.9.5", - "thiserror", + "thiserror 2.0.19", "tokio", "tokio-stream", ] @@ -2229,6 +2495,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a80800c0488c3a21695ea981a54918fbb37abf04f4d0720c453632255e2ff0e" +[[package]] +name = "owo-colors" +version = "4.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c45bb4a6ae1280ec0803b1ef9d3455eb50f01efbbe1447ab020f1d54fba9d8" + [[package]] name = "parking" version = "2.2.1" @@ -2326,6 +2598,17 @@ dependencies = [ "pest", ] +[[package]] +name = "petgraph" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8701b58ea97060d5e5b155d383a69952a60943f0e6dfe30b04c287beb0b27455" +dependencies = [ + "fixedbitset", + "hashbrown 0.15.5", + "indexmap", +] + [[package]] name = "pin-project" version = "1.1.13" @@ -2370,6 +2653,8 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0103b1cef7ec0cf76490e969665504990193874ea05c85ff9bab8b911d0a0564" dependencies = [ + "serde_core", + "writeable", "zerovec", ] @@ -2388,6 +2673,16 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn 2.0.119", +] + [[package]] name = "proc-macro2" version = "1.0.107" @@ -2407,6 +2702,27 @@ dependencies = [ "prost-derive", ] +[[package]] +name = "prost-build" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03da047801ff44bb6a4d407d4860c05fd70bb81714e6b2f3812603d5b145b042" +dependencies = [ + "heck", + "itertools", + "log", + "multimap", + "petgraph", + "prettyplease", + "prost", + "prost-types", + "pulldown-cmark", + "pulldown-cmark-to-cmark", + "regex", + "syn 2.0.119", + "tempfile", +] + [[package]] name = "prost-derive" version = "0.14.4" @@ -2429,6 +2745,90 @@ dependencies = [ "prost", ] +[[package]] +name = "protoc-bin-vendored" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8760a25b6ff9c620324822737e468478fa092234190d2e449760344354896ed9" +dependencies = [ + "protoc-bin-vendored-linux-aarch_64", + "protoc-bin-vendored-linux-ppcle_64", + "protoc-bin-vendored-linux-s390_64", + "protoc-bin-vendored-linux-x86_32", + "protoc-bin-vendored-linux-x86_64", + "protoc-bin-vendored-macos-aarch_64", + "protoc-bin-vendored-macos-x86_64", + "protoc-bin-vendored-win32", +] + +[[package]] +name = "protoc-bin-vendored-linux-aarch_64" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73fa2624782ca04cd44f51554566717377acd240e4c0016d757dd74fccc9324f" + +[[package]] +name = "protoc-bin-vendored-linux-ppcle_64" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2417e9817fa237dab803ad4dda7357a111656e242959cc6b8f9a1a583367d42" + +[[package]] +name = "protoc-bin-vendored-linux-s390_64" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d189c34636356a46a7ed3188233dc8a88c431278cc54d4a19b096a2d270e985" + +[[package]] +name = "protoc-bin-vendored-linux-x86_32" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "171e39f1e846e5f322ced1ac3b8d4cd3a3833ca24b6e5d58b3632574fe6204fa" + +[[package]] +name = "protoc-bin-vendored-linux-x86_64" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "873cdcc097593432086661aa432b8078f1cd87bfb02847c332e98ae2c119e966" + +[[package]] +name = "protoc-bin-vendored-macos-aarch_64" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eeb72df001783b8297847fe8f5f874ee400fd742c843d60583e8c23d96977c7f" + +[[package]] +name = "protoc-bin-vendored-macos-x86_64" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b04652167eca899dda05f32f5481adeaf25c623a98ce2fc146a001cc59a2add7" + +[[package]] +name = "protoc-bin-vendored-win32" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "263a3f48f01e7309e857138bd47f785585b4a005e8e56c6d2824ce91195999c3" + +[[package]] +name = "pulldown-cmark" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3db2be46835f013dd3f4fa7f5f7402c537787b54ebb6fe2b94bbdd7e2669bb7b" +dependencies = [ + "bitflags", + "memchr", + "unicase", +] + +[[package]] +name = "pulldown-cmark-to-cmark" +version = "22.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84bbb29c624230c4bd1047bbdb2aa47e41c860e9665ce62ba9504eebe91bf867" +dependencies = [ + "pulldown-cmark", +] + [[package]] name = "quote" version = "1.0.47" @@ -2450,13 +2850,24 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +[[package]] +name = "rand" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + [[package]] name = "rand" version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" dependencies = [ - "rand_chacha", + "rand_chacha 0.9.0", "rand_core 0.9.5", ] @@ -2471,6 +2882,16 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + [[package]] name = "rand_chacha" version = "0.9.0" @@ -2603,6 +3024,45 @@ version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" +[[package]] +name = "reqwest" +version = "0.12.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eddd3ca559203180a307f12d114c268abf583f59b03cb906fd0b3ff8646c1147" +dependencies = [ + "base64 0.22.1", + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-util", + "js-sys", + "log", + "percent-encoding", + "pin-project-lite", + "rustls", + "rustls-native-certs", + "rustls-pki-types", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tokio-rustls", + "tower", + "tower-http", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "ring" version = "0.17.14" @@ -2617,6 +3077,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustc-demangle" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b74b56ffa8bb2830709a538c2cbcae9aa062db0d2a42563bfb09bdaae44020eb" + [[package]] name = "rustc-hash" version = "2.1.3" @@ -2840,6 +3306,17 @@ dependencies = [ "zmij", ] +[[package]] +name = "serde_path_to_error" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457" +dependencies = [ + "itoa", + "serde", + "serde_core", +] + [[package]] name = "serde_repr" version = "0.1.21" @@ -2993,7 +3470,7 @@ checksum = "0d585997b0ac10be3c5ee635f1bab02d512760d14b7c468801ac8a01d9ae5f1d" dependencies = [ "num-bigint", "num-traits", - "thiserror", + "thiserror 2.0.19", "time", ] @@ -3072,7 +3549,7 @@ dependencies = [ "serde_json", "sha2 0.10.9", "smallvec", - "thiserror", + "thiserror 2.0.19", "tokio", "tokio-stream", "tracing", @@ -3113,7 +3590,7 @@ dependencies = [ "sqlx-postgres", "sqlx-sqlite", "syn 2.0.119", - "thiserror", + "thiserror 2.0.19", "tokio", "url", ] @@ -3140,7 +3617,7 @@ dependencies = [ "sha1 0.11.0", "sha2 0.11.0", "sqlx-core", - "thiserror", + "thiserror 2.0.19", "tracing", ] @@ -3174,7 +3651,7 @@ dependencies = [ "smallvec", "sqlx-core", "stringprep", - "thiserror", + "thiserror 2.0.19", "tracing", "whoami", ] @@ -3198,7 +3675,7 @@ dependencies = [ "percent-encoding", "serde", "sqlx-core", - "thiserror", + "thiserror 2.0.19", "tracing", "url", ] @@ -3253,6 +3730,27 @@ version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" +[[package]] +name = "supports-color" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c64fc7232dd8d2e4ac5ce4ef302b1d81e0b80d055b9d77c7c4f51f6aa4c867d6" +dependencies = [ + "is_ci", +] + +[[package]] +name = "supports-hyperlinks" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e396b6523b11ccb83120b115a0b7366de372751aa6edf19844dfb13a6af97e91" + +[[package]] +name = "supports-unicode" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7401a30af6cb5818bb64852270bb722533397edcfc7344954a38f420819ece2" + [[package]] name = "syn" version = "2.0.119" @@ -3280,6 +3778,9 @@ name = "sync_wrapper" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] [[package]] name = "synstructure" @@ -3292,6 +3793,17 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "synstructure" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.3", +] + [[package]] name = "tempfile" version = "3.27.0" @@ -3305,13 +3817,53 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "terminal_size" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "230a1b821ccbd75b185820a1f1ff7b14d21da1e442e22c0863ea5f08771a8874" +dependencies = [ + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "textwrap" +version = "0.16.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ecfad6c3abc80a577f2b91c1e412ee57e7a060d430b553c1b0c940974ebcd49" +dependencies = [ + "icu_segmenter", + "unicode-width 0.2.3", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + [[package]] name = "thiserror" version = "2.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09a43598840e33d5b0331f38c5e30d13bb11c11210a4b58f0d9b18a5a5eefcd9" dependencies = [ - "thiserror-impl", + "thiserror-impl 2.0.19", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", ] [[package]] @@ -3366,11 +3918,12 @@ dependencies = [ [[package]] name = "tinystr" -version = "0.8.3" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8323304221c2a851516f22236c5722a72eaa19749016521d6dff0824447d96d" +checksum = "abc840c238154d58f9e86b2792261edfde565a17696e1cda1a449de27979801a" dependencies = [ "displaydoc", + "serde_core", "zerovec", ] @@ -3429,6 +3982,22 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-tungstenite" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a9daff607c6d2bf6c16fd681ccb7eecc83e4e2cdc1ca067ffaadfca5de7f084" +dependencies = [ + "futures-util", + "log", + "rustls", + "rustls-native-certs", + "rustls-pki-types", + "tokio", + "tokio-rustls", + "tungstenite", +] + [[package]] name = "tokio-util" version = "0.7.19" @@ -3476,6 +4045,18 @@ dependencies = [ "tracing", ] +[[package]] +name = "tonic-build" +version = "0.14.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac6d916d206ed7bdda1778ddff9bda43303789c258877e7bc22fb44ee5c984b4" +dependencies = [ + "prettyplease", + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "tonic-prost" version = "0.14.6" @@ -3487,6 +4068,22 @@ dependencies = [ "tonic", ] +[[package]] +name = "tonic-prost-build" +version = "0.14.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ea216ddc448e08e7839ab09e20e60b06b6a1c32745ccb212c0ebd161c199f1b" +dependencies = [ + "prettyplease", + "proc-macro2", + "prost-build", + "prost-types", + "quote", + "syn 2.0.119", + "tempfile", + "tonic-build", +] + [[package]] name = "tonic-types" version = "0.14.6" @@ -3526,13 +4123,16 @@ dependencies = [ "base64 0.22.1", "bitflags", "bytes", + "futures-util", "http", "http-body", "mime", "pin-project-lite", + "tower", "tower-layer", "tower-service", "tracing", + "url", "uuid", ] @@ -3644,6 +4244,25 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "tungstenite" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4793cb5e56680ecbb1d843515b23b6de9a75eb04b66643e256a396d43be33c13" +dependencies = [ + "bytes", + "data-encoding", + "http", + "httparse", + "log", + "rand 0.9.5", + "rustls", + "rustls-pki-types", + "sha1 0.10.7", + "thiserror 2.0.19", + "utf-8", +] + [[package]] name = "typenum" version = "1.20.1" @@ -3656,6 +4275,12 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971" +[[package]] +name = "unicase" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357cc3acc6a036009fd6c973ed009037c732d60d0b4f6c673e9041497482a28f" + [[package]] name = "unicode-bidi" version = "0.3.18" @@ -3689,6 +4314,18 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7df058c713841ad818f1dc5d3fd88063241cc61f49f5fbea4b951e8cf5a8d71d" +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + +[[package]] +name = "unicode-width" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca718471876992059f251e33aafc334346ada08042b7661dd1fe313a9bced557" + [[package]] name = "unsafe-libyaml" version = "0.2.11" @@ -3717,8 +4354,15 @@ dependencies = [ "idna", "percent-encoding", "serde", + "serde_derive", ] +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + [[package]] name = "utf8_iter" version = "1.0.4" @@ -3796,9 +4440,9 @@ dependencies = [ [[package]] name = "wasm-bindgen" -version = "0.2.126" +version = "0.2.128" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b067c0c11094aef6b7a801c1e34a26affafdf3d051dba08456b868789aaf9a4" +checksum = "aecb87a33d3b0c5e3b7aa46336eaf486cffafbd281b195e4c8b80d50df2351bf" dependencies = [ "cfg-if", "once_cell", @@ -3807,11 +4451,21 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ef4c5d3d2cdf5c54f4231181768f5510842e350db025faf1f7163b1030ed928" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "wasm-bindgen-macro" -version = "0.2.126" +version = "0.2.128" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "167ce5e579f6bcf889c4f7175a8a5a585de84e8ff93976ce393efa5f2837aab1" +checksum = "a690d511e3c1a8b3a55e33511e3c2c00c78415cd23650f32b808627f5696b9ed" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -3819,26 +4473,36 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.126" +version = "0.2.128" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3997c7839262f4ef12cf90b818d6340c18e80f263f1a94bf157d0ec4420380e" +checksum = "411e4887f0071ef2d2164a9d5fdf2d20efbef78fccd3a78b0c10a1dc5295e48a" dependencies = [ "bumpalo", "proc-macro2", "quote", - "syn 2.0.119", + "syn 3.0.3", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.126" +version = "0.2.128" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc1b4cb0cc549fcf58d7dfc081778139b3d283a081644e833e84682ad71cea24" +checksum = "81941cd78d0c92026c33e5e01312845a4cb1e9af3407f9134b100dd03144103e" dependencies = [ "unicode-ident", ] +[[package]] +name = "web-sys" +version = "0.3.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fbddc4a036f00ec4f18c83445bd3115cb306a91da554919a099d9222fe4a7f8" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "web-time" version = "1.1.0" @@ -4035,9 +4699,9 @@ checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" [[package]] name = "writeable" -version = "0.6.3" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ffae5123b2d3fc086436f8834ae3ab053a283cfac8fe0a0b8eaae044768a4c4" +checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" [[package]] name = "x509-parser" @@ -4053,7 +4717,7 @@ dependencies = [ "nom", "oid-registry", "rusticata-macros", - "thiserror", + "thiserror 2.0.19", "time", ] @@ -4069,9 +4733,9 @@ dependencies = [ [[package]] name = "yoke" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "709fe23a0424b6a435d82152b1bd3fdfb0833487d5fa90d05d42762a9891fef5" +checksum = "417ef3fcf6c300b7dbd25c85531a746931c02ad536f3cf4786ed1e88223340b3" dependencies = [ "stable_deref_trait", "yoke-derive", @@ -4080,14 +4744,14 @@ dependencies = [ [[package]] name = "yoke-derive" -version = "0.8.2" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de844c262c8848816172cef550288e7dc6c7b7814b4ee56b3e1553f275f1858e" +checksum = "ec8ebde2db3681e8c9980cc27822030e68752690ddfa9473e739aeb4dbde6d71" dependencies = [ "proc-macro2", "quote", - "syn 2.0.119", - "synstructure", + "syn 3.0.3", + "synstructure 0.14.0", ] [[package]] @@ -4128,7 +4792,7 @@ dependencies = [ "proc-macro2", "quote", "syn 2.0.119", - "synstructure", + "synstructure 0.13.2", ] [[package]] @@ -4153,21 +4817,23 @@ dependencies = [ [[package]] name = "zerotrie" -version = "0.2.4" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f9152d31db0792fa83f70fb2f83148effb5c1f5b8c7686c3459e361d9bc20bf" +checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f" dependencies = [ "displaydoc", "yoke", "zerofrom", + "zerovec", ] [[package]] name = "zerovec" -version = "0.11.6" +version = "0.11.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90f911cbc359ab6af17377d242225f4d75119aec87ea711a880987b18cd7b239" +checksum = "f58a2799cfe9a1670c7ea8edbd52ef8edb93cb37a7f83bf31ba9e35d9dae6a40" dependencies = [ + "serde", "yoke", "zerofrom", "zerovec-derive", @@ -4175,13 +4841,13 @@ dependencies = [ [[package]] name = "zerovec-derive" -version = "0.11.3" +version = "0.11.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "625dc425cab0dca6dc3c3319506e6593dcb08a9f387ea3b284dbd52a92c40555" +checksum = "c3743ddbf47738d929cd1ae2e199d40d0162979dfe31aef8ae646d2bf00bf97a" dependencies = [ "proc-macro2", "quote", - "syn 2.0.119", + "syn 3.0.3", ] [[package]] diff --git a/e2e/rust/Cargo.toml b/e2e/rust/Cargo.toml index 5e557abcdf..f7520984ed 100644 --- a/e2e/rust/Cargo.toml +++ b/e2e/rust/Cargo.toml @@ -48,7 +48,8 @@ e2e-kubernetes-shared-supervisor-ha = ["e2e-kubernetes", "e2e-host-gateway"] e2e-kubernetes-shared-supervisor-scaling = ["e2e-kubernetes", "e2e-host-gateway"] e2e-podman = ["e2e", "e2e-host-gateway", "e2e-local-container-driver"] e2e-podman-gpu = ["e2e-podman", "e2e-gpu"] -e2e-oidc-pkce = [] +# The OIDC tests call the gateway through the SDK's generated client. +e2e-oidc-pkce = ["dep:openshell-sdk"] e2e-vm = ["e2e", "e2e-host-gateway"] [[test]] @@ -71,6 +72,11 @@ name = "oidc_pkce" path = "tests/oidc_pkce.rs" required-features = ["e2e-oidc-pkce"] +[[test]] +name = "oidc_authz" +path = "tests/oidc_authz.rs" +required-features = ["e2e-oidc-pkce"] + [[test]] name = "vm_overlay" path = "tests/vm_overlay.rs" @@ -264,6 +270,7 @@ required-features = ["e2e-local-container-driver"] [dependencies] openshell-crypto = { path = "../../crates/openshell-crypto" } openshell-otel-test-support = { path = "../../crates/openshell-otel-test-support" } +openshell-sdk = { path = "../../crates/openshell-sdk", optional = true } opentelemetry-proto = { version = "0.33", default-features = false, features = ["gen-tonic", "trace"] } base64 = "0.22" bollard = "0.20" diff --git a/e2e/rust/tests/oidc_authz.rs b/e2e/rust/tests/oidc_authz.rs new file mode 100644 index 0000000000..6c372cdab3 --- /dev/null +++ b/e2e/rust/tests/oidc_authz.rs @@ -0,0 +1,1208 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +#![cfg(target_os = "linux")] + +//! OIDC authentication and workspace authorization on a live gateway, with +//! access tokens Keycloak issues for the fixture realm. +//! +//! `oidc_pkce` covers interactive login through the CLI. These tests get +//! tokens with password and client-credentials grants and call the gateway's +//! gRPC API directly, so they can check every workspace-scoped RPC: the +//! gateway validates the real token, the authorization middleware lets a +//! member-less user through, and the handler's membership check refuses it +//! with the remediation the CLI shows. +//! +//! Ported from `e2e/python/oidc/oidc_auth_test.py` and +//! `e2e/python/oidc/workspace_authz_test.py`. Run with +//! `mise run e2e:oidc-pkce:docker`, which starts Keycloak and a gateway that +//! trusts it. + +use std::process::Stdio; + +use base64::Engine as _; +use openshell_sdk::raw::{AuthedGrpcClient, proto}; +use openshell_sdk::{AuthConfig, ClientConfig, OpenShellClient}; +use serial_test::serial; +use tokio::process::Command; +use tonic::{Code, Status}; + +const USER_ROLE: i32 = proto::WorkspaceRole::User as i32; +const ADMIN_ROLE: i32 = proto::WorkspaceRole::Admin as i32; +const ALL_SCOPES: &str = "openid openshell:all"; +/// A sandbox name no test creates, for RPCs that name one. +const MISSING: &str = "nonexistent"; + +// ── Tokens and clients ───────────────────────────────────────────────── + +fn env(name: &str) -> String { + std::env::var(name).unwrap_or_else(|_| panic!("OIDC authz E2E requires {name}")) +} + +/// Request an access token from the realm's token endpoint. +async fn token(form: &[(&str, &str)]) -> String { + let endpoint = format!( + "{}/protocol/openid-connect/token", + env("OPENSHELL_E2E_OIDC_ISSUER") + ); + let mut command = Command::new("curl"); + command.args(["-sS", "--fail-with-body", "--max-time", "20"]); + for (key, value) in form { + command + .arg("--data-urlencode") + .arg(format!("{key}={value}")); + } + let output = command + .arg(&endpoint) + .stdin(Stdio::null()) + .output() + .await + .expect("run curl"); + let body = String::from_utf8_lossy(&output.stdout); + assert!( + output.status.success(), + "token request to {endpoint} failed: {body} {}", + String::from_utf8_lossy(&output.stderr) + ); + let response: serde_json::Value = serde_json::from_str(&body).expect("token response JSON"); + response["access_token"] + .as_str() + .unwrap_or_else(|| panic!("token response has no access_token: {body}")) + .to_string() +} + +/// A password-grant token for a fixture user through the CLI's client. +async fn password_token(username: &str, password: &str, scope: Option<&str>) -> String { + let mut form = vec![ + ("grant_type", "password"), + ("client_id", "openshell-cli"), + ("username", username), + ("password", password), + ]; + if let Some(scope) = scope { + form.push(("scope", scope)); + } + token(&form).await +} + +/// The `sub` claim of an access token, which workspace membership names. +fn subject(token: &str) -> String { + let payload = token.split('.').nth(1).expect("JWT payload"); + let claims = base64::engine::general_purpose::URL_SAFE_NO_PAD + .decode(payload.trim_end_matches('=')) + .expect("JWT payload base64"); + let claims: serde_json::Value = serde_json::from_slice(&claims).expect("JWT claims JSON"); + claims["sub"].as_str().expect("JWT sub").to_string() +} + +/// A gateway client that sends `token` as its bearer, or no token. +async fn client(token: Option<&str>) -> AuthedGrpcClient { + let mut config = ClientConfig::new(env("OPENSHELL_E2E_OIDC_GATEWAY_ENDPOINT")); + if let Ok(ca) = std::env::var("OPENSHELL_E2E_GATEWAY_CA_CERT") { + config.ca_cert = Some(std::fs::read(&ca).expect("read the gateway CA")); + } + config.auth = token.map(AuthConfig::oidc); + OpenShellClient::connect(config) + .await + .expect("connect to the OIDC gateway") + .raw_grpc() +} + +struct Identity { + client: AuthedGrpcClient, + sub: String, +} + +async fn identity(username: &str, password: &str) -> Identity { + let token = password_token(username, password, Some(ALL_SCOPES)).await; + Identity { + sub: subject(&token), + client: client(Some(&token)).await, + } +} + +async fn admin() -> Identity { + identity("admin@test", "admin").await +} + +async fn user() -> Identity { + identity("user@test", "user").await +} + +/// A workspace name unique to this run. Workspace names are at most 19 +/// characters, so `label` is at most five. +fn unique_workspace(label: &str) -> String { + assert!(label.len() <= 5, "workspace label {label:?} is too long"); + let suffix: u32 = rand::random(); + format!("az-{label}-{suffix:08x}") +} + +// ── Workspace fixture ────────────────────────────────────────────────── + +/// A workspace the Platform Admin creates, with a seeded provider so reads +/// have something to return. Call [`Workspace::delete`] at the end. +struct Workspace { + name: String, + provider: String, +} + +impl Workspace { + async fn create(admin: &mut Identity, label: &str) -> Self { + let name = unique_workspace(label); + admin + .client + .create_workspace(proto::CreateWorkspaceRequest { + name: name.clone(), + ..Default::default() + }) + .await + .unwrap_or_else(|status| panic!("create workspace {name}: {status}")); + let provider = "e2e-authz-provider".to_string(); + admin + .client + .create_provider(provider_request(&name, &provider)) + .await + .unwrap_or_else(|status| panic!("seed provider in {name}: {status}")); + Self { name, provider } + } + + async fn delete(self, admin: &mut Identity) { + let _ = admin + .client + .delete_provider(proto::DeleteProviderRequest { + name: self.provider.clone(), + workspace_scope: Some(proto::workspace_selector(&self.name)), + ..Default::default() + }) + .await; + if let Err(status) = admin + .client + .delete_workspace(proto::DeleteWorkspaceRequest { + name: self.name.clone(), + ..Default::default() + }) + .await + { + eprintln!("warning: delete workspace {}: {status}", self.name); + } + } +} + +async fn add_member(admin: &mut Identity, workspace: &str, subject: &str, role: i32) { + admin + .client + .add_workspace_member(proto::AddWorkspaceMemberRequest { + workspace_scope: Some(proto::workspace_selector(workspace)), + principal_subject: subject.to_string(), + role, + ..Default::default() + }) + .await + .unwrap_or_else(|status| panic!("add {subject} to {workspace}: {status}")); +} + +async fn remove_member(admin: &mut Identity, workspace: &str, subject: &str) { + let _ = admin + .client + .remove_workspace_member(proto::RemoveWorkspaceMemberRequest { + workspace_scope: Some(proto::workspace_selector(workspace)), + principal_subject: subject.to_string(), + ..Default::default() + }) + .await; +} + +fn provider_request(workspace: &str, name: &str) -> proto::CreateProviderRequest { + proto::CreateProviderRequest { + workspace_scope: Some(proto::workspace_selector(workspace)), + provider: Some(proto::Provider { + metadata: Some(proto::ObjectMeta { + name: name.to_string(), + workspace: workspace.to_string(), + ..Default::default() + }), + r#type: "claude-code".to_string(), + credentials: [("ANTHROPIC_API_KEY".to_string(), "v".to_string())].into(), + ..Default::default() + }), + ..Default::default() + } +} + +// ── The RPCs under test ──────────────────────────────────────────────── + +/// Workspace-scoped RPCs, each called with a minimal request against a +/// workspace. RPCs that name a sandbox name one that doesn't exist. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Rpc { + GetWorkspace, + ListWorkspaceMembers, + AddWorkspaceMember, + RemoveWorkspaceMember, + CreateSandbox, + GetSandbox, + ListSandboxes, + DeleteSandbox, + ListSandboxProviders, + AttachSandboxProvider, + DetachSandboxProvider, + CreateProvider, + GetProvider, + ListProviders, + UpdateProvider, + DeleteProvider, + ListProviderProfiles, + GetProviderProfile, + ImportProviderProfiles, + UpdateProviderProfiles, + LintProviderProfiles, + DeleteProviderProfile, + GetProviderRefreshStatus, + ConfigureProviderRefresh, + RotateProviderCredential, + DeleteProviderRefresh, + ExposeService, + GetService, + ListServices, + DeleteService, + GetSandboxPolicyStatus, + ListSandboxPolicies, + GetDraftPolicy, + ApproveDraftChunk, + RejectDraftChunk, + ApproveAllDraftChunks, + EditDraftChunk, + UndoDraftChunk, + ClearDraftChunks, + GetDraftHistory, + UpdateConfig, +} + +impl Rpc { + const ALL: [Self; 41] = [ + Self::GetWorkspace, + Self::ListWorkspaceMembers, + Self::AddWorkspaceMember, + Self::RemoveWorkspaceMember, + Self::CreateSandbox, + Self::GetSandbox, + Self::ListSandboxes, + Self::DeleteSandbox, + Self::ListSandboxProviders, + Self::AttachSandboxProvider, + Self::DetachSandboxProvider, + Self::CreateProvider, + Self::GetProvider, + Self::ListProviders, + Self::UpdateProvider, + Self::DeleteProvider, + Self::ListProviderProfiles, + Self::GetProviderProfile, + Self::ImportProviderProfiles, + Self::UpdateProviderProfiles, + Self::LintProviderProfiles, + Self::DeleteProviderProfile, + Self::GetProviderRefreshStatus, + Self::ConfigureProviderRefresh, + Self::RotateProviderCredential, + Self::DeleteProviderRefresh, + Self::ExposeService, + Self::GetService, + Self::ListServices, + Self::DeleteService, + Self::GetSandboxPolicyStatus, + Self::ListSandboxPolicies, + Self::GetDraftPolicy, + Self::ApproveDraftChunk, + Self::RejectDraftChunk, + Self::ApproveAllDraftChunks, + Self::EditDraftChunk, + Self::UndoDraftChunk, + Self::ClearDraftChunks, + Self::GetDraftHistory, + Self::UpdateConfig, + ]; + + /// Whether the RPC looks up a sandbox by name first. Those answer a + /// caller who can't see the sandbox with `NOT_FOUND`, so its existence in + /// another workspace doesn't leak. + fn names_sandbox(self) -> bool { + matches!( + self, + Self::GetSandbox + | Self::DeleteSandbox + | Self::ListSandboxProviders + | Self::AttachSandboxProvider + | Self::DetachSandboxProvider + | Self::ExposeService + | Self::GetService + | Self::DeleteService + | Self::GetSandboxPolicyStatus + | Self::ListSandboxPolicies + | Self::GetDraftPolicy + | Self::ApproveDraftChunk + | Self::RejectDraftChunk + | Self::ApproveAllDraftChunks + | Self::EditDraftChunk + | Self::UndoDraftChunk + | Self::ClearDraftChunks + | Self::GetDraftHistory + | Self::UpdateConfig + ) + } + + /// Call the RPC against workspace `ws`. + #[allow(clippy::too_many_lines)] + async fn call(self, c: &mut AuthedGrpcClient, ws: &str) -> Result<(), Status> { + let scope = Some(proto::workspace_selector(ws)); + let sandbox = MISSING.to_string(); + match self { + Self::GetWorkspace => c + .get_workspace(proto::GetWorkspaceRequest { + name: ws.to_string(), + }) + .await + .map(drop), + Self::ListWorkspaceMembers => c + .list_workspace_members(proto::ListWorkspaceMembersRequest { + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::AddWorkspaceMember => c + .add_workspace_member(proto::AddWorkspaceMemberRequest { + workspace_scope: scope, + principal_subject: "fake".to_string(), + role: USER_ROLE, + ..Default::default() + }) + .await + .map(drop), + Self::RemoveWorkspaceMember => c + .remove_workspace_member(proto::RemoveWorkspaceMemberRequest { + workspace_scope: scope, + principal_subject: "fake".to_string(), + ..Default::default() + }) + .await + .map(drop), + Self::CreateSandbox => c + .create_sandbox(proto::CreateSandboxRequest { + workspace_scope: scope, + spec: Some(proto::SandboxSpec { + template: Some(proto::SandboxTemplate { + image: "ubuntu:24.04".to_string(), + ..Default::default() + }), + ..Default::default() + }), + ..Default::default() + }) + .await + .map(drop), + Self::GetSandbox => c + .get_sandbox(proto::GetSandboxRequest { + name: sandbox, + workspace_scope: scope, + }) + .await + .map(drop), + Self::ListSandboxes => c + .list_sandboxes(proto::ListSandboxesRequest { + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::DeleteSandbox => c + .delete_sandbox(proto::DeleteSandboxRequest { + name: sandbox, + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::ListSandboxProviders => c + .list_sandbox_providers(proto::ListSandboxProvidersRequest { + sandbox, + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::AttachSandboxProvider => c + .attach_sandbox_provider(proto::AttachSandboxProviderRequest { + sandbox, + provider: MISSING.to_string(), + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::DetachSandboxProvider => c + .detach_sandbox_provider(proto::DetachSandboxProviderRequest { + sandbox, + provider: MISSING.to_string(), + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::CreateProvider => c + .create_provider(provider_request(ws, "authz-test")) + .await + .map(drop), + Self::GetProvider => c + .get_provider(proto::GetProviderRequest { + name: MISSING.to_string(), + workspace_scope: scope, + }) + .await + .map(drop), + Self::ListProviders => c + .list_providers(proto::ListProvidersRequest { + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::UpdateProvider => { + let created = provider_request(ws, MISSING); + c.update_provider(proto::UpdateProviderRequest { + workspace_scope: scope, + provider: created.provider, + ..Default::default() + }) + .await + .map(drop) + } + Self::DeleteProvider => c + .delete_provider(proto::DeleteProviderRequest { + name: MISSING.to_string(), + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::ListProviderProfiles => c + .list_provider_profiles(proto::ListProviderProfilesRequest { + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::GetProviderProfile => c + .get_provider_profile(proto::GetProviderProfileRequest { + id: MISSING.to_string(), + workspace_scope: scope, + }) + .await + .map(drop), + Self::ImportProviderProfiles => c + .import_provider_profiles(proto::ImportProviderProfilesRequest { + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::UpdateProviderProfiles => c + .update_provider_profiles(proto::UpdateProviderProfilesRequest { + workspace_scope: scope, + id: MISSING.to_string(), + ..Default::default() + }) + .await + .map(drop), + Self::LintProviderProfiles => c + .lint_provider_profiles(proto::LintProviderProfilesRequest { + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::DeleteProviderProfile => c + .delete_provider_profile(proto::DeleteProviderProfileRequest { + id: MISSING.to_string(), + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::GetProviderRefreshStatus => c + .get_provider_refresh_status(proto::GetProviderRefreshStatusRequest { + provider: MISSING.to_string(), + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::ConfigureProviderRefresh => c + .configure_provider_refresh(proto::ConfigureProviderRefreshRequest { + provider: MISSING.to_string(), + credential_key: "k".to_string(), + strategy: proto::ProviderCredentialRefreshStrategy::Static as i32, + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::RotateProviderCredential => c + .rotate_provider_credential(proto::RotateProviderCredentialRequest { + provider: MISSING.to_string(), + credential_key: "k".to_string(), + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::DeleteProviderRefresh => c + .delete_provider_refresh(proto::DeleteProviderRefreshRequest { + provider: MISSING.to_string(), + credential_key: "k".to_string(), + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::ExposeService => c + .expose_service(proto::ExposeServiceRequest { + sandbox, + name: "svc".to_string(), + target_port: 8080, + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::GetService => c + .get_service(proto::GetServiceRequest { + sandbox, + name: "svc".to_string(), + workspace_scope: scope, + }) + .await + .map(drop), + Self::ListServices => c + .list_services(proto::ListServicesRequest { + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::DeleteService => c + .delete_service(proto::DeleteServiceRequest { + sandbox, + name: "svc".to_string(), + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::GetSandboxPolicyStatus => c + .get_sandbox_policy_status(proto::GetSandboxPolicyStatusRequest { + sandbox, + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::ListSandboxPolicies => c + .list_sandbox_policies(proto::ListSandboxPoliciesRequest { + sandbox, + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::GetDraftPolicy => c + .get_draft_policy(proto::GetDraftPolicyRequest { + sandbox, + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::ApproveDraftChunk => c + .approve_draft_chunk(proto::ApproveDraftChunkRequest { + sandbox, + chunk_id: "x".to_string(), + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::RejectDraftChunk => c + .reject_draft_chunk(proto::RejectDraftChunkRequest { + sandbox, + chunk_id: "x".to_string(), + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::ApproveAllDraftChunks => c + .approve_all_draft_chunks(proto::ApproveAllDraftChunksRequest { + sandbox, + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::EditDraftChunk => c + .edit_draft_chunk(proto::EditDraftChunkRequest { + sandbox, + chunk_id: "x".to_string(), + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::UndoDraftChunk => c + .undo_draft_chunk(proto::UndoDraftChunkRequest { + sandbox, + chunk_id: "x".to_string(), + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::ClearDraftChunks => c + .clear_draft_chunks(proto::ClearDraftChunksRequest { + sandbox, + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + Self::GetDraftHistory => c + .get_draft_history(proto::GetDraftHistoryRequest { + sandbox, + workspace_scope: scope, + }) + .await + .map(drop), + Self::UpdateConfig => c + .update_config(proto::UpdateConfigRequest { + sandbox, + workspace_scope: scope, + ..Default::default() + }) + .await + .map(drop), + } + } +} + +/// Platform-scoped RPCs: provider profiles without a workspace and global +/// policy reads. +#[derive(Clone, Copy, Debug)] +enum PlatformRpc { + ListProviderProfiles, + GetProviderProfile, + ImportProviderProfiles, + UpdateProviderProfiles, + LintProviderProfiles, + DeleteProviderProfile, + GetGlobalPolicyStatus, + ListGlobalPolicies, +} + +impl PlatformRpc { + const ALL: [Self; 8] = [ + Self::ListProviderProfiles, + Self::GetProviderProfile, + Self::ImportProviderProfiles, + Self::UpdateProviderProfiles, + Self::LintProviderProfiles, + Self::DeleteProviderProfile, + Self::GetGlobalPolicyStatus, + Self::ListGlobalPolicies, + ]; + + async fn call(self, c: &mut AuthedGrpcClient) -> Result<(), Status> { + match self { + Self::ListProviderProfiles => c + .list_provider_profiles(proto::ListProviderProfilesRequest::default()) + .await + .map(drop), + Self::GetProviderProfile => c + .get_provider_profile(proto::GetProviderProfileRequest { + id: MISSING.to_string(), + ..Default::default() + }) + .await + .map(drop), + Self::ImportProviderProfiles => c + .import_provider_profiles(proto::ImportProviderProfilesRequest::default()) + .await + .map(drop), + Self::UpdateProviderProfiles => c + .update_provider_profiles(proto::UpdateProviderProfilesRequest { + id: MISSING.to_string(), + ..Default::default() + }) + .await + .map(drop), + Self::LintProviderProfiles => c + .lint_provider_profiles(proto::LintProviderProfilesRequest::default()) + .await + .map(drop), + Self::DeleteProviderProfile => c + .delete_provider_profile(proto::DeleteProviderProfileRequest { + id: MISSING.to_string(), + ..Default::default() + }) + .await + .map(drop), + Self::GetGlobalPolicyStatus => c + .get_sandbox_policy_status(proto::GetSandboxPolicyStatusRequest { + global: true, + ..Default::default() + }) + .await + .map(drop), + Self::ListGlobalPolicies => c + .list_sandbox_policies(proto::ListSandboxPoliciesRequest { + global: true, + ..Default::default() + }) + .await + .map(drop), + } + } +} + +// ── Expectations ─────────────────────────────────────────────────────── + +/// The refusal a non-member gets from the handler, naming the workspace and +/// the command that would let them in. +fn check_non_member(result: Result<(), Status>, ws: &str, sub: &str) -> Result<(), String> { + let status = result.err().ok_or("succeeded")?; + let command = + format!("openshell workspace member add --workspace '{ws}' --subject '{sub}' --role user"); + if status.code() == Code::PermissionDenied + && status + .message() + .contains(&format!("not a member of workspace '{ws}'")) + && status.message().contains(&command) + { + Ok(()) + } else { + Err(format!("{:?}: {}", status.code(), status.message())) + } +} + +fn check_workspace_admin_required( + result: Result<(), Status>, + ws: &str, + sub: &str, +) -> Result<(), String> { + let status = result.err().ok_or("succeeded")?; + let command = + format!("openshell workspace member add --workspace '{ws}' --subject '{sub}' --role admin"); + if status.code() == Code::PermissionDenied + && status.message().contains(&format!( + "workspace role 'admin' required in workspace '{ws}'" + )) + && status.message().contains(&command) + { + Ok(()) + } else { + Err(format!("{:?}: {}", status.code(), status.message())) + } +} + +fn check_code(result: Result<(), Status>, code: Code) -> Result<(), String> { + match result { + Err(status) if status.code() == code => Ok(()), + Err(status) => Err(format!("{:?}: {}", status.code(), status.message())), + Ok(()) => Err("succeeded".to_string()), + } +} + +fn check_platform_admin_required(result: Result<(), Status>) -> Result<(), String> { + check_code(result.clone(), Code::PermissionDenied)?; + let message = result.err().map(|status| status.message().to_string()); + if message + .as_deref() + .is_some_and(|message| message.contains("platform admin role required")) + { + Ok(()) + } else { + Err(format!( + "refused without naming the platform admin role: {message:?}" + )) + } +} + +/// The call wasn't refused for authorization. It may still fail, for +/// example because the sandbox it names doesn't exist. +fn check_not_denied(result: Result<(), Status>) -> Result<(), String> { + match result { + Err(status) if status.code() == Code::PermissionDenied => { + Err(format!("denied: {}", status.message())) + } + _ => Ok(()), + } +} + +fn check_ok(result: Result<(), Status>) -> Result<(), String> { + result.map_err(|status| format!("{:?}: {}", status.code(), status.message())) +} + +/// Collects mismatches, so one run reports every RPC that misbehaves. +#[derive(Default)] +struct Failures(Vec); + +impl Failures { + fn check(&mut self, what: impl std::fmt::Display, outcome: Result<(), String>) { + if let Err(error) = outcome { + self.0.push(format!("{what}: {error}")); + } + } + + fn assert_none(self) { + assert!(self.0.is_empty(), "\n{}", self.0.join("\n")); + } +} + +async fn list_workspace_names(client: &mut AuthedGrpcClient) -> Vec { + client + .list_workspaces(proto::ListWorkspacesRequest::default()) + .await + .expect("list workspaces") + .into_inner() + .workspaces + .into_iter() + .filter_map(|workspace| workspace.metadata.map(|metadata| metadata.name)) + .collect() +} + +// ── Tests ────────────────────────────────────────────────────────────── + +/// Health answers without a token; a user RPC without a token is +/// unauthenticated. +#[tokio::test] +#[serial(oidc_pkce)] +async fn requests_without_a_token() { + let mut anonymous = client(None).await; + let health = anonymous + .health(proto::HealthRequest::default()) + .await + .expect("health without a token") + .into_inner(); + assert_eq!(health.status, proto::ServiceStatus::Healthy as i32); + + let status = Rpc::ListSandboxes + .call(&mut anonymous, "default") + .await + .expect_err("list sandboxes without a token"); + assert_eq!(status.code(), Code::Unauthenticated, "{status}"); +} + +/// The gateway enforces the scopes in a real Keycloak token: sandbox scopes +/// allow sandbox reads but not provider reads, and a token without any +/// `OpenShell` scope is refused, even for the Platform Admin. +#[tokio::test] +#[serial(oidc_pkce)] +async fn token_scopes_limit_rpcs() { + let sandbox_scoped = password_token( + "admin@test", + "admin", + Some("openid sandbox:read sandbox:write"), + ) + .await; + let mut scoped = client(Some(&sandbox_scoped)).await; + Rpc::ListSandboxes + .call(&mut scoped, "default") + .await + .expect("list sandboxes with sandbox scopes"); + let status = Rpc::ListProviders + .call(&mut scoped, "default") + .await + .expect_err("list providers with only sandbox scopes"); + assert_eq!(status.code(), Code::PermissionDenied, "{status}"); + assert!(status.message().contains("provider:read"), "{status}"); + + let unscoped = password_token("admin@test", "admin", None).await; + let status = Rpc::ListSandboxes + .call(&mut client(Some(&unscoped)).await, "default") + .await + .expect_err("list sandboxes without OpenShell scopes"); + assert_eq!(status.code(), Code::PermissionDenied, "{status}"); +} + +/// A client-credentials token for the CI client is a user the gateway can +/// make a workspace member. +#[tokio::test] +#[serial(oidc_pkce)] +async fn client_credentials_token_acts_as_member() { + let mut admin = admin().await; + let workspace = Workspace::create(&mut admin, "ci").await; + let ci_token = token(&[ + ("grant_type", "client_credentials"), + ("client_id", "openshell-ci"), + ("client_secret", "ci-test-secret"), + ]) + .await; + add_member(&mut admin, &workspace.name, &subject(&ci_token), USER_ROLE).await; + + let result = Rpc::ListSandboxes + .call(&mut client(Some(&ci_token)).await, &workspace.name) + .await; + workspace.delete(&mut admin).await; + result.expect("list sandboxes with the CI client's token"); +} + +/// A user with a valid token who isn't a member of a workspace gets past the +/// authorization middleware on every workspace-scoped RPC, and the handler +/// refuses them: with the membership remediation, or `NOT_FOUND` for RPCs that +/// name a sandbox. The workspace isn't listed for them either. +#[tokio::test] +#[serial(oidc_pkce)] +async fn non_member_is_refused_on_every_workspace_rpc() { + let mut admin = admin().await; + let mut user = user().await; + let workspace = Workspace::create(&mut admin, "nomem").await; + let ws = workspace.name.clone(); + + let mut failures = Failures::default(); + for rpc in Rpc::ALL { + let result = rpc.call(&mut user.client, &ws).await; + let outcome = if rpc.names_sandbox() { + check_code(result, Code::NotFound) + } else { + check_non_member(result, &ws, &user.sub) + }; + failures.check(format!("{rpc:?}"), outcome); + } + let listed = list_workspace_names(&mut user.client).await; + failures.check( + "ListWorkspaces", + if listed.contains(&ws) { + Err(format!("lists {ws}")) + } else { + Ok(()) + }, + ); + + workspace.delete(&mut admin).await; + failures.assert_none(); +} + +/// The Platform Admin reaches a workspace they aren't a member of. +#[tokio::test] +#[serial(oidc_pkce)] +async fn platform_admin_bypasses_membership() { + let mut admin = admin().await; + let workspace = Workspace::create(&mut admin, "admin").await; + let ws = workspace.name.clone(); + let admin_sub = admin.sub.clone(); + remove_member(&mut admin, &ws, &admin_sub).await; + + let mut failures = Failures::default(); + for rpc in [Rpc::GetWorkspace, Rpc::ListSandboxes, Rpc::ListServices] { + let outcome = check_ok(rpc.call(&mut admin.client, &ws).await); + failures.check(format!("{rpc:?}"), outcome); + } + let provider = admin + .client + .get_provider(proto::GetProviderRequest { + name: workspace.provider.clone(), + workspace_scope: Some(proto::workspace_selector(&ws)), + }) + .await + .map(drop); + failures.check("GetProvider (seeded)", check_ok(provider)); + let history = Rpc::GetDraftHistory.call(&mut admin.client, &ws).await; + failures.check("GetDraftHistory", check_not_denied(history)); + + workspace.delete(&mut admin).await; + failures.assert_none(); +} + +/// A workspace user reads the workspace but needs the workspace admin role +/// to change it; it lists only the workspaces they belong to. A workspace +/// admin manages providers and user members, but can't grant the admin role, +/// list across workspaces, or change global settings, which need the +/// Platform Admin. +#[tokio::test] +#[serial(oidc_pkce)] +async fn workspace_roles_bound_what_members_can_do() { + let mut admin = admin().await; + let mut user = user().await; + let workspace = Workspace::create(&mut admin, "roles").await; + let other = Workspace::create(&mut admin, "other").await; + let ws = workspace.name.clone(); + let sub = user.sub.clone(); + let mut failures = Failures::default(); + + // As a workspace user. + add_member(&mut admin, &workspace.name, &sub, USER_ROLE).await; + for rpc in [ + Rpc::GetWorkspace, + Rpc::ListSandboxes, + Rpc::ListProviders, + Rpc::ListServices, + Rpc::ListWorkspaceMembers, + ] { + let outcome = check_ok(rpc.call(&mut user.client, &ws).await); + failures.check(format!("user {rpc:?}"), outcome); + } + let provider = user + .client + .get_provider(proto::GetProviderRequest { + name: workspace.provider.clone(), + workspace_scope: Some(proto::workspace_selector(&ws)), + }) + .await + .map(drop); + failures.check("user GetProvider (seeded)", check_ok(provider)); + for rpc in [Rpc::CreateProvider, Rpc::AddWorkspaceMember] { + let outcome = + check_workspace_admin_required(rpc.call(&mut user.client, &ws).await, &ws, &sub); + failures.check(format!("user {rpc:?}"), outcome); + } + let approve = Rpc::ApproveDraftChunk.call(&mut user.client, &ws).await; + failures.check( + "user ApproveDraftChunk", + check_code(approve, Code::NotFound), + ); + let listed = list_workspace_names(&mut user.client).await; + failures.check( + "user ListWorkspaces", + if listed.contains(&ws) + && !listed.contains(&other.name) + && !listed.iter().any(|name| name == "default") + { + Ok(()) + } else { + Err(format!( + "expected {ws} and neither {} nor default; got {listed:?}", + other.name + )) + }, + ); + + // As a workspace admin. + remove_member(&mut admin, &ws, &sub).await; + add_member(&mut admin, &workspace.name, &sub, ADMIN_ROLE).await; + let created = user + .client + .create_provider(provider_request(&ws, "e2e-authz-ws-admin-prov")) + .await + .map(drop); + failures.check("admin CreateProvider", check_ok(created)); + let added = user + .client + .add_workspace_member(proto::AddWorkspaceMemberRequest { + workspace_scope: Some(proto::workspace_selector(&ws)), + principal_subject: "another-subject".to_string(), + role: USER_ROLE, + ..Default::default() + }) + .await + .map(drop); + failures.check("admin AddWorkspaceMember (user role)", check_ok(added)); + let granted = user + .client + .add_workspace_member(proto::AddWorkspaceMemberRequest { + workspace_scope: Some(proto::workspace_selector(&ws)), + principal_subject: "admin-subject".to_string(), + role: ADMIN_ROLE, + ..Default::default() + }) + .await + .map(drop); + failures.check( + "admin AddWorkspaceMember (admin role)", + check_code(granted, Code::PermissionDenied), + ); + let all = Some(proto::all_workspaces_selector()); + let sandboxes = user + .client + .list_sandboxes(proto::ListSandboxesRequest { + workspace_scope: all.clone(), + ..Default::default() + }) + .await + .map(drop); + failures.check( + "admin ListSandboxes (all workspaces)", + check_code(sandboxes, Code::PermissionDenied), + ); + let providers = user + .client + .list_providers(proto::ListProvidersRequest { + workspace_scope: all.clone(), + ..Default::default() + }) + .await + .map(drop); + failures.check( + "admin ListProviders (all workspaces)", + check_code(providers, Code::PermissionDenied), + ); + let services = user + .client + .list_services(proto::ListServicesRequest { + workspace_scope: all, + ..Default::default() + }) + .await + .map(drop); + failures.check( + "admin ListServices (all workspaces)", + check_code(services, Code::PermissionDenied), + ); + + // A workspace admin of `default` still can't change global settings. + add_member(&mut admin, "default", &sub, ADMIN_ROLE).await; + let global = user + .client + .update_config(proto::UpdateConfigRequest { + setting_key: "log_level".to_string(), + delete_setting: true, + global: true, + ..Default::default() + }) + .await + .map(drop); + failures.check( + "default admin UpdateConfig (global)", + check_platform_admin_required(global), + ); + remove_member(&mut admin, "default", &sub).await; + + let _ = admin + .client + .delete_provider(proto::DeleteProviderRequest { + name: "e2e-authz-ws-admin-prov".to_string(), + workspace_scope: Some(proto::workspace_selector(&ws)), + ..Default::default() + }) + .await; + remove_member(&mut admin, &ws, &sub).await; + remove_member(&mut admin, &ws, "another-subject").await; + other.delete(&mut admin).await; + workspace.delete(&mut admin).await; + failures.assert_none(); +} + +/// Platform provider profiles and global policy history need the Platform +/// Admin: a user is refused, the Platform Admin isn't. +#[tokio::test] +#[serial(oidc_pkce)] +async fn platform_scope_requires_platform_admin() { + let mut admin = admin().await; + let mut user = user().await; + let mut failures = Failures::default(); + for rpc in PlatformRpc::ALL { + let outcome = check_platform_admin_required(rpc.call(&mut user.client).await); + failures.check(format!("user {rpc:?}"), outcome); + let outcome = check_not_denied(rpc.call(&mut admin.client).await); + failures.check(format!("admin {rpc:?}"), outcome); + } + failures.assert_none(); +} diff --git a/e2e/python/Dockerfile.workload b/e2e/workload/Dockerfile similarity index 100% rename from e2e/python/Dockerfile.workload rename to e2e/workload/Dockerfile diff --git a/tasks/scripts/e2e-build-workload.sh b/tasks/scripts/e2e-build-workload.sh index a7679d9679..2988eeb080 100644 --- a/tasks/scripts/e2e-build-workload.sh +++ b/tasks/scripts/e2e-build-workload.sh @@ -6,7 +6,7 @@ set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" # shellcheck source=tasks/scripts/container-engine.sh source "${ROOT}/tasks/scripts/container-engine.sh" -ce_build --load --file "${ROOT}/e2e/python/Dockerfile.workload" \ +ce_build --load --file "${ROOT}/e2e/workload/Dockerfile" \ --build-arg "PYTHON_VERSION=$(cat "${ROOT}/.python-version")" \ --tag openshell/e2e-python:dev \ - "${ROOT}/e2e/python" + "${ROOT}/e2e/workload" diff --git a/tasks/test.toml b/tasks/test.toml index 26c2ddd6a5..4a26722667 100644 --- a/tasks/test.toml +++ b/tasks/test.toml @@ -124,8 +124,8 @@ run = [ hide = true [e2e] -description = "Run all end-to-end tests (Rust + Python + MCP)" -depends = ["e2e:rust", "e2e:python", "e2e:mcp"] +description = "Run all end-to-end tests (Rust + MCP)" +depends = ["e2e:rust", "e2e:mcp"] ["e2e:test"] description = "Build the current checkout and run a named host or Nix test-guest E2E suite" @@ -201,6 +201,10 @@ run = "OPENSHELL_TEST_CONTAINER_ENGINE=docker e2e/with-docker-gateway.sh cargo t description = "Run the shared OCI image feature suite against a Podman-backed gateway" run = "OPENSHELL_TEST_CONTAINER_ENGINE=podman e2e/with-podman-gateway.sh cargo test --locked --manifest-path tests/suites/features/Cargo.toml -p oci-image -- --test-threads 1 --nocapture" +["e2e:gateway-mtls"] +description = "Run the gateway mTLS feature suite against a Docker-backed gateway" +run = "e2e/with-docker-gateway.sh cargo test --locked --manifest-path tests/suites/features/Cargo.toml -p gateway-mtls -- --test-threads 1 --nocapture" + ["e2e:testkit"] description = "Run the testkit suites in tests/suites/conformance against a Docker-backed gateway" depends = ["e2e:workload:build"] @@ -246,15 +250,6 @@ run = "bash e2e/mcp-conformance.sh" description = "Alias for e2e:mcp" depends = ["e2e:mcp"] -["e2e:python"] -description = "Run Python e2e tests against a Docker-backed gateway (E2E_PARALLEL=N or 'auto'; default 5)" -depends = ["python:proto"] -env = { UV_NO_SYNC = "1", PYTHONPATH = "python" } -run = [ - "CONTAINER_ENGINE=docker bash tasks/scripts/e2e-build-workload.sh", - "OPENSHELL_E2E_DOCKER_SANDBOX_IMAGE=openshell/e2e-python:dev OPENSHELL_E2E_DOCKER_SANDBOX_IMAGE_PULL_POLICY=never e2e/with-docker-gateway.sh uv run pytest -o python_files='test_*.py *_test.py' -m 'not gpu' -n ${E2E_PARALLEL:-5} e2e/python", -] - ["e2e:podman"] description = "Run Rust CLI e2e tests against a Podman-backed gateway" run = "e2e/rust/e2e-podman.sh" @@ -264,23 +259,15 @@ description = "Run the Podman Rust e2e targets enabled in required branch CI" run = "OPENSHELL_E2E_PODMAN_TEST_SET=ci e2e/rust/e2e-podman.sh" ["e2e:oidc-pkce"] -description = "Run Linux browser PKCE and RBAC e2e tests against Keycloak and a Podman gateway" +description = "Run Linux browser PKCE, RBAC, and workspace authorization e2e tests against Keycloak and a Podman gateway" run = [ - "CONTAINER_RUNTIME=podman e2e/with-keycloak.sh env OPENSHELL_E2E_OIDC_GATEWAY=1 e2e/with-podman-gateway.sh cargo test --manifest-path e2e/rust/Cargo.toml --features e2e-oidc-pkce --test oidc_pkce", + "CONTAINER_RUNTIME=podman e2e/with-keycloak.sh env OPENSHELL_E2E_OIDC_GATEWAY=1 e2e/with-podman-gateway.sh cargo test --manifest-path e2e/rust/Cargo.toml --features e2e-oidc-pkce --test oidc_pkce --test oidc_authz", ] ["e2e:oidc-pkce:docker"] -description = "Run Linux browser PKCE and RBAC e2e tests against Keycloak and a Docker gateway" -run = [ - "CONTAINER_RUNTIME=docker e2e/with-keycloak.sh env OPENSHELL_E2E_OIDC_GATEWAY=1 e2e/with-docker-gateway.sh cargo test --manifest-path e2e/rust/Cargo.toml --features e2e-oidc-pkce --test oidc_pkce", -] - -["e2e:oidc-python:docker"] -description = "Run Python OIDC and workspace authorization e2e tests against Keycloak and a Docker gateway" -depends = ["python:proto"] -env = { UV_NO_SYNC = "1", PYTHONPATH = "python" } +description = "Run Linux browser PKCE, RBAC, and workspace authorization e2e tests against Keycloak and a Docker gateway" run = [ - "CONTAINER_RUNTIME=docker e2e/with-keycloak.sh env OPENSHELL_E2E_OIDC_GATEWAY=1 e2e/with-docker-gateway.sh uv run pytest -m 'not gpu' e2e/python/oidc", + "CONTAINER_RUNTIME=docker e2e/with-keycloak.sh env OPENSHELL_E2E_OIDC_GATEWAY=1 e2e/with-docker-gateway.sh cargo test --manifest-path e2e/rust/Cargo.toml --features e2e-oidc-pkce --test oidc_pkce --test oidc_authz", ] ["e2e:podman:gpu"] diff --git a/tests/suites/conformance/src/lib.rs b/tests/suites/conformance/src/lib.rs index cabd7f3a3b..5b1cae6961 100644 --- a/tests/suites/conformance/src/lib.rs +++ b/tests/suites/conformance/src/lib.rs @@ -17,7 +17,11 @@ mod labels; mod lifecycle; mod persistent_workspace; mod policy_advisor; +mod policy_enforcement; +mod policy_validation; mod process; +mod provider_credentials; mod settings; mod smoke; +mod support; mod templates; diff --git a/tests/suites/conformance/src/policy_enforcement.rs b/tests/suites/conformance/src/policy_enforcement.rs new file mode 100644 index 0000000000..44ebf192b5 --- /dev/null +++ b/tests/suites/conformance/src/policy_enforcement.rs @@ -0,0 +1,167 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! Policy enforcement inside the sandbox: the filesystem rules Landlock +//! applies to exec'd processes, the identity they run as, and connections +//! the network policy doesn't allow. +//! +//! Ported from `e2e/python/test_sandbox_landlock.py`, +//! `e2e/python/test_sandbox_policy.py` and the Landlock part of +//! `e2e/python/test_sandbox_venv.py`. + +use testkit::Context; +use testkit::policy; + +use crate::support::python_sandbox; + +/// Probes the filesystem from inside the sandbox and prints one +/// ` ` line each. `OK` means allowed; `EACCES`, `EPERM` and +/// `ERROR:` mean refused. +const FILESYSTEM_PROBES: &str = r#" +import errno, os, pwd + +def outcome(action): + try: + action() + return "OK" + except OSError as error: + return errno.errorcode.get(error.errno, f"ERROR:{error.errno}") + +def write(directory): + with open(os.path.join(directory, ".landlock-probe"), "w") as f: + f.write("probe") + +def rename_across_directories(): + os.makedirs("/sandbox/refer-src", exist_ok=True) + os.makedirs("/sandbox/refer-dst", exist_ok=True) + with open("/sandbox/refer-src/file", "w") as f: + f.write("probe") + os.rename("/sandbox/refer-src/file", "/sandbox/refer-dst/file") + +print("user", pwd.getpwuid(os.getuid()).pw_name) +for path in ["/usr", "/tmp", "/sandbox", "/home/sandbox"]: + print("write", path, outcome(lambda: write(path))) +for path in ["/usr", "/etc", "/opt", "/root"]: + print("read", path, outcome(lambda: os.listdir(path))) +print("rename /sandbox", outcome(rename_across_directories)) +home = os.stat("/home/sandbox").st_uid == os.getuid() if os.path.isdir("/home/sandbox") else False +print("owns /home/sandbox", home) +"#; + +/// Exec'd commands run as the policy's user. Landlock lets them read and +/// write the policy's read-write paths, read but not write its read-only +/// paths, and touch nothing outside the policy, even a directory the user +/// owns. Renames between directories work, which uv and pip rely on +/// (Landlock ABI v2's refer right). +#[testkit::test] +async fn landlock_enforces_filesystem_policy(cx: &mut Context) -> testkit::Result { + let sandbox = python_sandbox(cx, "ll", policy::base(), &[]).await?; + let output = sandbox + .exec(["python3", "-c", FILESYSTEM_PROBES]) + .await? + .success()?; + let probes: Vec<(String, String)> = output + .stdout + .lines() + .filter_map(|line| line.rsplit_once(' ')) + .map(|(probe, result)| (probe.to_string(), result.to_string())) + .collect(); + let result = |probe: &str| { + probes + .iter() + .find(|(name, _)| name == probe) + .map(|(_, result)| result.as_str()) + }; + let refused = |probe: &str| { + result(probe).is_some_and(|result| { + matches!(result, "EACCES" | "EPERM") || result.starts_with("ERROR:") + }) + }; + + testkit::ensure!( + result("user") == Some("sandbox"), + "exec ran as {:?}; expected the policy's user sandbox\n{}", + result("user"), + output.stdout + ); + for allowed in [ + "write /tmp", + "write /sandbox", + "read /usr", + "read /etc", + "rename /sandbox", + ] { + testkit::ensure!( + result(allowed) == Some("OK"), + "{allowed} was {:?}; expected OK\n{}", + result(allowed), + output.stdout + ); + } + for denied in ["write /usr", "read /opt", "read /root"] { + testkit::ensure!( + refused(denied), + "{denied} was {:?}; expected a refusal\n{}", + result(denied), + output.stdout + ); + } + // Not every image has a home directory for the sandbox user. When it + // does, owning it doesn't get around Landlock. + if result("owns /home/sandbox") == Some("True") { + testkit::ensure!( + refused("write /home/sandbox"), + "write /home/sandbox was {:?}; Landlock should refuse it although the user owns it\n{}", + result("write /home/sandbox"), + output.stdout + ); + } + Ok(()) +} + +/// Prints the errno of a TCP connect to each `host:port` argument, or 0 if +/// it connected. +const CONNECT_ERRNO: &str = r" +import socket, sys +for target in sys.argv[1:]: + host, port = target.rsplit(':', 1) + try: + with socket.create_connection((host, int(port)), timeout=5): + print(target, 0) + except OSError as error: + print(target, error.errno or -1) +"; + +/// A workload's own socket fails fast with a permission error when it +/// connects to a destination no rule allows, to an allowed host on a port +/// the rule doesn't list, or to an allowed endpoint from a binary the rule +/// doesn't list. Nothing leaves the sandbox, so the destinations needn't be +/// reachable. +#[testkit::test] +async fn transparent_tcp_denies_unauthorized_connections(cx: &mut Context) -> testkit::Result { + let mut policy = policy::base(); + policy::allow(&mut policy, "wrong_port", "1.1.1.1", 80, &["/**"]); + policy::allow(&mut policy, "wrong_binary", "1.0.0.1", 443, &["/bin/false"]); + let sandbox = python_sandbox(cx, "tcp", policy, &[]).await?; + + // 9.9.9.9 has no rule, 1.1.1.1 is allowed only on port 80, and 1.0.0.1 + // only for /bin/false. + let targets = ["9.9.9.9:443", "1.1.1.1:443", "1.0.0.1:443"]; + let mut command = vec!["python3", "-c", CONNECT_ERRNO]; + command.extend(targets); + let output = sandbox.exec(command).await?.success()?; + for target in targets { + let errno = output + .stdout + .lines() + .find_map(|line| line.strip_prefix(target)) + .map(str::trim); + // EPERM is 1 and EACCES is 13 on Linux. + testkit::ensure!( + matches!(errno, Some("1" | "13")), + "connect to {target} ended with errno {errno:?}; expected EPERM or EACCES\n{}", + output.stdout + ); + } + Ok(()) +} diff --git a/tests/suites/conformance/src/policy_validation.rs b/tests/suites/conformance/src/policy_validation.rs new file mode 100644 index 0000000000..566ca581d0 --- /dev/null +++ b/tests/suites/conformance/src/policy_validation.rs @@ -0,0 +1,120 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! Policy validation at the gateway: which sandbox policies create and +//! update refuse, and what the gateway fills in before it stores a policy. +//! +//! Ported from `e2e/python/test_policy_validation.py` and the ambiguity case +//! of `e2e/python/test_sandbox_policy.py`. + +use testkit::policy; +use testkit::sdk::raw::proto; +use testkit::{Context, Error, GrpcCode, Result}; + +/// The MCP protocol version the gateway stores for an MCP endpoint that +/// names none. Changing the default changes what existing policies mean, so +/// this test pins it. +const DEFAULT_MCP_VERSION: &str = "2025-11-25"; + +/// Create refuses a policy that runs the workload as root, has `..` in a +/// filesystem path, or makes `/` writable, and says which rule it broke. +#[testkit::test] +async fn create_rejects_unsafe_policies(cx: &mut Context) -> testkit::Result { + let mut root = policy::base(); + root.process.get_or_insert_default().run_as_user = "root".to_string(); + + let mut traversal = policy::base(); + traversal.filesystem.get_or_insert_default().read_only = vec!["/usr/../etc/shadow".to_string()]; + + let mut broad = policy::base(); + broad.filesystem.get_or_insert_default().read_write = vec!["/".to_string()]; + + for (suffix, policy, reason) in [ + ("ur", root, "root"), + ("ut", traversal, "traversal"), + ("ub", broad, "broad"), + ] { + let message = expect_refused(cx, suffix, policy, GrpcCode::InvalidArgument).await?; + testkit::ensure!( + message.to_lowercase().contains(reason), + "policy {suffix} was refused without mentioning {reason:?}: {message}" + ); + } + Ok(()) +} + +/// Create refuses a policy whose rules pin the same endpoint to different +/// destination addresses, before it launches anything. +#[testkit::test] +async fn create_rejects_ambiguous_endpoint_pinning(cx: &mut Context) -> testkit::Result { + const HOST: &str = "10.200.0.2"; + const PORT: u16 = 19876; + let mut ambiguous = policy::base(); + policy::allow(&mut ambiguous, "user_rule", HOST, PORT, &["/**"]); + policy::allow(&mut ambiguous, "approved_rule", HOST, PORT, &["/**"]).allowed_ips = + vec!["10.200.0.0/24".to_string()]; + + let message = expect_refused(cx, "amb", ambiguous, GrpcCode::FailedPrecondition).await?; + testkit::ensure!( + message.contains("network endpoint ambiguity validation failed") + && message.contains("allowed_ips"), + "the refusal doesn't name the ambiguous allowed_ips: {message}" + ); + Ok(()) +} + +/// An MCP endpoint without MCP options is stored, and served to the +/// supervisor, with the default protocol version. +#[testkit::test] +async fn create_stores_default_mcp_version(cx: &mut Context) -> testkit::Result { + let mut mcp = policy::base(); + let endpoint = policy::allow(&mut mcp, "mcp_default", "mcp.example.com", 443, &["/**"]); + endpoint.protocol = "mcp".to_string(); + endpoint.rules = vec![proto::L7Rule { + allow: Some(proto::L7Allow { + method: "initialize".to_string(), + ..proto::L7Allow::default() + }), + }]; + testkit::ensure!(endpoint.mcp.is_none(), "the test policy names MCP options"); + + // The policy is stored at create, so the sandbox needn't become ready. + let sandbox = cx.sandbox("mcp").policy(mcp).create().await?; + + let stored = sandbox.spec().await?.and_then(|spec| spec.policy); + expect_default_mcp_version("the stored policy", stored.as_ref())?; + + let served = sandbox.config().await?.policy; + expect_default_mcp_version("the sandbox config", served.as_ref()) +} + +/// Fail unless the gateway refuses to create a sandbox with `policy` with +/// `code`, and return the refusal. +async fn expect_refused( + cx: &Context, + suffix: &str, + policy: proto::SandboxPolicy, + code: GrpcCode, +) -> Result { + match cx.sandbox(suffix).policy(policy).create().await { + Ok(sandbox) => Err(Error::msg(format!( + "the gateway accepted sandbox {}", + sandbox.name() + ))), + Err(error) if error.grpc_code() == Some(code) => Ok(error.to_string()), + Err(error) => Err(error.context(format!("expected {code:?}"))), + } +} + +fn expect_default_mcp_version(what: &str, policy: Option<&proto::SandboxPolicy>) -> Result { + let versions = policy + .and_then(|policy| policy.network_policies.get("mcp_default")) + .and_then(|rule| rule.endpoints.first()) + .and_then(|endpoint| endpoint.mcp.as_ref()) + .map(|mcp| mcp.versions.clone()); + testkit::ensure!( + versions.as_deref() == Some(&[DEFAULT_MCP_VERSION.to_string()][..]), + "{what} has MCP versions {versions:?}; expected [{DEFAULT_MCP_VERSION:?}]" + ); + Ok(()) +} diff --git a/tests/suites/conformance/src/provider_credentials.rs b/tests/suites/conformance/src/provider_credentials.rs new file mode 100644 index 0000000000..be7e251bb0 --- /dev/null +++ b/tests/suites/conformance/src/provider_credentials.rs @@ -0,0 +1,365 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! Provider credentials: where the supervisor substitutes them in requests, +//! what the Google Cloud metadata server in the sandbox hands out, and that +//! the gateway neither stores them in the sandbox spec nor accepts an +//! unknown provider. +//! +//! Ported from `e2e/python/test_sandbox_providers.py`. Its other cases have +//! Rust coverage already; `PLAN-kw23.md` on the porting branch maps them. +//! The upstream server runs on the test host ([`host_server_support`]), so +//! the placement tests require sandboxes to reach it as +//! `host.openshell.internal`. + +use host_server_support::{HttpRecorder, Request}; +use testkit::policy::{self, Access, Endpoint as _}; +use testkit::sdk::raw::proto; +use testkit::{Context, Error, GrpcCode, PYTHON_BINARIES, Result, ResultExt as _}; + +use crate::support::python_sandbox; + +const TEST_HOST: &str = "host.openshell.internal"; +const TOKEN_ENV: &str = "E2E_PLACEMENT_TOKEN"; +const TEST_SECRET: &str = "synthetic-placement-secret"; +const PLACEHOLDER_PREFIX: &str = "openshell:resolve:env:"; + +/// Sends the provider's token from `argv[2]`'s placement to `TEST_HOST` on +/// the port in `argv[1]`: first in three places the profile doesn't name +/// (a query parameter, another header, the path), then in a form body, then +/// in the place the profile names. Prints one ` ` line each. +const PLACEMENT_CLIENT: &str = r#" +import os, sys, urllib.error, urllib.parse, urllib.request +port, placement = sys.argv[1], sys.argv[2] +token = os.environ["E2E_PLACEMENT_TOKEN"] +root = f"http://host.openshell.internal:{port}" +quoted = urllib.parse.quote(token, safe="") +opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) +requests = [ + urllib.request.Request(root + "/search?" + urllib.parse.urlencode({"q": "rust", "probe": token})), + urllib.request.Request(root + "/search", headers={"X-Other-Credential": token}), + urllib.request.Request(root + f"/{quoted}/search"), + urllib.request.Request(root + "/search", data=urllib.parse.urlencode({"token": token}).encode(), + headers={"Content-Type": "application/x-www-form-urlencoded"}), +] +if placement == "query": + requests.append(urllib.request.Request(root + "/search?" + urllib.parse.urlencode({"q": "rust", "api_key": token}))) +else: + requests.append(urllib.request.Request(root + "/search?q=rust", headers={"Authorization": f"token {token}"})) +for request in requests: + try: + with opener.open(request, timeout=30) as response: + print(response.status, response.read().decode().replace("\n", " ")) + except urllib.error.HTTPError as error: + print(error.code, error.read().decode().replace("\n", " ")) +"#; + +/// A credential the profile places in the Authorization header is refused, +/// not substituted, when the workload puts it in a query parameter, another +/// header or the path, so an upstream that reflects requests can't reveal +/// it. The endpoint's opt-in body rewriting and the Authorization header +/// get the real value. +#[testkit::test] +async fn header_credential_is_refused_in_other_request_locations( + cx: &mut Context, +) -> testkit::Result { + let upstream = expect_placement(cx, "phdr", "authorization").await?; + let valid = &upstream[1]; + testkit::ensure!( + valid + .head + .to_lowercase() + .contains(&format!("authorization: token {TEST_SECRET}")), + "the Authorization header didn't reach upstream with the credential:\n{}", + valid.head + ); + Ok(()) +} + +/// The same, for a credential the profile places in a query parameter: only +/// that parameter gets the real value. +#[testkit::test] +async fn query_credential_is_refused_in_other_request_locations( + cx: &mut Context, +) -> testkit::Result { + let upstream = expect_placement(cx, "pqry", "query").await?; + let valid = &upstream[1]; + let request_line = valid.head.lines().next().unwrap_or_default(); + testkit::ensure!( + request_line.contains(&format!("api_key={TEST_SECRET}")), + "the api_key parameter didn't reach upstream with the credential: {request_line}" + ); + Ok(()) +} + +/// Runs [`PLACEMENT_CLIENT`] for `placement` and checks what every placement +/// shares: the three reflective requests get a 403 naming the mismatch, +/// without the secret, and never reach upstream; the form body and the +/// valid request do. Returns those two upstream requests. +async fn expect_placement(cx: &Context, suffix: &str, placement: &str) -> Result> { + let server = HttpRecorder::start("ok").await?; + let credential = match placement { + "query" => " auth_style: query\n query_param: api_key\n", + _ => " auth_style: bearer\n header_name: authorization\n", + }; + let provider = cx.name(suffix); + let profile = format!( + "id: {provider} +display_name: E2E Credential Placement +category: other +credentials: + - name: token + env_vars: [{TOKEN_ENV}] + required: true +{credential}endpoints: + - host: {TEST_HOST} + port: {port} + protocol: rest + access: full + request_body_credential_rewrite: true +binaries: +{binaries}", + port = server.port, + binaries = binaries_yaml(), + ); + cx.install_provider(&provider, &profile, &[(TOKEN_ENV, TEST_SECRET)]) + .await?; + let sandbox = python_sandbox(cx, suffix, policy::base(), &[&provider]).await?; + + let output = sandbox + .exec([ + "python3", + "-c", + PLACEMENT_CLIENT, + &server.port.to_string(), + placement, + ]) + .await? + .success() + .context("run the placement client in the sandbox")?; + let responses: Vec<&str> = output.stdout.lines().collect(); + testkit::ensure!( + responses.len() == 5, + "expected five responses; got:\n{}", + output.stdout + ); + for (location, response) in ["query", "other header", "path"].iter().zip(&responses) { + testkit::ensure!( + response.starts_with("403 ") + && response.contains("credential_placement_mismatch") + && !response.contains(TEST_SECRET), + "the credential in the {location} got {response:?}; expected a 403 placement mismatch without the secret" + ); + } + for response in &responses[3..] { + testkit::ensure!( + response.starts_with("200 "), + "expected the body rewrite and the valid placement to succeed:\n{}", + output.stdout + ); + } + + let upstream = server.wait_for_requests(2).await?; + testkit::ensure!( + upstream.len() == 2, + "expected only the body rewrite and the valid request upstream; got {}:\n{upstream:#?}", + upstream.len() + ); + testkit::ensure!( + upstream[0].body == format!("token={TEST_SECRET}"), + "the opt-in body rewrite didn't substitute the credential: {:?}", + upstream[0].body + ); + Ok(upstream) +} + +/// Fetches the Google Cloud metadata server named by `GCE_METADATA_HOST` +/// the way Google's SDKs do, and prints what it returned as JSON. +const METADATA_CLIENT: &str = r#" +import json, os, urllib.error, urllib.request +host = os.environ["GCE_METADATA_HOST"] +opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) +def get(path): + request = urllib.request.Request(f"http://{host}/computeMetadata/v1/{path}", headers={"Metadata-Flavor": "Google"}) + try: + with opener.open(request, timeout=10) as response: + return response.status, response.read().decode() + except urllib.error.HTTPError as error: + return error.code, error.read().decode() +print(json.dumps({ + "metadata_host": host, + "metadata_ip": os.environ.get("GCE_METADATA_IP"), + "project": get("project/project-id"), + "email": get("instance/service-accounts/default/email"), + "token": get("instance/service-accounts/default/token"), +})) +"#; + +/// The metadata server in the sandbox serves a Google Cloud provider's +/// project and service account, and an access token that is a placeholder +/// for the credential, when a policy binds the provider. Without a binding +/// it withholds the token. +#[testkit::test] +async fn google_metadata_serves_placeholder_token(cx: &mut Context) -> testkit::Result { + const PROJECT: &str = "metadata-test-project"; + const EMAIL: &str = "sdk@metadata-test-project.iam.gserviceaccount.com"; + const SECRET: &str = "gcp-metadata-real-secret"; + const KEY: &str = "GCP_ADC_ACCESS_TOKEN"; + + let provider = cx.name("gcp"); + let cli = cx.cli_handle(); + let owned = provider.clone(); + cx.on_cleanup(format!("delete provider {provider}"), async move || { + cli.run(["provider", "delete", &owned]).await?.success()?; + Ok(()) + }); + cx.cli_with_env( + &[(KEY, SECRET)], + [ + "provider", + "create", + "--name", + &provider, + "--type", + "google-cloud", + "--credential", + KEY, + "--config", + &format!("project_id={PROJECT}"), + "--config", + &format!("service_account_email={EMAIL}"), + ], + ) + .await? + .success() + .context(format!("create provider {provider}"))?; + + let mut bound = policy::base(); + policy::allow( + &mut bound, + "gcp_api", + "storage.googleapis.com", + 443, + PYTHON_BINARIES, + ) + .rest(Access::Full) + .credential_binding(&provider); + let metadata = fetch_metadata(cx, "gcpb", bound, &provider).await?; + let field = |name: &str| metadata[name].clone(); + testkit::ensure!( + field("metadata_host") == "127.0.0.1:8174" && field("metadata_ip") == "127.0.0.1:8174", + "GCE_METADATA_HOST and GCE_METADATA_IP should both name the sandbox's metadata server: {metadata}" + ); + testkit::ensure!( + field("project") == serde_json::json!([200, PROJECT]) + && field("email") == serde_json::json!([200, EMAIL]), + "unexpected project or service account: {metadata}" + ); + testkit::ensure!( + !metadata.to_string().contains(SECRET), + "the metadata server revealed the credential: {metadata}" + ); + let token = field("token"); + testkit::ensure!(token[0] == 200, "the token request failed: {metadata}"); + let token: serde_json::Value = serde_json::from_str(token[1].as_str().unwrap_or_default()) + .context("parse the token response")?; + let access_token = token["access_token"].as_str().unwrap_or_default(); + testkit::ensure!( + is_placeholder_for(access_token, KEY) && token["expires_in"].as_u64().is_some(), + "the token response isn't a placeholder with an expiry: {token}" + ); + + let unbound = fetch_metadata(cx, "gcpf", policy::base(), &provider).await?; + testkit::ensure!( + unbound["token"][0] == 503 && !unbound.to_string().contains(SECRET), + "without a policy binding the token should be withheld with 503: {unbound}" + ); + Ok(()) +} + +async fn fetch_metadata( + cx: &Context, + suffix: &str, + policy: proto::SandboxPolicy, + provider: &str, +) -> Result { + let sandbox = python_sandbox(cx, suffix, policy, &[provider]).await?; + let output = sandbox + .exec(["python3", "-c", METADATA_CLIENT]) + .await? + .success() + .context("query the metadata server in the sandbox")?; + serde_json::from_str(output.stdout.trim()).context(format!( + "parse the metadata client output: {}", + output.stdout + )) +} + +/// Create refuses a sandbox that names a provider that doesn't exist, and +/// names it. +#[testkit::test] +async fn create_rejects_unknown_provider(cx: &mut Context) -> testkit::Result { + let missing = cx.name("nope"); + let result = cx + .sandbox("unk") + .spec(|spec| spec.providers = vec![missing.clone()]) + .create() + .await; + match result { + Ok(sandbox) => Err(Error::msg(format!( + "the gateway accepted sandbox {} with unknown provider {missing}", + sandbox.name() + ))), + Err(error) if error.grpc_code() == Some(GrpcCode::FailedPrecondition) => { + testkit::ensure!( + error.to_string().contains(&missing), + "the refusal doesn't name the provider: {error}" + ); + Ok(()) + } + Err(error) => Err(error.context("expected a failed precondition")), + } +} + +/// The gateway resolves provider credentials when it launches the sandbox, +/// and doesn't copy them into the stored sandbox spec. +#[testkit::test] +async fn provider_credentials_are_not_stored_in_sandbox_spec(cx: &mut Context) -> testkit::Result { + const KEY: &str = "ANTHROPIC_API_KEY"; + let provider = cx.name("nop"); + cx.create_provider(&provider, "claude-code", &[(KEY, "sk-should-not-persist")]) + .await?; + let sandbox = cx + .sandbox("nop") + .spec(|spec| spec.providers = vec![provider.clone()]) + .command(["sleep", "infinity"]) + .create() + .await?; + sandbox.wait_ready().await?; + + let environment = sandbox + .spec() + .await? + .map(|spec| spec.environment) + .unwrap_or_default(); + testkit::ensure!( + !environment.contains_key(KEY), + "the stored sandbox spec has the provider credential {KEY} in its environment" + ); + Ok(()) +} + +/// Whether `value` is a credential placeholder for the environment variable +/// `key`: the plain form, or a versioned or stable one ending in `_`. +fn is_placeholder_for(value: &str, key: &str) -> bool { + value.strip_prefix(PLACEHOLDER_PREFIX).is_some_and(|token| { + token == key || (token.starts_with(['v', 's']) && token.ends_with(&format!("_{key}"))) + }) +} + +fn binaries_yaml() -> String { + PYTHON_BINARIES + .iter() + .map(|path| format!(" - path: {path}\n")) + .collect() +} diff --git a/tests/suites/conformance/src/support.rs b/tests/suites/conformance/src/support.rs new file mode 100644 index 0000000000..a0f60f8f8a --- /dev/null +++ b/tests/suites/conformance/src/support.rs @@ -0,0 +1,30 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! Helpers shared by the suites in this crate. + +use testkit::sdk::raw::proto; +use testkit::{Context, Result, Sandbox}; + +/// Create a long-running sandbox from the Python image +/// ([`testkit::python_image`]) with `policy` and `providers`, and wait until +/// it is ready. +pub(crate) async fn python_sandbox( + cx: &Context, + suffix: &str, + policy: proto::SandboxPolicy, + providers: &[&str], +) -> Result { + let sandbox = cx + .sandbox(suffix) + .policy(policy) + .spec(|spec| { + spec.image = Some(testkit::python_image()); + spec.providers = providers.iter().map(ToString::to_string).collect(); + }) + .command(["sleep", "infinity"]) + .create() + .await?; + sandbox.wait_ready().await?; + Ok(sandbox) +} diff --git a/tests/suites/features/Cargo.lock b/tests/suites/features/Cargo.lock index 0d91b3a2a9..22baedfe4d 100644 --- a/tests/suites/features/Cargo.lock +++ b/tests/suites/features/Cargo.lock @@ -996,6 +996,14 @@ dependencies = [ "slab", ] +[[package]] +name = "gateway-mtls" +version = "0.0.0" +dependencies = [ + "openshell-crypto", + "openshell-testkit", +] + [[package]] name = "generic-array" version = "0.14.7" diff --git a/tests/suites/features/Cargo.toml b/tests/suites/features/Cargo.toml index a592b6f028..db5baafa5c 100644 --- a/tests/suites/features/Cargo.toml +++ b/tests/suites/features/Cargo.toml @@ -3,4 +3,4 @@ [workspace] resolver = "2" -members = ["cdi", "host-gateway", "keycloak-provider-refresh", "kubernetes-updates", "oci-image"] +members = ["cdi", "gateway-mtls", "host-gateway", "keycloak-provider-refresh", "kubernetes-updates", "oci-image"] diff --git a/tests/suites/features/gateway-mtls/Cargo.toml b/tests/suites/features/gateway-mtls/Cargo.toml new file mode 100644 index 0000000000..88abd8be85 --- /dev/null +++ b/tests/suites/features/gateway-mtls/Cargo.toml @@ -0,0 +1,16 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +[package] +name = "gateway-mtls" +description = "Features suite: an mTLS gateway accepts only clients with a certificate from its CA as users" +version = "0.0.0" +edition = "2024" +rust-version = "1.94" +license = "Apache-2.0" +repository = "https://github.com/NVIDIA/OpenShell" +publish = false + +[dependencies] +openshell-crypto = { path = "../../../../crates/openshell-crypto" } +openshell-testkit = { path = "../../../testkit/testkit" } diff --git a/tests/suites/features/gateway-mtls/tests/gateway_mtls.rs b/tests/suites/features/gateway-mtls/tests/gateway_mtls.rs new file mode 100644 index 0000000000..77f46acaa2 --- /dev/null +++ b/tests/suites/features/gateway-mtls/tests/gateway_mtls.rs @@ -0,0 +1,182 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! An mTLS gateway's user boundary. The gateway accepts TLS clients without +//! a certificate, so supervisors can authenticate with sandbox tokens, but +//! only a client certificate from the gateway's CA makes a caller a user. +//! Health stays public. +//! +//! Ported from `e2e/python/test_security_tls.py`. The suite needs a gateway +//! registered with mTLS, such as the packaged gateway or the one +//! `e2e/with-docker-gateway.sh` starts. + +use std::path::{Path, PathBuf}; + +use testkit::sdk::raw::proto; +use testkit::sdk::{AuthConfig, ClientConfig, ClientIdentity, OpenShellClient, ServiceStatus}; +use testkit::{Context, Error, Gateway, GrpcCode, Result, ResultExt as _}; + +/// A client with the CLI's certificate calls Health and a user RPC. +#[testkit::test] +async fn client_certificate_authenticates_user(_cx: &mut Context) -> testkit::Result { + let (gateway, dir) = mtls_gateway()?; + let mut config = ClientConfig::new(gateway.endpoint.clone()); + config.ca_cert = Some(read(&dir.join("ca.crt"))?); + config.client_identity = Some(ClientIdentity::new( + read(&dir.join("tls.crt"))?, + read(&dir.join("tls.key"))?, + )); + let client = connect(config).await?; + expect_healthy(&client).await?; + list_sandboxes(&client, &gateway.workspace) + .await + .context("list sandboxes with the client certificate")?; + Ok(()) +} + +/// A client that trusts the gateway's CA but has no certificate reaches +/// Health, but a user RPC is unauthenticated, with or without a bearer +/// token the gateway can't verify. +#[testkit::test] +async fn ca_only_client_is_not_a_user(_cx: &mut Context) -> testkit::Result { + let (gateway, dir) = mtls_gateway()?; + let ca = read(&dir.join("ca.crt"))?; + + let mut anonymous = ClientConfig::new(gateway.endpoint.clone()); + anonymous.ca_cert = Some(ca.clone()); + let client = connect(anonymous).await?; + expect_healthy(&client).await?; + expect_unauthenticated(&client, &gateway.workspace, "without a certificate").await?; + + let mut bearer = ClientConfig::new(gateway.endpoint.clone()); + bearer.ca_cert = Some(ca); + bearer.auth = Some(AuthConfig::oidc("invalid-token")); + let client = connect(bearer).await?; + expect_unauthenticated( + &client, + &gateway.workspace, + "with an unverifiable bearer token", + ) + .await +} + +/// The gateway refuses the TLS handshake of a client whose certificate +/// another CA signed, so not even Health answers. +#[testkit::test] +async fn foreign_client_certificate_is_rejected(_cx: &mut Context) -> testkit::Result { + let (gateway, dir) = mtls_gateway()?; + let rogue = + openshell_crypto::pki::generate_simple_self_signed(vec!["rogue-client".to_string()]) + .map_err(|error| Error::msg(error.to_string())) + .context("generate a self-signed client certificate")?; + let key = rogue + .key_pair + .serialize_pem() + .map_err(|error| Error::msg(error.to_string())) + .context("encode the rogue key")?; + let mut config = ClientConfig::new(gateway.endpoint.clone()); + config.ca_cert = Some(read(&dir.join("ca.crt"))?); + config.client_identity = Some(ClientIdentity::new(rogue.cert.pem(), key)); + expect_refused(config, "a client certificate from another CA").await +} + +/// The gateway's port doesn't answer gRPC over plaintext. +#[testkit::test] +async fn plaintext_client_is_rejected(_cx: &mut Context) -> testkit::Result { + let (gateway, _) = mtls_gateway()?; + let endpoint = gateway + .endpoint + .strip_prefix("https://") + .map(|rest| format!("http://{rest}")) + .ok_or_else(|| Error::msg(format!("{} isn't an https endpoint", gateway.endpoint)))?; + expect_refused(ClientConfig::new(endpoint), "a plaintext connection").await +} + +/// The gateway under test, which must be an mTLS gateway, and its mTLS +/// directory. +fn mtls_gateway() -> Result<(Gateway, PathBuf)> { + let gateway = Gateway::from_env()?; + let dir = gateway.mtls_dir().map(Path::to_path_buf).ok_or_else(|| { + Error::msg(format!( + "this suite needs an mTLS gateway; {}", + gateway.describe() + )) + })?; + Ok((gateway, dir)) +} + +async fn connect(config: ClientConfig) -> Result { + let endpoint = config.gateway.clone(); + OpenShellClient::connect(config) + .await + .context(format!("connect to {endpoint}")) +} + +async fn expect_healthy(client: &OpenShellClient) -> Result { + let health = client.health().await.context("health")?; + testkit::ensure!( + health.status == ServiceStatus::Healthy, + "gateway reports {:?}", + health.status + ); + Ok(()) +} + +/// List the workspace's sandboxes, keeping the gateway's status code on +/// failure. +async fn list_sandboxes(client: &OpenShellClient, workspace: &str) -> Result { + client + .raw_grpc() + .list_sandboxes(proto::ListSandboxesRequest { + workspace_scope: Some(proto::workspace_selector(workspace)), + ..proto::ListSandboxesRequest::default() + }) + .await + .map(drop) + .map_err(Error::from) +} + +async fn expect_unauthenticated(client: &OpenShellClient, workspace: &str, how: &str) -> Result { + match list_sandboxes(client, workspace).await { + Ok(()) => Err(Error::msg(format!( + "the gateway listed sandboxes for a client {how}" + ))), + Err(error) if error.grpc_code() == Some(GrpcCode::Unauthenticated) => Ok(()), + Err(error) => Err(error.context(format!("expected unauthenticated for a client {how}"))), + } +} + +/// Fail unless a client with `config` can't connect, or its Health call +/// fails at the transport. +async fn expect_refused(config: ClientConfig, what: &str) -> Result { + let Ok(client) = OpenShellClient::connect(config).await else { + return Ok(()); + }; + match client + .raw_grpc() + .health(proto::HealthRequest::default()) + .await + { + Ok(_) => Err(Error::msg(format!( + "the gateway answered Health over {what}" + ))), + Err(status) + if matches!( + status.code(), + GrpcCode::Unavailable + | GrpcCode::Unknown + | GrpcCode::Internal + | GrpcCode::Unimplemented + ) => + { + Ok(()) + } + Err(status) => { + Err(Error::from(status).context(format!("expected a transport refusal for {what}"))) + } + } +} + +fn read(path: &Path) -> Result> { + std::fs::read(path).context(format!("read {}", path.display())) +} diff --git a/tests/testkit/README.md b/tests/testkit/README.md index 3ce6f58013..3997b2c708 100644 --- a/tests/testkit/README.md +++ b/tests/testkit/README.md @@ -85,7 +85,7 @@ fails. A killed test process skips cleanup, so names are unique to the run. `OPENSHELL_TEST_BASE_IMAGE`, or OpenShell's default sandbox image. - `testkit::python_image()` for sandboxes that run `python3`, curl, git, or openssl: `OPENSHELL_TEST_PYTHON_IMAGE`, or `openshell/e2e-python:dev` - (`e2e/python/Dockerfile.workload`). The tmachine installers load it into + (`e2e/workload/Dockerfile`). The tmachine installers load it into every environment; `mise run e2e:workload:build` builds it locally. An override image must run `python3` from one of `testkit::PYTHON_BINARIES`. - `cx.cli([...])` to run the installed CLI against the same gateway, with diff --git a/tests/testkit/testkit/src/context.rs b/tests/testkit/testkit/src/context.rs index 47221f68ec..bdd747ad08 100644 --- a/tests/testkit/testkit/src/context.rs +++ b/tests/testkit/testkit/src/context.rs @@ -614,6 +614,25 @@ impl Sandbox { .into_inner()) } + /// The sandbox's stored spec, as the gateway persisted it. + pub async fn spec(&self) -> Result> { + let request = proto::GetSandboxRequest { + name: self.name.clone(), + workspace_scope: self.handles.grpc.scope(), + }; + Ok(self + .handles + .grpc + .client() + .await? + .get_sandbox(request) + .await + .context(format!("get sandbox {}", self.name))? + .into_inner() + .sandbox + .and_then(|sandbox| sandbox.spec)) + } + /// The sandbox's current state. pub async fn get(&self) -> Result { self.handles diff --git a/tests/testkit/testkit/src/gateway.rs b/tests/testkit/testkit/src/gateway.rs index fef05833b7..670a6f64ce 100644 --- a/tests/testkit/testkit/src/gateway.rs +++ b/tests/testkit/testkit/src/gateway.rs @@ -121,6 +121,13 @@ impl Gateway { }) } + /// The directory holding the gateway's CA (`ca.crt`) and the client + /// certificate and key (`tls.crt`, `tls.key`) for mTLS, or `None` for a + /// gateway testkit reaches without a client certificate. + pub fn mtls_dir(&self) -> Option<&Path> { + self.mtls_dir.as_deref() + } + /// One line naming the gateway, for test logs. pub fn describe(&self) -> String { let auth = match (&self.mtls_dir, self.endpoint.starts_with("https://")) { diff --git a/tests/testkit/testkit/src/image.rs b/tests/testkit/testkit/src/image.rs index 07a6e2d937..0b3dc71ddc 100644 --- a/tests/testkit/testkit/src/image.rs +++ b/tests/testkit/testkit/src/image.rs @@ -14,7 +14,7 @@ const DEFAULT_BASE_IMAGE: &str = "nvcr.io/nvidia/base/ubuntu:24.04"; /// match it, and its connections are denied. const PYTHON_IMAGE_ENV: &str = "OPENSHELL_TEST_PYTHON_IMAGE"; /// The workload image for tests that run `python3`, curl, git or openssl in a -/// sandbox, built from `e2e/python/Dockerfile.workload`. It isn't published: +/// sandbox, built from `e2e/workload/Dockerfile`. It isn't published: /// the tmachine installers load it into every environment, and /// `mise run e2e:workload:build` builds it locally. const DEFAULT_PYTHON_IMAGE: &str = "openshell/e2e-python:dev"; diff --git a/tests/tmachine/artifacts.nix b/tests/tmachine/artifacts.nix index ef1be7ed98..756ef530d4 100644 --- a/tests/tmachine/artifacts.nix +++ b/tests/tmachine/artifacts.nix @@ -299,10 +299,10 @@ rec { docker build \ --platform linux/${dockerArch} \ - --file e2e/python/Dockerfile.workload \ + --file e2e/workload/Dockerfile \ --build-arg "PYTHON_VERSION=$(cat .python-version)" \ --tag openshell/e2e-python:dev \ - e2e/python + e2e/workload mkdir -p artifacts/images docker save \