mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Accept inbound Noise connections in remote exec-server (#62325)
## Why Remote Noise executors previously connected outbound to a rendezvous relay. An inbound listener lets harnesses that can reach the executor connect directly while retaining registry-backed authorization. ## What changed Use `--listen ws://IP:PORT` with `--remote` in Noise mode to accept inbound WebSockets. The listener serves RPCs over encrypted Noise streams after the registry authorizes the harness. It limits concurrent connections and handshake attempts, closes sockets that miss the first authorization deadline, and drains connections on shutdown. Noise clients can attach optional admission headers from the connection bundle to `wss://` requests. ## Testing The inbound integration test exercises a registry-authorized file read, rejection of a registry-denied harness, the absence of a plaintext RPC response, and connection closure on shutdown. Unit tests cover handshake limits and rejection of admission headers before dialing a plaintext WebSocket. GitOrigin-RevId: b388672e49a3fe806e38f6db38825435ba25d27a
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
//! Exec-server stdio and WebSocket listeners. Configured authentication gates
|
||||
//! WebSocket upgrades; unauthenticated listeners remain supported for compatibility.
|
||||
|
||||
use crate::noise_channel::NoiseChannelIdentity;
|
||||
use crate::noise_relay::MAX_NOISE_RELAY_WEBSOCKET_MESSAGE_SIZE;
|
||||
use crate::noise_relay::admission::HandshakeBudget;
|
||||
use crate::noise_relay::admission::InboundAdmission;
|
||||
use crate::relay::run_multiplexed_environment;
|
||||
use axum::Router;
|
||||
use axum::body::Body;
|
||||
use axum::extract::ConnectInfo;
|
||||
use axum::extract::State;
|
||||
use axum::extract::ws::Message as AxumMessage;
|
||||
use axum::extract::ws::WebSocketUpgrade;
|
||||
use axum::http::HeaderMap;
|
||||
use axum::http::Request;
|
||||
@@ -21,12 +27,21 @@ use codex_websocket_auth::WebsocketAuthPolicy;
|
||||
use codex_websocket_auth::WebsocketAuthSettings;
|
||||
use codex_websocket_auth::authorize_upgrade;
|
||||
use codex_websocket_auth::policy_from_settings;
|
||||
use futures::SinkExt;
|
||||
use futures::StreamExt;
|
||||
use std::io::Write as _;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::io;
|
||||
use tokio::io::AsyncRead;
|
||||
use tokio::io::AsyncWrite;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::sync::Notify;
|
||||
use tokio::sync::Semaphore;
|
||||
use tokio_tungstenite::tungstenite::Message as NoiseMessage;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tokio_util::task::TaskTracker;
|
||||
use tracing::info;
|
||||
use tracing::warn;
|
||||
|
||||
@@ -195,26 +210,108 @@ async fn run_websocket_listener(
|
||||
println!("ws://{local_addr}");
|
||||
std::io::stdout().flush()?;
|
||||
|
||||
serve_websocket(
|
||||
listener,
|
||||
ExecServerWebSocketState {
|
||||
processor,
|
||||
auth_policy,
|
||||
noise: None,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
const FIRST_NOISE_AUTHORIZATION_TIMEOUT: Duration = Duration::from_secs(20);
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub(crate) struct NoiseListenerControl {
|
||||
shutdown: CancellationToken,
|
||||
tasks: TaskTracker,
|
||||
budget: Arc<HandshakeBudget>,
|
||||
}
|
||||
|
||||
impl NoiseListenerControl {
|
||||
pub(crate) fn stop(&self) {
|
||||
self.shutdown.cancel();
|
||||
}
|
||||
pub(crate) async fn cancelled(&self) {
|
||||
self.shutdown.cancelled().await;
|
||||
}
|
||||
pub(crate) async fn wait(&self) {
|
||||
self.tasks.close();
|
||||
self.tasks.wait().await;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct NoiseListener {
|
||||
environment_id: String,
|
||||
registration_id: String,
|
||||
identity: NoiseChannelIdentity,
|
||||
validator: crate::remote::RegistryHarnessKeyValidator,
|
||||
permits: Arc<Semaphore>,
|
||||
control: NoiseListenerControl,
|
||||
}
|
||||
|
||||
pub(crate) async fn run_noise_websocket_listener(
|
||||
listener: TcpListener,
|
||||
processor: ConnectionProcessor,
|
||||
environment_id: String,
|
||||
registration_id: String,
|
||||
identity: NoiseChannelIdentity,
|
||||
validator: crate::remote::RegistryHarnessKeyValidator,
|
||||
control: NoiseListenerControl,
|
||||
) -> std::io::Result<()> {
|
||||
serve_websocket(
|
||||
listener,
|
||||
ExecServerWebSocketState {
|
||||
processor,
|
||||
auth_policy: WebsocketAuthPolicy::default(),
|
||||
noise: Some(NoiseListener {
|
||||
environment_id,
|
||||
registration_id,
|
||||
identity,
|
||||
validator,
|
||||
permits: Arc::new(Semaphore::new(64)),
|
||||
control,
|
||||
}),
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn serve_websocket(
|
||||
listener: TcpListener,
|
||||
state: ExecServerWebSocketState,
|
||||
) -> std::io::Result<()> {
|
||||
let shutdown = state
|
||||
.noise
|
||||
.as_ref()
|
||||
.map(|noise| noise.control.shutdown.clone());
|
||||
let router = Router::new()
|
||||
.route("/", any(websocket_upgrade_handler))
|
||||
.route("/readyz", get(readiness_handler))
|
||||
.layer(middleware::from_fn(reject_requests_with_origin_header))
|
||||
.with_state(ExecServerWebSocketState {
|
||||
processor,
|
||||
auth_policy,
|
||||
});
|
||||
.with_state(state);
|
||||
axum::serve(
|
||||
listener,
|
||||
router.into_make_service_with_connect_info::<SocketAddr>(),
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
.with_graceful_shutdown(async move {
|
||||
match shutdown {
|
||||
Some(shutdown) => shutdown.cancelled().await,
|
||||
None => std::future::pending::<()>().await,
|
||||
}
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ExecServerWebSocketState {
|
||||
processor: ConnectionProcessor,
|
||||
auth_policy: WebsocketAuthPolicy,
|
||||
noise: Option<NoiseListener>,
|
||||
}
|
||||
|
||||
async fn readiness_handler() -> StatusCode {
|
||||
@@ -246,19 +343,88 @@ async fn websocket_upgrade_handler(
|
||||
if let Err(err) = authorize_upgrade(&headers, &state.auth_policy) {
|
||||
return (err.status_code(), err.message()).into_response();
|
||||
}
|
||||
let (websocket, permit) = if let Some(noise) = &state.noise {
|
||||
let Ok(permit) = Arc::clone(&noise.permits).try_acquire_owned() else {
|
||||
return StatusCode::SERVICE_UNAVAILABLE.into_response();
|
||||
};
|
||||
(
|
||||
websocket
|
||||
.max_message_size(MAX_NOISE_RELAY_WEBSOCKET_MESSAGE_SIZE)
|
||||
.max_frame_size(MAX_NOISE_RELAY_WEBSOCKET_MESSAGE_SIZE),
|
||||
Some(permit),
|
||||
)
|
||||
} else {
|
||||
(websocket, None)
|
||||
};
|
||||
info!(%peer_addr, "exec-server websocket client connected");
|
||||
// Register before Axum spawns the upgrade callback, including upgrades still pending.
|
||||
let task = state
|
||||
.noise
|
||||
.as_ref()
|
||||
.map(|noise| noise.control.tasks.token());
|
||||
let mut response = websocket
|
||||
.on_upgrade(move |stream| async move {
|
||||
state
|
||||
.processor
|
||||
.run_connection(
|
||||
JsonRpcConnection::from_axum_websocket(
|
||||
stream,
|
||||
format!("exec-server websocket {peer_addr}"),
|
||||
),
|
||||
ConnectionTransport::WebSocket,
|
||||
)
|
||||
.await;
|
||||
let _task = task;
|
||||
let _permit = permit;
|
||||
if let Some(noise) = state.noise {
|
||||
let stream = stream
|
||||
.with(|message: NoiseMessage| {
|
||||
futures::future::ready(Ok::<_, axum::Error>(match message {
|
||||
NoiseMessage::Binary(v) => AxumMessage::Binary(v),
|
||||
NoiseMessage::Text(v) => AxumMessage::Text(v.as_str().into()),
|
||||
NoiseMessage::Ping(v) => AxumMessage::Ping(v),
|
||||
NoiseMessage::Pong(v) => AxumMessage::Pong(v),
|
||||
NoiseMessage::Close(_) | NoiseMessage::Frame(_) => {
|
||||
AxumMessage::Close(None)
|
||||
}
|
||||
}))
|
||||
})
|
||||
.map(|result| {
|
||||
result.map(|message| match message {
|
||||
AxumMessage::Binary(v) => NoiseMessage::Binary(v),
|
||||
AxumMessage::Text(v) => NoiseMessage::Text(v.as_str().into()),
|
||||
AxumMessage::Ping(v) => NoiseMessage::Ping(v),
|
||||
AxumMessage::Pong(v) => NoiseMessage::Pong(v),
|
||||
AxumMessage::Close(_) => NoiseMessage::Close(None),
|
||||
})
|
||||
});
|
||||
let admission = InboundAdmission {
|
||||
budget: Arc::clone(&noise.control.budget),
|
||||
authorized: Arc::new(Notify::new()),
|
||||
shutdown: noise.control.shutdown.child_token(),
|
||||
};
|
||||
let authorized = Arc::clone(&admission.authorized);
|
||||
let shutdown = admission.shutdown.clone();
|
||||
let run = run_multiplexed_environment(
|
||||
stream,
|
||||
state.processor,
|
||||
noise.environment_id,
|
||||
noise.registration_id,
|
||||
noise.identity,
|
||||
noise.validator,
|
||||
Some(admission),
|
||||
);
|
||||
tokio::pin!(run);
|
||||
tokio::select! {
|
||||
_ = &mut run => {},
|
||||
_ = authorized.notified() => { run.await; },
|
||||
_ = tokio::time::sleep(FIRST_NOISE_AUTHORIZATION_TIMEOUT) => {
|
||||
shutdown.cancel();
|
||||
run.await;
|
||||
},
|
||||
}
|
||||
} else {
|
||||
state
|
||||
.processor
|
||||
.run_connection(
|
||||
JsonRpcConnection::from_axum_websocket(
|
||||
stream,
|
||||
format!("exec-server websocket {peer_addr}"),
|
||||
),
|
||||
ConnectionTransport::WebSocket,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
})
|
||||
.into_response();
|
||||
if let Some(request_id) = headers.get("x-request-id") {
|
||||
|
||||
Reference in New Issue
Block a user