Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions crates/openshell-core/src/driver_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,61 @@ use std::path::{Path, PathBuf};

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

/// Check that a launch bundle's provider identities belong to this sandbox launch.
///
/// The stable ID must end in this gateway and sandbox. The launch ID must be
/// that stable ID followed by `/launch/<generation>`, so it changes exactly
/// when the gateway issues a new runtime generation. A driver calls this
/// before it marks a supervisor with the launch ID.
#[cfg(feature = "jwt")]
#[allow(clippy::result_large_err)]
pub fn validate_provider_launch_identity(
sandbox_id: &str,
generation_id: &str,
authentication: &crate::jwt::SandboxLaunchAuthentication,
) -> Result<(), tonic::Status> {
let invalid =
|| tonic::Status::invalid_argument("provider identity does not match the sandbox launch");
let supervisor = &authentication.supervisor;
if supervisor.runtime_generation.as_str() != generation_id {
return Err(invalid());
}
let sandbox_path = format!(
"/gateway/{}/sandbox/{sandbox_id}",
authentication.gateway_id
);
let prefix = supervisor
.provider_spiffe_id
.as_deref()
.and_then(|id| id.strip_suffix(&sandbox_path))
.ok_or_else(invalid)?;
let prefix = openshell_spiffe::SandboxSpiffeIdPrefix::parse(prefix).map_err(|_| invalid())?;
let expected = prefix
.supervisor_launch_spiffe_id(&authentication.gateway_id, sandbox_id, generation_id)
.map_err(|_| invalid())?;
if supervisor.provider_launch_spiffe_id.as_deref() != Some(expected.as_str()) {
return Err(invalid());
}
Ok(())
}

/// Gateway alias available inside a sandbox VM.
pub const VM_HOST_GATEWAY_ALIAS: &str = "host.openshell.internal";

/// Gateway hostnames that VM host supervisors rewrite to numeric loopback.
pub const VM_HOST_LOOPBACK_ALIASES: &[&str] = &[
VM_HOST_GATEWAY_ALIAS,
"host.containers.internal",
"host.docker.internal",
];

/// Supervisor command that reports whether it can see a Workload API socket.
pub const PROVIDER_WORKLOAD_IDENTITY_PROBE: &str = "provider-workload-identity-probe";

/// Exit statuses for the provider probe with a socket path argument.
pub const PROVIDER_WORKLOAD_SOCKET_PROBE_PRESENT: i32 = 70;
pub const PROVIDER_WORKLOAD_SOCKET_PROBE_UNAVAILABLE: i32 = 71;

// ---------------------------------------------------------------------------
// Sandbox container/pod label keys (openshell.ai/ namespace)
// ---------------------------------------------------------------------------
Expand All @@ -34,6 +89,9 @@ pub const LABEL_SANDBOX_WORKSPACE: &str = "openshell.ai/sandbox-workspace";
/// Label carrying the gateway identity on managed namespaces.
pub const LABEL_GATEWAY_ID: &str = "openshell.ai/gateway-id";

/// SPIFFE ID registered for one driver-owned supervisor launch.
pub const LABEL_PROVIDER_LAUNCH_SPIFFE_ID: &str = "openshell.ai/provider-launch-spiffe-id";

/// Label selector that matches all OpenShell-managed resources which carry a
/// sandbox ID label. Used by list and watch operations to exclude foreign
/// resources from the same namespace.
Expand Down Expand Up @@ -743,6 +801,24 @@ pub fn validate_provider_spiffe_unix_socket(path: &Path) -> Result<(), String> {
Ok(())
}

/// Check that an absolute path resolves to a Unix socket in the current filesystem.
pub fn provider_spiffe_socket_visible(path: &Path) -> bool {
#[cfg(unix)]
{
use std::os::unix::fs::FileTypeExt as _;

path.is_absolute()
&& path
.metadata()
.is_ok_and(|meta| meta.file_type().is_socket())
}
#[cfg(not(unix))]
{
let _ = path;
false
}
}

