mirror of
https://github.com/NVIDIA/OpenShell.git
synced 2026-10-17 05:31:16 +08:00
* feat(workspace): implement workspace model (Phase 1 of RFC 0011) Implements workspace and membership model providing hard isolation boundaries for multi-player OpenShell deployments. Workspace CRUD with Kubernetes-style Terminating phase for graceful deletion. All resources scoped by workspace via ObjectMeta. Membership RPCs for workspace access control. Persistence migration shifts name uniqueness to (object_type, workspace, name). Provider profiles support platform and workspace scoping. Service routing uses workspace-prefixed DNS labels. Inference routes renamed and workspace-scoped with DeleteInferenceRoute RPC. Python SDK with WorkspaceClient, two-method list pattern (workspace-scoped and for_all_workspaces), and workspace parameter on all methods. CLI workspace flags, TUI workspace cycling. K8s driver filters unmanaged CRs and uses delete preconditions. Podman driver uses immutable container IDs. Label serialization fixed across all put_if call sites. Signed-off-by: Derek Carr <decarr@redhat.com> * fix(cli): delegate sandbox upload command to existing upload function The standalone `sandbox upload` command reimplemented upload logic inline with two bugs: it used `Path::exists()` which follows symlinks (rejecting dangling symlinks), and it ran git-aware filtering on symlink sources. The `run::sandbox_upload()` function already handles both cases correctly via `sandbox_upload_plan()`. Replace the inline logic with a call to the existing function. Signed-off-by: Derek Carr <decarr@redhat.com> * fix(e2e): shorten sandbox names and fix test compatibility Shorten the sandbox name in initial_sparse_policy_is_acknowledged_as_loaded from 'e2e-2159-sparse-enrich' (22 chars) to 'e2e-sparse-enrich' (17 chars) to comply with MAX_ROUTABLE_NAME_LEN (19 chars). Also capture stderr in create_keep_with_args so future sandbox creation failures include the actual CLI error instead of reporting empty output. Signed-off-by: Derek Carr <decarr@redhat.com> * test(workspace): add test coverage for workspace CRUD and persistence isolation Add unit tests for workspace create happy path, get round-trip, get not-found, get empty-name rejection, already-exists error, and resolve_workspace not-found. Add persistence test proving cross-workspace name uniqueness (same name in different workspaces produces separate records). Add workspace name max-length boundary tests. Fix e2e harness to include stderr in name-parse-failure error path. Align Python e2e test_workspace_crud with try/finally pattern. Document provider profile catalog workspace scoping gap in RFC 0011. Signed-off-by: Derek Carr <decarr@redhat.com> * fix(examples): update examples for workspace model compatibility Shorten sandbox names in demo scripts to fit the 19-character MAX_ROUTABLE_NAME_LEN limit: policy-demo prefix to pd-, multi-agent notepad derives a short SANDBOX_TAG from the run ID, governance interceptor uses gs-PID-RANDOM. Update vscode-remote-sandbox.md SSH host aliases from openshell-{name} to openshell-{name}.{workspace} format. Signed-off-by: Derek Carr <decarr@redhat.com> * feat(sdk): add workspace-scoped client and workspace CRUD Add WorkspaceScopedClient modeled after kube::Api::namespaced — captures workspace once and injects it into every sandbox request. Add workspace CRUD methods (create, get, list, delete) and list_sandboxes_all_workspaces on OpenShellClient. Extend SandboxRef with workspace field and add WorkspaceRef type. Include mock tests for all new operations. Signed-off-by: Derek Carr <decarr@redhat.com> * fix(lint): resolve clippy warnings in workspace test assertions Signed-off-by: Derek Carr <decarr@redhat.com> * fix(docs): convert indented code blocks to fenced in RFC 0011 Signed-off-by: Derek Carr <decarr@redhat.com> * fix(lint): resolve clippy warnings and apply cargo fmt across workspace Auto-format with cargo fmt and fix clippy warnings exposed by the reformat: unnecessary qualifications, map_unwrap_or, identical match arms, unused variable prefix, dead code annotations, and let-unit-value in e2e harness. Signed-off-by: Derek Carr <decarr@redhat.com> * fix(workspace): address workspace scoping issues from review - Add workspace field to settings JSON output (CLI) - Skip Podman containers missing workspace label instead of defaulting to empty string, matching K8s driver behavior - Add resource_version to list_by_scope SELECT in both SQLite and Postgres backends, with regression test - Gate PolicyLocalContext proposal/lookup routes on workspace readiness, returning 503 when workspace is not yet discovered - Block sandbox and provider creation in TUI all-workspaces mode - Clear workspace vectors in TUI reset_sandbox_state Signed-off-by: Derek Carr <decarr@redhat.com> * fix(workspace): make provider profile catalog workspace-aware Thread workspace through snapshot_catalog so the EffectiveProviderProfileCatalog enforces workspace boundaries on both read and write paths. UserProviderProfileSource now loads platform-scoped profiles (workspace "") plus the target workspace's profiles, preventing cross-workspace duplicate profile ID collisions that previously caused global catalog failures. Update RFC 0011 to reflect catalog scoping is implemented in Phase 1 rather than deferred to future work. Signed-off-by: Derek Carr <decarr@redhat.com> * fix(persistence): include workspace column in atomic policy revision INSERT put_policy_revision_atomic omitted the workspace column from the INSERT into the objects table in both SQLite and Postgres backends, causing atomically-written policy revisions to lose their workspace association. Add workspace field to AtomicPolicyRevisionWrite and thread it through both backend INSERT statements, matching the non-atomic put_policy_revision path which already included it. Signed-off-by: Derek Carr <decarr@redhat.com> * fix(proxy): skip ancestor walk when socket owner is the entrypoint collect_ancestor_identities walked the entire process tree above the entrypoint when the connecting process was the entrypoint itself, SHA256-hashing every ancestor binary (IDE, shell, container runtime). On dev machines with large binaries in the ancestor chain this exceeded the 30-second test timeout. When start_pid == stop_pid there are no intermediate ancestors to verify, so return an empty list immediately. Signed-off-by: Derek Carr <decarr@redhat.com> * fix(workspace): make provider profile catalog scope-aware Allow the same profile ID at platform and workspace scopes by introducing layered catalog entries where workspace profiles shadow platform profiles. Add source and scope fields to the ProviderProfile proto and CLI output. Migrate List/Get handlers to the catalog, fixing divergence with runtime profile resolution. Signed-off-by: Derek Carr <decarr@redhat.com> * fix(e2e): align podman e2e labels with centralized driver constants The podman driver moved its container labels to the centralized openshell.ai/ prefix, but the e2e test harness and cleanup script still referenced the old openshell.sandbox-* keys, causing the local_driver_token_restart test to fail on container lookup. Signed-off-by: Derek Carr <decarr@redhat.com> * fix(e2e): align python profile isolation test with scope-aware catalog Platform profiles are now visible in workspace listings as fallbacks per the layered catalog design. Update the assertion to match. Signed-off-by: Derek Carr <decarr@redhat.com> * fix(workspace): honor profile_workspace in runtime profile resolution Runtime profile lookups now consult provider.profile_workspace via get_type_profile_for_scope. Providers created with --global-profile (profile_workspace="") resolve to the platform profile even when a workspace profile shadows the same ID. All 6 runtime call sites updated; type-only call sites remain scope-agnostic. Signed-off-by: Derek Carr <decarr@redhat.com> --------- Signed-off-by: Derek Carr <decarr@redhat.com>
736 lines
22 KiB
Rust
736 lines
22 KiB
Rust
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//! E2E tests for live policy updates on a running sandbox.
|
|
//!
|
|
//! Covers the full round-trip:
|
|
//! - Create sandbox with policy A
|
|
//! - Verify initial policy version via `policy get`
|
|
//! - Push same policy A again -> no version bump (idempotent)
|
|
//! - Push different policy B -> new version, `--wait` for sandbox to load it
|
|
//! - Verify policy history via `policy list`
|
|
//!
|
|
//! These tests replace the Python e2e tests `test_live_policy_update_and_logs`
|
|
//! and `test_live_policy_update_from_empty_network_policies`, which were flaky
|
|
//! due to hard-coded 90s poll timeouts. The Rust tests use the CLI's built-in
|
|
//! `--wait` flag for reliable synchronization.
|
|
//!
|
|
//! Note: the removed Python tests also covered `GetSandboxLogs` RPC and
|
|
//! verified actual proxy connectivity after policy update. Those are tracked
|
|
//! as follow-up coverage gaps -- the proxy enforcement path is covered by the
|
|
//! existing L4/L7/SSRF Python e2e tests, and log fetching needs a dedicated
|
|
//! test.
|
|
|
|
#![cfg(feature = "e2e")]
|
|
|
|
use std::fmt::Write as _;
|
|
use std::io::Write;
|
|
use std::process::Stdio;
|
|
|
|
use openshell_e2e::harness::binary::openshell_cmd;
|
|
use openshell_e2e::harness::output::{extract_field, strip_ansi};
|
|
use openshell_e2e::harness::sandbox::SandboxGuard;
|
|
use tempfile::NamedTempFile;
|
|
|
|
#[cfg(feature = "e2e-docker")]
|
|
const LOCAL_OVERRIDE_REGO: &str = include_str!(concat!(
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
"/../../crates/openshell-supervisor-network/data/sandbox-policy.rego"
|
|
));
|
|
|
|
#[cfg(feature = "e2e-docker")]
|
|
const LOCAL_OVERRIDE_DOCKERFILE: &str = r#"FROM public.ecr.aws/docker/library/python:3.13-slim
|
|
|
|
RUN apt-get update && apt-get install -y --no-install-recommends iproute2 \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
RUN groupadd -g 1000660000 sandbox && \
|
|
useradd -m -u 1000660000 -g sandbox sandbox
|
|
|
|
COPY local-policy.rego /etc/openshell/local-policy.rego
|
|
COPY local-policy.yaml /etc/openshell/local-policy.yaml
|
|
|
|
ENV OPENSHELL_POLICY_RULES=/etc/openshell/local-policy.rego
|
|
ENV OPENSHELL_POLICY_DATA=/etc/openshell/local-policy.yaml
|
|
ENV OPENSHELL_POLICY_POLL_INTERVAL_SECS=1
|
|
|
|
CMD ["sleep", "infinity"]
|
|
"#;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Policy YAML builders
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// Build a policy YAML that allows any binary to reach the given hosts on
|
|
/// port 443.
|
|
///
|
|
/// NOTE: The indentation in the format string is load-bearing YAML structure.
|
|
fn write_policy(hosts: &[&str]) -> Result<NamedTempFile, String> {
|
|
let mut file = NamedTempFile::new().map_err(|e| format!("create temp policy file: {e}"))?;
|
|
|
|
let mut network_rules = String::new();
|
|
for (i, host) in hosts.iter().enumerate() {
|
|
let _ = write!(
|
|
network_rules,
|
|
r#" rule_{i}:
|
|
name: rule_{i}
|
|
endpoints:
|
|
- host: {host}
|
|
port: 443
|
|
binaries:
|
|
- path: "/**"
|
|
"#
|
|
);
|
|
}
|
|
|
|
let policy = format!(
|
|
r"version: 1
|
|
|
|
filesystem_policy:
|
|
include_workdir: true
|
|
read_only:
|
|
- /usr
|
|
- /lib
|
|
- /proc
|
|
- /dev/urandom
|
|
- /app
|
|
- /etc
|
|
- /var/log
|
|
read_write:
|
|
- /sandbox
|
|
- /tmp
|
|
- /dev/null
|
|
|
|
landlock:
|
|
compatibility: best_effort
|
|
|
|
process:
|
|
run_as_user: sandbox
|
|
run_as_group: sandbox
|
|
|
|
network_policies:
|
|
{network_rules}"
|
|
);
|
|
|
|
file.write_all(policy.as_bytes())
|
|
.map_err(|e| format!("write temp policy file: {e}"))?;
|
|
file.flush()
|
|
.map_err(|e| format!("flush temp policy file: {e}"))?;
|
|
Ok(file)
|
|
}
|
|
|
|
/// Build a minimal policy YAML with no network rules.
|
|
fn write_empty_network_policy() -> Result<NamedTempFile, String> {
|
|
let mut file = NamedTempFile::new().map_err(|e| format!("create temp policy file: {e}"))?;
|
|
|
|
let policy = r"version: 1
|
|
|
|
filesystem_policy:
|
|
include_workdir: true
|
|
read_only:
|
|
- /usr
|
|
- /lib
|
|
- /proc
|
|
- /dev/urandom
|
|
- /app
|
|
- /etc
|
|
- /var/log
|
|
read_write:
|
|
- /sandbox
|
|
- /tmp
|
|
- /dev/null
|
|
|
|
landlock:
|
|
compatibility: best_effort
|
|
|
|
process:
|
|
run_as_user: sandbox
|
|
run_as_group: sandbox
|
|
";
|
|
|
|
file.write_all(policy.as_bytes())
|
|
.map_err(|e| format!("write temp policy file: {e}"))?;
|
|
file.flush()
|
|
.map_err(|e| format!("flush temp policy file: {e}"))?;
|
|
Ok(file)
|
|
}
|
|
|
|
#[cfg(feature = "e2e-docker")]
|
|
fn write_local_override_image() -> Result<tempfile::TempDir, String> {
|
|
let dir = tempfile::tempdir().map_err(|e| format!("create image context: {e}"))?;
|
|
std::fs::write(dir.path().join("Dockerfile"), LOCAL_OVERRIDE_DOCKERFILE)
|
|
.map_err(|e| format!("write local override Dockerfile: {e}"))?;
|
|
std::fs::write(dir.path().join("local-policy.rego"), LOCAL_OVERRIDE_REGO)
|
|
.map_err(|e| format!("write local override Rego policy: {e}"))?;
|
|
std::fs::write(
|
|
dir.path().join("local-policy.yaml"),
|
|
r"version: 1
|
|
|
|
filesystem_policy:
|
|
include_workdir: true
|
|
read_only:
|
|
- /usr
|
|
- /lib
|
|
- /proc
|
|
- /dev/urandom
|
|
- /etc
|
|
read_write:
|
|
- /sandbox
|
|
- /tmp
|
|
- /dev/null
|
|
|
|
landlock:
|
|
compatibility: best_effort
|
|
|
|
process:
|
|
run_as_user: sandbox
|
|
run_as_group: sandbox
|
|
|
|
network_policies: {}
|
|
",
|
|
)
|
|
.map_err(|e| format!("write local override policy data: {e}"))?;
|
|
Ok(dir)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// CLI helpers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
struct CliResult {
|
|
success: bool,
|
|
output: String,
|
|
exit_code: Option<i32>,
|
|
}
|
|
|
|
/// Run an `openshell` CLI command and return the result.
|
|
async fn run_cli(args: &[&str]) -> CliResult {
|
|
let mut cmd = openshell_cmd();
|
|
cmd.args(args).stdout(Stdio::piped()).stderr(Stdio::piped());
|
|
|
|
let output = cmd.output().await.expect("spawn openshell command");
|
|
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
let combined = strip_ansi(&format!("{stdout}{stderr}"));
|
|
|
|
CliResult {
|
|
success: output.status.success(),
|
|
output: combined,
|
|
exit_code: output.status.code(),
|
|
}
|
|
}
|
|
|
|
/// Extract the policy version number from `policy get` output.
|
|
///
|
|
/// Uses the shared `extract_field` helper to find `Version: <n>` or
|
|
/// `Revision: <n>` in CLI tabular output.
|
|
fn extract_version(output: &str) -> Option<u32> {
|
|
extract_field(output, "Version")
|
|
.or_else(|| extract_field(output, "Revision"))
|
|
.and_then(|v| v.parse::<u32>().ok())
|
|
}
|
|
|
|
/// Extract the policy hash from `policy get` output.
|
|
fn extract_hash(output: &str) -> Option<String> {
|
|
extract_field(output, "Hash").or_else(|| extract_field(output, "Policy hash"))
|
|
}
|
|
|
|
/// Check that a version number appears in `policy list` output as a
|
|
/// distinct field value (not just a substring of some other number).
|
|
///
|
|
/// Looks for the version number preceded by whitespace or at the start
|
|
/// of a line, to avoid matching "2" inside "12" or timestamps.
|
|
fn list_output_contains_version(output: &str, version: u32) -> bool {
|
|
let v = version.to_string();
|
|
output.lines().any(|line| {
|
|
line.split_whitespace()
|
|
.any(|word| word == v || word.starts_with(&format!("{v} ")))
|
|
})
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Tests
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// Test the full live policy update lifecycle:
|
|
///
|
|
/// 1. Create sandbox with `--keep`
|
|
/// 2. Set policy A, verify initial version >= 1
|
|
/// 3. Push same policy A -> version unchanged (idempotent)
|
|
/// 4. Push policy B (adds example.com) with `--wait` -> new version
|
|
/// 5. Push policy B again -> idempotent
|
|
/// 6. Verify policy list shows both versions
|
|
#[tokio::test]
|
|
#[allow(clippy::too_many_lines)]
|
|
async fn live_policy_update_round_trip() {
|
|
// --- Write two distinct policy files ---
|
|
let policy_a = write_policy(&["api.anthropic.com"]).expect("write policy A");
|
|
let policy_b = write_policy(&["api.anthropic.com", "example.com"]).expect("write policy B");
|
|
|
|
let policy_a_path = policy_a
|
|
.path()
|
|
.to_str()
|
|
.expect("policy A path should be utf-8")
|
|
.to_string();
|
|
let policy_b_path = policy_b
|
|
.path()
|
|
.to_str()
|
|
.expect("policy B path should be utf-8")
|
|
.to_string();
|
|
|
|
// --- Create a long-running sandbox ---
|
|
let mut guard =
|
|
SandboxGuard::create_keep(&["sh", "-c", "echo Ready && sleep infinity"], "Ready")
|
|
.await
|
|
.expect("create keep sandbox");
|
|
|
|
// --- Set initial policy A ---
|
|
let r = run_cli(&[
|
|
"policy",
|
|
"set",
|
|
&guard.name,
|
|
"--policy",
|
|
&policy_a_path,
|
|
"--wait",
|
|
"--timeout",
|
|
"120",
|
|
])
|
|
.await;
|
|
assert!(
|
|
r.success,
|
|
"policy set A should succeed (exit {:?}):\n{}",
|
|
r.exit_code, r.output
|
|
);
|
|
|
|
// --- Verify initial policy version ---
|
|
let r = run_cli(&["policy", "get", &guard.name]).await;
|
|
assert!(
|
|
r.success,
|
|
"policy get should succeed (exit {:?}):\n{}",
|
|
r.exit_code, r.output
|
|
);
|
|
|
|
let initial_version = extract_version(&r.output).unwrap_or_else(|| {
|
|
panic!(
|
|
"could not parse version from policy get output:\n{}",
|
|
r.output
|
|
)
|
|
});
|
|
assert!(
|
|
initial_version >= 1,
|
|
"initial policy version should be >= 1, got {initial_version}"
|
|
);
|
|
|
|
let initial_hash = extract_hash(&r.output);
|
|
|
|
// --- Push same policy A again -> should be idempotent ---
|
|
let r = run_cli(&[
|
|
"policy",
|
|
"set",
|
|
&guard.name,
|
|
"--policy",
|
|
&policy_a_path,
|
|
"--wait",
|
|
"--timeout",
|
|
"120",
|
|
])
|
|
.await;
|
|
assert!(
|
|
r.success,
|
|
"policy set A (repeat) should succeed (exit {:?}):\n{}",
|
|
r.exit_code, r.output
|
|
);
|
|
|
|
let r = run_cli(&["policy", "get", &guard.name]).await;
|
|
assert!(
|
|
r.success,
|
|
"policy get after repeat should succeed:\n{}",
|
|
r.output
|
|
);
|
|
|
|
let repeat_version = extract_version(&r.output)
|
|
.unwrap_or_else(|| panic!("could not parse version after repeat:\n{}", r.output));
|
|
assert_eq!(
|
|
repeat_version, initial_version,
|
|
"same policy should not bump version: expected {initial_version}, got {repeat_version}"
|
|
);
|
|
|
|
if let (Some(ih), Some(rh)) = (&initial_hash, &extract_hash(&r.output)) {
|
|
assert_eq!(ih, rh, "same policy should produce same hash");
|
|
}
|
|
|
|
// --- Push policy B -> should create new version ---
|
|
let r = run_cli(&[
|
|
"policy",
|
|
"set",
|
|
&guard.name,
|
|
"--policy",
|
|
&policy_b_path,
|
|
"--wait",
|
|
"--timeout",
|
|
"120",
|
|
])
|
|
.await;
|
|
assert!(
|
|
r.success,
|
|
"policy set B should succeed (exit {:?}):\n{}",
|
|
r.exit_code, r.output
|
|
);
|
|
|
|
let r = run_cli(&["policy", "get", &guard.name]).await;
|
|
assert!(
|
|
r.success,
|
|
"policy get after B should succeed:\n{}",
|
|
r.output
|
|
);
|
|
|
|
let new_version = extract_version(&r.output)
|
|
.unwrap_or_else(|| panic!("could not parse version after B:\n{}", r.output));
|
|
assert!(
|
|
new_version > initial_version,
|
|
"different policy should bump version: expected > {initial_version}, got {new_version}"
|
|
);
|
|
|
|
if let (Some(ih), Some(nh)) = (&initial_hash, &extract_hash(&r.output)) {
|
|
assert_ne!(ih, nh, "different policy should produce different hash");
|
|
}
|
|
|
|
// --- Push policy B again -> idempotent ---
|
|
let r = run_cli(&[
|
|
"policy",
|
|
"set",
|
|
&guard.name,
|
|
"--policy",
|
|
&policy_b_path,
|
|
"--wait",
|
|
"--timeout",
|
|
"120",
|
|
])
|
|
.await;
|
|
assert!(
|
|
r.success,
|
|
"policy set B (repeat) should succeed (exit {:?}):\n{}",
|
|
r.exit_code, r.output
|
|
);
|
|
|
|
let r = run_cli(&["policy", "get", &guard.name]).await;
|
|
assert!(
|
|
r.success,
|
|
"policy get after B repeat should succeed:\n{}",
|
|
r.output
|
|
);
|
|
|
|
let repeat_b_version = extract_version(&r.output)
|
|
.unwrap_or_else(|| panic!("could not parse version after B repeat:\n{}", r.output));
|
|
assert_eq!(
|
|
repeat_b_version, new_version,
|
|
"same policy B should not bump version: expected {new_version}, got {repeat_b_version}"
|
|
);
|
|
|
|
// --- Verify policy list shows revision history ---
|
|
let r = run_cli(&["policy", "list", &guard.name]).await;
|
|
assert!(
|
|
r.success,
|
|
"policy list should succeed (exit {:?}):\n{}",
|
|
r.exit_code, r.output
|
|
);
|
|
|
|
// Both versions should appear in the list output.
|
|
assert!(
|
|
list_output_contains_version(&r.output, new_version),
|
|
"policy list should contain version {new_version}:\n{}",
|
|
r.output
|
|
);
|
|
assert!(
|
|
list_output_contains_version(&r.output, initial_version),
|
|
"policy list should contain initial version {initial_version}:\n{}",
|
|
r.output
|
|
);
|
|
|
|
guard.cleanup().await;
|
|
}
|
|
|
|
/// Test live policy update from an initially empty network policy:
|
|
///
|
|
/// 1. Create sandbox with `--keep`
|
|
/// 2. Set policy with no network rules
|
|
/// 3. Push policy with a network rule using `--wait`
|
|
/// 4. Verify the version bumped
|
|
#[tokio::test]
|
|
async fn live_policy_update_from_empty_network_policies() {
|
|
let empty_policy = write_empty_network_policy().expect("write empty network policy");
|
|
let full_policy = write_policy(&["example.com"]).expect("write full policy");
|
|
|
|
let empty_path = empty_policy
|
|
.path()
|
|
.to_str()
|
|
.expect("empty policy path should be utf-8")
|
|
.to_string();
|
|
let full_path = full_policy
|
|
.path()
|
|
.to_str()
|
|
.expect("full policy path should be utf-8")
|
|
.to_string();
|
|
|
|
// Create sandbox with empty network policy.
|
|
let mut guard =
|
|
SandboxGuard::create_keep(&["sh", "-c", "echo Ready && sleep infinity"], "Ready")
|
|
.await
|
|
.expect("create keep sandbox");
|
|
|
|
// Set initial empty policy.
|
|
let r = run_cli(&[
|
|
"policy",
|
|
"set",
|
|
&guard.name,
|
|
"--policy",
|
|
&empty_path,
|
|
"--wait",
|
|
"--timeout",
|
|
"120",
|
|
])
|
|
.await;
|
|
assert!(
|
|
r.success,
|
|
"policy set (empty) should succeed (exit {:?}):\n{}",
|
|
r.exit_code, r.output
|
|
);
|
|
|
|
let r = run_cli(&["policy", "get", &guard.name]).await;
|
|
assert!(
|
|
r.success,
|
|
"policy get (empty) should succeed:\n{}",
|
|
r.output
|
|
);
|
|
|
|
let initial_version = extract_version(&r.output)
|
|
.unwrap_or_else(|| panic!("could not parse version from empty policy:\n{}", r.output));
|
|
|
|
// Push policy with network rules.
|
|
let r = run_cli(&[
|
|
"policy",
|
|
"set",
|
|
&guard.name,
|
|
"--policy",
|
|
&full_path,
|
|
"--wait",
|
|
"--timeout",
|
|
"120",
|
|
])
|
|
.await;
|
|
assert!(
|
|
r.success,
|
|
"policy set (full) should succeed (exit {:?}):\n{}",
|
|
r.exit_code, r.output
|
|
);
|
|
|
|
let r = run_cli(&["policy", "get", &guard.name]).await;
|
|
assert!(r.success, "policy get (full) should succeed:\n{}", r.output);
|
|
|
|
let new_version = extract_version(&r.output).unwrap_or_else(|| {
|
|
panic!(
|
|
"could not parse version after adding network rules:\n{}",
|
|
r.output
|
|
)
|
|
});
|
|
assert!(
|
|
new_version > initial_version,
|
|
"adding network rules should create new version > {initial_version}, got {new_version}"
|
|
);
|
|
|
|
guard.cleanup().await;
|
|
}
|
|
|
|
/// Regression for #2159: a sparse initial policy that the supervisor enriches
|
|
/// with baseline filesystem paths during startup must have its resulting
|
|
/// revision acknowledged as loaded, not left `Pending`.
|
|
///
|
|
/// Reproduction (matches the maintainer's triage): create a sandbox with the
|
|
/// network-only `examples/policy-advisor/sandbox-policy.yaml`. The supervisor
|
|
/// adds baseline filesystem paths, syncs the enriched policy back to the
|
|
/// gateway (creating revision 2, superseding revision 1), builds the OPA engine
|
|
/// and then acknowledges revision 2 as loaded. Before the fix, revision 2
|
|
/// stayed `Pending` even though the sandbox was `Ready` and the policy was
|
|
/// effective.
|
|
///
|
|
/// NOTE: This exercises the Docker-backed supervisor built from this branch.
|
|
/// The exact `policy list` status wording ("Loaded"/"Superseded") may differ by
|
|
/// CLI version; the assertions below key on the effective version reaching 2 and
|
|
/// no revision remaining `Pending` once the acknowledgement lands.
|
|
#[tokio::test]
|
|
async fn initial_sparse_policy_is_acknowledged_as_loaded() {
|
|
// Repo-relative path to the sparse network-only policy fixture.
|
|
let sparse_policy = concat!(
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
"/../../examples/policy-advisor/sandbox-policy.yaml"
|
|
);
|
|
|
|
let mut guard = SandboxGuard::create_keep_with_args(
|
|
&[
|
|
"--name",
|
|
"e2e-sparse-enrich",
|
|
"--policy",
|
|
sparse_policy,
|
|
"--no-tty",
|
|
],
|
|
&["sh", "-c", "echo Ready && sleep infinity"],
|
|
"Ready",
|
|
)
|
|
.await
|
|
.expect("create keep sandbox with sparse policy");
|
|
|
|
// The enriched revision (2) is synced during startup; the acknowledgement
|
|
// (LOADED) is delivered by the supervisor's poll loop shortly after Ready.
|
|
// Poll until the effective policy is version 2 and no revision is Pending.
|
|
let mut acknowledged = false;
|
|
let mut last_list = String::new();
|
|
for _ in 0..30 {
|
|
let get = run_cli(&["policy", "get", &guard.name]).await;
|
|
let version = get.success.then(|| extract_version(&get.output)).flatten();
|
|
|
|
let list = run_cli(&["policy", "list", &guard.name]).await;
|
|
last_list = list.output.clone();
|
|
let pending = list.output.to_lowercase().contains("pending");
|
|
|
|
if version == Some(2) && list.success && !pending {
|
|
acknowledged = true;
|
|
break;
|
|
}
|
|
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
|
|
}
|
|
|
|
assert!(
|
|
acknowledged,
|
|
"enriched initial policy should reach revision 2 with no Pending revision.\n\
|
|
last `policy list` output:\n{last_list}"
|
|
);
|
|
|
|
// Both the superseded original (1) and the loaded enriched revision (2)
|
|
// must appear in the revision history.
|
|
assert!(
|
|
list_output_contains_version(&last_list, 2),
|
|
"policy list should contain revision 2:\n{last_list}"
|
|
);
|
|
assert!(
|
|
list_output_contains_version(&last_list, 1),
|
|
"policy list should contain revision 1:\n{last_list}"
|
|
);
|
|
|
|
guard.cleanup().await;
|
|
}
|
|
|
|
/// An explicit local Rego/data override remains authoritative even when the
|
|
/// sandbox has a gateway policy and that policy changes while it is running.
|
|
/// Gateway polling must continue for settings and providers without replacing
|
|
/// the locally loaded OPA engine.
|
|
#[cfg(feature = "e2e-docker")]
|
|
#[tokio::test]
|
|
async fn local_policy_override_survives_gateway_policy_polls() {
|
|
let image_context = write_local_override_image().expect("write local override image");
|
|
let dockerfile = image_context.path().join("Dockerfile");
|
|
let dockerfile = dockerfile
|
|
.to_str()
|
|
.expect("Dockerfile path should be utf-8");
|
|
|
|
let gateway_policy_a_file = write_policy(&["example.com"]).expect("write gateway policy A");
|
|
let gateway_policy_a_path = gateway_policy_a_file
|
|
.path()
|
|
.to_str()
|
|
.expect("gateway policy A path should be utf-8")
|
|
.to_string();
|
|
let gateway_policy_b_file =
|
|
write_policy(&["example.com", "api.anthropic.com"]).expect("write gateway policy B");
|
|
let gateway_policy_b_path = gateway_policy_b_file
|
|
.path()
|
|
.to_str()
|
|
.expect("gateway policy B path should be utf-8")
|
|
.to_string();
|
|
|
|
let mut guard = SandboxGuard::create_keep_with_args(
|
|
&[
|
|
"--name",
|
|
"e2e-lcl-pol-ovrd",
|
|
"--from",
|
|
dockerfile,
|
|
"--policy",
|
|
&gateway_policy_a_path,
|
|
"--no-tty",
|
|
],
|
|
&["sh", "-c", "echo Ready && sleep infinity"],
|
|
"Ready",
|
|
)
|
|
.await
|
|
.expect("create sandbox with local policy override");
|
|
|
|
// Allow several one-second poll intervals. Before the fix, the first poll
|
|
// immediately reloaded gateway policy A over the local override.
|
|
tokio::time::sleep(std::time::Duration::from_secs(4)).await;
|
|
let initial_logs = run_cli(&[
|
|
"logs",
|
|
&guard.name,
|
|
"-n",
|
|
"500",
|
|
"--since",
|
|
"1m",
|
|
"--source",
|
|
"sandbox",
|
|
])
|
|
.await;
|
|
assert!(
|
|
initial_logs.success,
|
|
"fetch initial sandbox logs:\n{}",
|
|
initial_logs.output
|
|
);
|
|
assert!(
|
|
initial_logs
|
|
.output
|
|
.contains("Loading OPA policy engine from local files"),
|
|
"sandbox should load the explicit local policy:\n{}",
|
|
initial_logs.output
|
|
);
|
|
assert!(
|
|
!initial_logs.output.contains("Policy reloaded successfully"),
|
|
"the first gateway poll must not replace the local policy:\n{}",
|
|
initial_logs.output
|
|
);
|
|
|
|
let update = run_cli(&[
|
|
"policy",
|
|
"set",
|
|
&guard.name,
|
|
"--policy",
|
|
&gateway_policy_b_path,
|
|
])
|
|
.await;
|
|
assert!(
|
|
update.success,
|
|
"publish gateway policy B:\n{}",
|
|
update.output
|
|
);
|
|
|
|
// A later gateway revision must also remain observational in local mode.
|
|
tokio::time::sleep(std::time::Duration::from_secs(4)).await;
|
|
let updated_logs = run_cli(&[
|
|
"logs",
|
|
&guard.name,
|
|
"-n",
|
|
"500",
|
|
"--since",
|
|
"1m",
|
|
"--source",
|
|
"sandbox",
|
|
])
|
|
.await;
|
|
assert!(
|
|
updated_logs.success,
|
|
"fetch updated sandbox logs:\n{}",
|
|
updated_logs.output
|
|
);
|
|
assert!(
|
|
!updated_logs.output.contains("Policy reloaded successfully"),
|
|
"gateway policy updates must not replace the local override:\n{}",
|
|
updated_logs.output
|
|
);
|
|
|
|
guard.cleanup().await;
|
|
}
|