Skip to content

Commit 9e5a2f3

Browse files
vshalpnjabiclaude
andcommitted
feat(sandbox): add Interactive enforcement mode type, policy schema, and client
- Extended EnforcementMode with Interactive { endpoint, timeout, fallback } - Added FallbackMode { Allow, Deny } enum and INTERACTIVE_DEFAULT_TIMEOUT const - Changed EnforcementMode derive from Copy to Clone (now carries a String) - New crates/openshell-sandbox/src/l7/interactive.rs: - InteractiveContext, InteractiveDecision types - consult_interactive_endpoint() async fn: POST to decision endpoint, tokio::time::timeout wrapping, Semaphore(16) cap, default-deny on all error paths (timeout / non-2xx / malformed JSON) - 16 unit tests using wiremock (including semaphore exhaustion) - Added reqwest = { workspace = true } to openshell-sandbox Cargo.toml - Added wiremock dev-dependency to openshell-sandbox Cargo.toml - Extended openshell-policy EnforcementDef: untagged Bare(String) | Object(InteractiveEnforcementDef) with proto round-trip helpers; 4 tests - docs/interactive-enforcement/: AGENTBOX_INTEGRATION.md, PR_BODY.md, RFC_ISSUE.md, CLAUDE.md updated with session NVIDIA#5 status Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
1 parent 7b147b2 commit 9e5a2f3

9 files changed

Lines changed: 1801 additions & 118 deletions

File tree

‎crates/openshell-policy/src/lib.rs‎