/// Return the guest/container path for a projected provider SPIFFE socket.
pub fn projected_provider_spiffe_socket_path(path: &Path) -> Result<String, String> {
validate_provider_spiffe_unix_socket(path)?;
Expand Down Expand Up @@ -1550,4 +1626,93 @@ mod tests {
})
.expect("an intercepting proxy's CA is meaningful without an https:// proxy URL");
}

#[cfg(feature = "jwt")]
#[test]
fn provider_launch_identity_binds_generation_sandbox_and_launch() {
use crate::jwt::{
CredentialEpoch, SandboxLaunchAuthentication, SecretJwt, SessionRotation,
SupervisorAuthBundle,
};
use crate::sandbox_generation::SandboxGenerationId;
use crate::sandbox_session::SandboxSessionId;

let sandbox = "spiffe://example.test/gateway/gateway/sandbox/sandbox-a";
let authentication = SandboxLaunchAuthentication {
supervisor: SupervisorAuthBundle {
session_id: SandboxSessionId::new(),
runtime_generation: SandboxGenerationId::parse("generation").unwrap(),
session_rotation: SessionRotation::new(1).unwrap(),
auth_epoch: CredentialEpoch::new(1).unwrap(),
gateway_token: SecretJwt::parse("gateway-token").unwrap(),
gateway_expires_at: 0,
sandbox_token: SecretJwt::parse("sandbox-token").unwrap(),
sandbox_expires_at: 0,
provider_spiffe_id: Some(sandbox.to_string()),
provider_launch_spiffe_id: Some(format!("{sandbox}/launch/generation")),
ssh_host_private_key: None,
traces_otlp_headers: None,
ocsf_otlp_headers: None,
agent_otlp_headers: None,
otlp_tls: Vec::new(),
traces_otlp_tls: None,
ocsf_otlp_tls: None,
agent_otlp_tls: None,
},
gateway_id: "gateway".to_string(),
verification_keys: Vec::new(),
};
validate_provider_launch_identity("sandbox-a", "generation", &authentication).unwrap();

let with_launch = |launch: Option<String>| {
let mut changed = authentication.clone();
changed.supervisor.provider_launch_spiffe_id = launch;
changed
};
for (case, sandbox_id, generation_id, bundle) in [
(
"another generation",
"sandbox-a",
"other-generation",
authentication.clone(),
),
(
"another sandbox",
"sandbox-b",
"generation",
authentication.clone(),
),
(
"no launch identity",
"sandbox-a",
"generation",
with_launch(None),
),
(
"a launch identity of another sandbox",
"sandbox-a",
"generation",
with_launch(Some(
"spiffe://example.test/gateway/gateway/sandbox/sandbox-b/launch/generation"
.to_string(),
)),
),
(
"a launch identity for another generation",
"sandbox-a",
"generation",
with_launch(Some(format!("{sandbox}/launch/earlier-generation"))),
),
(
"a launch identity with extra segments",
"sandbox-a",
"generation",
with_launch(Some(format!("{sandbox}/launch/generation/extra"))),
),
] {
let error =
validate_provider_launch_identity(sandbox_id, generation_id, &bundle).unwrap_err();
assert_eq!(error.code(), tonic::Code::InvalidArgument, "{case}");
}
}
}
27 changes: 14 additions & 13 deletions crates/openshell-core/src/extension_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ pub const COMPUTE_SUSPEND_RESUME: &str = "openshell.compute.suspend-resume";
pub const COMPUTE_UPDATE_SANDBOX_CONFIGURATION: &str =
"openshell.compute.update-sandbox-configuration";

/// Driver gives each dedicated supervisor a Workload API socket and marks it
/// with the launch SPIFFE ID from its launch bundle. A start whose bundle
/// names a different launch replaces the supervisor.
pub const COMPUTE_PROVIDER_WORKLOAD_IDENTITY: &str = "openshell.compute.provider-workload-identity";

