55
66use rustls:: pki_types:: { CertificateDer , pem:: PemObject } ;
77use std:: path:: { Path , PathBuf } ;
8+ use std:: time:: Duration ;
89
910use 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.
3597pub 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.
747830pub 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}
0 commit comments