mirror of
https://github.com/NVIDIA/OpenShell.git
synced 2026-10-18 05:39:50 +08:00
* feat(sandbox): integrate OCSF structured logging for all sandbox events WIP: Replace ad-hoc tracing calls with OCSF event builders across all sandbox subsystems (network, SSH, process, filesystem, config, lifecycle). - Register ocsf_logging_enabled setting (defaults false) - Replace stdout/file fmt layers with OcsfShorthandLayer - Add conditional OcsfJsonlLayer for /var/log/openshell-ocsf.log - Update LogPushLayer to extract OCSF shorthand for gRPC push - Migrate ~106 log sites to OCSF builders (NetworkActivity, HttpActivity, SshActivity, ProcessActivity, DetectionFinding, ConfigStateChange, AppLifecycle) - Add openshell-ocsf to all Docker build contexts * fix(scripts): attach provider to all smoke test phases to avoid rate limits GitHub's unauthenticated API rate limit (60/hour) causes flaky 403s for Phases 1, 2, and 4. Fix by attaching the provider to all sandboxes and upgrading the Phase 1 policy to L7 so credential injection works. Phase 4 (tls:skip) cannot inject credentials by design, so relax the assertion to accept either 200 or 403 from upstream -- both prove the proxy forwarded the request. * fix(ocsf): remove timestamp from shorthand format to avoid double-timestamp The display layer (gateway logs, TUI, sandbox logs CLI) already prepends a timestamp. Having one in the shorthand output too produces redundant double-timestamps like: 15:49:11 sandbox INFO 15:49:11.649 I NET:OPEN ALLOWED ... Now the shorthand is just the severity + structured content: 15:49:11 sandbox INFO I NET:OPEN ALLOWED ... * refactor(ocsf): replace single-char severity with bracketed labels Replace cryptic single-character severity codes (I/L/M/H/C/F) with readable bracketed labels: [LOW], [MED], [HIGH], [CRIT], [FATAL]. Informational severity (the happy-path default) is omitted entirely to keep normal log output clean and avoid redundancy with the tracing-level INFO that the display layer already provides. Before: sandbox INFO I NET:OPEN ALLOWED ... After: sandbox INFO NET:OPEN ALLOWED ... Before: sandbox INFO M NET:OPEN DENIED ... After: sandbox INFO [MED] NET:OPEN DENIED ... * feat(sandbox): use OCSF level label for structured events in log push Set the level field to 'OCSF' instead of 'INFO' for OCSF events in the gRPC log push. This visually distinguishes structured OCSF events from plain tracing output in the TUI and CLI sandbox logs: sandbox OCSF NET:OPEN [INFO] ALLOWED python3(42) -> api.example.com:443 sandbox OCSF NET:OPEN [MED] DENIED python3(42) -> blocked.com:443 sandbox INFO Fetching sandbox policy via gRPC * fix(sandbox): convert new Landlock path-skip warning to OCSF PR #677 added a warn!() for inaccessible Landlock paths in best-effort mode. Convert to ConfigStateChangeBuilder with degraded state so it flows through the OCSF shorthand format consistently. * fix(sandbox): use rolling appender for OCSF JSONL file Match the main openshell.log rotation mechanics (daily, 3 files max) instead of a single unbounded append-only file. Prevents disk exhaustion when ocsf_logging_enabled is left on in long-running sandboxes. * fix(sandbox): address reviewer warnings for OCSF integration W1: Remove redundant 'OCSF' prefix from shorthand file layer — the class name (NET:OPEN, HTTP:GET) already identifies structured events and the LogPushLayer separately sets the level field. W2: Log a debug message when OCSF_CTX.set() is called a second time instead of silently discarding via let _. W3: Document the boundary between OCSF-migrated events and intentionally plain tracing calls (DEBUG/TRACE, transient, internal plumbing). W4: Migrate remaining iptables LOG rule failure warnings in netns.rs (IPv4 TCP/UDP, IPv6 TCP/UDP) to ConfigStateChangeBuilder for consistency with the IPv4 bypass rule failure already migrated. W5: Migrate malformed inference request warn to NetworkActivity with ActivityId::Refuse and SeverityId::Medium. W6: Use Medium severity for L7 deny decisions (both CONNECT tunnel and FORWARD proxy paths) to match the CONNECT deny severity pattern. Allows and audits remain Informational. * refactor(sandbox): rename ocsf_logging_enabled to ocsf_json_enabled The shorthand logs are already OCSF-structured events. The setting specifically controls the JSONL file export, so the name should reflect that: ocsf_json_enabled. * fix(ocsf): add timestamps to shorthand file layer output The OcsfShorthandLayer writes directly to the log file with no outer display layer to supply timestamps. Add a UTC timestamp prefix to every line so the file output matches what tracing::fmt used to provide. Before: CONFIG:VALIDATED [INFO] Validated 'sandbox' user exists in image After: 2026-04-01T15:49:11.649Z CONFIG:VALIDATED [INFO] Validated ... * fix(docker): touch openshell-ocsf source to invalidate cargo cache The supervisor-workspace stage touches sandbox and core sources to force recompilation over the rust-deps dummy stubs, but openshell-ocsf was missing. This caused the Docker cargo cache to use stale ocsf objects from the deps stage, preventing changes to the ocsf crate (like the timestamp fix) from appearing in the final binary. Also adds a shorthand layer test verifying timestamp output, and drafts the observability docs section. * fix(ocsf): add OCSF level prefix to file layer shorthand output Without a level prefix, OCSF events in the log file have no visual anchor at the position where standard tracing lines show INFO/WARN. This makes scanning the file harder since the eye has nothing consistent to lock onto after the timestamp. Before: 2026-04-01T04:04:13.065Z CONFIG:DISCOVERY [INFO] ... After: 2026-04-01T04:04:13.065Z OCSF CONFIG:DISCOVERY [INFO] ... * fix(ocsf): clean up shorthand formatting for listen and SSH events - Fix double space in NET:LISTEN, SSH:LISTEN, and other events where action is empty (e.g., 'NET:LISTEN [INFO] 10.200.0.1' -> 'NET:LISTEN [INFO] 10.200.0.1') - Add listen address to SSH:LISTEN event (was empty) - Downgrade SSH handshake intermediate steps (reading preface, verifying) from OCSF events to debug!() traces. Only the final verdict (accepted/denied) is an OCSF event now, reducing noise from 3 events to 1 per SSH connection. - Apply same spacing fix to HTTP shorthand for consistency. * docs(observability): update examples with OCSF prefix and formatting fixes Align doc examples with the deployed output: - Add OCSF level prefix to all shorthand examples in the log file - Show mixed OCSF + standard tracing in the file format section - Update listen events (no double space, SSH includes address) - Show one SSH:OPEN per connection instead of three - Update grep patterns to use 'OCSF NET:' etc. * docs(agents): add OCSF logging guidance to AGENTS.md Add a Sandbox Logging (OCSF) section to AGENTS.md so agents have in-context guidance for deciding whether new log emissions should use OCSF structured logging or plain tracing. Covers event class selection, severity guidelines, builder API usage, dual-emit pattern for security findings, and the no-secrets rule. Also adds openshell-ocsf to the Architecture Overview table. * fix: remove workflow files accidentally included during rebase These files were already merged to main in separate PRs. They got pulled into our branch during rebase conflict resolution for the deleted docs-preview-pr.yml file. * docs(observability): use sandbox connect instead of raw SSH Users access sandboxes via 'openshell sandbox connect', not direct SSH. * fix(docs): correct settings CLI syntax in OCSF JSON export page The settings CLI requires --key and --value named flags, not positional arguments. Also fix the per-sandbox form: the sandbox name is a positional argument, not a --sandbox flag. * fix(e2e): update log assertions for OCSF shorthand format The E2E tests asserted on the old tracing::fmt key=value format (action=allow, l7_decision=audit, FORWARD, L7_REQUEST, always-blocked). Update to match the new OCSF shorthand (ALLOWED/DENIED, HTTP:, NET:, engine:ssrf, policy:). * feat(sandbox): convert WebSocket upgrade log calls to OCSF PR #718 added two log calls for WebSocket upgrade handling: - 101 Switching Protocols info → NetworkActivity with Upgrade activity. This is a significant state change (L7 enforcement drops to raw relay). - Unsolicited 101 without client Upgrade header → DetectionFinding with High severity. A non-compliant upstream sending 101 without a client Upgrade request could be attempting to bypass L7 inspection.
493 lines
17 KiB
Bash
Executable File
493 lines
17 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
# =============================================================================
|
|
# Network Policy Smoke Test
|
|
# =============================================================================
|
|
#
|
|
# End-to-end smoke test for sandbox network policies, TLS auto-termination,
|
|
# credential injection, and L4/L7 enforcement. Uses GitHub's API as the target.
|
|
#
|
|
# Prerequisites:
|
|
# - A running OpenShell gateway (`openshell status` shows Healthy)
|
|
# - GITHUB_TOKEN or GH_TOKEN env var set with a valid GitHub token
|
|
# - The `openshell` CLI on PATH
|
|
#
|
|
# Usage:
|
|
# GITHUB_TOKEN=ghp_xxx ./scripts/smoke-test-network-policy.sh
|
|
#
|
|
# What it tests:
|
|
#
|
|
# Phase 1 — L4 allow/deny (credential injection, TLS auto-terminated):
|
|
# Creates a sandbox with L4+L7 policy for api.github.com (provider
|
|
# attached for authenticated requests).
|
|
# - curl api.github.com/zen -> should succeed (authenticated, 200)
|
|
# - curl httpbin.org -> should be blocked (implicit deny)
|
|
#
|
|
# Phase 2 — L7 enforcement (method + path rules):
|
|
# Creates a sandbox with read-only L7 enforcement (provider attached).
|
|
# - GET /zen -> should succeed (200)
|
|
# - POST /user/repos -> should be blocked (403)
|
|
#
|
|
# Phase 3 — Credential injection:
|
|
# Creates a sandbox with provider attached and full L7 access.
|
|
# - curl /user (no auth header) -> should return authenticated response
|
|
# (proxy auto-injects GITHUB_TOKEN via TLS MITM)
|
|
#
|
|
# Phase 4 — tls: skip escape hatch:
|
|
# Creates a sandbox with tls: skip (provider attached but no MITM).
|
|
# - curl /zen -> should get response from upstream (raw tunnel)
|
|
# - curl /user -> should get 401 (no credential injection)
|
|
#
|
|
# After all tests, sandboxes are kept alive for log inspection.
|
|
# The script prompts before cleanup.
|
|
#
|
|
# =============================================================================
|
|
#
|
|
# Embedded Policy YAMLs
|
|
# =============================================================================
|
|
#
|
|
# POLICY_L4 (allow api.github.com:443 with credential injection, deny everything else):
|
|
# network_policies:
|
|
# github_api:
|
|
# endpoints: [{ host: api.github.com, port: 443, protocol: rest, access: full }]
|
|
# binaries: [{ path: /usr/bin/curl }]
|
|
#
|
|
# POLICY_L7_READONLY (L7 read-only enforcement):
|
|
# network_policies:
|
|
# github_api:
|
|
# endpoints:
|
|
# - host: api.github.com
|
|
# port: 443
|
|
# protocol: rest
|
|
# enforcement: enforce
|
|
# access: read-only
|
|
# binaries: [{ path: /usr/bin/curl }]
|
|
#
|
|
# POLICY_CRED_INJECT (L7 full access, provider credential injection):
|
|
# Same as L7 but with access: full
|
|
#
|
|
# POLICY_TLS_SKIP (L4 with tls: skip — raw tunnel):
|
|
# network_policies:
|
|
# github_api:
|
|
# endpoints: [{ host: api.github.com, port: 443, tls: skip }]
|
|
# binaries: [{ path: /usr/bin/curl }]
|
|
#
|
|
# =============================================================================
|
|
|
|
set -uo pipefail
|
|
# Note: NOT using set -e so we can capture exit codes without exiting.
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[0;33m'
|
|
BOLD='\033[1m'
|
|
RESET='\033[0m'
|
|
|
|
PASS_COUNT=0
|
|
FAIL_COUNT=0
|
|
|
|
pass() { ((PASS_COUNT++)); echo -e " ${GREEN}PASS${RESET} $1"; }
|
|
fail() { ((FAIL_COUNT++)); echo -e " ${RED}FAIL${RESET} $1\n $2"; }
|
|
header() { echo -e "\n${BOLD}=== $1 ===${RESET}"; }
|
|
|
|
PROVIDER_NAME="smoke-test-github"
|
|
SANDBOXES=()
|
|
POLICY_DIR=""
|
|
|
|
# Resolve token from GITHUB_TOKEN or GH_TOKEN
|
|
TOKEN="${GITHUB_TOKEN:-${GH_TOKEN:-}}"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Preflight
|
|
# ---------------------------------------------------------------------------
|
|
|
|
header "Preflight"
|
|
|
|
if [[ -z "$TOKEN" ]]; then
|
|
echo -e "${RED}Error: GITHUB_TOKEN or GH_TOKEN env var is required${RESET}"
|
|
exit 1
|
|
fi
|
|
echo " Token is set"
|
|
|
|
if ! openshell status >/dev/null 2>&1; then
|
|
echo -e "${RED}Error: No healthy gateway. Run: openshell gateway start${RESET}"
|
|
exit 1
|
|
fi
|
|
echo " Gateway is healthy"
|
|
|
|
POLICY_DIR=$(mktemp -d)
|
|
echo " Policy dir: $POLICY_DIR"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
write_policy() {
|
|
local name="$1"
|
|
local file="$POLICY_DIR/${name}.yaml"
|
|
cat > "$file"
|
|
echo "$file"
|
|
}
|
|
|
|
# Create a sandbox with --keep and a sleep, wait for Ready.
|
|
create_sandbox() {
|
|
local name="$1"
|
|
shift
|
|
local provider_flag=("$@")
|
|
|
|
echo " Creating sandbox: $name"
|
|
openshell sandbox create --name "$name" --keep "${provider_flag[@]}" \
|
|
-- sh -c "echo Ready && sleep 3600" >/dev/null 2>&1 &
|
|
local pid=$!
|
|
|
|
local attempts=0
|
|
while [[ $attempts -lt 40 ]]; do
|
|
if openshell sandbox list 2>/dev/null | grep -q "$name.*Ready"; then
|
|
echo " Sandbox $name is Ready"
|
|
SANDBOXES+=("$name")
|
|
# Kill the blocking create process (sandbox stays alive with --keep)
|
|
kill "$pid" 2>/dev/null || true
|
|
wait "$pid" 2>/dev/null || true
|
|
# Brief settle time — SSH server inside the sandbox may still be
|
|
# binding its port even though the status flipped to Ready.
|
|
sleep 3
|
|
return 0
|
|
fi
|
|
sleep 2
|
|
((attempts++))
|
|
done
|
|
|
|
echo -e " ${RED}TIMEOUT waiting for $name${RESET}"
|
|
kill "$pid" 2>/dev/null || true
|
|
wait "$pid" 2>/dev/null || true
|
|
return 1
|
|
}
|
|
|
|
# Run a command inside a sandbox via SSH.
|
|
sandbox_exec() {
|
|
local name="$1"
|
|
shift
|
|
|
|
local ssh_config
|
|
ssh_config=$(openshell sandbox ssh-config "$name" 2>/dev/null)
|
|
local ssh_host
|
|
ssh_host=$(echo "$ssh_config" | grep "^Host " | awk '{print $2}')
|
|
local ssh_config_file="$POLICY_DIR/ssh_config_${name}"
|
|
echo "$ssh_config" > "$ssh_config_file"
|
|
|
|
ssh -F "$ssh_config_file" \
|
|
-o StrictHostKeyChecking=no \
|
|
-o UserKnownHostsFile=/dev/null \
|
|
-o LogLevel=ERROR \
|
|
-o ConnectTimeout=15 \
|
|
"$ssh_host" "$@" 2>&1
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Write policies
|
|
# ---------------------------------------------------------------------------
|
|
|
|
POLICY_L4=$(write_policy l4-allow-deny <<'YAML'
|
|
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:
|
|
github_api:
|
|
name: github-api-l4
|
|
endpoints:
|
|
- host: api.github.com
|
|
port: 443
|
|
protocol: rest
|
|
enforcement: enforce
|
|
access: full
|
|
binaries:
|
|
- { path: /usr/bin/curl }
|
|
YAML
|
|
)
|
|
|
|
POLICY_L7_RO=$(write_policy l7-readonly <<'YAML'
|
|
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:
|
|
github_api:
|
|
name: github-api-l7-readonly
|
|
endpoints:
|
|
- host: api.github.com
|
|
port: 443
|
|
protocol: rest
|
|
enforcement: enforce
|
|
access: read-only
|
|
binaries:
|
|
- { path: /usr/bin/curl }
|
|
YAML
|
|
)
|
|
|
|
POLICY_CRED=$(write_policy l7-cred-inject <<'YAML'
|
|
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:
|
|
github_api:
|
|
name: github-api-cred-inject
|
|
endpoints:
|
|
- host: api.github.com
|
|
port: 443
|
|
protocol: rest
|
|
enforcement: enforce
|
|
access: full
|
|
binaries:
|
|
- { path: /usr/bin/curl }
|
|
YAML
|
|
)
|
|
|
|
POLICY_SKIP=$(write_policy tls-skip <<'YAML'
|
|
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:
|
|
github_api:
|
|
name: github-api-skip
|
|
endpoints:
|
|
- host: api.github.com
|
|
port: 443
|
|
tls: skip
|
|
binaries:
|
|
- { path: /usr/bin/curl }
|
|
YAML
|
|
)
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Phase 0: Provider setup
|
|
# ---------------------------------------------------------------------------
|
|
|
|
header "Phase 0: Provider Setup"
|
|
|
|
openshell provider delete "$PROVIDER_NAME" >/dev/null 2>&1 || true
|
|
|
|
if openshell provider create \
|
|
--name "$PROVIDER_NAME" \
|
|
--type github \
|
|
--credential "GITHUB_TOKEN=$TOKEN" >/dev/null 2>&1; then
|
|
pass "Provider '$PROVIDER_NAME' created"
|
|
else
|
|
fail "Provider creation failed" ""
|
|
exit 1
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Phase 1: L4 allow/deny
|
|
# ---------------------------------------------------------------------------
|
|
|
|
header "Phase 1: L4 Allow/Deny (TLS auto-terminated, credential injection)"
|
|
|
|
SB1="smoke-l4"
|
|
if create_sandbox "$SB1" --provider "$PROVIDER_NAME"; then
|
|
echo " Setting L4-only policy..."
|
|
openshell policy set "$SB1" --policy "$POLICY_L4" >/dev/null 2>&1
|
|
echo " Waiting for policy propagation (15s)..."
|
|
sleep 15
|
|
|
|
# Test 1: L4 allow (authenticated via credential injection)
|
|
echo " Running: curl api.github.com/zen"
|
|
output=$(sandbox_exec "$SB1" 'curl -s -o /dev/null -w "%{http_code}" --max-time 10 -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/zen')
|
|
if [[ "$output" == *"200"* ]]; then
|
|
pass "L4 allow: curl to api.github.com succeeded (HTTP 200)"
|
|
else
|
|
fail "L4 allow: expected HTTP 200" "got: $output"
|
|
fi
|
|
|
|
# Test 2: L4 deny (implicit deny for httpbin.org)
|
|
echo " Running: curl httpbin.org (should be blocked)"
|
|
output=$(sandbox_exec "$SB1" "curl -s -o /dev/null -w '%{http_code}' --max-time 10 https://httpbin.org/get" || true)
|
|
if [[ "$output" == *"403"* || "$output" == *"000"* || -z "$output" ]]; then
|
|
pass "L4 deny: curl to httpbin.org blocked"
|
|
else
|
|
fail "L4 deny: expected connection failure" "got: $output"
|
|
fi
|
|
else
|
|
fail "L4 sandbox creation failed" ""
|
|
fail "L4 deny test skipped" "sandbox not created"
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Phase 2: L7 enforcement
|
|
# ---------------------------------------------------------------------------
|
|
|
|
header "Phase 2: L7 Enforcement (read-only, TLS auto-terminated)"
|
|
|
|
SB2="smoke-l7"
|
|
if create_sandbox "$SB2" --provider "$PROVIDER_NAME"; then
|
|
echo " Setting L7 read-only policy..."
|
|
openshell policy set "$SB2" --policy "$POLICY_L7_RO" >/dev/null 2>&1
|
|
echo " Waiting for policy propagation (15s)..."
|
|
sleep 15
|
|
|
|
# Test 3: L7 allow (GET, authenticated via credential injection)
|
|
echo " Running: GET /zen"
|
|
output=$(sandbox_exec "$SB2" 'curl -s -o /dev/null -w "%{http_code}" --max-time 10 -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/zen')
|
|
if [[ "$output" == *"200"* ]]; then
|
|
pass "L7 allow: GET /zen succeeded (read-only allows GET)"
|
|
else
|
|
fail "L7 allow: expected HTTP 200 for GET" "got: $output"
|
|
fi
|
|
|
|
# Test 4: L7 deny (POST blocked by read-only)
|
|
echo " Running: POST /user/repos (should be blocked)"
|
|
output=$(sandbox_exec "$SB2" "curl -s -o /dev/null -w '%{http_code}' --max-time 10 -X POST https://api.github.com/user/repos -d '{\"name\":\"should-not-create\"}'" || true)
|
|
if [[ "$output" == *"403"* ]]; then
|
|
pass "L7 deny: POST blocked by read-only enforcement"
|
|
else
|
|
fail "L7 deny: expected HTTP 403 for POST" "got: $output"
|
|
fi
|
|
else
|
|
fail "L7 sandbox creation failed" ""
|
|
fail "L7 deny test skipped" "sandbox not created"
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Phase 3: Credential injection
|
|
# ---------------------------------------------------------------------------
|
|
|
|
header "Phase 3: Credential Injection (provider attached, TLS auto-terminated)"
|
|
|
|
SB3="smoke-cred"
|
|
if create_sandbox "$SB3" --provider "$PROVIDER_NAME"; then
|
|
echo " Setting L7 full policy..."
|
|
openshell policy set "$SB3" --policy "$POLICY_CRED" >/dev/null 2>&1
|
|
echo " Waiting for policy propagation (15s)..."
|
|
sleep 15
|
|
|
|
# Test 5: Credential injection — curl /user using the placeholder env var.
|
|
# The sandbox process sees GITHUB_TOKEN=openshell:resolve:env:GITHUB_TOKEN
|
|
# in its environment. When curl sends this as an Authorization header,
|
|
# the proxy's SecretResolver rewrites the placeholder to the real token.
|
|
echo " Running: curl /user -H 'Authorization: token \$GITHUB_TOKEN'"
|
|
output=$(sandbox_exec "$SB3" 'curl -s --max-time 10 -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/user' || true)
|
|
if [[ "$output" == *"login"* ]]; then
|
|
pass "Credential injection: /user returned authenticated response"
|
|
elif [[ "$output" == *"401"* || "$output" == *"Unauthorized"* ]]; then
|
|
fail "Credential injection: got 401 (placeholder may have leaked)" "$output"
|
|
else
|
|
fail "Credential injection: unexpected response" "$output"
|
|
fi
|
|
else
|
|
fail "Credential injection sandbox creation failed" ""
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Phase 4: tls: skip escape hatch
|
|
# ---------------------------------------------------------------------------
|
|
|
|
header "Phase 4: tls: skip (raw tunnel, no MITM)"
|
|
|
|
SB4="smoke-skip"
|
|
if create_sandbox "$SB4" --provider "$PROVIDER_NAME"; then
|
|
echo " Setting tls: skip policy..."
|
|
openshell policy set "$SB4" --policy "$POLICY_SKIP" >/dev/null 2>&1
|
|
echo " Waiting for policy propagation (15s)..."
|
|
sleep 15
|
|
|
|
# Test 6: L4 connection reaches upstream (raw tunnel, no MITM).
|
|
# Without credential injection the request is unauthenticated, so
|
|
# GitHub may return 200 or 403 (rate-limited). Either proves the
|
|
# proxy forwarded the request — a proxy block would return "000"
|
|
# or the sandbox-policy 403 body.
|
|
echo " Running: curl /zen (should reach upstream via raw tunnel)"
|
|
output=$(sandbox_exec "$SB4" "curl -s -o /dev/null -w '%{http_code}' --max-time 10 https://api.github.com/zen" || true)
|
|
if [[ "$output" == *"200"* || "$output" == *"403"* ]]; then
|
|
pass "tls: skip: request reached upstream (raw tunnel, HTTP $output)"
|
|
else
|
|
fail "tls: skip: expected upstream response (200 or 403)" "got: $output"
|
|
fi
|
|
|
|
# Test 7: Credential injection does NOT work with tls: skip.
|
|
# The placeholder leaks verbatim since there's no MITM to rewrite it.
|
|
echo " Running: curl /user with \$GITHUB_TOKEN (should fail, placeholder leaks)"
|
|
output=$(sandbox_exec "$SB4" 'curl -s --max-time 10 -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/user' || true)
|
|
if [[ "$output" == *"401"* || "$output" == *"Unauthorized"* || "$output" == *"Bad credentials"* ]]; then
|
|
pass "tls: skip: /user returned 401 (credential injection bypassed)"
|
|
elif [[ "$output" == *"login"* ]]; then
|
|
fail "tls: skip: /user was authenticated (MITM should be disabled)" "$output"
|
|
else
|
|
pass "tls: skip: /user not authenticated (expected)"
|
|
fi
|
|
else
|
|
fail "tls: skip sandbox creation failed" ""
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Results
|
|
# ---------------------------------------------------------------------------
|
|
|
|
header "Results"
|
|
echo -e " ${GREEN}Passed: ${PASS_COUNT}${RESET}"
|
|
echo -e " ${RED}Failed: ${FAIL_COUNT}${RESET}"
|
|
echo ""
|
|
|
|
if [[ ${#SANDBOXES[@]} -gt 0 ]]; then
|
|
echo -e "${BOLD}Sandboxes kept for inspection:${RESET}"
|
|
for sb in "${SANDBOXES[@]}"; do
|
|
echo " - $sb"
|
|
done
|
|
echo ""
|
|
echo "Inspect logs with:"
|
|
echo " openshell logs <name> --source sandbox"
|
|
echo ""
|
|
|
|
read -r -p "Delete all smoke test sandboxes and provider? [y/N] " answer
|
|
if [[ "$answer" =~ ^[Yy]$ ]]; then
|
|
echo ""
|
|
for sb in "${SANDBOXES[@]}"; do
|
|
openshell sandbox delete "$sb" >/dev/null 2>&1 && echo " Deleted $sb" || true
|
|
done
|
|
openshell provider delete "$PROVIDER_NAME" >/dev/null 2>&1 && echo " Deleted provider $PROVIDER_NAME" || true
|
|
else
|
|
echo " Sandboxes left running. Clean up manually:"
|
|
echo " openshell sandbox delete --all"
|
|
echo " openshell provider delete $PROVIDER_NAME"
|
|
fi
|
|
fi
|
|
|
|
# Clean up temp files
|
|
rm -rf "$POLICY_DIR"
|
|
|
|
echo ""
|
|
if [[ $FAIL_COUNT -gt 0 ]]; then
|
|
echo -e "${RED}${BOLD}SMOKE TEST FAILED${RESET}"
|
|
exit 1
|
|
else
|
|
echo -e "${GREEN}${BOLD}SMOKE TEST PASSED${RESET}"
|
|
exit 0
|
|
fi
|