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
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 68 additions & 8 deletions crates/openshell-cli/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ use openshell_core::proto::{
ListServicesRequest, PolicySource, PolicyStatus, RejectDraftChunkRequest, ResourceRequirements,
ResumeSandboxRequest, RevokeSshSessionRequest, Sandbox, SandboxCondition, SandboxPhase,
SandboxPolicy, SandboxResources, SandboxResponse, SandboxRestartPolicy, SandboxServiceExposure,
SandboxServiceLevel, SandboxSpec, SandboxStartup, SandboxTemplate, SandboxWorkloadConfig,
SandboxWorkloadTemplate, SandboxWorkloadTemplateSpec, ServiceAuthorizationMode,
ServiceBackendHttpVersion, ServiceEndpointResponse, SettingScope, StartSandboxRequest,
StopSandboxRequest, SuspendSandboxRequest, TcpForwardFrame, TcpForwardInit, TcpRelayTarget,
UpdateConfigRequest, WatchSandboxRequest, exec_sandbox_event, tcp_forward_init,
SandboxServiceLevel, SandboxSpec, SandboxStartup, SandboxTemplate,
SandboxTemplateWarmPoolStatus, SandboxWorkloadConfig, SandboxWorkloadTemplate,
SandboxWorkloadTemplateSpec, ServiceAuthorizationMode, ServiceBackendHttpVersion,
ServiceEndpointResponse, SettingScope, StartSandboxRequest, StopSandboxRequest,
SuspendSandboxRequest, TcpForwardFrame, TcpForwardInit, TcpRelayTarget, UpdateConfigRequest,
WatchSandboxRequest, exec_sandbox_event, tcp_forward_init,
};
use openshell_core::rpc_error::{MUTATION_LOCK_TIMEOUT, decode_details, is_mutation_lock_timeout};
use openshell_core::settings;
Expand Down Expand Up @@ -4051,20 +4052,59 @@ pub async fn sandbox_template_get(
)),
})
.await
.into_diagnostic()?;
.into_diagnostic()?
.into_inner();
let template = response
.into_inner()
.template
.ok_or_else(|| miette!("sandbox template missing from response"))?;
let warm_pool = response.warm_pool;

if crate::output::print_output_single(output, &template, sandbox_template_to_json)? {
if crate::output::print_output_single(output, &template, |template| {
let mut value = sandbox_template_to_json(template);
if let Some(pool) = &warm_pool {
value["warm_pool"] = warm_pool_status_to_json(pool);
}
value
})? {
return Ok(());
}

print_sandbox_template_detail(&template);
if let Some(pool) = &warm_pool {
print_warm_pool_status(pool);
}
Ok(())
}

fn warm_pool_status_to_json(pool: &SandboxTemplateWarmPoolStatus) -> serde_json::Value {
serde_json::json!({
"target": pool.target,
"ready": pool.ready,
"pending": pool.pending,
"retiring": pool.retiring,
"revision": pool.revision,
"suspended": pool.suspended,
})
}

fn print_warm_pool_status(pool: &SandboxTemplateWarmPoolStatus) {
println!();
println!(" {}", "Warm pool:".cyan().bold());
println!(" {} {}", "Target:".dimmed(), pool.target);
println!(" {} {}", "Ready:".dimmed(), pool.ready);
println!(" {} {}", "Pending:".dimmed(), pool.pending);
println!(" {} {}", "Retiring:".dimmed(), pool.retiring);
println!(" {} {}", "Revision:".dimmed(), pool.revision);
let suspended = if pool.suspended {
"yes (deploy a higher warm_pool.revision to resume refills)"
.yellow()
.to_string()
} else {
"no".to_string()
};
println!(" {} {suspended}", "Suspended:".dimmed());
}

#[allow(clippy::too_many_arguments)]
pub async fn sandbox_template_list(
server: &str,
Expand Down Expand Up @@ -9565,6 +9605,26 @@ mod tests {
assert_eq!(json["annotations"]["owner"], "platform");
}

#[test]
fn warm_pool_status_to_json_reports_counts_and_suspension() {
let json = super::warm_pool_status_to_json(&super::SandboxTemplateWarmPoolStatus {
target: 4,
ready: 2,
pending: 1,
retiring: 3,
revision: 7,
suspended: true,
});

assert_eq!(
json,
serde_json::json!({
"target": 4, "ready": 2, "pending": 1, "retiring": 3,
"revision": 7, "suspended": true,
})
);
}

#[test]
fn sandbox_template_to_json_formats_default_gpu_like_display_output() {
let template = SandboxWorkloadTemplate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ impl OpenShell for TestOpenShell {
.push(request);
Ok(Response::new(SandboxTemplateResponse {
template: Some(template),
warm_pool: None,
}))
}

Expand Down Expand Up @@ -403,6 +404,7 @@ impl OpenShell for TestOpenShell {
}),
spec: None,
}),
warm_pool: None,
}))
}