/// Capability for supervisor middleware that implements HTTP hooks.
///
/// A service selects HTTP hooks for all of its HTTP bindings by
Expand Down Expand Up @@ -160,6 +165,7 @@ pub fn gateway_metadata(family: ExtensionFamily) -> PeerMetadata {
supported_capabilities.push(COMPUTE_LAUNCH_AUTHENTICATION.to_string());
supported_capabilities.push(COMPUTE_ISOLATION_BACKEND.to_string());
supported_capabilities.push(COMPUTE_UPDATE_SANDBOX_CONFIGURATION.to_string());
supported_capabilities.push(COMPUTE_PROVIDER_WORKLOAD_IDENTITY.to_string());
}
ExtensionFamily::SupervisorMiddleware => {
supported_capabilities.push(SUPERVISOR_MIDDLEWARE_HTTP_HOOKS.to_string());
Expand Down Expand Up @@ -458,20 +464,15 @@ mod tests {
use super::*;

#[test]
fn compute_gateway_advertises_both_optional_extensions() {
fn compute_gateway_advertises_optional_extensions() {
let gateway = gateway_metadata(ExtensionFamily::Compute);
assert!(
gateway
.supported_capabilities
.iter()
.any(|c| c == COMPUTE_ISOLATION_BACKEND)
);
assert!(
gateway
.supported_capabilities
.iter()
.any(|c| c == COMPUTE_UPDATE_SANDBOX_CONFIGURATION)
);
for capability in [
COMPUTE_ISOLATION_BACKEND,
COMPUTE_UPDATE_SANDBOX_CONFIGURATION,
COMPUTE_PROVIDER_WORKLOAD_IDENTITY,
] {
assert!(has_capability(&gateway.supported_capabilities, capability));
}
}

fn compatible() -> (PeerMetadata, PeerMetadata) {
Expand Down
12 changes: 12 additions & 0 deletions crates/openshell-core/src/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,12 @@ mod session {
pub gateway_expires_at: i64,
pub sandbox_token: SecretJwt,
pub sandbox_expires_at: i64,
/// Exact provider identity selected by the gateway for this supervisor.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub provider_spiffe_id: Option<String>,
/// SPIFFE ID the issuer assigns to this supervisor launch.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub provider_launch_spiffe_id: Option<String>,
/// Never delivered to the workload. Missing only in older bundles.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ssh_host_private_key: Option<SecretSshHostKey>,
Expand Down Expand Up @@ -1787,6 +1793,8 @@ mod tests {
gateway_expires_at: pair.gateway.expires_at,
sandbox_token: pair.sandbox.token,
sandbox_expires_at: pair.sandbox.expires_at,
provider_spiffe_id: None,
provider_launch_spiffe_id: None,
ssh_host_private_key: None,
traces_otlp_headers: None,
ocsf_otlp_headers: None,
Expand Down Expand Up @@ -1935,6 +1943,8 @@ mod tests {
gateway_expires_at: pair.gateway.expires_at,
sandbox_token: pair.sandbox.token,
sandbox_expires_at: pair.sandbox.expires_at,
provider_spiffe_id: None,
provider_launch_spiffe_id: None,
ssh_host_private_key: None,
traces_otlp_headers: headers("traces"),
ocsf_otlp_headers: headers("ocsf"),
Expand Down Expand Up @@ -1974,6 +1984,8 @@ mod tests {
gateway_expires_at: pair.gateway.expires_at,
sandbox_token: pair.sandbox.token,
sandbox_expires_at: pair.sandbox.expires_at,
provider_spiffe_id: None,
provider_launch_spiffe_id: None,
ssh_host_private_key: Some(SecretSshHostKey::new(
"private-ssh-host-key".to_string(),
)),
Expand Down
2 changes: 2 additions & 0 deletions crates/openshell-driver-docker/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub fn test_launch_authentication() -> Vec<u8> {
gateway_expires_at: i64::MAX,
sandbox_token: SecretJwt::parse("sandbox.token.value").unwrap(),
sandbox_expires_at: i64::MAX,
provider_spiffe_id: None,
provider_launch_spiffe_id: None,
ssh_host_private_key: None,
traces_otlp_headers: None,
ocsf_otlp_headers: None,
Expand Down
2 changes: 2 additions & 0 deletions crates/openshell-driver-podman/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2704,6 +2704,8 @@ mod tests {
gateway_expires_at: i64::MAX,
sandbox_token: SecretJwt::parse("sandbox.token.value").unwrap(),
sandbox_expires_at: i64::MAX,
provider_spiffe_id: None,
provider_launch_spiffe_id: None,
ssh_host_private_key: None,
traces_otlp_headers: None,
ocsf_otlp_headers: None,
Expand Down
2 changes: 2 additions & 0 deletions crates/openshell-driver-podman/src/isolation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ mod tests {
gateway_expires_at: i64::MAX,
sandbox_token: SecretJwt::parse("sandbox.token.value").unwrap(),
sandbox_expires_at: i64::MAX,
provider_spiffe_id: None,
provider_launch_spiffe_id: None,
ssh_host_private_key: None,
traces_otlp_headers: None,
ocsf_otlp_headers: None,
Expand Down
2 changes: 2 additions & 0 deletions crates/openshell-driver-vm/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11265,6 +11265,8 @@ mod tests {
gateway_expires_at: 1,
sandbox_token: SecretJwt::parse(format!("sandbox-{label}")).expect("sandbox token"),
sandbox_expires_at: 1,
provider_spiffe_id: None,
provider_launch_spiffe_id: None,
ssh_host_private_key: None,
traces_otlp_headers: None,
ocsf_otlp_headers: None,
Expand Down
1 change: 1 addition & 0 deletions crates/openshell-gateway/src/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ impl<'a> ConfigureInputs<'a> {
.admission
.and_then(|policy| serde_json::to_string(policy).ok())
.unwrap_or_default(),
verified_provider_workload_identity: None,
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/openshell-server/src/auth/sandbox_jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ impl SandboxSessionJwtAuthority {
gateway_expires_at: pair.gateway.expires_at,
sandbox_token: pair.sandbox.token,
sandbox_expires_at: pair.sandbox.expires_at,
provider_spiffe_id: None,
provider_launch_spiffe_id: None,
ssh_host_private_key: None,
traces_otlp_headers: None,
ocsf_otlp_headers: None,
Expand Down Expand Up @@ -616,6 +618,9 @@ mod tests {
let first = authority
.mint_persisted_launch("sandbox-a", &identity)
.unwrap();
// The session authority leaves the provider identities unset.
assert!(first.supervisor.provider_spiffe_id.is_none());
assert!(first.supervisor.provider_launch_spiffe_id.is_none());
let mut annotations = std::collections::HashMap::new();
identity.write(&mut annotations);
let mut reloaded =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ async fn postgres_concurrent_sandbox_creates_with_ssh_identity_do_not_exhaust_lo
gateway_expires_at: 0,
sandbox_token: SecretJwt::parse("sandbox-token").unwrap(),
sandbox_expires_at: 0,
provider_spiffe_id: None,
provider_launch_spiffe_id: None,
ssh_host_private_key: None,
traces_otlp_headers: None,
ocsf_otlp_headers: None,
Expand Down Expand Up @@ -211,6 +213,8 @@ fn ssh_launch_authentication() -> openshell_core::jwt::SandboxLaunchAuthenticati
gateway_expires_at: 0,
sandbox_token: SecretJwt::parse("sandbox-token").unwrap(),
sandbox_expires_at: 0,
provider_spiffe_id: None,
provider_launch_spiffe_id: None,
ssh_host_private_key: None,
traces_otlp_headers: None,
ocsf_otlp_headers: None,
Expand Down
2 changes: 2 additions & 0 deletions crates/openshell-server/src/ssh_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ mod tests {
gateway_expires_at: 0,
sandbox_token: SecretJwt::parse("sandbox-token").unwrap(),
sandbox_expires_at: 0,
provider_spiffe_id: None,
provider_launch_spiffe_id: None,
ssh_host_private_key: None,
traces_otlp_headers: None,
ocsf_otlp_headers: None,
Expand Down
Loading
Loading