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:
richardopenai
2026-11-18 17:55:49 +00:00
committed by copyberry
parent f2d3086682
commit c1ed51ca2c
21 changed files with 646 additions and 48 deletions
+32 -3
View File
@@ -34,6 +34,7 @@ use crate::noise_channel::NoiseChannelPublicKey;
use crate::noise_channel::PendingResponderHandshake;
use crate::noise_channel::noise_channel_prologue;
use crate::noise_relay::NOISE_RELAY_RESET_REASON;
use crate::noise_relay::admission::InboundAdmission;
use crate::noise_relay::executor_stream::ClosedNoiseVirtualStream;
use crate::noise_relay::executor_stream::NoiseVirtualStream;
use crate::noise_relay::executor_stream::spawn_noise_virtual_stream;
@@ -48,6 +49,7 @@ use crate::relay_proto::relay_message_frame;
use crate::server::ConnectionProcessor;
use crate::telemetry::ExecutorRegistration;
use crate::websocket_pong_watchdog::WEBSOCKET_PONG_TIMEOUT_REASON;
use tokio_util::task::TaskTracker;
#[path = "relay_writer.rs"]
mod writer;
@@ -517,6 +519,7 @@ pub(crate) async fn run_multiplexed_environment<T, E, V, H>(
executor_registration_id: String,
identity: NoiseChannelIdentity,
validator: V,
admission: Option<InboundAdmission>,
) -> RendezvousDisconnectReason
where
T: Sink<Message, Error = E> + Stream<Item = Result<Message, E>> + Unpin + Send + 'static,
@@ -545,6 +548,11 @@ where
let mut streams: HashMap<String, NoiseVirtualStream<H>> = HashMap::new();
let mut pending_handshakes: HashMap<String, PendingHandshake> = HashMap::new();
let mut validation_tasks: JoinSet<HarnessKeyValidationResult> = JoinSet::new();
let stream_tasks = admission.as_ref().map(|_| TaskTracker::new());
let shutdown = admission
.as_ref()
.map(|a| a.shutdown.clone())
.unwrap_or_default();
let mut failed_handshakes = 0usize;
let mut handshake_cooldown_until = None;
let mut next_validation_id = 0u64;
@@ -589,6 +597,7 @@ where
loop {
// Registry calls run separately so a slow check does not block the relay.
let frame = tokio::select! {
_ = shutdown.cancelled() => break,
writer_result = &mut physical_writer_task => {
match writer_result {
Ok(reason) => disconnect_reason = reason,
@@ -715,8 +724,8 @@ where
streams.insert(
validation_result.stream_id.clone(),
spawn_noise_virtual_stream(
validation_result.stream_id,
validation_result.validation_id,
ClosedNoiseVirtualStream { stream_id: validation_result.stream_id, instance_id: validation_result.validation_id },
stream_tasks.as_ref(),
handler.clone(),
physical_outgoing_tx.clone(),
closed_stream_tx.clone(),
@@ -724,6 +733,9 @@ where
executor_registration.clone(),
),
);
if let Some(admission) = &admission {
admission.authorized.notify_one();
}
}
Some(Err(error)) => {
warn!("Noise relay harness key validation task failed: {error}");
@@ -815,6 +827,15 @@ where
send_reset(&physical_outgoing_tx, stream_id);
continue;
}
let handshake_permit = if let Some(admission) = &admission {
let Some(permit) = admission.budget.try_admit() else {
send_reset(&physical_outgoing_tx, stream_id);
continue;
};
Some(permit)
} else {
None
};
let prologue =
noise_channel_prologue(&environment_id, &executor_registration_id, &stream_id);
let request = match frame.into_handshake_payload() {
@@ -880,6 +901,7 @@ where
// Failed validation leaves no transport state. Preserve the
// trusted registry status for request-scoped rejection handling.
validation_tasks.spawn(async move {
let _handshake_permit = handshake_permit;
let result = match timeout(
HARNESS_KEY_VALIDATION_TIMEOUT,
validator.validate_harness_key(
@@ -954,7 +976,13 @@ where
for (_stream_id, stream) in streams {
stream.disconnect();
}
// Dropping the JoinSet aborts any registry checks still running.
// Reporters must not block on a closed physical connection while handlers drain.
drop(closed_stream_rx);
if let Some(stream_tasks) = stream_tasks {
validation_tasks.shutdown().await;
stream_tasks.close();
stream_tasks.wait().await;
}
if !physical_writer_task.is_finished() {
physical_writer_task.abort();
let _ = physical_writer_task.await;
@@ -1259,6 +1287,7 @@ mod tests {
"test-registration".to_string(),
NoiseChannelIdentity::generate()?,
AllowHarnessKeyValidator,
/*admission*/ None,
));
assert!(matches!(outbound_rx.next().await, Some(Message::Ping(_))));