Expand Down
55 changes: 54 additions & 1 deletion crates/openshell-core/src/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ mod session {
runtime_generation: SandboxGenerationId,
auth_epoch: CredentialEpoch,
component: SessionComponent,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
resource_binding: BTreeMap<String, String>,
}

/// Claims of the supervisor token. It names no sandbox, generation, or
Expand Down Expand Up @@ -889,6 +891,26 @@ mod session {
gateway_token_id: Uuid,
sandbox_token_id: Uuid,
issued_at: i64,
) -> Result<MintedSessionTokenPair, SessionJwtError> {
self.mint_bound_pair_with_token_metadata(
identity,
gateway_token_id,
sandbox_token_id,
issued_at,
BTreeMap::new(),
)
}

/// Mint a reproducible pair, binding only the Sandbox Protocol token
/// to a prepared runtime's physical resources. Gateway authorization
/// continues to use the persisted logical identity and token lineage.
pub fn mint_bound_pair_with_token_metadata(
&self,
identity: &SandboxRuntimeIdentity,
gateway_token_id: Uuid,
sandbox_token_id: Uuid,
issued_at: i64,
resource_binding: BTreeMap<String, String>,
) -> Result<MintedSessionTokenPair, SessionJwtError> {
Ok(MintedSessionTokenPair {
gateway: self.mint(
Expand All @@ -897,11 +919,12 @@ mod session {
gateway_token_id,
issued_at,
)?,
sandbox: self.mint(
sandbox: self.mint_bound(
SessionTokenProfile::Sandbox,
identity,
sandbox_token_id,
issued_at,
resource_binding,
)?,
auth_epoch: identity.auth_epoch,
})
Expand Down Expand Up @@ -990,6 +1013,32 @@ mod session {
identity: &SandboxRuntimeIdentity,
token_id: Uuid,
issued_at: i64,
) -> Result<MintedSessionToken, SessionJwtError> {
self.mint_bound(profile, identity, token_id, issued_at, BTreeMap::new())
}

/// Authorize initial assignment to an exact driver-provisioned runtime.
pub fn mint_bound_sandbox_token(
&self,
identity: &SandboxRuntimeIdentity,
resource_binding: BTreeMap<String, String>,
) -> Result<MintedSessionToken, SessionJwtError> {
self.mint_bound(
SessionTokenProfile::Sandbox,
identity,
Uuid::new_v4(),
self.clock.now_unix_seconds(),
resource_binding,
)
}

fn mint_bound(
&self,
profile: SessionTokenProfile,
identity: &SandboxRuntimeIdentity,
token_id: Uuid,
issued_at: i64,
resource_binding: BTreeMap<String, String>,
) -> Result<MintedSessionToken, SessionJwtError> {
let expires_at = self.ttl.map_or(0, |ttl| {
issued_at.saturating_add(i64::try_from(ttl.as_secs()).unwrap_or(i64::MAX))
Expand All @@ -1005,6 +1054,7 @@ mod session {
runtime_generation: identity.runtime_generation.clone(),
auth_epoch: identity.auth_epoch,
component: SessionComponent::OpenShellSupervisor,
resource_binding,
};
let mut header = Header::new(Algorithm::EdDSA);
header.kid = Some(self.key_id.clone());
Expand Down Expand Up @@ -1048,6 +1098,8 @@ mod session {

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AuthenticatedSandboxSession {
/// Signed physical-runtime binding, required for initial late assignment.
pub resource_binding: BTreeMap<String, String>,
pub sandbox_id: SandboxId,
pub runtime_generation: SandboxGenerationId,
pub auth_epoch: CredentialEpoch,
Expand Down Expand Up @@ -1152,6 +1204,7 @@ mod session {
let token_id = Uuid::parse_str(&claims.jti).map_err(|_| SessionJwtError::InvalidJti)?;
validate_lifetime(claims.iat, claims.exp, self.clock.now_unix_seconds())?;
Ok(AuthenticatedSandboxSession {
resource_binding: claims.resource_binding,
sandbox_id: claims.sandbox_id,
runtime_generation: claims.runtime_generation,
auth_epoch: claims.auth_epoch,
Expand Down
2 changes: 2 additions & 0 deletions crates/openshell-driver-kubernetes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tonic = { workspace = true, features = ["transport"] }
prost = { workspace = true }
prost-types = { workspace = true }
futures = { workspace = true }
metrics = { workspace = true }
tokio-stream = { workspace = true }
kube = { workspace = true }
kube-runtime = { workspace = true }
Expand All @@ -54,6 +55,7 @@ rustls = { workspace = true }
tokio-rustls = { workspace = true }

[dev-dependencies]
metrics-util = { workspace = true, features = ["debugging"] }
openshell-otel-test-support = { path = "../openshell-otel-test-support" }
opentelemetry_sdk = { workspace = true, features = ["testing"] }
temp-env = "0.3"
Expand Down
Loading