@@ -8,6 +8,61 @@ use std::path::{Path, PathBuf};
88
99use crate :: proto:: compute:: v1:: DriverSandbox ;
1010
11+ /// Check that a launch bundle's provider identities belong to this sandbox launch.
12+ ///
13+ /// The stable ID must end in this gateway and sandbox. The launch ID must be
14+ /// that stable ID followed by `/launch/<generation>`, so it changes exactly
15+ /// when the gateway issues a new runtime generation. A driver calls this
16+ /// before it marks a supervisor with the launch ID.
17+ #[ cfg( feature = "jwt" ) ]
18+ #[ allow( clippy:: result_large_err) ]
19+ pub fn validate_provider_launch_identity (
20+ sandbox_id : & str ,
21+ generation_id : & str ,
22+ authentication : & crate :: jwt:: SandboxLaunchAuthentication ,
23+ ) -> Result < ( ) , tonic:: Status > {
24+ let invalid =
25+ || tonic:: Status :: invalid_argument ( "provider identity does not match the sandbox launch" ) ;
26+ let supervisor = & authentication. supervisor ;
27+ if supervisor. runtime_generation . as_str ( ) != generation_id {
28+ return Err ( invalid ( ) ) ;
29+ }
30+ let sandbox_path = format ! (
31+ "/gateway/{}/sandbox/{sandbox_id}" ,
32+ authentication. gateway_id
33+ ) ;
34+ let prefix = supervisor
35+ . provider_spiffe_id
36+ . as_deref ( )
37+ . and_then ( |id| id. strip_suffix ( & sandbox_path) )
38+ . ok_or_else ( invalid) ?;
39+ let prefix = openshell_spiffe:: SandboxSpiffeIdPrefix :: parse ( prefix) . map_err ( |_| invalid ( ) ) ?;
40+ let expected = prefix
41+ . supervisor_launch_spiffe_id ( & authentication. gateway_id , sandbox_id, generation_id)
42+ . map_err ( |_| invalid ( ) ) ?;
43+ if supervisor. provider_launch_spiffe_id . as_deref ( ) != Some ( expected. as_str ( ) ) {
44+ return Err ( invalid ( ) ) ;
45+ }
46+ Ok ( ( ) )
47+ }
48+
49+ /// Gateway alias available inside a sandbox VM.
50+ pub const VM_HOST_GATEWAY_ALIAS : & str = "host.openshell.internal" ;
51+
52+ /// Gateway hostnames that VM host supervisors rewrite to numeric loopback.
53+ pub const VM_HOST_LOOPBACK_ALIASES : & [ & str ] = & [
54+ VM_HOST_GATEWAY_ALIAS ,
55+ "host.containers.internal" ,
56+ "host.docker.internal" ,
57+ ] ;
58+
59+ /// Supervisor command that reports whether it can see a Workload API socket.
60+ pub const PROVIDER_WORKLOAD_IDENTITY_PROBE : & str = "provider-workload-identity-probe" ;
61+
62+ /// Exit statuses for the provider probe with a socket path argument.
63+ pub const PROVIDER_WORKLOAD_SOCKET_PROBE_PRESENT : i32 = 70 ;
64+ pub const PROVIDER_WORKLOAD_SOCKET_PROBE_UNAVAILABLE : i32 = 71 ;
65+
1166// ---------------------------------------------------------------------------
1267// Sandbox container/pod label keys (openshell.ai/ namespace)
1368// ---------------------------------------------------------------------------
@@ -34,6 +89,9 @@ pub const LABEL_SANDBOX_WORKSPACE: &str = "openshell.ai/sandbox-workspace";
3489/// Label carrying the gateway identity on managed namespaces.
3590pub const LABEL_GATEWAY_ID : & str = "openshell.ai/gateway-id" ;
3691
92+ /// SPIFFE ID registered for one driver-owned supervisor launch.
93+ pub const LABEL_PROVIDER_LAUNCH_SPIFFE_ID : & str = "openshell.ai/provider-launch-spiffe-id" ;
94+
3795/// Label selector that matches all OpenShell-managed resources which carry a
3896/// sandbox ID label. Used by list and watch operations to exclude foreign
3997/// resources from the same namespace.
@@ -743,6 +801,24 @@ pub fn validate_provider_spiffe_unix_socket(path: &Path) -> Result<(), String> {
743801 Ok ( ( ) )
744802}
745803
804+ /// Check that an absolute path resolves to a Unix socket in the current filesystem.
805+ pub fn provider_spiffe_socket_visible ( path : & Path ) -> bool {
806+ #[ cfg( unix) ]
807+ {
808+ use std:: os:: unix:: fs:: FileTypeExt as _;
809+
810+ path. is_absolute ( )
811+ && path
812+ . metadata ( )
813+ . is_ok_and ( |meta| meta. file_type ( ) . is_socket ( ) )
814+ }
815+ #[ cfg( not( unix) ) ]
816+ {
817+ let _ = path;
818+ false
819+ }
820+ }
821+
746822/// Return the guest/container path for a projected provider SPIFFE socket.
747823pub fn projected_provider_spiffe_socket_path ( path : & Path ) -> Result < String , String > {
748824 validate_provider_spiffe_unix_socket ( path) ?;
@@ -1550,4 +1626,93 @@ mod tests {
15501626 } )
15511627 . expect ( "an intercepting proxy's CA is meaningful without an https:// proxy URL" ) ;
15521628 }
1629+
1630+ #[ cfg( feature = "jwt" ) ]
1631+ #[ test]
1632+ fn provider_launch_identity_binds_generation_sandbox_and_launch ( ) {
1633+ use crate :: jwt:: {
1634+ CredentialEpoch , SandboxLaunchAuthentication , SecretJwt , SessionRotation ,
1635+ SupervisorAuthBundle ,
1636+ } ;
1637+ use crate :: sandbox_generation:: SandboxGenerationId ;
1638+ use crate :: sandbox_session:: SandboxSessionId ;
1639+
1640+ let sandbox = "spiffe://example.test/gateway/gateway/sandbox/sandbox-a" ;
1641+ let authentication = SandboxLaunchAuthentication {
1642+ supervisor : SupervisorAuthBundle {
1643+ session_id : SandboxSessionId :: new ( ) ,
1644+ runtime_generation : SandboxGenerationId :: parse ( "generation" ) . unwrap ( ) ,
1645+ session_rotation : SessionRotation :: new ( 1 ) . unwrap ( ) ,
1646+ auth_epoch : CredentialEpoch :: new ( 1 ) . unwrap ( ) ,
1647+ gateway_token : SecretJwt :: parse ( "gateway-token" ) . unwrap ( ) ,
1648+ gateway_expires_at : 0 ,
1649+ sandbox_token : SecretJwt :: parse ( "sandbox-token" ) . unwrap ( ) ,
1650+ sandbox_expires_at : 0 ,
1651+ provider_spiffe_id : Some ( sandbox. to_string ( ) ) ,
1652+ provider_launch_spiffe_id : Some ( format ! ( "{sandbox}/launch/generation" ) ) ,
1653+ ssh_host_private_key : None ,
1654+ traces_otlp_headers : None ,
1655+ ocsf_otlp_headers : None ,
1656+ agent_otlp_headers : None ,
1657+ otlp_tls : Vec :: new ( ) ,
1658+ traces_otlp_tls : None ,
1659+ ocsf_otlp_tls : None ,
1660+ agent_otlp_tls : None ,
1661+ } ,
1662+ gateway_id : "gateway" . to_string ( ) ,
1663+ verification_keys : Vec :: new ( ) ,
1664+ } ;
1665+ validate_provider_launch_identity ( "sandbox-a" , "generation" , & authentication) . unwrap ( ) ;
1666+
1667+ let with_launch = |launch : Option < String > | {
1668+ let mut changed = authentication. clone ( ) ;
1669+ changed. supervisor . provider_launch_spiffe_id = launch;
1670+ changed
1671+ } ;
1672+ for ( case, sandbox_id, generation_id, bundle) in [
1673+ (
1674+ "another generation" ,
1675+ "sandbox-a" ,
1676+ "other-generation" ,
1677+ authentication. clone ( ) ,
1678+ ) ,
1679+ (
1680+ "another sandbox" ,
1681+ "sandbox-b" ,
1682+ "generation" ,
1683+ authentication. clone ( ) ,
1684+ ) ,
1685+ (
1686+ "no launch identity" ,
1687+ "sandbox-a" ,
1688+ "generation" ,
1689+ with_launch ( None ) ,
1690+ ) ,
1691+ (
1692+ "a launch identity of another sandbox" ,
1693+ "sandbox-a" ,
1694+ "generation" ,
1695+ with_launch ( Some (
1696+ "spiffe://example.test/gateway/gateway/sandbox/sandbox-b/launch/generation"
1697+ . to_string ( ) ,
1698+ ) ) ,
1699+ ) ,
1700+ (
1701+ "a launch identity for another generation" ,
1702+ "sandbox-a" ,
1703+ "generation" ,
1704+ with_launch ( Some ( format ! ( "{sandbox}/launch/earlier-generation" ) ) ) ,
1705+ ) ,
1706+ (
1707+ "a launch identity with extra segments" ,
1708+ "sandbox-a" ,
1709+ "generation" ,
1710+ with_launch ( Some ( format ! ( "{sandbox}/launch/generation/extra" ) ) ) ,
1711+ ) ,
1712+ ] {
1713+ let error =
1714+ validate_provider_launch_identity ( sandbox_id, generation_id, & bundle) . unwrap_err ( ) ;
1715+ assert_eq ! ( error. code( ) , tonic:: Code :: InvalidArgument , "{case}" ) ;
1716+ }
1717+ }
15531718}
0 commit comments