Lines changed: 194 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,12 @@ struct NetworkEndpointDef {
104104
protocol: String,
105105
#[serde(default, skip_serializing_if = "String::is_empty")]
106106
tls: String,
107-
#[serde(default, skip_serializing_if = "String::is_empty")]
108-
enforcement: String,
107+
/// Enforcement mode. Accepts either a bare string (`audit` / `enforce`)
108+
/// for backward compatibility, or an object form for `interactive` mode
109+
/// (see `InteractiveEnforcementDef`). Absent → falls through to the
110+
/// engine default.
111+
#[serde(default, skip_serializing_if = "Option::is_none")]
112+
enforcement: Option<EnforcementDef>,
109113
#[serde(default, skip_serializing_if = "String::is_empty")]
110114
access: String,
111115
#[serde(default, skip_serializing_if = "Vec::is_empty")]
@@ -149,6 +153,85 @@ fn is_zero_u32(v: &u32) -> bool {
149153
*v == 0
150154
}
151155

156+
/// YAML representation of the endpoint `enforcement` field.
157+
///
158+
/// Two shapes are accepted:
159+
///
160+
/// ```yaml
161+
/// enforcement: enforce # bare-string form (back-compat)
162+
/// ```
163+
///
164+
/// ```yaml
165+
/// enforcement: # object form (interactive mode)
166+
/// mode: interactive
167+
/// endpoint: http://host.openshell.internal:53789/decide
168+
/// timeout_seconds: 60
169+
/// fallback: deny
170+
/// ```
171+
///
172+
/// The proto layer represents `enforcement` as a single string. The bare-string
173+
/// form is stored verbatim; the object form is round-tripped via a canonical
174+
/// JSON encoding (see `enforcement_def_to_proto_string` /
175+
/// `enforcement_def_from_proto_string`).
176+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
177+
#[serde(untagged)]
178+
enum EnforcementDef {
179+
Bare(String),
180+
Object(InteractiveEnforcementDef),
181+
}
182+
183+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
184+
#[serde(deny_unknown_fields)]
185+
struct InteractiveEnforcementDef {
186+
/// Required discriminant. Currently only `"interactive"` is meaningful;
187+
/// other values round-trip but are ignored by the proxy.
188+
mode: String,
189+
#[serde(default, skip_serializing_if = "String::is_empty")]
190+
endpoint: String,
191+
/// Per-request decision timeout. `0` means "use the engine default" and is
192+
/// omitted from serialized YAML.
193+
#[serde(default, skip_serializing_if = "is_zero_u32")]
194+
timeout_seconds: u32,
195+
/// `"allow"` or `"deny"`. Empty means "use the engine default" (deny).
196+
#[serde(default, skip_serializing_if = "String::is_empty")]
197+
fallback: String,
198+
}
199+
200+
/// Serialize an `EnforcementDef` for storage in the proto's `string enforcement`
201+
/// field. The bare-string form is passed through; the object form is encoded
202+
/// as canonical JSON so the round-trip and the OPA-data emission can recover
203+
/// the structure later.
204+
fn enforcement_def_to_proto_string(def: &EnforcementDef) -> String {
205+
match def {
206+
EnforcementDef::Bare(s) => s.clone(),
207+
EnforcementDef::Object(obj) => {
208+
// serde_json::to_string on a struct with all-known fields cannot
209+
// fail; if it ever does, fall back to a sentinel so the proto
210+
// field is not silently empty (which would be interpreted as the
211+
// permissive default).
212+
serde_json::to_string(obj).unwrap_or_else(|_| "interactive".to_string())
213+
}
214+
}
215+
}
216+
217+
/// Reverse of `enforcement_def_to_proto_string`. An empty proto string
218+
/// becomes `None`; a JSON-shaped string is parsed back into the object form;
219+
/// anything else is treated as a bare keyword.
220+
fn enforcement_def_from_proto_string(raw: &str) -> Option<EnforcementDef> {
221+
if raw.is_empty() {
222+
return None;
223+
}
224+
let trimmed = raw.trim_start();
225+
if trimmed.starts_with('{') {
226+
if let Ok(obj) = serde_json::from_str::<InteractiveEnforcementDef>(raw) {
227+
return Some(EnforcementDef::Object(obj));
228+
}
229+
// Malformed JSON shouldn't crash serialization; preserve as bare
230+
// string so the operator can spot the bad value in the round-trip.
231+
}
232+
Some(EnforcementDef::Bare(raw.to_string()))
233+
}
234+
152235
#[derive(Debug, Serialize, Deserialize)]
153236
#[serde(deny_unknown_fields)]
154237
struct GraphqlOperationDef {
@@ -263,7 +346,11 @@ fn to_proto(raw: PolicyFile) -> SandboxPolicy {
263346
ports: normalized_ports,
264347
protocol: e.protocol,
265348
tls: e.tls,
266-
enforcement: e.enforcement,
349+
enforcement: e
350+
.enforcement
351+
.as_ref()
352+
.map(enforcement_def_to_proto_string)
353+
.unwrap_or_default(),
267354
access: e.access,
268355
rules: e
269356
.rules
@@ -430,7 +517,7 @@ fn from_proto(policy: &SandboxPolicy) -> PolicyFile {
430517
ports,
431518
protocol: e.protocol.clone(),
432519
tls: e.tls.clone(),
433-
enforcement: e.enforcement.clone(),
520+
enforcement: enforcement_def_from_proto_string(&e.enforcement),
434521
access: e.access.clone(),
435522
rules: e
436523
.rules
@@ -1818,4 +1905,107 @@ network_policies:
18181905
"port >65535 should fail to parse"
18191906
);
18201907
}
1908+
1909+
#[test]
1910+
fn enforcement_bare_string_back_compat() {
1911+
// Existing YAML using bare-string enforcement must keep parsing into
1912+
// the same proto representation as before.
1913+
let yaml = r"
1914+
version: 1
1915+
network_policies:
1916+
test:
1917+
endpoints:
1918+
- host: api.example.com
1919+
port: 443
1920+
enforcement: enforce
1921+
binaries:
1922+
- path: /usr/bin/curl
1923+
";
1924+
let proto = parse_sandbox_policy(yaml).expect("parse failed");
1925+
assert_eq!(proto.network_policies["test"].endpoints[0].enforcement, "enforce");
1926+
}
1927+
1928+
#[test]
1929+
fn enforcement_interactive_object_round_trip() {
1930+
let yaml = r"
1931+
version: 1
1932+
network_policies:
1933+
test:
1934+
endpoints:
1935+
- host: api.example.com
1936+
port: 443
1937+
enforcement:
1938+
mode: interactive
1939+
endpoint: http://host.openshell.internal:53789/decide
1940+
timeout_seconds: 60
1941+
fallback: deny
1942+
binaries:
1943+
- path: /usr/bin/curl
1944+
";
1945+
let proto1 = parse_sandbox_policy(yaml).expect("parse failed");
1946+
// Proto stores the object form as a JSON-encoded string.
1947+
let stored = &proto1.network_policies["test"].endpoints[0].enforcement;
1948+
assert!(stored.starts_with('{'), "expected JSON-encoded form, got {stored:?}");
1949+
assert!(stored.contains("\"mode\":\"interactive\""));
1950+
assert!(stored.contains("\"endpoint\":\"http://host.openshell.internal:53789/decide\""));
1951+
assert!(stored.contains("\"timeout_seconds\":60"));
1952+
assert!(stored.contains("\"fallback\":\"deny\""));
1953+
1954+
// Round-trip through YAML preserves the structure.
1955+
let yaml_out = serialize_sandbox_policy(&proto1).expect("serialize failed");
1956+
let proto2 = parse_sandbox_policy(&yaml_out).expect("re-parse failed");
1957+
assert_eq!(
1958+
proto1.network_policies["test"].endpoints[0].enforcement,
1959+
proto2.network_policies["test"].endpoints[0].enforcement
1960+
);
1961+
// YAML output uses the human-friendly map form, not JSON-in-a-string.
1962+
assert!(yaml_out.contains("mode: interactive"), "yaml_out:\n{yaml_out}");
1963+
assert!(yaml_out.contains("endpoint: http://host.openshell.internal:53789/decide"));
1964+
}
1965+
1966+
#[test]
1967+
fn enforcement_interactive_minimal_object() {
1968+
// Only `mode` and `endpoint` provided — optional fields stay absent.
1969+
let yaml = r"
1970+
version: 1
1971+
network_policies:
1972+
test:
1973+
endpoints:
1974+
- host: api.example.com
1975+
port: 443
1976+
enforcement:
1977+
mode: interactive
1978+
endpoint: http://h:1/d
1979+
binaries:
1980+
- path: /usr/bin/curl
1981+
";
1982+
let proto1 = parse_sandbox_policy(yaml).expect("parse failed");
1983+
let yaml_out = serialize_sandbox_policy(&proto1).expect("serialize failed");
1984+
assert!(yaml_out.contains("mode: interactive"));
1985+
// Defaulted fields should not appear in serialized YAML.
1986+
assert!(!yaml_out.contains("timeout_seconds"));
1987+
assert!(!yaml_out.contains("fallback"));
1988+
}
1989+
1990+
#[test]
1991+
fn enforcement_object_rejects_unknown_field() {
1992+
let yaml = r"
1993+
version: 1
1994+
network_policies:
1995+
test:
1996+
endpoints:
1997+
- host: api.example.com
1998+
port: 443
1999+
enforcement:
2000+
mode: interactive
2001+
endpoint: http://h:1/d
2002+
bogus: true
2003+
binaries:
2004+
- path: /usr/bin/curl
2005+
";
2006+
assert!(
2007+
parse_sandbox_policy(yaml).is_err(),
2008+
"unknown field under enforcement object should be rejected"
2009+
);
2010+
}
18212011
}

‎crates/openshell-sandbox/Cargo.toml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ rustls-pemfile = { workspace = true }
5050
rcgen = { workspace = true }
5151
webpki-roots = { workspace = true }
5252

53-
# HTTP
53+
# HTTP (outbound — for interactive-enforcement decision client)
54+
reqwest = { workspace = true }
5455
bytes = { workspace = true }
5556

56-
# UUID
57+
# UUID (workspace dep includes v4 feature)
5758
uuid = { workspace = true }
5859

5960
# Encoding
@@ -94,6 +95,7 @@ tempfile = "3"
9495
temp-env = "0.3"
9596
tokio-tungstenite = { workspace = true }
9697
futures = { workspace = true }
98+
wiremock = "0.6"
9799

98100
[lints]
99101
workspace = true

0 commit comments

Comments
 (0)