Skip to content

Commit b68c1cf

Browse files
committed
feat(compute): add ordered provider start contract and launch journal
Add the compute driver contract for replayable provider launches and the driver SDK journal that implements it. - StartSandbox and StopSandbox carry an OrderedStart operation and return a durable OrderedStartResult receipt. Receipt-only lookup and settle-only starts let a gateway resolve an operation without changing compute. - The launch bundle can carry the stable and per-launch SPIFFE IDs that a driver binds to the supervisor it starts. - The driver SDK journals each accepted operation and its terminal outcome under a per-sandbox lock, replays exact retries, and rejects superseded operations. Admitted work survives caller cancellation and drains on shutdown. - The journal lock is unlocked before its file closes, so a child process that briefly holds a copy of the descriptor cannot keep it held. - The journal can live below a state directory that is a symlink. The journal directory itself must be a real directory. - Drivers and the gateway keep their current behavior. Existing call sites only initialize the new fields. Signed-off-by: Shiju <shiju@nvidia.com>
1 parent 659b45f commit b68c1cf

33 files changed

Lines changed: 1908 additions & 25 deletions

File tree

‎crates/openshell-core/src/driver_utils.rs‎

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,71 @@
55
66
use rustls::pki_types::{CertificateDer, pem::PemObject};
77
use std::path::{Path, PathBuf};
8+
use std::time::Duration;
89

910
use crate::proto::compute::v1::DriverSandbox;
1011

12+
/// Time a managed driver defers an external stop signal while awaiting release.
13+
pub const EXTERNAL_STOP_GRACE: Duration = Duration::from_secs(30);
14+
15+
/// Time a driver gives admitted provider launch work to record its outcome.
16+
pub const PROVIDER_LAUNCH_DRAIN_GRACE: Duration = Duration::from_mins(3);
17+
18+
/// Bind an ordered start and its private credentials to one sandbox launch.
19+
#[cfg(feature = "jwt")]
20+
#[allow(clippy::result_large_err)]
21+
pub fn validate_ordered_start_authentication(
22+
sandbox_id: &str,
23+
generation_id: &str,
24+
operation: &crate::proto::compute::v1::OrderedStart,
25+
authentication: &crate::jwt::SandboxLaunchAuthentication,
26+
) -> Result<(), tonic::Status> {
27+
let invalid =
28+
|| tonic::Status::invalid_argument("ordered start does not match launch identity");
29+
let supervisor = &authentication.supervisor;
30+
if supervisor.runtime_generation.as_str() != generation_id {
31+
return Err(invalid());
32+
}
33+
let sandbox_path = format!(
34+
"/gateway/{}/sandbox/{sandbox_id}",
35+
authentication.gateway_id
36+
);
37+
let prefix = supervisor
38+
.provider_spiffe_id
39+
.as_deref()
40+
.and_then(|id| id.strip_suffix(&sandbox_path))
41+
.ok_or_else(invalid)?;
42+
let prefix = openshell_spiffe::SandboxSpiffeIdPrefix::parse(prefix).map_err(|_| invalid())?;
43+
let expected = prefix
44+
.supervisor_launch_spiffe_id(
45+
&authentication.gateway_id,
46+
sandbox_id,
47+
&operation.operation_id,
48+
)
49+
.map_err(|_| invalid())?;
50+
if supervisor.provider_launch_spiffe_id.as_deref() != Some(expected.as_str()) {
51+
return Err(invalid());
52+
}
53+
Ok(())
54+
}
55+
56+
/// Gateway alias available inside a sandbox VM.
57+
pub const VM_HOST_GATEWAY_ALIAS: &str = "host.openshell.internal";
58+
59+
/// Gateway hostnames that VM host supervisors rewrite to numeric loopback.
60+
pub const VM_HOST_LOOPBACK_ALIASES: &[&str] = &[
61+
VM_HOST_GATEWAY_ALIAS,
62+
"host.containers.internal",
63+
"host.docker.internal",
64+
];
65+
66+
/// Supervisor command that reports whether it can see a Workload API socket.
67+
pub const PROVIDER_WORKLOAD_IDENTITY_PROBE: &str = "provider-workload-identity-probe";
68+
69+
/// Exit statuses for the provider probe with a socket path argument.
70+
pub const PROVIDER_WORKLOAD_SOCKET_PROBE_PRESENT: i32 = 70;
71+
pub const PROVIDER_WORKLOAD_SOCKET_PROBE_UNAVAILABLE: i32 = 71;
72+
1173
// ---------------------------------------------------------------------------
1274
// Sandbox container/pod label keys (openshell.ai/ namespace)
1375
// ---------------------------------------------------------------------------
@@ -34,6 +96,9 @@ pub const LABEL_SANDBOX_WORKSPACE: &str = "openshell.ai/sandbox-workspace";
3496
/// Label carrying the gateway identity on managed namespaces.
3597
pub const LABEL_GATEWAY_ID: &str = "openshell.ai/gateway-id";
3698

