Skip to content

Commit 7ce3d62

Browse files
committed
feat(supervisor): add workspace to middleware request context
Signed-off-by: Artem Lytvyn <[email protected]>
1 parent 8ce06aa commit 7ce3d62

11 files changed

Lines changed: 209 additions & 19 deletions

File tree

‎crates/openshell-supervisor-middleware/src/lib.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ pub struct HttpRequestInput {
438438
pub request_id: String,
439439
pub sandbox_id: String,
440440
pub sandbox_name: String,
441+
pub workspace: String,
441442
pub scheme: String,
442443
pub host: String,
443444
pub port: u16,
@@ -1583,6 +1584,7 @@ impl ChainRunner {
15831584
request_id,
15841585
sandbox_id,
15851586
sandbox_name,
1587+
workspace,
15861588
scheme,
15871589
host,
15881590
port,
@@ -1600,6 +1602,7 @@ impl ChainRunner {
16001602
request_id,
16011603
sandbox_id,
16021604
sandbox_name,
1605+
workspace,
16031606
originating_process: None,
16041607
};
16051608
let target = HttpRequestTarget {
@@ -2052,6 +2055,7 @@ mod tests {
20522055
request_id: "req".into(),
20532056
sandbox_id: "sbx-id".into(),
20542057
sandbox_name: "sbx-name".into(),
2058+
workspace: "wrks-default".into(),
20552059
scheme: "https".into(),
20562060
host: "api.example.com".into(),
20572061
port: 443,
@@ -3221,6 +3225,7 @@ mod tests {
32213225
assert_eq!(context.request_id, "req");
32223226
assert_eq!(context.sandbox_id, "sbx-id");
32233227
assert_eq!(context.sandbox_name, "sbx-name");
3228+
assert_eq!(context.workspace, "wrks-default");
32243229
assert!(context.originating_process.is_none());
32253230
let target = received[0].target.as_ref().expect("request target");
32263231
assert_eq!(target.scheme, "https");
@@ -4684,6 +4689,7 @@ mod tests {
46844689
request_id: "request".into(),
46854690
sandbox_id: "sandbox".into(),
46864691
sandbox_name: "sandbox-name".into(),
4692+
workspace: "wrks-default".into(),
46874693
scheme: "wss".into(),
46884694
host: "api.openai.com".into(),
46894695
port: 443,
@@ -5118,6 +5124,7 @@ mod tests {
51185124
request_id: "request".into(),
51195125
sandbox_id: "sandbox".into(),
51205126
sandbox_name: "sandbox-name".into(),
5127+
workspace: "wrks-default".into(),
51215128
scheme: "wss".into(),
51225129
host: "api.openai.com".into(),
51235130
port: 443,
@@ -5185,6 +5192,7 @@ mod tests {
51855192
request_id: "request".into(),
51865193
sandbox_id: "sandbox".into(),
51875194
sandbox_name: "sandbox-name".into(),
5195+
workspace: "wrks-default".into(),
51885196
scheme: "wss".into(),
51895197
host: "api.openai.com".into(),
51905198
port: 443,
@@ -5248,6 +5256,7 @@ mod tests {
52485256
request_id: "request".into(),
52495257
sandbox_id: "sandbox".into(),
52505258
sandbox_name: "sandbox-name".into(),
5259+
workspace: "wrks-default".into(),
52515260
scheme: "wss".into(),
52525261
host: "api.openai.com".into(),
52535262
port: 443,
@@ -5320,6 +5329,7 @@ mod tests {
53205329
request_id: "request".into(),
53215330
sandbox_id: "sandbox".into(),
53225331
sandbox_name: "sandbox-name".into(),
5332+
workspace: "wrks-default".into(),
53235333
scheme: "wss".into(),
53245334
host: "api.openai.com".into(),
53255335
port: 443,
@@ -5405,6 +5415,7 @@ mod tests {
54055415
request_id: "request".into(),
54065416
sandbox_id: "sandbox".into(),
54075417
sandbox_name: "sandbox-name".into(),
5418+
workspace: "wrks-default".into(),
54085419
scheme: "ws".into(),
54095420
host: "api.openai.com".into(),
54105421
port: 80,
@@ -5701,6 +5712,7 @@ mod tests {
57015712
request_id: "request".into(),
57025713
sandbox_id: "sandbox".into(),
57035714
sandbox_name: "sandbox-name".into(),
5715+
workspace: "wrks-default".into(),
57045716
scheme: "wss".into(),
57055717
host: "api.openai.com".into(),
57065718
port: 443,

‎crates/openshell-supervisor-middleware/src/websocket.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub struct WebSocketPreflightInput {
3939
pub request_id: String,
4040
pub sandbox_id: String,
4141
pub sandbox_name: String,
42+
pub workspace: String,
4243
pub scheme: String,
4344
pub host: String,
4445
pub port: u16,
@@ -922,6 +923,7 @@ async fn open_stage(entry: DescribedChainEntry, input: WebSocketPreflightInput)
922923
request_id: input.request_id,
923924
sandbox_id: input.sandbox_id,
924925
sandbox_name: input.sandbox_name,
926+
workspace: input.workspace,
925927
originating_process: None,
926928
}),
927929
target: Some(HttpRequestTarget {

‎crates/openshell-supervisor-network/src/l7/middleware.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ pub(super) fn middleware_request_input(
624624
request_id: uuid::Uuid::new_v4().to_string(),
625625
sandbox_id: sandbox.sandbox_id.clone(),
626626
sandbox_name: sandbox.sandbox_name.clone(),
627+
workspace: ctx.workspace.clone(),
627628
scheme: scheme.into(),
628629
host: ctx.host.clone(),
629630
port: ctx.port,
@@ -1099,6 +1100,7 @@ mod tests {
10991100
let eval = L7EvalContext {
11001101
host: "api.example.test".into(),
11011102
port: 443,
1103+
workspace: "wrks-default".into(),
11021104
policy_name: "api-policy".into(),
11031105
binary_path: "/usr/bin/curl".into(),
11041106
ancestors: Vec::new(),
@@ -1127,6 +1129,7 @@ mod tests {
11271129

11281130
assert_eq!(input.sandbox_name, "nightly-build");
11291131
assert_eq!(input.sandbox_id, "sbx-123");
1132+
assert_eq!(input.workspace, "wrks-default");
11301133
}
11311134

11321135
#[tokio::test]

‎crates/openshell-supervisor-network/src/l7/relay.rs‎

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub struct L7EvalContext {
4242
pub host: String,
4343
/// Port from the CONNECT request.
4444
pub port: u16,
45+
/// Workspace the sandbox belongs to, learned from `GetSandboxConfigResponse`.
46+
pub workspace: String,
4547
/// Default authority port for the inspected HTTP transport (80 for
4648
/// plaintext, 443 after TLS termination).
4749
pub(crate) request_default_port: Option<u16>,
@@ -983,22 +985,39 @@ pub(crate) async fn websocket_middleware_preflight(
983985
.map_or(req.raw_header.len(), |position| position + 4);
984986
let requested_subprotocols =
985987
crate::l7::rest::websocket_requested_subprotocols(&req.raw_header[..header_end])?;
986-
runner
987-
.preflight_websocket(
988-
chain,
989-
openshell_supervisor_middleware::WebSocketPreflightInput {
990-
session_id: uuid::Uuid::new_v4().to_string(),
991-
request_id: uuid::Uuid::new_v4().to_string(),
992-
sandbox_id: openshell_ocsf::ctx::ctx().sandbox_id.clone(),
993-
sandbox_name: openshell_ocsf::ctx::ctx().sandbox_name.clone(),
994-
scheme: scheme.to_string(),
995-
host: ctx.host.clone(),
996-
port: ctx.port,
997-
path: req.target.clone(),
998-
requested_subprotocols,
999-
},
1000-
)
1001-
.await
988+
let input = websocket_preflight_input(
989+
openshell_ocsf::ctx::ctx(),
990+
ctx,
991+
req,
992+
scheme,
993+
requested_subprotocols,
994+
);
995+
runner.preflight_websocket(chain, input).await
996+
}
997+
998+
/// Build the WebSocket preflight input from the sandbox and evaluation
999+
/// contexts. Kept separate from `websocket_middleware_preflight` (and taking an
1000+
/// explicit `SandboxContext`) so the identifier copy is unit-testable with a
1001+
/// real sandbox name, mirroring `middleware_request_input` on the HTTP path.
1002+
fn websocket_preflight_input(
1003+
sandbox: &openshell_ocsf::SandboxContext,
1004+
ctx: &L7EvalContext,
1005+
req: &crate::l7::provider::L7Request,
1006+
scheme: &str,
1007+
requested_subprotocols: Vec<String>,
1008+
) -> openshell_supervisor_middleware::WebSocketPreflightInput {
1009+
openshell_supervisor_middleware::WebSocketPreflightInput {
1010+
session_id: uuid::Uuid::new_v4().to_string(),
1011+
request_id: uuid::Uuid::new_v4().to_string(),
1012+
sandbox_id: sandbox.sandbox_id.clone(),
1013+
sandbox_name: sandbox.sandbox_name.clone(),
1014+
workspace: ctx.workspace.clone(),
1015+
scheme: scheme.to_string(),
1016+
host: ctx.host.clone(),
1017+
port: ctx.port,
1018+
path: req.target.clone(),
1019+
requested_subprotocols,
1020+
}
10021021
}
10031022

10041023
/// Handle an upgraded connection (101 Switching Protocols).
@@ -2833,6 +2852,45 @@ mod tests {
28332852
(state, resolver)
28342853
}
28352854

2855+
#[test]
2856+
fn websocket_preflight_input_carries_real_sandbox_name() {
2857+
let sandbox = openshell_ocsf::SandboxContext {
2858+
sandbox_id: "sbx-123".into(),
2859+
sandbox_name: "nightly-build".into(),
2860+
container_image: String::new(),
2861+
hostname: "h".into(),
2862+
product_version: "0".into(),
2863+
proxy_ip: [127, 0, 0, 1].into(),
2864+
proxy_port: 3128,
2865+
};
2866+
2867+
let eval = L7EvalContext {
2868+
host: "api.example.test".into(),
2869+
port: 443,
2870+
workspace: "team-a".into(),
2871+
policy_name: "api-policy".into(),
2872+
binary_path: "/usr/bin/curl".into(),
2873+
ancestors: Vec::new(),
2874+
cmdline_paths: Vec::new(),
2875+
secret_resolver: None,
2876+
..Default::default()
2877+
};
2878+
let req = crate::l7::provider::L7Request {
2879+
action: "GET".into(),
2880+
target: "/v1/stream".into(),
2881+
query_params: std::collections::HashMap::new(),
2882+
raw_header: Vec::new(),
2883+
body_length: crate::l7::provider::BodyLength::None,
2884+
};
2885+
2886+
let input =
2887+
websocket_preflight_input(&sandbox, &eval, &req, "wss", vec!["chat".to_string()]);
2888+
2889+
assert_eq!(input.sandbox_id, "sbx-123");
2890+
assert_eq!(input.sandbox_name, "nightly-build");
2891+
assert_eq!(input.workspace, "team-a");
2892+
}
2893+
28362894
#[test]
28372895
fn scoped_context_captures_endpoint_resolver_and_revision_together() {
28382896
let state = ProviderCredentialState::from_bound_environment(

‎crates/openshell-supervisor-network/src/l7/websocket.rs‎

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3367,6 +3367,10 @@ network_policies:
33673367
close_on_first_message: bool,
33683368
message_received: Option<Arc<tokio::sync::Notify>>,
33693369
release_message: Option<Arc<tokio::sync::Notify>>,
3370+
// Opt-in capture of the preflight request context's workspace. Kept
3371+
// separate from `observed` so it does not perturb the session-event
3372+
// ordering the other tests assert on.
3373+
preflight_observed: Option<tokio::sync::mpsc::UnboundedSender<String>>,
33703374
}
33713375

33723376
#[tonic::async_trait]
@@ -3433,11 +3437,20 @@ network_policies:
34333437
let close_on_first_message = self.close_on_first_message;
34343438
let message_received = self.message_received.clone();
34353439
let release_message = self.release_message.clone();
3440+
let preflight_observed = self.preflight_observed.clone();
34363441
let (responses_tx, responses_rx) = tokio::sync::mpsc::channel(4);
34373442
tokio::spawn(async move {
34383443
while let Ok(Some(request)) = requests.message().await {
34393444
let response = match request.event {
3440-
Some(web_socket_session_event::Event::Preflight(_)) => {
3445+
Some(web_socket_session_event::Event::Preflight(preflight)) => {
3446+
if let Some(preflight_observed) = &preflight_observed {
3447+
let workspace = preflight
3448+
.context
3449+
.as_ref()
3450+
.map(|context| context.workspace.clone())
3451+
.unwrap_or_default();
3452+
let _ = preflight_observed.send(workspace);
3453+
}
34413454
Some(WebSocketSessionEventResult {
34423455
result: Some(
34433456
web_socket_session_event_result::Result::PreflightDecision(
@@ -3599,6 +3612,7 @@ network_policies:
35993612
request_id: "request".into(),
36003613
sandbox_id: "sandbox".into(),
36013614
sandbox_name: "sandbox-name".into(),
3615+
workspace: "workspace".into(),
36023616
scheme: scheme.into(),
36033617
host: "api.openai.com".into(),
36043618
port: if scheme == "wss" { 443 } else { 80 },
@@ -3617,6 +3631,80 @@ network_policies:
36173631
)
36183632
}
36193633

3634+
// Companion to the HTTP-path assertion in openshell-supervisor-middleware:
3635+
// proves the WebSocket preflight forwards the request context's workspace to
3636+
// the middleware service.
3637+
#[tokio::test]
3638+
async fn websocket_preflight_forwards_workspace_to_middleware() {
3639+
use openshell_core::proto::SupervisorMiddlewareService;
3640+
use openshell_supervisor_middleware::{ChainEntry, MiddlewareRegistry, OnError};
3641+
3642+
let (preflight_tx, mut preflight_rx) = tokio::sync::mpsc::unbounded_channel();
3643+
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
3644+
.await
3645+
.expect("bind WebSocket middleware");
3646+
let address = listener.local_addr().expect("middleware address");
3647+
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
3648+
let server = tonic::transport::Server::builder()
3649+
.add_service(SupervisorMiddlewareServer::new(OpenAiWebSocketRedactor {
3650+
preflight_observed: Some(preflight_tx),
3651+
..Default::default()
3652+
}))
3653+
.serve_with_incoming_shutdown(TcpListenerStream::new(listener), async {
3654+
let _ = shutdown_rx.await;
3655+
});
3656+
let server_task = tokio::spawn(server);
3657+
let registry = MiddlewareRegistry::connect_services(
3658+
Vec::new(),
3659+
vec![SupervisorMiddlewareService {
3660+
name: "openai-redactor".into(),
3661+
grpc_endpoint: format!("http://{address}"),
3662+
max_payload_bytes: openshell_supervisor_middleware::MAX_MIDDLEWARE_PAYLOAD_BYTES
3663+
as u64,
3664+
timeout: "2s".into(),
3665+
tls_ca_cert_pem: Vec::new(),
3666+
audience: String::new(),
3667+
allow_insecure_transport: false,
3668+
}],
3669+
)
3670+
.await
3671+
.expect("connect middleware");
3672+
let runner = openshell_supervisor_middleware::ChainRunner::from_registry(registry);
3673+
runner
3674+
.preflight_websocket(
3675+
&[ChainEntry {
3676+
name: "redact-openai".into(),
3677+
implementation: "openai-redactor".into(),
3678+
order: 0,
3679+
config: prost_types::Struct::default(),
3680+
on_error: OnError::FailClosed,
3681+
}],
3682+
openshell_supervisor_middleware::WebSocketPreflightInput {
3683+
session_id: "session".into(),
3684+
request_id: "request".into(),
3685+
sandbox_id: "sandbox".into(),
3686+
sandbox_name: "sandbox-name".into(),
3687+
workspace: "team-a".into(),
3688+
scheme: "wss".into(),
3689+
host: "api.openai.com".into(),
3690+
port: 443,
3691+
path: "/v1/responses".into(),
3692+
requested_subprotocols: Vec::new(),
3693+
},
3694+
)
3695+
.await
3696+
.expect("preflight");
3697+
3698+
assert_eq!(
3699+
preflight_rx.recv().await,
3700+
Some("team-a".to_string()),
3701+
"WebSocket preflight must forward the workspace to the middleware"
3702+
);
3703+
3704+
let _ = shutdown_tx.send(());
3705+
let _ = server_task.await;
3706+
}
3707+
36203708
async fn assert_invalid_close_termination(payload: Vec<u8>, expected_close_code: u16) {
36213709
let (mut session, mut observed, shutdown_tx, server_task) =
36223710
recording_middleware_session("wss").await;
@@ -4484,6 +4572,7 @@ network_policies:
44844572
request_id: "request".into(),
44854573
sandbox_id: "sandbox".into(),
44864574
sandbox_name: "sandbox-name".into(),
4575+
workspace: "workspace".into(),
44874576
scheme: "wss".into(),
44884577
host: "api.openai.com".into(),
44894578
port: 443,
@@ -4632,6 +4721,7 @@ network_policies:
46324721
request_id: "request".into(),
46334722
sandbox_id: "sandbox".into(),
46344723
sandbox_name: "sandbox-name".into(),
4724+
workspace: "workspace".into(),
46354725
scheme: "wss".into(),
46364726
host: "api.openai.com".into(),
46374727
port: 443,
@@ -4752,6 +4842,7 @@ network_policies:
47524842
request_id: "request".into(),
47534843
sandbox_id: "sandbox".into(),
47544844
sandbox_name: "sandbox-name".into(),
4845+
workspace: "workspace".into(),
47554846
scheme: "wss".into(),
47564847
host: "api.openai.com".into(),
47574848
port: 443,

0 commit comments

Comments
 (0)