Skip to content

Commit 7873aaa

Browse files
authored
fix(server): budget SSH relays and service forwards separately (#5063)
Open service forwards no longer block SSH. Both per-sandbox caps are configurable and the limit error names the exhausted budget. Signed-off-by: Eric Curtin <eric.curtin@docker.com>
1 parent eee2b9f commit 7873aaa

7 files changed

Lines changed: 256 additions & 40 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ pub const DEFAULT_DOCKER_NETWORK_NAME: &str = "openshell-docker";
4646
/// Default domain used for browser-facing sandbox service URLs.
4747
pub const DEFAULT_SERVICE_ROUTING_DOMAIN: &str = "openshell.localhost";
4848

49+
/// Default per-sandbox cap for each relay budget (SSH and service forwards).
50+
pub const DEFAULT_MAX_CONNECTIONS_PER_SANDBOX: u32 = 20;
51+
4952
/// Gateway delivery path for supervisor configuration.
5053
///
5154
/// `Push` sends configuration over the supervisor session to supervisors that
@@ -324,6 +327,14 @@ pub struct Config {
324327
/// TTL for SSH session tokens, in seconds. 0 disables expiry.
325328
pub ssh_session_ttl_secs: u64,
326329

330+
/// Maximum concurrent SSH relays per sandbox. Must be at least 1.
331+
pub max_ssh_connections_per_sandbox: u32,
332+
333+
/// Maximum concurrent service forward relays per sandbox. Must be at
334+
/// least 1. Kept apart from the SSH budget so open forwards cannot block
335+
/// SSH access.
336+
pub max_service_connections_per_sandbox: u32,
337+
327338
/// Absolute image preparation and initial supervisor startup budget for new
328339
/// sandbox attempts, in seconds. Must be between 1 and 86400, inclusive.
329340
pub image_preparation_timeout_seconds: u32,
@@ -1047,6 +1058,8 @@ impl Config {
10471058
credential_drivers: Vec::new(),
10481059
default_credential_driver: None,
10491060
ssh_session_ttl_secs: default_ssh_session_ttl_secs(),
1061+
max_ssh_connections_per_sandbox: DEFAULT_MAX_CONNECTIONS_PER_SANDBOX,
1062+
max_service_connections_per_sandbox: DEFAULT_MAX_CONNECTIONS_PER_SANDBOX,
10501063
image_preparation_timeout_seconds: 1800,
10511064
grpc_rate_limit_requests: None,
10521065
grpc_rate_limit_window_secs: None,

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,19 @@ fn prepare_server_config_with_drivers(
679679
config = config.with_ssh_session_ttl_secs(ttl);
680680
}
681681

682+
if let Some(gateway_file) = file.as_ref().map(|f| &f.openshell.gateway) {
683+
apply_connection_cap(
684+
"max_ssh_connections_per_sandbox",
685+
gateway_file.max_ssh_connections_per_sandbox,
686+
&mut config.max_ssh_connections_per_sandbox,
687+
)?;
688+
apply_connection_cap(
689+
"max_service_connections_per_sandbox",
690+
gateway_file.max_service_connections_per_sandbox,
691+
&mut config.max_service_connections_per_sandbox,
692+
)?;
693+
}
694+
682695
if let Some(mode) = file
683696
.as_ref()
684697
.and_then(|f| f.openshell.gateway.policy_validation_failure_mode)
@@ -1653,6 +1666,17 @@ fn validate_grpc_rate_limit_args(requests: Option<u64>, window_seconds: Option<u
16531666
Ok(())
16541667
}
16551668

1669+
/// Apply an optional per-sandbox relay cap from the gateway config file.
1670+
fn apply_connection_cap(name: &str, value: Option<u32>, cap: &mut u32) -> Result<()> {
1671+
if let Some(value) = value {
1672+
if value == 0 {
1673+
return Err(miette::miette!("{name} must be at least 1"));
1674+
}
1675+
*cap = value;
1676+
}
1677+
Ok(())
1678+
}
1679+
16561680
fn normalize_compute_driver_socket_args(args: &mut RunArgs) -> Result<()> {
16571681
let Some(socket) = args.compute_driver_socket.as_ref() else {
16581682
return Ok(());
@@ -4959,6 +4983,63 @@ mem_mib = "not-a-number"
49594983
});
49604984
}
49614985

4986+
#[test]
4987+
fn server_config_sets_and_validates_per_sandbox_connection_caps() {
4988+
let state = tempfile::tempdir().unwrap();
4989+
let tls = tempfile::tempdir().unwrap();
4990+
Environment::new()
4991+
.set("XDG_STATE_HOME", state.path().to_str().unwrap())
4992+
.set("OPENSHELL_LOCAL_TLS_DIR", tls.path().to_str().unwrap())
4993+
.run(|| {
4994+
let state = std::path::PathBuf::from(std::env::var_os("XDG_STATE_HOME").unwrap());
4995+
let config_path = state.join("gateway.toml");
4996+
let prepare = |gateway: &str| {
4997+
std::fs::write(
4998+
&config_path,
4999+
format!("[openshell]\nversion = 2\n[openshell.gateway]\n{gateway}\n"),
5000+
)
5001+
.unwrap();
5002+
let (mut args, matches) = parse_with_args(&[
5003+
"openshell-gateway",
5004+
"--config",
5005+
config_path.to_str().unwrap(),
5006+
"--db-url",
5007+
"sqlite::memory:",
5008+
"--compute-driver",
5009+
"docker",
5010+
"--disable-tls",
5011+
]);
5012+
super::prepare_server_config(&mut args, &matches)
5013+
};
5014+
5015+
let defaults = prepare("").expect("defaults");
5016+
assert_eq!(defaults.config.max_ssh_connections_per_sandbox, 20);
5017+
assert_eq!(defaults.config.max_service_connections_per_sandbox, 20);
5018+
5019+
let set = prepare(
5020+
"max_ssh_connections_per_sandbox = 5\nmax_service_connections_per_sandbox = 50",
5021+
)
5022+
.expect("caps are applied");
5023+
assert_eq!(set.config.max_ssh_connections_per_sandbox, 5);
5024+
assert_eq!(set.config.max_service_connections_per_sandbox, 50);
5025+
5026+
for key in [
5027+
"max_ssh_connections_per_sandbox",
5028+
"max_service_connections_per_sandbox",
5029+
] {
5030+
let Err(error) = prepare(&format!("{key} = 0")) else {
5031+
panic!("a zero cap must be rejected");
5032+
};
5033+
assert!(
5034+
error
5035+
.to_string()
5036+
.contains(&format!("{key} must be at least 1")),
5037+
"{error}"
5038+
);
5039+
}
5040+
});
5041+
}
5042+
49625043
#[test]
49635044
fn explicit_launch_signing_config_ignores_partial_local_bundle() {
49645045
let directory = tempfile::tempdir().unwrap();

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ pub struct GatewayFileSection {
146146
// ── Sandbox / SSH ────────────────────────────────────────────────────
147147
#[serde(default)]
148148
pub ssh_session_ttl_secs: Option<u64>,
149+
/// Per-sandbox cap on concurrent SSH relays; omit to keep the default of 20.
150+
#[serde(default)]
151+
pub max_ssh_connections_per_sandbox: Option<u32>,
152+
/// Per-sandbox cap on concurrent service forward relays; omit to keep the
153+
/// default of 20.
154+
#[serde(default)]
155+
pub max_service_connections_per_sandbox: Option<u32>,
149156
/// Absolute preparation budget for new attempts; existing deadlines persist.
150157
#[serde(default)]
151158
pub image_preparation_timeout_seconds: Option<u32>,

0 commit comments

Comments
 (0)