Skip to content

Commit f1b9873

Browse files
committed
feat(sandbox): stop only the attested execution
Bind conditional stops and extension credentials to each launch. Preserve historical receipts across retries and reject stale execution identities. Fixes: NVIDIA#4009 Signed-off-by: Daneyon Hansen <[email protected]>
1 parent 634ca7d commit f1b9873

47 files changed

Lines changed: 3207 additions & 1462 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎crates/openshell-cli/src/main.rs‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,10 @@ enum SandboxCommands {
17041704
/// Sandbox name (defaults to last-used sandbox).
17051705
#[arg(add = ArgValueCompleter::new(completers::complete_sandbox_names))]
17061706
name: Option<String>,
1707+
1708+
/// Stop only this execution; reject a restarted or replaced sandbox.
1709+
#[arg(long)]
1710+
execution_id: Option<String>,
17071711
},
17081712

17091713
/// Start a stopped sandbox.
@@ -3609,9 +3613,16 @@ async fn run_async() -> Result<()> {
36093613
)
36103614
.await?;
36113615
}
3612-
SandboxCommands::Stop { name } => {
3616+
SandboxCommands::Stop { name, execution_id } => {
36133617
let name = resolve_sandbox_name(name, &ctx.name, &cli.workspace)?;
3614-
run::sandbox_stop(endpoint, &name, &cli.workspace, &tls).await?;
3618+
run::sandbox_stop(
3619+
endpoint,
3620+
&name,
3621+
&cli.workspace,
3622+
execution_id.as_deref(),
3623+
&tls,
3624+
)
3625+
.await?;
36153626
}
36163627
SandboxCommands::Start { name } => {
36173628
let name = resolve_sandbox_name(name, &ctx.name, &cli.workspace)?;
@@ -5555,7 +5566,7 @@ mod tests {
55555566
assert!(matches!(
55565567
stop.command,
55575568
Some(Commands::Sandbox {
5558-
command: Some(SandboxCommands::Stop { name: Some(ref name) }),
5569+
command: Some(SandboxCommands::Stop { name: Some(ref name), execution_id: None }),
55595570
}) if name == "demo"
55605571
));
55615572

@@ -5601,6 +5612,22 @@ mod tests {
56015612
));
56025613
}
56035614

5615+
#[test]
5616+
fn sandbox_stop_accepts_execution_precondition() {
5617+
let cli = Cli::try_parse_from([
5618+
"openshell",
5619+
"sandbox",
5620+
"stop",
5621+
"demo",
5622+
"--execution-id",
5623+
"e2f8625a-f7c5-4bca-bbb3-4d793c646643",
5624+
])
5625+
.expect("conditional stop should parse");
5626+
assert!(matches!(cli.command, Some(Commands::Sandbox {
5627+
command: Some(SandboxCommands::Stop { name: Some(ref name), execution_id: Some(ref id) })
5628+
}) if name == "demo" && id == "e2f8625a-f7c5-4bca-bbb3-4d793c646643"));
5629+
}
5630+
56045631
#[test]
56055632
fn sandbox_list_accepts_output_json() {
56065633
let cli = Cli::try_parse_from(["openshell", "sandbox", "list", "-o", "json"])

‎crates/openshell-cli/src/run.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3118,6 +3118,7 @@ fn sandbox_to_json(sandbox: &Sandbox) -> serde_json::Value {
31183118
"resource_version": meta.map_or(0, |m| m.resource_version),
31193119
"created_at": format_epoch_ms(meta.map_or(0, |m| proto_timestamp_ms(m.created_time.as_ref()))),
31203120
"phase": phase_name(sandbox.phase()),
3121+
"execution_id": sandbox.status.as_ref().map(|status| status.execution_id.as_str()).filter(|id| !id.is_empty()),
31213122
"current_policy_version": sandbox.current_policy_version(),
31223123
"exit_code": sandbox.status.as_ref().and_then(|status| status.exit_code),
31233124
"conditions": conditions,
@@ -4091,8 +4092,31 @@ pub async fn sandbox_stop(
40914092
server: &str,
40924093
name: &str,
40934094
workspace: &str,
4095+
execution_id: Option<&str>,
40944096
tls: &TlsOptions,
40954097
) -> Result<()> {
4098+
if let Some(execution_id) = execution_id {
4099+
let mut client = grpc_client(server, tls).await?;
4100+
let receipt = client
4101+
.stop_sandbox_execution(openshell_core::proto::StopSandboxExecutionRequest {
4102+
workspace_scope: Some(openshell_core::proto::workspace_selector(workspace)),
4103+
name: name.to_string(),
4104+
execution_id: execution_id.to_string(),
4105+
request_id: String::new(),
4106+
})
4107+
.await
4108+
.into_diagnostic()?
4109+
.into_inner();
4110+
// Do not poll by name or change name-keyed forwards: either could now
4111+
// belong to a later execution. This is the requested execution's receipt.
4112+
let phase = SandboxPhase::try_from(receipt.phase).unwrap_or(SandboxPhase::Unknown);
4113+
println!(
4114+
"{} Execution {} stop receipt: {phase:?}",
4115+
"✓".green().bold(),
4116+
receipt.execution_id
4117+
);
4118+
return Ok(());
4119+
}
40964120
if let Ok(stopped) = stop_forwards_for_sandbox(workspace, name) {
40974121
for port in stopped {
40984122
eprintln!(

‎crates/openshell-cli/tests/ensure_providers_integration.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ impl OpenShell for TestOpenShell {
176176
Err(Status::unimplemented("unused"))
177177
}
178178

179+
async fn stop_sandbox_execution(
180+
&self,
181+
_request: tonic::Request<openshell_core::proto::StopSandboxExecutionRequest>,
182+
) -> Result<Response<openshell_core::proto::StopSandboxExecutionResponse>, Status> {
183+
Err(Status::unimplemented(
184+
"execution stop is not implemented by this fixture",
185+
))
186+
}
187+
179188
async fn start_sandbox(
180189
&self,
181190
_request: tonic::Request<openshell_core::proto::StartSandboxRequest>,

‎crates/openshell-cli/tests/mtls_integration.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ impl OpenShell for TestOpenShell {
130130
Err(Status::unimplemented("unused"))
131131
}
132132

133+
async fn stop_sandbox_execution(
134+
&self,
135+
_request: tonic::Request<openshell_core::proto::StopSandboxExecutionRequest>,
136+
) -> Result<Response<openshell_core::proto::StopSandboxExecutionResponse>, Status> {
137+
Err(Status::unimplemented(
138+
"execution stop is not implemented by this fixture",
139+
))
140+
}
141+
133142
async fn start_sandbox(
134143
&self,
135144
_request: tonic::Request<openshell_core::proto::StartSandboxRequest>,

‎crates/openshell-cli/tests/provider_commands_integration.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,15 @@ impl OpenShell for TestOpenShell {
331331
Err(Status::unimplemented("unused"))
332332
}
333333

334+
async fn stop_sandbox_execution(
335+
&self,
336+
_request: tonic::Request<openshell_core::proto::StopSandboxExecutionRequest>,
337+
) -> Result<Response<openshell_core::proto::StopSandboxExecutionResponse>, Status> {
338+
Err(Status::unimplemented(
339+
"execution stop is not implemented by this fixture",
340+
))
341+
}
342+
334343
async fn start_sandbox(
335344
&self,
336345
_request: tonic::Request<openshell_core::proto::StartSandboxRequest>,

‎crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ impl OpenShell for TestOpenShell {
245245
}))
246246
}
247247

248+
async fn stop_sandbox_execution(
249+
&self,
250+
_request: tonic::Request<openshell_core::proto::StopSandboxExecutionRequest>,
251+
) -> Result<Response<openshell_core::proto::StopSandboxExecutionResponse>, Status> {
252+
Err(Status::unimplemented(
253+
"execution stop is not implemented by this fixture",
254+
))
255+
}
256+
248257
async fn start_sandbox(
249258
&self,
250259
_request: tonic::Request<openshell_core::proto::StartSandboxRequest>,

‎crates/openshell-cli/tests/sandbox_exec_streaming_integration.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ mock_gateway! {
235235
get_sandbox_provider_status(proto::GetSandboxProviderStatusRequest) -> proto::GetSandboxProviderStatusResponse;
236236
delete_sandbox(proto::DeleteSandboxRequest) -> proto::DeleteSandboxResponse;
237237
stop_sandbox(proto::StopSandboxRequest) -> proto::SandboxResponse;
238+
stop_sandbox_execution(proto::StopSandboxExecutionRequest) -> proto::StopSandboxExecutionResponse;
238239
start_sandbox(proto::StartSandboxRequest) -> proto::SandboxResponse;
239240
suspend_sandbox(proto::SuspendSandboxRequest) -> proto::SandboxResponse;
240241
resume_sandbox(proto::ResumeSandboxRequest) -> proto::SandboxResponse;

‎crates/openshell-cli/tests/sandbox_name_fallback_integration.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ impl OpenShell for TestOpenShell {
143143
Err(Status::unimplemented("unused"))
144144
}
145145

146+
async fn stop_sandbox_execution(
147+
&self,
148+
_request: tonic::Request<openshell_core::proto::StopSandboxExecutionRequest>,
149+
) -> Result<Response<openshell_core::proto::StopSandboxExecutionResponse>, Status> {
150+
Err(Status::unimplemented(
151+
"execution stop is not implemented by this fixture",
152+
))
153+
}
154+
146155
async fn start_sandbox(
147156
&self,
148157
_request: tonic::Request<openshell_core::proto::StartSandboxRequest>,

‎crates/openshell-extension-core/src/jwt.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ pub struct ExtensionJwtClaims {
4343
pub caller_kind: ExtensionCallerKind,
4444
#[serde(skip_serializing_if = "Option::is_none")]
4545
pub sandbox_id: Option<String>,
46+
/// Opaque launch identity for conditional lifecycle actions. Consumers must
47+
/// use this claim only after verifying the JWT and matching `sandbox_id` to
48+
/// the observation. Absent on gateway calls and legacy supervisor tokens.
49+
#[serde(default, skip_serializing_if = "Option::is_none")]
50+
pub execution_id: Option<String>,
4651
}
4752

4853
#[cfg(test)]
@@ -72,6 +77,7 @@ mod tests {
7277
jti: "unique".to_string(),
7378
caller_kind: ExtensionCallerKind::Gateway,
7479
sandbox_id: None,
80+
execution_id: None,
7581
};
7682
let json = serde_json::to_value(claims).unwrap();
7783
assert!(json.get("sandbox_id").is_none());

‎crates/openshell-sdk/src/client.rs‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::routing::OwnerCache;
1818
use crate::types::{
1919
DeleteOptions, DeletionResult, ExecOptions, ExecResult, Health, ListOptions, ResumeOptions,
2020
SandboxPhase, SandboxRef, SandboxSpec, SandboxTemplateCreateSpec, SandboxTemplateListOptions,
21-
SandboxWorkloadTemplate, WorkspaceRef,
21+
SandboxWorkloadTemplate, StopExecutionOptions, StopExecutionResult, WorkspaceRef,
2222
};
2323
use crate::{WatchEvent, WatchOptions, transport};
2424
use futures::{Stream, StreamExt};
@@ -399,6 +399,22 @@ impl OpenShellClient {
399399
sandbox_from_response(response.sandbox)
400400
}
401401

402+
/// Stop only the requested execution in the default workspace.
403+
///
404+
/// Stale identities fail without stopping a replacement. Unsupported
405+
/// gateways return `Unimplemented`; this never falls back to stopping by name.
406+
/// A replay returns a historical receipt, not the current sandbox state.
407+
pub async fn stop_sandbox_execution(
408+
&self,
409+
name: &str,
410+
execution_id: &str,
411+
opts: StopExecutionOptions,
412+
) -> Result<StopExecutionResult> {
413+
self.workspace("default")
414+
.stop_sandbox_execution(name, execution_id, opts)
415+
.await
416+
}
417+
402418
/// Start a stopped sandbox by name.
403419
pub async fn start_sandbox(&self, name: &str) -> Result<SandboxRef> {
404420
let response = self
@@ -1213,6 +1229,39 @@ impl WorkspaceScopedClient {
12131229
sandbox_from_response(response.sandbox)
12141230
}
12151231

1232+
/// Stop only the requested execution in this workspace.
1233+
///
1234+
/// The execution identity must come from sandbox status or a verified
1235+
/// supervisor extension JWT. Unsupported gateways never fall back to stop.
1236+
pub async fn stop_sandbox_execution(
1237+
&self,
1238+
name: &str,
1239+
execution_id: &str,
1240+
opts: StopExecutionOptions,
1241+
) -> Result<StopExecutionResult> {
1242+
if execution_id.is_empty() {
1243+
return Err(SdkError::from_status(tonic::Status::invalid_argument(
1244+
"execution ID must not be empty",
1245+
)));
1246+
}
1247+
let response = self
1248+
.client
1249+
.unary(|mut grpc| {
1250+
let request = proto::StopSandboxExecutionRequest {
1251+
request_id: opts.request_id.clone().unwrap_or_default(),
1252+
name: name.to_string(),
1253+
execution_id: execution_id.to_string(),
1254+
workspace_scope: Some(proto::workspace_selector(&self.workspace)),
1255+
};
1256+
async move { grpc.stop_sandbox_execution(request).await }
1257+
})
1258+
.await?;
1259+
Ok(StopExecutionResult {
1260+
execution_id: response.execution_id,
1261+
phase: response.phase.into(),
1262+
})
1263+
}
1264+
12161265
/// Start a stopped sandbox by name in this workspace.
12171266
pub async fn start_sandbox(&self, name: &str) -> Result<SandboxRef> {
12181267
let response = self

0 commit comments

Comments
 (0)