99+
/// SPIFFE ID registered for one driver-owned supervisor launch.
100+
pub const LABEL_PROVIDER_LAUNCH_SPIFFE_ID: &str = "openshell.ai/provider-launch-spiffe-id";
101+
37102
/// Label selector that matches all OpenShell-managed resources which carry a
38103
/// sandbox ID label. Used by list and watch operations to exclude foreign
39104
/// resources from the same namespace.
@@ -743,6 +808,24 @@ pub fn validate_provider_spiffe_unix_socket(path: &Path) -> Result<(), String> {
743808
Ok(())
744809
}
745810

811+
/// Check that an absolute path resolves to a Unix socket in the current filesystem.
812+
pub fn provider_spiffe_socket_visible(path: &Path) -> bool {
813+
#[cfg(unix)]
814+
{
815+
use std::os::unix::fs::FileTypeExt as _;
816+
817+
path.is_absolute()
818+
&& path
819+
.metadata()
820+
.is_ok_and(|meta| meta.file_type().is_socket())
821+
}
822+
#[cfg(not(unix))]
823+
{
824+
let _ = path;
825+
false
826+
}
827+
}
828+
746829
/// Return the guest/container path for a projected provider SPIFFE socket.
747830
pub fn projected_provider_spiffe_socket_path(path: &Path) -> Result<String, String> {
748831
validate_provider_spiffe_unix_socket(path)?;
@@ -1257,7 +1340,7 @@ mod tests {
12571340
let err = read_upstream_proxy_credential_file(fifo.to_str().unwrap()).unwrap_err();
12581341
assert!(err.contains("regular file"), "{err}");
12591342
assert!(
1260-
start.elapsed() < std::time::Duration::from_secs(5),
1343+
start.elapsed() < Duration::from_secs(5),
12611344
"reading a FIFO must not block"
12621345
);
12631346
}
@@ -1550,4 +1633,68 @@ mod tests {
15501633
})
15511634
.expect("an intercepting proxy's CA is meaningful without an https:// proxy URL");
15521635
}
1636+
1637+
#[cfg(feature = "jwt")]
1638+
#[test]
1639+
fn ordered_start_authentication_binds_generation_sandbox_and_launch() {
1640+
use crate::jwt::{
1641+
CredentialEpoch, SandboxLaunchAuthentication, SecretJwt, SessionRotation,
1642+
SupervisorAuthBundle,
1643+
};
1644+
use crate::proto::compute::v1::OrderedStart;
1645+
use crate::sandbox_generation::SandboxGenerationId;
1646+
use crate::sandbox_session::SandboxSessionId;
1647+
1648+
let sandbox = "spiffe://example.test/gateway/gateway/sandbox/sandbox-a";
1649+
let authentication = SandboxLaunchAuthentication {
1650+
supervisor: SupervisorAuthBundle {
1651+
session_id: SandboxSessionId::new(),
1652+
runtime_generation: SandboxGenerationId::parse("generation").unwrap(),
1653+
session_rotation: SessionRotation::new(1).unwrap(),
1654+
auth_epoch: CredentialEpoch::new(1).unwrap(),
1655+
gateway_token: SecretJwt::parse("gateway-token").unwrap(),
1656+
gateway_expires_at: 0,
1657+
sandbox_token: SecretJwt::parse("sandbox-token").unwrap(),
1658+
sandbox_expires_at: 0,
1659+
provider_spiffe_id: Some(sandbox.to_string()),
1660+
provider_launch_spiffe_id: Some(format!("{sandbox}/launch/launch-1")),
1661+
ssh_host_private_key: None,
1662+
traces_otlp_headers: None,
1663+
ocsf_otlp_headers: None,
1664+
agent_otlp_headers: None,
1665+
otlp_tls: Vec::new(),
1666+
traces_otlp_tls: None,
1667+
ocsf_otlp_tls: None,
1668+
agent_otlp_tls: None,
1669+
},
1670+
gateway_id: "gateway".to_string(),
1671+
verification_keys: Vec::new(),
1672+
};
1673+
let validate = |sandbox_id: &str, generation_id: &str, launch_id: &str| {
1674+
let operation = OrderedStart {
1675+
revision: 1,
1676+
operation_id: launch_id.to_string(),
1677+
};
1678+
validate_ordered_start_authentication(
1679+
sandbox_id,
1680+
generation_id,
1681+
&operation,
1682+
&authentication,
1683+
)
1684+
};
1685+
1686+
validate("sandbox-a", "generation", "launch-1").unwrap();
1687+
for (sandbox_id, generation_id, launch_id) in [
1688+
("sandbox-a", "other-generation", "launch-1"),
1689+
("sandbox-b", "generation", "launch-1"),
1690+
("sandbox-a", "generation", "launch-2"),
1691+
] {
1692+
let error = validate(sandbox_id, generation_id, launch_id).unwrap_err();
1693+
assert_eq!(
1694+
error.code(),
1695+
tonic::Code::InvalidArgument,
1696+
"{sandbox_id} {generation_id} {launch_id}"
1697+
);
1698+
}
1699+
}
15531700
}

