forked from NVIDIA/OpenShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
285 lines (255 loc) · 9.34 KB
/
lib.rs
File metadata and controls
285 lines (255 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//! `OpenShell` Server library.
//!
//! This crate provides the server implementation for `OpenShell`, including:
//! - gRPC service implementation
//! - HTTP health endpoints
//! - Protocol multiplexing (gRPC + HTTP on same port)
//! - mTLS support
mod auth;
mod grpc;
mod http;
mod inference;
mod multiplex;
mod persistence;
mod sandbox;
mod sandbox_index;
mod sandbox_watch;
mod ssh_tunnel;
mod tls;
pub mod tracing_bus;
mod ws_tunnel;
use openshell_core::{Config, Error, Result};
use std::collections::HashMap;
use std::io::ErrorKind;
use std::sync::{Arc, Mutex};
use tokio::net::TcpListener;
use tracing::{debug, error, info};
pub use grpc::OpenShellService;
pub use http::{health_router, http_router};
pub use multiplex::ALPN_H2;
pub use multiplex::{MultiplexService, MultiplexedService};
use persistence::Store;
use sandbox::{SandboxClient, spawn_sandbox_watcher, spawn_store_reconciler};
use sandbox_index::SandboxIndex;
use sandbox_watch::{SandboxWatchBus, spawn_kube_event_tailer};
pub use tls::TlsAcceptor;
use tracing_bus::TracingLogBus;
/// Server state shared across handlers.
#[derive(Debug)]
pub struct ServerState {
/// Server configuration.
pub config: Config,
/// Persistence store.
pub store: Arc<Store>,
/// Kubernetes sandbox client.
pub sandbox_client: SandboxClient,
/// In-memory sandbox correlation index.
pub sandbox_index: SandboxIndex,
/// In-memory bus for sandbox update notifications.
pub sandbox_watch_bus: SandboxWatchBus,
/// In-memory bus for server process logs.
pub tracing_log_bus: TracingLogBus,
/// Active SSH tunnel connection counts per session token.
pub ssh_connections_by_token: Mutex<HashMap<String, u32>>,
/// Active SSH tunnel connection counts per sandbox id.
pub ssh_connections_by_sandbox: Mutex<HashMap<String, u32>>,
/// Serializes settings mutations (global and sandbox) to prevent
/// read-modify-write races. Held for the duration of any setting
/// set/delete operation, including the precedence check on sandbox
/// mutations that reads global state.
pub settings_mutex: tokio::sync::Mutex<()>,
}
fn is_benign_tls_handshake_failure(error: &std::io::Error) -> bool {
matches!(
error.kind(),
ErrorKind::UnexpectedEof | ErrorKind::ConnectionReset
)
}
impl ServerState {
/// Create new server state.
#[must_use]
pub fn new(
config: Config,
store: Arc<Store>,
sandbox_client: SandboxClient,
sandbox_index: SandboxIndex,
sandbox_watch_bus: SandboxWatchBus,
tracing_log_bus: TracingLogBus,
) -> Self {
Self {
config,
store,
sandbox_client,
sandbox_index,
sandbox_watch_bus,
tracing_log_bus,
ssh_connections_by_token: Mutex::new(HashMap::new()),
ssh_connections_by_sandbox: Mutex::new(HashMap::new()),
settings_mutex: tokio::sync::Mutex::new(()),
}
}
}
/// Run the `OpenShell` server.
///
/// This starts a multiplexed gRPC/HTTP server on the configured bind address.
///
/// # Errors
///
/// Returns an error if the server fails to start or encounters a fatal error.
pub async fn run_server(config: Config, tracing_log_bus: TracingLogBus) -> Result<()> {
let database_url = config.database_url.trim();
if database_url.is_empty() {
return Err(Error::config("database_url is required"));
}
if config.ssh_handshake_secret.is_empty() {
return Err(Error::config(
"ssh_handshake_secret is required. Set --ssh-handshake-secret or OPENSHELL_SSH_HANDSHAKE_SECRET",
));
}
let store = Store::connect(database_url).await?;
let sandbox_client = SandboxClient::new(
config.sandbox_namespace.clone(),
config.sandbox_image.clone(),
config.sandbox_image_pull_policy.clone(),
config.grpc_endpoint.clone(),
format!("0.0.0.0:{}", config.sandbox_ssh_port),
config.ssh_handshake_secret.clone(),
config.ssh_handshake_skew_secs,
config.client_tls_secret_name.clone(),
config.host_gateway_ip.clone(),
)
.await
.map_err(|e| Error::execution(format!("failed to create kubernetes client: {e}")))?;
let store = Arc::new(store);
let sandbox_index = SandboxIndex::new();
let sandbox_watch_bus = SandboxWatchBus::new();
let state = Arc::new(ServerState::new(
config.clone(),
store.clone(),
sandbox_client,
sandbox_index,
sandbox_watch_bus,
tracing_log_bus,
));
spawn_sandbox_watcher(
store.clone(),
state.sandbox_client.clone(),
state.sandbox_index.clone(),
state.sandbox_watch_bus.clone(),
state.tracing_log_bus.clone(),
);
spawn_store_reconciler(
store.clone(),
state.sandbox_client.clone(),
state.sandbox_index.clone(),
state.sandbox_watch_bus.clone(),
state.tracing_log_bus.clone(),
);
spawn_kube_event_tailer(state.clone());
ssh_tunnel::spawn_session_reaper(store.clone(), std::time::Duration::from_secs(3600));
// Create the multiplexed service
let service = MultiplexService::new(state.clone());
// Bind the TCP listener
let listener = TcpListener::bind(config.bind_address)
.await
.map_err(|e| Error::transport(format!("failed to bind to {}: {e}", config.bind_address)))?;
info!(address = %config.bind_address, "Server listening");
// Build TLS acceptor when TLS is configured; otherwise serve plaintext.
let tls_acceptor = if let Some(tls) = &config.tls {
let acceptor = TlsAcceptor::from_files(
&tls.cert_path,
&tls.key_path,
&tls.client_ca_path,
tls.allow_unauthenticated,
)?;
info!(
cert = %tls.cert_path.display(),
key = %tls.key_path.display(),
client_ca = %tls.client_ca_path.display(),
allow_unauthenticated = tls.allow_unauthenticated,
"TLS enabled — ALPN advertises h2 + http/1.1",
);
Some(acceptor)
} else {
info!("TLS disabled — accepting plaintext connections");
None
};
// Accept connections
loop {
let (stream, addr) = match listener.accept().await {
Ok(conn) => conn,
Err(e) => {
error!(error = %e, "Failed to accept connection");
continue;
}
};
let service = service.clone();
if let Some(ref acceptor) = tls_acceptor {
let tls_acceptor = acceptor.clone();
tokio::spawn(async move {
match tls_acceptor.inner().accept(stream).await {
Ok(tls_stream) => {
// Use ALPN-negotiated protocol when available. This
// avoids the byte-sniffing auto-detection in
// `serve()`, which can misidentify h2 connections as
// HTTP/1.1 when the first read returns a partial
// preface.
let alpn = tls_stream.get_ref().1.alpn_protocol().unwrap_or_default();
let result = if alpn == ALPN_H2 {
debug!(client = %addr, "ALPN negotiated h2 — serving HTTP/2");
service.serve_h2(tls_stream).await
} else {
debug!(client = %addr, alpn = ?String::from_utf8_lossy(alpn), "ALPN fallback — auto-detecting protocol");
service.serve(tls_stream).await
};
if let Err(e) = result {
error!(error = %e, client = %addr, "Connection error");
}
}
Err(e) => {
if is_benign_tls_handshake_failure(&e) {
debug!(error = %e, client = %addr, "TLS handshake closed early");
} else {
error!(error = %e, client = %addr, "TLS handshake failed");
}
}
}
});
} else {
tokio::spawn(async move {
if let Err(e) = service.serve(stream).await {
error!(error = %e, client = %addr, "Connection error");
}
});
}
}
}
#[cfg(test)]
mod tests {
use super::is_benign_tls_handshake_failure;
use std::io::{Error, ErrorKind};
#[test]
fn classifies_probe_style_tls_disconnects_as_benign() {
for kind in [ErrorKind::UnexpectedEof, ErrorKind::ConnectionReset] {
let error = Error::new(kind, "probe disconnected");
assert!(is_benign_tls_handshake_failure(&error));
}
}
#[test]
fn preserves_real_tls_failures_as_errors() {
for kind in [
ErrorKind::InvalidData,
ErrorKind::PermissionDenied,
ErrorKind::Other,
] {
let error = Error::new(kind, "real tls failure");
assert!(!is_benign_tls_handshake_failure(&error));
}
}
#[test]
fn alpn_h2_constant_matches_standard_protocol_id() {
assert_eq!(super::ALPN_H2, b"h2");
}
}