Skip to content

Commit a8d2d7f

Browse files
committed
feat(compute)!: admit service ports before provisioning
A requested service port should be rejected before provisioning a sandbox or opening a local forwarding listener when the driver cannot admit it. This checks create, exposure and forwarding requests against a retained driver admission record, and shows whether the target uses isolated or operator-approved shared-host loopback. Signed-off-by: Shiju <shiju@nvidia.com>
1 parent 760396e commit a8d2d7f

59 files changed

Lines changed: 2352 additions & 914 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎crates/openshell-cli/src/run.rs‎

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,11 @@ pub async fn sandbox_create(
702702
)),
703703
await_main_process_attachment,
704704
workload_template: template.unwrap_or_default().to_string(),
705+
forward_ports: forward
706+
.as_ref()
707+
.map(|spec| u32::from(spec.port))
708+
.into_iter()
709+
.collect(),
705710
service_exposures: expose
706711
.map(|target_port| SandboxServiceExposure {
707712
service: String::new(),
@@ -1688,6 +1693,33 @@ where
16881693
println!(" {} {}", "Id:".dimmed(), id);
16891694
println!(" {} {}", "Name:".dimmed(), name);
16901695
println!(" {} {}", "Phase:".dimmed(), phase_name(sandbox.phase()));
1696+
if let Some(ingress) = sandbox
1697+
.status
1698+
.as_ref()
1699+
.and_then(|status| status.ingress_admission.as_ref())
1700+
{
1701+
let ports = if ingress.all_ports {
1702+
"all".to_string()
1703+
} else {
1704+
ingress
1705+
.ports
1706+
.iter()
1707+
.map(u32::to_string)
1708+
.collect::<Vec<_>>()
1709+
.join(", ")
1710+
};
1711+
println!(
1712+
" {} {}; ports: {ports}",
1713+
"Ingress:".dimmed(),
1714+
ingress_scope_name(ingress.scope)
1715+
);
1716+
} else {
1717+
println!(
1718+
" {} legacy driver (admission unavailable)",
1719+
"Ingress:".dimmed()
1720+
);
1721+
}
1722+
16911723
if let Some(status) = sandbox.status.as_ref() {
16921724
for condition in &status.conditions {
16931725
if matches!(
@@ -2169,8 +2201,12 @@ pub async fn service_forward_tcp(
21692201
let (bind_addr, bind_port) = parse_tcp_forward_spec(local, target_port)?;
21702202
let mut client = grpc_client(server, tls).await?;
21712203

2172-
let mut replica = fetch_ready_sandbox_for_forward(&mut client, name, workspace).await?;
2204+
let (mut replica, scope) =
2205+
fetch_ready_sandbox_for_forward(&mut client, name, workspace, target_port).await?;
21732206

2207+
if scope == openshell_core::proto::IngressScope::SharedHostLoopback {
2208+
eprintln!("Target is an admitted shared-host loopback service.");
2209+
}
21742210
let listener = tokio::net::TcpListener::bind((bind_addr.as_str(), bind_port))
21752211
.await
21762212
.into_diagnostic()
@@ -2203,7 +2239,7 @@ pub async fn service_forward_tcp(
22032239
}
22042240

22052241
_ = health_check.tick() => {
2206-
replica = fetch_ready_sandbox_for_forward(&mut client, name, workspace).await?;
2242+
replica = fetch_ready_sandbox_for_forward(&mut client, name, workspace, target_port).await?.0;
22072243
}
22082244

22092245
accepted = listener.accept() => {
@@ -2274,7 +2310,11 @@ async fn fetch_ready_sandbox_for_forward(
22742310
client: &mut crate::tls::GrpcClient,
22752311
name: &str,
22762312
workspace: &str,
2277-
) -> Result<Option<AsciiMetadataValue>> {
2313+
target_port: u16,
2314+
) -> Result<(
2315+
Option<AsciiMetadataValue>,
2316+
openshell_core::proto::IngressScope,
2317+
)> {
22782318
let response = match client
22792319
.get_sandbox(with_owner_hint(GetSandboxRequest {
22802320
name: name.to_string(),
@@ -2307,7 +2347,17 @@ async fn fetch_ready_sandbox_for_forward(
23072347
));
23082348
}
23092349

2310-
Ok(replica)
2350+
let scope = openshell_core::ingress::admit_port(
2351+
sandbox
2352+
.status
2353+
.as_ref()
2354+
.and_then(|status| status.ingress_admission.as_ref()),
2355+
u32::from(target_port),
2356+
false,
2357+
)
2358+
.map_err(|error| miette::miette!("{error}"))?;
2359+
2360+
Ok((replica, scope))
23112361
}
23122362

23132363
#[derive(Debug)]
@@ -3012,6 +3062,16 @@ fn configuration_failure_message(sandbox: &Sandbox) -> Option<&str> {
30123062
.filter(|message| !message.is_empty())
30133063
}
30143064

3065+
fn ingress_scope_name(scope: i32) -> &'static str {
3066+
use openshell_core::proto::IngressScope;
3067+
match IngressScope::try_from(scope) {
3068+
Ok(IngressScope::IsolatedLoopback) => "isolated_loopback",
3069+
Ok(IngressScope::SharedHostLoopback) => "shared_host_loopback",
3070+
Ok(IngressScope::Unsupported) => "unsupported",
3071+
Ok(IngressScope::Unspecified) | Err(_) => "unknown",
3072+
}
3073+
}
3074+
30153075
fn sandbox_to_json(sandbox: &Sandbox) -> serde_json::Value {
30163076
use openshell_core::proto::ConfigurationAdmissionState;
30173077

@@ -3123,6 +3183,11 @@ fn sandbox_to_json(sandbox: &Sandbox) -> serde_json::Value {
31233183
"conditions": conditions,
31243184
"endpoint_statuses": endpoint_statuses,
31253185
"last_token_grant_failure": last_token_grant_failure,
3186+
"ingress_admission": sandbox.status.as_ref().and_then(|status| status.ingress_admission.as_ref()).map(|ingress| serde_json::json!({
3187+
"scope": ingress_scope_name(ingress.scope),
3188+
"all_ports": ingress.all_ports,
3189+
"ports": ingress.ports,
3190+
})),
31263191
"configuration_admission": admission,
31273192
"provisioning": provisioning,
31283193
"created_from_workload_template": created_from_workload_template,
@@ -8829,6 +8894,37 @@ mod tests {
88298894
.error
88308895
.clear();
88318896
assert_eq!(super::configuration_failure_message(&sandbox), None);
8897+
assert!(super::sandbox_to_json(&sandbox)["ingress_admission"].is_null());
8898+
for (scope, expected) in [
8899+
(
8900+
openshell_core::proto::IngressScope::IsolatedLoopback as i32,
8901+
"isolated_loopback",
8902+
),
8903+
(
8904+
openshell_core::proto::IngressScope::SharedHostLoopback as i32,
8905+
"shared_host_loopback",
8906+
),
8907+
(
8908+
openshell_core::proto::IngressScope::Unsupported as i32,
8909+
"unsupported",
8910+
),
8911+
(0, "unknown"),
8912+
(99, "unknown"),
8913+
] {
8914+
sandbox.status.as_mut().unwrap().ingress_admission =
8915+
Some(openshell_core::proto::SandboxIngressAdmission {
8916+
scope,
8917+
ports: vec![8080],
8918+
all_ports: false,
8919+
});
8920+
let output = super::sandbox_to_json(&sandbox);
8921+
assert_eq!(output["ingress_admission"]["scope"], expected);
8922+
assert_eq!(
8923+
output["ingress_admission"]["ports"],
8924+
serde_json::json!([8080])
8925+
);
8926+
assert_eq!(output["ingress_admission"]["all_ports"], false);
8927+
}
88328928
}
88338929

88348930
#[test]

‎crates/openshell-cli/src/ssh.rs‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ struct SshSessionConfig {
111111
gateway_url: String,
112112
token: String,
113113
main_terminal: bool,
114+
ingress_admission: Option<openshell_core::proto::SandboxIngressAdmission>,
114115
replica: Option<AsciiMetadataValue>,
115116
}
116117

@@ -203,6 +204,10 @@ async fn ssh_session_config(
203204
);
204205

205206
Ok(SshSessionConfig {
207+
ingress_admission: sandbox
208+
.status
209+
.as_ref()
210+
.and_then(|status| status.ingress_admission.clone()),
206211
proxy_command,
207212
sandbox_id: session.sandbox_id.clone(),
208213
sandbox_name: name.to_string(),
@@ -742,9 +747,18 @@ pub async fn sandbox_forward(
742747
tls: &TlsOptions,
743748
workspace: &str,
744749
) -> Result<()> {
745-
openshell_core::forward::check_port_available(spec)?;
746-
747750
let session = ssh_session_config(server, name, tls, workspace, None).await?;
751+
let scope = openshell_core::ingress::admit_port(
752+
session.ingress_admission.as_ref(),
753+
u32::from(spec.port),
754+
false,
755+
)
756+
.map_err(|error| miette::miette!("{error}"))?;
757+
if scope == openshell_core::proto::IngressScope::SharedHostLoopback {
758+
eprintln!("Target is an admitted shared-host loopback service.");
759+
}
760+
761+
openshell_core::forward::check_port_available(spec)?;
748762

749763
let mut command = TokioCommand::from(ssh_forward_command(
750764
&session.proxy_command,
@@ -2548,6 +2562,7 @@ mod tests {
25482562
async move {
25492563
if setup {
25502564
let session = SshSessionConfig {
2565+
ingress_admission: None,
25512566
proxy_command: String::new(),
25522567
sandbox_id: String::new(),
25532568
sandbox_name: String::new(),

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub const COMPUTE_SUSPEND_RESUME: &str = "openshell.compute.suspend-resume";
2828
/// Optional stopped-state workload configuration updates retaining the resource and storage.
2929
pub const COMPUTE_UPDATE_SANDBOX_CONFIGURATION: &str =
3030
"openshell.compute.update-sandbox-configuration";
31+
/// Immutable per-sandbox forwarding and service exposure admission.
32+
pub const COMPUTE_INGRESS_ADMISSION: &str = "openshell.compute.ingress-admission";
3133

3234
const MAX_IMPLEMENTATION_NAME_BYTES: usize = 128;
3335
const MAX_IMPLEMENTATION_VERSION_BYTES: usize = 128;
@@ -133,6 +135,7 @@ pub fn gateway_metadata(family: ExtensionFamily) -> PeerMetadata {
133135
supported_capabilities.push(COMPUTE_LAUNCH_AUTHENTICATION.to_string());
134136
supported_capabilities.push(COMPUTE_ISOLATION_BACKEND.to_string());
135137
supported_capabilities.push(COMPUTE_UPDATE_SANDBOX_CONFIGURATION.to_string());
138+
supported_capabilities.push(COMPUTE_INGRESS_ADMISSION.to_string());
136139
}
137140
PeerMetadata {
138141
protocol_version: Some(ProtocolVersion {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Shared validation of compute-admitted ingress; the backend enforces targets.
5+
6+
use crate::proto::{IngressScope, SandboxIngressAdmission};
7+
use std::collections::BTreeSet;
8+
9+
/// Validate a negotiated descriptor without treating unknown evidence as legacy.
10+
pub fn validate(admission: &SandboxIngressAdmission) -> Result<IngressScope, &'static str> {
11+
let scope =
12+
IngressScope::try_from(admission.scope).map_err(|_| "unknown compute ingress scope")?;
13+
if scope == IngressScope::Unspecified {
14+
return Err(
15+
"compute ingress admission is unavailable; recreate the sandbox with an ingress-aware driver",
16+
);
17+
}
18+
if (admission.all_ports && !admission.ports.is_empty())
19+
|| (scope == IngressScope::Unsupported
20+
&& (admission.all_ports || !admission.ports.is_empty()))
21+
|| admission.ports.len() > 65535
22+
{
23+
return Err("invalid compute ingress envelope");
24+
}
25+
let mut seen = BTreeSet::new();
26+
for port in &admission.ports {
27+
if !(1..=65535).contains(port) || !seen.insert(port) {
28+
return Err("compute ingress ports must be unique and between 1 and 65535");
29+
}
30+
}
31+
Ok(scope)
32+
}
33+
34+
/// Authorize before listeners, service persistence, or any pooled upstream use.
35+
/// Legacy absence is permitted only when the gateway has not negotiated ingress.
36+
pub fn admit_port(
37+
admission: Option<&SandboxIngressAdmission>,
38+
port: u32,
39+
required: bool,
40+
) -> Result<IngressScope, &'static str> {
41+
if !(1..=65535).contains(&port) {
42+
return Err("target port must be between 1 and 65535");
43+
}
44+
let Some(admission) = admission else {
45+
return if required {
46+
Err("compute ingress admission is missing; recreate the sandbox")
47+
} else {
48+
Ok(IngressScope::Unspecified)
49+
};
50+
};
51+
let scope = validate(admission)?;
52+
if scope == IngressScope::Unsupported {
53+
return Err("the compute driver does not support sandbox ingress");
54+
}
55+
if !admission.all_ports && !admission.ports.contains(&port) {
56+
return Err(
57+
"target port was not admitted by the compute driver; recreate the sandbox with that service port",
58+
);
59+
}
60+
Ok(scope)
61+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub mod gpu;
3535
pub mod grpc_client;
3636
pub mod host_pattern;
3737
pub mod image;
38+
pub mod ingress;
3839
pub mod isolation_registration;
3940
pub mod jwt;
4041
pub mod local_api_socket;

‎crates/openshell-driver-docker/src/lib.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ impl DockerComputeDriver {
10941094
openshell_core::extension_protocol::ExtensionFamily::Compute,
10951095
"openshell/docker",
10961096
openshell_core::VERSION,
1097-
[],
1097+
[openshell_core::extension_protocol::COMPUTE_INGRESS_ADMISSION.to_string()],
10981098
)),
10991099
}
11001100
}
@@ -3354,7 +3354,13 @@ impl ComputeDriver for DockerComputeDriver {
33543354
CdiGpuDefaultSelector::peek_device_ids,
33553355
)
33563356
.await?;
3357-
Ok(Response::new(ValidateSandboxCreateResponse {}))
3357+
Ok(Response::new(ValidateSandboxCreateResponse {
3358+
ingress_admission: Some(openshell_core::proto::SandboxIngressAdmission {
3359+
scope: openshell_core::proto::IngressScope::IsolatedLoopback as i32,
3360+
all_ports: true,
3361+
ports: Vec::new(),
3362+
}),
3363+
}))
33583364
}
33593365

33603366
async fn get_sandbox(

‎crates/openshell-driver-kubernetes/src/driver.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,7 @@ impl KubernetesComputeDriver {
13301330
[
13311331
openshell_core::extension_protocol::COMPUTE_UPDATE_SANDBOX_CONFIGURATION
13321332
.to_string(),
1333+
openshell_core::extension_protocol::COMPUTE_INGRESS_ADMISSION.to_string(),
13331334
],
13341335
)),
13351336
})

‎crates/openshell-driver-kubernetes/src/grpc.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,13 @@ impl ComputeDriver for ComputeDriverService {
102102
.sandbox
103103
.ok_or_else(|| Status::invalid_argument("sandbox is required"))?;
104104
self.driver.validate_sandbox_create(&sandbox).await?;
105-
Ok(Response::new(ValidateSandboxCreateResponse {}))
105+
Ok(Response::new(ValidateSandboxCreateResponse {
106+
ingress_admission: Some(openshell_core::proto::SandboxIngressAdmission {
107+
scope: openshell_core::proto::IngressScope::IsolatedLoopback as i32,
108+
all_ports: true,
109+
ports: Vec::new(),
110+
}),
111+
}))
106112
})
107113
.await
108114
}

‎crates/openshell-driver-mxc/src/driver.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ impl MxcComputeBackend {
480480
openshell_core::extension_protocol::ExtensionFamily::Compute,
481481
"openshell/mxc",
482482
openshell_core::VERSION,
483-
[],
483+
[openshell_core::extension_protocol::COMPUTE_INGRESS_ADMISSION.to_string()],
484484
)),
485485
}
486486
}

‎crates/openshell-driver-mxc/src/grpc.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ impl ComputeDriver for ComputeDriverService {
7070
.sandbox
7171
.ok_or_else(|| Status::invalid_argument("sandbox is required"))?;
7272
self.backend.validate_sandbox_create(&sandbox)?;
73-
Ok(Response::new(ValidateSandboxCreateResponse {}))
73+
Ok(Response::new(ValidateSandboxCreateResponse {
74+
ingress_admission: Some(openshell_core::proto::SandboxIngressAdmission {
75+
scope: openshell_core::proto::IngressScope::Unsupported as i32,
76+
all_ports: false,
77+
ports: Vec::new(),
78+
}),
79+
}))
7480
}
7581

7682
async fn get_sandbox(

0 commit comments

Comments
 (0)