Skip to content

Commit 594bd13

Browse files
committed
feat(gateway): name each supervisor launch in its launch bundle
With provider workload identity configured, tell each dedicated supervisor which SPIFFE identities to request. - The launch bundle carries the stable sandbox SPIFFE ID and the launch SPIFFE ID. The gateway derives both from the configured prefix, its gateway ID, the sandbox ID and the sandbox's runtime generation. No new state is stored. - At startup the gateway requires the selected driver to advertise provider workload identity and names the capability when it is missing. - SpiffeWorkloadIdentityConfig holds the prefix and the gateway's Workload API socket path. Start, stop, restart and recovery are unchanged. No gateway setting enables the mode yet. Signed-off-by: Shiju <shiju@nvidia.com>
1 parent a48bc30 commit 594bd13

5 files changed

Lines changed: 272 additions & 7 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@ impl SpiffeBrokerConfig {
335335
}
336336
}
337337

338+
/// Validate provider assertions from dedicated, platform-attested supervisors.
339+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
340+
#[serde(deny_unknown_fields)]
341+
pub struct SpiffeWorkloadIdentityConfig {
342+
pub spiffe_id_prefix: String,
343+
pub workload_api_socket_path: PathBuf,
344+
}
345+
338346
/// Server configuration.
339347
///
340348
/// Built programmatically in [`crate::Config::new`] and the gateway CLI from
@@ -419,6 +427,7 @@ pub struct Config {
419427
pub gateway_jwt: Option<GatewayJwtConfig>,
420428
/// Gateway-only access to SPIRE; shared supervisors receive access tokens.
421429
pub spiffe_broker: Option<SpiffeBrokerConfig>,
430+
pub spiffe_workload_identity: Option<SpiffeWorkloadIdentityConfig>,
422431

423432
/// Database URL for persistence.
424433
pub database_url: String,
@@ -1340,6 +1349,7 @@ impl Config {
13401349
audit: GatewayAuditConfig::default(),
13411350
gateway_jwt: None,
13421351
spiffe_broker: None,
1352+
spiffe_workload_identity: None,
13431353
database_url: String::new(),
13441354
database_auth: DatabaseAuthMode::default(),
13451355
database_max_connections: None,

‎crates/openshell-server/src/compute/mod.rs‎

Lines changed: 183 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ use futures::{Stream, StreamExt};
2929
#[cfg(unix)]
3030
use hyper_util::rt::TokioIo;
3131
use openshell_core::extension_protocol::{
32-
ExtensionFamily, NegotiatedExtension, gateway_metadata, negotiate,
32+
COMPUTE_PROVIDER_WORKLOAD_IDENTITY, ExtensionFamily, NegotiatedExtension, gateway_metadata,
33+
has_capability, negotiate,
3334
};
3435
#[cfg(test)]
3536
use openshell_core::proto::ServiceAuthorizationMode;
@@ -1325,6 +1326,7 @@ pub struct ComputeRuntime {
13251326
image_preparation_timeout_seconds: u32,
13261327
provisioning_repair_window_ms: i64,
13271328
sandbox_updates_enabled: bool,
1329+
provider_workload_identity: Option<crate::spiffe_broker::SupervisorWorkloadIdentity>,
13281330
update_recovery_sweep: Arc<tokio::sync::Semaphore>,
13291331
update_recovery_backoff: Arc<Mutex<HashMap<String, image_update::UpdateRecoveryBackoff>>>,
13301332
store: Arc<Store>,
@@ -1448,6 +1450,7 @@ impl ComputeRuntime {
14481450
image_preparation_timeout_seconds: 1800,
14491451
provisioning_repair_window_ms: 300_000,
14501452
sandbox_updates_enabled: false,
1453+
provider_workload_identity: None,
14511454
update_recovery_sweep: Arc::new(tokio::sync::Semaphore::new(1)),
14521455
update_recovery_backoff: Arc::default(),
14531456
store,
@@ -1487,6 +1490,40 @@ impl ComputeRuntime {
14871490
self.supervisor_host.as_ref()
14881491
}
14891492

1493+
/// Give every launch bundle the sandbox's stable and launch SPIFFE IDs.
1494+
///
1495+
/// # Errors
1496+
///
1497+
/// The mode needs dedicated supervisors and a driver that advertises
1498+
/// [`COMPUTE_PROVIDER_WORKLOAD_IDENTITY`].
1499+
pub(crate) fn with_provider_workload_identity(
1500+
mut self,
1501+
identity: Option<crate::spiffe_broker::SupervisorWorkloadIdentity>,
1502+
) -> Result<Self, String> {
1503+
if identity.is_some() {
1504+
if self.supervisor_host.is_some() {
1505+
return Err("spiffe_workload_identity requires dedicated supervisors".into());
1506+
}
1507+
if !has_capability(
1508+
&self.driver_info.negotiated_extension.supported_capabilities,
1509+
COMPUTE_PROVIDER_WORKLOAD_IDENTITY,
1510+
) {
1511+
return Err(format!(
1512+
"compute driver '{}' did not advertise {COMPUTE_PROVIDER_WORKLOAD_IDENTITY}, required by spiffe_workload_identity; check the running driver's version and Workload API configuration",
1513+
self.driver_info.name,
1514+
));
1515+
}
1516+
}
1517+
self.provider_workload_identity = identity;
1518+
Ok(self)
1519+
}
1520+
1521+
pub(crate) fn provider_workload_identity(
1522+
&self,
1523+
) -> Option<&crate::spiffe_broker::SupervisorWorkloadIdentity> {
1524+
self.provider_workload_identity.as_ref()
1525+
}
1526+
14901527
pub(crate) fn configure_ssh_identities(
14911528
&self,
14921529
credentials: crate::credentials::CredentialRuntime,
@@ -1522,11 +1559,32 @@ impl ComputeRuntime {
15221559
let _ = self.supervisor_otlp_tls.set(tls);
15231560
}
15241561

1525-
pub(crate) async fn prepare_ssh_identity(
1562+
/// Add the identities a launch bundle carries besides its session tokens.
1563+
pub(crate) async fn prepare_launch_identity(
15261564
&self,
15271565
sandbox: &mut Sandbox,
15281566
authentication: &mut openshell_core::jwt::SandboxLaunchAuthentication,
15291567
) -> Result<(), Status> {
1568+
if let Some(identity) = self.provider_workload_identity() {
1569+
// A launch is the runtime generation the bundle was minted for.
1570+
let spiffe_id = identity
1571+
.sandbox_spiffe_id(sandbox.object_id())
1572+
.map_err(Status::failed_precondition)?;
1573+
let launch_spiffe_id = identity
1574+
.launch_spiffe_id(
1575+
sandbox.object_id(),
1576+
&authentication.supervisor.runtime_generation,
1577+
)
1578+
.map_err(Status::failed_precondition)?;
1579+
info!(
1580+
sandbox_id = sandbox.object_id(),
1581+
%spiffe_id,
1582+
%launch_spiffe_id,
1583+
"Configured sandbox SPIFFE identity for provider grants"
1584+
);
1585+
authentication.supervisor.provider_spiffe_id = Some(spiffe_id);
1586+
authentication.supervisor.provider_launch_spiffe_id = Some(launch_spiffe_id);
1587+
}
15301588
if let Some(identities) = self.ssh_identities.get() {
15311589
identities.prepare(sandbox, authentication).await?;
15321590
}
@@ -1546,7 +1604,7 @@ impl ComputeRuntime {
15461604
mut authentication: openshell_core::jwt::SandboxLaunchAuthentication,
15471605
) -> Result<Vec<u8>, Status> {
15481606
let mut with_identity = sandbox.clone();
1549-
self.prepare_ssh_identity(&mut with_identity, &mut authentication)
1607+
self.prepare_launch_identity(&mut with_identity, &mut authentication)
15501608
.await?;
15511609
if let Some(headers) = self.supervisor_otlp_headers.get() {
15521610
headers.apply(&mut authentication.supervisor);
@@ -9238,6 +9296,7 @@ pub fn new_test_runtime_with_driver(
92389296
image_preparation_timeout_seconds: 1800,
92399297
provisioning_repair_window_ms: 300_000,
92409298
sandbox_updates_enabled: false,
9299+
provider_workload_identity: None,
92419300
update_recovery_sweep: Arc::new(tokio::sync::Semaphore::new(1)),
92429301
update_recovery_backoff: Arc::default(),
92439302
store,
@@ -10582,6 +10641,7 @@ mod tests {
1058210641
image_preparation_timeout_seconds: 1800,
1058310642
provisioning_repair_window_ms: 300_000,
1058410643
sandbox_updates_enabled: false,
10644+
provider_workload_identity: None,
1058510645
update_recovery_sweep: Arc::new(Semaphore::new(1)),
1058610646
update_recovery_backoff: Arc::default(),
1058710647
store,
@@ -14384,6 +14444,126 @@ mod tests {
1438414444
}
1438514445
}
1438614446

14447+
fn test_provider_workload_identity() -> crate::spiffe_broker::SupervisorWorkloadIdentity {
14448+
crate::spiffe_broker::SupervisorWorkloadIdentity::from_settings(
14449+
Some(&openshell_core::config::SpiffeWorkloadIdentityConfig {
14450+
spiffe_id_prefix: "spiffe://test.example/openshell".into(),
14451+
workload_api_socket_path: "/run/spire/agent.sock".into(),
14452+
}),
14453+
Some(&openshell_core::config::GatewayJwtConfig {
14454+
// The gateway ID of `test_session_authority`.
14455+
gateway_id: "test-gateway".into(),
14456+
ttl_secs: std::num::NonZeroU64::new(300),
14457+
signing_key_path: PathBuf::default(),
14458+
public_key_path: PathBuf::default(),
14459+
kid_path: PathBuf::default(),
14460+
}),
14461+
false,
14462+
)
14463+
.unwrap()
14464+
.unwrap()
14465+
}
14466+
14467+
#[tokio::test]
14468+
async fn provider_workload_identity_requires_the_driver_capability() {
14469+
let identity = test_provider_workload_identity();
14470+
let runtime = test_runtime(ControlledDriver::new()).await;
14471+
14472+
let error = runtime
14473+
.clone()
14474+
.with_provider_workload_identity(Some(identity.clone()))
14475+
.unwrap_err();
14476+
assert!(
14477+
error.contains(&format!(
14478+
"did not advertise {COMPUTE_PROVIDER_WORKLOAD_IDENTITY}"
14479+
)),
14480+
"{error}"
14481+
);
14482+
// The capability is required only when the mode is configured.
14483+
let unselected = runtime
14484+
.clone()
14485+
.with_provider_workload_identity(None)
14486+
.unwrap();
14487+
assert!(unselected.provider_workload_identity().is_none());
14488+
14489+
let mut capable = runtime;
14490+
capable
14491+
.driver_info
14492+
.negotiated_extension
14493+
.supported_capabilities
14494+
.push(COMPUTE_PROVIDER_WORKLOAD_IDENTITY.to_string());
14495+
let selected = capable
14496+
.with_provider_workload_identity(Some(identity))
14497+
.unwrap();
14498+
assert!(selected.provider_workload_identity().is_some());
14499+
}
14500+
14501+
/// A bundle's launch ID is the stable ID plus the persisted generation, so
14502+
/// a retried start names the same launch and a new start a new one.
14503+
#[tokio::test]
14504+
async fn start_bundles_name_the_launch_of_the_persisted_generation() {
14505+
let driver = ControlledDriver::new();
14506+
let mut runtime = test_runtime(driver.clone()).await;
14507+
let identity = test_provider_workload_identity();
14508+
runtime.provider_workload_identity = Some(identity.clone());
14509+
let sandbox = sandbox_record("sb-launch", "sandbox-launch", SandboxPhase::Stopped);
14510+
runtime.store.put_message(&sandbox).await.unwrap();
14511+
let authority = test_session_authority();
14512+
let stable = identity.sandbox_spiffe_id(sandbox.object_id()).unwrap();
14513+
14514+
let mut launches = Vec::new();
14515+
// A start, its retry while `Starting`, and a start after a stop.
14516+
for stop_first in [false, false, true] {
14517+
if stop_first {
14518+
runtime
14519+
.store
14520+
.update_message_cas::<Sandbox, _>(sandbox.object_id(), 0, |sandbox| {
14521+
sandbox.set_phase(SandboxPhase::Stopped as i32);
14522+
})
14523+
.await
14524+
.unwrap();
14525+
}
14526+
runtime
14527+
.start_sandbox_authenticated(
14528+
sandbox.object_workspace(),
14529+
sandbox.object_name(),
14530+
Some(&authority),
14531+
)
14532+
.await
14533+
.unwrap();
14534+
let stored = runtime
14535+
.store
14536+
.get_message::<Sandbox>(sandbox.object_id())
14537+
.await
14538+
.unwrap()
14539+
.unwrap();
14540+
let generation = sandbox_runtime_generation(&stored).unwrap();
14541+
let encoded = driver.start_authentications().pop().unwrap();
14542+
let authentication: openshell_core::jwt::SandboxLaunchAuthentication =
14543+
serde_json::from_slice(&encoded).unwrap();
14544+
let supervisor = &authentication.supervisor;
14545+
assert_eq!(supervisor.provider_spiffe_id.as_ref(), Some(&stable));
14546+
assert_eq!(
14547+
supervisor.provider_launch_spiffe_id,
14548+
Some(format!("{stable}/launch/{}", generation.as_str()))
14549+
);
14550+
// A driver accepts the bundle for this sandbox and generation only.
14551+
let validate = |sandbox_id, generation_id| {
14552+
openshell_core::driver_utils::validate_provider_launch_identity(
14553+
sandbox_id,
14554+
generation_id,
14555+
&authentication,
14556+
)
14557+
};
14558+
validate(sandbox.object_id(), generation.as_str()).unwrap();
14559+
validate("another-sandbox", generation.as_str()).unwrap_err();
14560+
validate(sandbox.object_id(), "another-generation").unwrap_err();
14561+
launches.push(supervisor.provider_launch_spiffe_id.clone());
14562+
}
14563+
assert_eq!(launches[0], launches[1]);
14564+
assert_ne!(launches[1], launches[2]);
14565+
}
14566+
1438714567
#[tokio::test]
1438814568
async fn concurrent_authenticated_start_keeps_the_claimed_launch_identity() {
1438914569
let driver = ControlledDriver::new();

‎crates/openshell-server/src/grpc/sandbox.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2123,7 +2123,7 @@ pub async fn mint_persisted_authentication(
21232123
let mut with_identity = sandbox.clone();
21242124
state
21252125
.compute
2126-
.prepare_ssh_identity(&mut with_identity, &mut authentication)
2126+
.prepare_launch_identity(&mut with_identity, &mut authentication)
21272127
.await?;
21282128
Ok(authentication)
21292129
}

‎crates/openshell-server/src/lib.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,12 @@ impl ServerState {
663663
let sandbox_identity =
664664
spiffe_broker::sandbox_identity_provider(&config).map_err(Error::config)?;
665665
let replica_id = compute::lease::replica_id();
666-
let compute = compute.with_sandbox_updates(config.enable_sandbox_updates);
666+
let workload_identity = spiffe_broker::SupervisorWorkloadIdentity::from_config(&config)
667+
.map_err(Error::config)?;
668+
let compute = compute
669+
.with_sandbox_updates(config.enable_sandbox_updates)
670+
.with_provider_workload_identity(workload_identity)
671+
.map_err(Error::config)?;
667672
compute.configure_ssh_identities(credentials.clone());
668673
let service_route_index = Arc::new(service_routing::ServiceRouteIndex::new(
669674
config.service_routing_discriminator().map(str::to_string),

‎crates/openshell-server/src/spiffe_broker.rs‎

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
//! `[openshell.gateway.spiffe_broker]` is configured.
99
use std::sync::Arc;
1010

11-
use openshell_core::config::{Config, SpiffeBrokerConfig};
11+
use openshell_core::config::{
12+
Config, GatewayJwtConfig, SpiffeBrokerConfig, SpiffeWorkloadIdentityConfig,
13+
};
14+
use openshell_core::sandbox_generation::SandboxGenerationId;
1215
use openshell_core::token_grant_failure::TokenGrantFailure;
13-
use openshell_spiffe::SandboxIdentityProvider;
16+
use openshell_spiffe::{SandboxIdentityProvider, SandboxSpiffeIdPrefix};
1417

1518
/// Check the broker settings `config` enables without building them.
1619
///
@@ -19,9 +22,76 @@ use openshell_spiffe::SandboxIdentityProvider;
1922
/// Rejects broker without gateway session JWTs and a positive session
2023
/// TTL, and settings [`SpiffeBrokerConfig::validate`] rejects.
2124
pub fn validate_config(config: &Config) -> Result<(), String> {
25+
SupervisorWorkloadIdentity::from_config(config)?;
2226
enabled(config).map(drop)
2327
}
2428

29+
/// Trusted identity settings shared by launch provisioning and the provider broker.
30+
#[derive(Clone)]
31+
pub struct SupervisorWorkloadIdentity {
32+
prefix: SandboxSpiffeIdPrefix,
33+
gateway_id: String,
34+
}
35+
36+
impl SupervisorWorkloadIdentity {
37+
pub(crate) fn from_config(config: &Config) -> Result<Option<Self>, String> {
38+
Self::from_settings(
39+
config.spiffe_workload_identity.as_ref(),
40+
config.gateway_jwt.as_ref(),
41+
config.spiffe_broker.is_some(),
42+
)
43+
}
44+
45+
pub(crate) fn from_settings(
46+
workload: Option<&SpiffeWorkloadIdentityConfig>,
47+
jwt: Option<&GatewayJwtConfig>,
48+
broker_enabled: bool,
49+
) -> Result<Option<Self>, String> {
50+
let Some(workload) = workload else {
51+
return Ok(None);
52+
};
53+
if broker_enabled {
54+
return Err("configure either spiffe_workload_identity or spiffe_broker".into());
55+
}
56+
let jwt = jwt
57+
.filter(|jwt| jwt.ttl_secs.is_some())
58+
.ok_or("spiffe_workload_identity requires a positive gateway_jwt.ttl_secs")?;
59+
let prefix = SandboxSpiffeIdPrefix::parse(&workload.spiffe_id_prefix).map_err(|_| {
60+
"spiffe_workload_identity.spiffe_id_prefix must be a SPIFFE ID prefix with a lowercase trust domain and optional path segments"
61+
})?;
62+
// The longest launch SPIFFE ID must fit: a UUID sandbox ID and a
63+
// runtime generation of the maximum length.
64+
let sandbox_id = uuid::Uuid::nil().to_string();
65+
let generation = "0".repeat(SandboxGenerationId::MAX_LEN);
66+
prefix
67+
.supervisor_launch_spiffe_id(&jwt.gateway_id, &sandbox_id, &generation)
68+
.map_err(|reason| format!("spiffe_workload_identity launch SPIFFE ID: {reason}"))?;
69+
openshell_core::driver_utils::validate_provider_spiffe_unix_socket(
70+
&workload.workload_api_socket_path,
71+
)
72+
.map_err(|reason| format!("spiffe_workload_identity.workload_api_socket_path: {reason}"))?;
73+
Ok(Some(Self {
74+
prefix,
75+
gateway_id: jwt.gateway_id.clone(),
76+
}))
77+
}
78+
79+
pub(crate) fn sandbox_spiffe_id(&self, sandbox_id: &str) -> Result<String, String> {
80+
self.prefix.sandbox_spiffe_id(&self.gateway_id, sandbox_id)
81+
}
82+
83+
/// The SPIFFE ID of the supervisor launched for `generation`: the stable
84+
/// ID followed by `/launch/<generation>`.
85+
pub(crate) fn launch_spiffe_id(
86+
&self,
87+
sandbox_id: &str,
88+
generation: &SandboxGenerationId,
89+
) -> Result<String, String> {
90+
self.prefix
91+
.supervisor_launch_spiffe_id(&self.gateway_id, sandbox_id, generation.as_str())
92+
}
93+
}
94+
2595
/// The sandbox identity provider `config` enables, if any.
2696
///
2797
/// # Errors

0 commit comments

Comments
 (0)