‎crates/openshell-core/src/error.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ pub enum ComputeDriverError {
122122
/// A precondition for the operation was not met.
123123
#[error("{0}")]
124124
Precondition(String),
125+
/// Communication ended before the backend operation outcome was known.
126+
#[error("{0}")]
127+
BackendUnavailable(String),
125128
/// Generic error message.
126129
#[error("{0}")]
127130
Message(String),
@@ -134,6 +137,7 @@ impl From<ComputeDriverError> for tonic::Status {
134137
ComputeDriverError::NotFound => Self::not_found("sandbox not found"),
135138
ComputeDriverError::InvalidArgument(m) => Self::invalid_argument(m),
136139
ComputeDriverError::Precondition(m) => Self::failed_precondition(m),
140+
ComputeDriverError::BackendUnavailable(m) => Self::unavailable(m),
137141
ComputeDriverError::Message(m) => Self::internal(m),
138142
}
139143
}

‎crates/openshell-core/src/extension_protocol.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ pub const COMPUTE_SUSPEND_RESUME: &str = "openshell.compute.suspend-resume";
4747
pub const COMPUTE_UPDATE_SANDBOX_CONFIGURATION: &str =
4848
"openshell.compute.update-sandbox-configuration";
4949

50+
/// Driver provisions the Workload API, exposes the launch ID, and durably orders starts and stops.
51+
pub const COMPUTE_PROVIDER_WORKLOAD_IDENTITY: &str =
52+
"openshell.compute.provider-workload-identity.v5";
53+
54+
/// Driver durably orders and replays starts for verified provider identity.
55+
pub const COMPUTE_ORDERED_PROVIDER_START: &str = "openshell.compute.ordered-provider-start";
56+
57+
/// Driver can return an ordered start receipt without admitting a start.
58+
pub const COMPUTE_ORDERED_PROVIDER_START_RECEIPT_LOOKUP: &str =
59+
"openshell.compute.ordered-provider-start.receipt-lookup";
60+
5061
/// Capability for supervisor middleware that implements HTTP hooks.
5162
///
5263
/// A service selects HTTP hooks for all of its HTTP bindings by

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,12 @@ mod session {
499499
pub gateway_expires_at: i64,
500500
pub sandbox_token: SecretJwt,
501501
pub sandbox_expires_at: i64,
502+
/// Exact provider identity selected by the gateway for this supervisor.
503+
#[serde(default, skip_serializing_if = "Option::is_none")]
504+
pub provider_spiffe_id: Option<String>,
505+
/// SPIFFE ID the issuer assigns to this supervisor launch.
506+
#[serde(default, skip_serializing_if = "Option::is_none")]
507+
pub provider_launch_spiffe_id: Option<String>,
502508
/// Never delivered to the workload. Missing only in older bundles.
503509
#[serde(default, skip_serializing_if = "Option::is_none")]
504510
pub ssh_host_private_key: Option<SecretSshHostKey>,
@@ -1787,6 +1793,8 @@ mod tests {
17871793
gateway_expires_at: pair.gateway.expires_at,
17881794
sandbox_token: pair.sandbox.token,
17891795
sandbox_expires_at: pair.sandbox.expires_at,
1796+
provider_spiffe_id: None,
1797+
provider_launch_spiffe_id: None,
17901798
ssh_host_private_key: None,
17911799
traces_otlp_headers: None,
17921800
ocsf_otlp_headers: None,
@@ -1935,6 +1943,8 @@ mod tests {
19351943
gateway_expires_at: pair.gateway.expires_at,
19361944
sandbox_token: pair.sandbox.token,
19371945
sandbox_expires_at: pair.sandbox.expires_at,
1946+
provider_spiffe_id: None,
1947+
provider_launch_spiffe_id: None,
19381948
ssh_host_private_key: None,
19391949
traces_otlp_headers: headers("traces"),
19401950
ocsf_otlp_headers: headers("ocsf"),
@@ -1974,6 +1984,8 @@ mod tests {
19741984
gateway_expires_at: pair.gateway.expires_at,
19751985
sandbox_token: pair.sandbox.token,
19761986
sandbox_expires_at: pair.sandbox.expires_at,
1987+
provider_spiffe_id: None,
1988+
provider_launch_spiffe_id: None,
19771989
ssh_host_private_key: Some(SecretSshHostKey::new(
19781990
"private-ssh-host-key".to_string(),
19791991
)),

‎crates/openshell-driver-docker/src/image_update/tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,7 @@ async fn startup_cleanup_and_stop_preserve_pending_update_without_a_workload() {
11051105
Request::new(StopSandboxRequest {
11061106
sandbox_id: String::new(),
11071107
name: fixture.p().name.clone(),
1108+
ordered_stop: None,
11081109
}),
11091110
)
11101111
.await

‎crates/openshell-driver-docker/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3629,7 +3629,7 @@ impl ComputeDriver for DockerComputeDriver {
36293629
.clear_stop(&request.sandbox_id, &request.name);
36303630
return Err(error);
36313631
}
3632-
span_status.finish(Ok(Response::new(StopSandboxResponse {})))
3632+
span_status.finish(Ok(Response::new(StopSandboxResponse::default())))
36333633
}
36343634

36353635
async fn start_sandbox(

‎crates/openshell-driver-docker/src/tests.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ pub fn test_launch_authentication() -> Vec<u8> {
6363
gateway_expires_at: i64::MAX,
6464
sandbox_token: SecretJwt::parse("sandbox.token.value").unwrap(),
6565
sandbox_expires_at: i64::MAX,
66+
provider_spiffe_id: None,
67+
provider_launch_spiffe_id: None,
6668
ssh_host_private_key: None,
6769
traces_otlp_headers: None,
6870
ocsf_otlp_headers: None,

‎crates/openshell-driver-kubernetes/src/grpc.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl ComputeDriver for ComputeDriverService {
192192
.map_err(|error| {
193193
Status::from(openshell_core::ComputeDriverError::from(error))
194194
})?;
195-
Ok(Response::new(StopSandboxResponse {}))
195+
Ok(Response::new(StopSandboxResponse::default()))
196196
})
197197
.await
198198
}
@@ -218,7 +218,10 @@ impl ComputeDriver for ComputeDriverService {
218218
.map_err(|error| {
219219
Status::from(openshell_core::ComputeDriverError::from(error))
220220
})?;
221-
Ok(Response::new(StartSandboxResponse { runtime_identity }))
221+
Ok(Response::new(StartSandboxResponse {
222+
runtime_identity,
223+
..Default::default()
224+
}))
222225
}),
223226
)
224227
.await

‎crates/openshell-driver-mxc/src/grpc.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl ComputeDriver for ComputeDriverService {
134134
return Err(Status::invalid_argument("name is required"));
135135
}
136136
self.backend.stop_sandbox(&req.name).await?;
137-
Ok(Response::new(StopSandboxResponse {}))
137+
Ok(Response::new(StopSandboxResponse::default()))
138138
}
139139

140140
async fn start_sandbox(

‎crates/openshell-driver-podman/src/driver.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,6 +2704,8 @@ mod tests {
27042704
gateway_expires_at: i64::MAX,
27052705
sandbox_token: SecretJwt::parse("sandbox.token.value").unwrap(),
27062706
sandbox_expires_at: i64::MAX,
2707+
provider_spiffe_id: None,
2708+
provider_launch_spiffe_id: None,
27072709
ssh_host_private_key: None,
27082710
traces_otlp_headers: None,
27092711
ocsf_otlp_headers: None,

0 commit comments

Comments
 (0)