fix(aws): serialise and diff port-mapping forwards; refuse the gateway sidecar's port

Opens hold a per-project lock and re-read the stored mappings, so
overlapping saves converge on the last one without false 'in use' errors.
Unchanged forwards keep their live connections. The sidecar's 4000 is
refused while it runs (mappings reach workspace loopback). Stored rows
that would be refused no longer block unrelated saves; they are skipped
with a warning at connect. Port-mapping logs get their own prefix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Opus 5.5 committed 2026-11-16 09:45:37 -08:00
1 parent 08c779ad36
commit 45aef8da7d
8 files changed
+447 -130

No files matched your search

+11 -6
View File
@@ -300,12 +300,17 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li
every tunnel (`for_project` is `None` while none is open; `for_project_waiting` covers the
background re-attach after an app restart). AWS SDK calls stay in `aws/` (`runtime.rs`,
`tasks.rs`, `workspace.rs`).
`port_forwards.rs` is an AWS project's port mappings: TCP only (validated on save, with the
browser-view ranges and the agent port refused), each a **loopback-only** auth-bridge
`PortForward::bind_mapping` (no first-byte gate, unlike the bridge) over socat-over-attach.
One set per project, opened after every connect (start, re-attach) and on a save while
connected; closed by every `tunnels::close*` path. A failing mapping is a `workspace-warning`
(`topic: "ports"`), the rest still open; an idle listener makes no agent request.
`port_forwards.rs` is an AWS project's port mappings: each TCP mapping is a **loopback-only**
auth-bridge `PortForward::bind_mapping` over socat-over-attach. Two deliberate differences from
Docker: it reaches the workspace's **loopback** (Docker publishes to eth0), so the agent port,
the browser-view ranges and — with `gateway_sidecar` on — the sidecar's 4000 are refused; and it
has **no gate** (no first-byte wait, no cross-site check, unlike the bridge). Validated on save
(rows the save adds/edits; everything when newly on AWS or the sidecar is switched); a stored row
that would be refused is skipped with a warning at connect. Opens are serialised per project and
re-read the stored mappings under that lock; unchanged forwards are kept with their connections.
Opened after every connect and on a save while connected; closed by every `tunnels::close*`
path. A failing mapping is a `workspace-warning` (`topic: "ports"`). An idle listener makes no
agent request; a connection carrying traffic is activity (keeps the workspace from idling).
`agent.rs` reuses `docker/exec.rs`'s `OneshotOutput`, host-file reader and uid probe so the two
variants cannot drift; an `Agent` target's token never appears in `Debug` or error text.
**Container-interior code must go through `ContainerTarget`, never `docker::exec`, bollard or
+34 -11
View File
@@ -205,8 +205,8 @@ impl PortForward {
// bridge reports itself healthy — a silent failure with no
// thread back to this line.
log::warn!(
"Auth bridge: bound 127.0.0.1:{} but not [::1]:{} ({}) — continuing with IPv4 only; \
a client that resolves localhost to ::1 without falling back will not reach it",
"{}: bound 127.0.0.1:{} but not [::1]:{} ({}) — continuing with IPv4 only; \
a client that resolves localhost to ::1 without falling back will not reach it", gate.label(),
port,
port,
e
@@ -258,6 +258,16 @@ enum Gate {
None,
}
impl Gate {
/// The log prefix of a forward of this kind.
fn label(self) -> &'static str {
match self {
Gate::SignInCallback => "Auth bridge",
Gate::None => "Port mapping",
}
}
}
/// Accept on both loopback listeners until aborted. Dropping this future drops
/// the listeners (freeing the port) and the `JoinSet` (aborting live tunnels).
async fn accept_loop(
@@ -290,15 +300,15 @@ async fn accept_loop(
// client side is whatever can dial loopback, so a queue is
// just a slower way to run out of execs.
log::warn!(
"Auth bridge: refusing connection from {} to bridged port {} — \
{} concurrent connections already open on it",
"{}: refusing connection from {} to bridged port {} — \
{} concurrent connections already open on it", gate.label(),
peer,
port,
MAX_CONNECTIONS
);
continue;
}
log::debug!("Auth bridge: connection from {} to bridged port {}", peer, port);
log::debug!("{}: connection from {} to bridged port {}", gate.label(), peer, port);
let _ = stream.set_nodelay(true);
match gate {
Gate::SignInCallback => conns.spawn(tunnel_connection(
@@ -307,7 +317,8 @@ async fn accept_loop(
stream,
port,
)),
Gate::None => conns.spawn(tunnel_connection_with_prelude(
Gate::None => conns.spawn(tunnel_labelled(
gate.label(),
container.clone(),
target.clone(),
stream,
@@ -317,7 +328,7 @@ async fn accept_loop(
};
}
Err(e) => {
log::warn!("Auth bridge: accept failed on port {}: {} — stopping listener", port, e);
log::warn!("{}: accept failed on port {}: {} — stopping listener", gate.label(), port, e);
return;
}
}
@@ -574,6 +585,18 @@ pub async fn tunnel_connection_with_prelude(
stream: TcpStream,
port: u16,
prelude: Vec<u8>,
) {
tunnel_labelled("Auth bridge", container, target, stream, port, prelude).await
}
/// [`tunnel_connection_with_prelude`], logging under `label`.
async fn tunnel_labelled(
label: &'static str,
container: ContainerTarget,
target: String,
stream: TcpStream,
port: u16,
prelude: Vec<u8>,
) {
let cmd = vec!["socat".to_string(), "-".to_string(), target.clone()];
@@ -585,7 +608,7 @@ pub async fn tunnel_connection_with_prelude(
Ok(e) => e,
Err(e) => {
log::warn!(
"Auth bridge: failed to open tunnel exec for port {} ({}): {}",
"{}: failed to open tunnel exec for port {} ({}): {}", label,
port,
target,
e
@@ -633,7 +656,7 @@ pub async fn tunnel_connection_with_prelude(
Ok(chunk) => chunk,
Err(_) => {
log::debug!(
"Auth bridge: bridged port {} idle for {}s — closing the tunnel",
"{}: bridged port {} idle for {}s — closing the tunnel", label,
port,
IDLE_TIMEOUT.as_secs()
);
@@ -651,14 +674,14 @@ pub async fn tunnel_connection_with_prelude(
}
Ok(ExecChunk::StdErr(message)) => {
log::debug!(
"Auth bridge: socat stderr for port {}: {}",
"{}: socat stderr for port {}: {}", label,
port,
String::from_utf8_lossy(&message).trim()
);
}
Ok(_) => {}
Err(e) => {
log::debug!("Auth bridge: tunnel stream error on port {}: {}", port, e);
log::debug!("{}: tunnel stream error on port {}: {}", label, port, e);
break;
}
}
+26 -13
View File
@@ -1153,7 +1153,7 @@ pub async fn update_project(
// AWS (Q6): a connected workspace's forwards follow the saved mappings
// now — there is no container to recreate. Nothing when not connected.
if saved.is_aws() && mappings_changed {
if let Some(detail) = crate::host::port_forwards::open_for_project(&saved).await {
if let Some(detail) = crate::host::port_forwards::open_for_project(&state.projects_store, &saved.id).await {
emit_ports_warning(&app_handle, &saved.id, &saved.name, &detail);
}
}
@@ -1222,8 +1222,13 @@ fn validate_aws_update(
crate::models::validate_region(&place.region)?;
crate::aws::tofu_modules::validate_environment_name(&place.environment)?;
crate::models::validate_aws_workspace(cfg)?;
// Q6: forwarded over loopback, TCP only, never a reserved port.
crate::host::port_forwards::validate_aws_port_mappings(&project.port_mappings)?;
// Q6: forwarded over loopback, TCP only, never a reserved port
// (rows the save adds or edits; everything when newly on AWS).
crate::host::port_forwards::validate_aws_port_mappings_update(
stored.aws.as_ref().map(|a| (stored.port_mappings.as_slice(), a.gateway_sidecar)),
&project.port_mappings,
cfg.gateway_sidecar,
)?;
}
_ => {
return Err(
@@ -1507,7 +1512,7 @@ async fn start_project_container_locked(
// AWS (Q6): the project's port mappings become loopback forwards into
// the workspace; a mapping that can't open is a warning, the rest work.
if project.is_aws() {
if let Some(detail) = crate::host::port_forwards::open_for_project(&project).await {
if let Some(detail) = crate::host::port_forwards::open_for_project(&state.projects_store, &project_id).await {
emit_ports_warning(&app_handle, &project_id, &project.name, &detail);
}
}
@@ -3111,26 +3116,32 @@ mod tests {
}
#[test]
fn an_aws_project_refuses_udp_and_reserved_mappings_and_a_local_one_does_not() {
fn an_aws_project_refuses_new_udp_and_reserved_mappings_and_a_local_one_does_not() {
let hosts = vec![host("h1")];
let udp = crate::models::PortMapping {
host_port: 5353,
container_port: 5353,
protocol: "udp".into(),
};
let stored = aws_project();
let mut aws = aws_project();
aws.port_mappings = vec![udp.clone()];
let e = validate_aws_update(&aws, &aws, &hosts).unwrap_err();
let e = validate_aws_update(&stored, &aws, &hosts).unwrap_err();
assert!(e.contains("only TCP"), "{e}");
// A stored UDP row does not block an unrelated save (it is skipped,
// with a warning, at connect).
assert!(validate_aws_update(&aws, &aws, &hosts).is_ok());
// Moving a local project with one to AWS checks everything.
let (mut local, _) = stored_and_stale_payload();
local.port_mappings = vec![udp.clone()];
assert!(validate_aws_update(&local, &aws, &hosts).unwrap_err().contains("only TCP"));
aws.port_mappings[0].container_port = crate::host::ssm::AGENT_PORT;
aws.port_mappings[0].protocol = "tcp".into();
assert!(validate_aws_update(&aws, &aws, &hosts).is_err());
assert!(validate_aws_update(&stored, &aws, &hosts).is_err());
aws.port_mappings[0].container_port = 3000;
assert!(validate_aws_update(&aws, &aws, &hosts).is_ok());
assert!(validate_aws_update(&stored, &aws, &hosts).is_ok());
// Docker publishes UDP itself: a local project is not checked.
let (mut local, _) = stored_and_stale_payload();
local.port_mappings = vec![udp];
assert!(validate_aws_update(&local, &local, &hosts).is_ok());
}
@@ -3141,19 +3152,21 @@ mod tests {
let src = include_str!("project_commands.rs");
let start = fn_body(src, "start_project_container_locked");
let running = start.find("update_status(&project_id, ProjectStatus::Running)").unwrap();
let open = start.find("port_forwards::open_for_project(&project)").expect("the start opens them");
let open = start
.find("port_forwards::open_for_project(&state.projects_store, &project_id)")
.expect("the start opens them");
assert!(open > running);
let guard = start[..open].rfind("if project.is_aws() {").expect("AWS-only");
assert!(start[guard..open].lines().count() <= 2);
assert!(start[open..].lines().nth(1).unwrap().contains("emit_ports_warning("));
let update = fn_body(src, "update_project");
assert!(update.contains("port_forwards::open_for_project(&saved)"));
assert!(update.contains("port_forwards::open_for_project(&state.projects_store, &saved.id)"));
let reconcile = fn_body(src, "reconcile_aws_projects");
assert!(reconcile.contains("on_ports_warning,"));
assert!(reconcile.contains("emit_ports_warning(&ports_handle, &ports_id, &ports_name, &detail)"));
let aws_host = include_str!("../host/aws_host.rs");
let reattached = aws_host.find("Ok(true) => {").unwrap();
let opened = aws_host.find("port_forwards::open_for_project(&project)").unwrap();
let opened = aws_host.find("port_forwards::open_for_project(&store, &project.id)").unwrap();
let pushed = aws_host[reattached..].find("connect::apply_connect(").unwrap() + reattached;
assert!(reattached < opened && opened < pushed);
}
+1 -1
View File
@@ -1064,7 +1064,7 @@ pub async fn reconcile(
Ok(true) => {
// Q6: the port mappings' forwards, before the
// (slower) credential copy.
if let Some(detail) = super::port_forwards::open_for_project(&project).await {
if let Some(detail) = super::port_forwards::open_for_project(&store, &project.id).await {
on_ports_warning(detail);
}
if let Err(e) = connect::apply_connect(&project, &settings).await {
+1 -1
View File
@@ -30,9 +30,9 @@
pub mod agent;
pub mod aws_host;
pub mod lifecycle;
pub mod port_forwards;
pub mod sessions;
pub mod ssm;
pub mod port_forwards;
pub mod tunnels;
pub mod watch;
+239 -90
View File
@@ -1,31 +1,56 @@
//! A project's port mappings on a host with no Docker port publishing (AWS).
//!
//! Docker publishes a mapping itself (`0.0.0.0:<host>` → the container). A
//! Fargate task has no such thing, so each TCP mapping becomes a host
//! **loopback** listener (`127.0.0.1` and `::1`, never a wildcard) carried
//! into the task over `socat` on an attached exec — the auth bridge's
//! [`PortForward`], which works through the agent like any other exec
//! (constraints Q6). Nothing is published beyond this machine, and the UI
//! says so.
//! Docker publishes a mapping itself (`0.0.0.0:<host>` → the container's
//! network interface). A Fargate task has no such thing, so each TCP mapping
//! becomes a host **loopback** listener (`127.0.0.1` and `::1`, never a
//! wildcard) carried into the task over `socat` on an attached exec — the
//! auth bridge's [`PortForward`], which works through the agent like any
//! other exec (constraints Q6). Nothing is published beyond this machine,
//! and the UI says so.
//!
//! ## Two differences from Docker, both deliberate
//!
//! * **It reaches the workspace's loopback, not its network interface**
//! (`socat … TCP:127.0.0.1:<port>` inside the task). So a mapping can
//! reach a service that bound loopback precisely to be private — which is
//! why the agent's port, the browser view's range and (when the project
//! runs it) the gateway sidecar's [`GATEWAY_SIDECAR_PORT`] are refused.
//! * **No gate** ([`PortForward::bind_mapping`]): unlike the auth bridge, a
//! client need not speak first and an HTTP request is not checked for a
//! cross-site origin — Docker's publishing has neither, and a
//! server-speaks-first protocol (MySQL, SMTP) must work. What a mapping
//! exposes is what the user chose to expose, to this machine only.
//!
//! The tunnel's bounds still apply: 16 concurrent connections per mapping,
//! and a connection the workspace sends nothing on for 600 s is closed.
//!
//! ## Lifetime
//!
//! One set per project, opened after every connect (the start, and the
//! background re-attach after an app restart) and replacing the set before
//! it. It is closed with the project's tunnel — `tunnels::close`,
//! `close_if_task` (the task died) and `close_all` (app exit) all call
//! [`close`] / [`close_all`] — so Stop, Remove, a task death and exit need
//! nothing of their own.
//! background re-attach after an app restart) and on a save of the mappings
//! while connected. Opens are serialised per project and re-read the stored
//! mappings under that lock, so the last save wins; an open keeps the
//! forwards it already has for an unchanged mapping (with their live
//! connections) and rebinds everything when the tunnel changed. The set is
//! closed with the project's tunnel — `tunnels::close`, `close_if_task` (the
//! task died) and `close_all` (app exit) call [`close`] / [`close_all`] — so
//! Stop, Remove, a task death and exit need nothing of their own.
//!
//! ## Idle stop
//!
//! An open listener costs the workspace nothing: no agent request is made
//! until a client connects, so it never counts as activity for the idle
//! stop. A connection is an attach, which counts only while bytes flow.
//! until a client connects. A connection is an attach, which counts as
//! activity while bytes flow — a client using a mapping keeps the workspace
//! from its idle stop; one connected but silent does not.
use std::collections::{HashMap, HashSet};
use std::sync::Mutex;
use std::sync::{Arc, Mutex};
use crate::auth_bridge::proc_net::PortFamily;
use crate::auth_bridge::tunnel::PortForward;
use crate::auth_bridge::{RESERVED_CONTAINER_PORTS, RESERVED_HOST_PORTS};
use crate::models::PortMapping;
use crate::storage::projects_store::ProjectsStore;
use super::ContainerTarget;
@@ -36,19 +61,43 @@ pub struct PortForwardResult {
pub error: Option<String>,
}
fn registry() -> &'static Mutex<HashMap<String, Vec<PortForward>>> {
static R: std::sync::OnceLock<Mutex<HashMap<String, Vec<PortForward>>>> =
std::sync::OnceLock::new();
/// The container port the gateway sidecar (Task 8, `gateway_sidecar`)
/// listens on, on the workspace's loopback.
pub const GATEWAY_SIDECAR_PORT: u16 = 4000;
/// A project's open forwards and the target (tunnel endpoint) they lead to.
struct OpenSet {
target_id: String,
forwards: Vec<PortForward>,
}
type Registry = HashMap<String, OpenSet>;
fn registry() -> &'static Mutex<Registry> {
static R: std::sync::OnceLock<Mutex<Registry>> = std::sync::OnceLock::new();
R.get_or_init(Default::default)
}
fn lock() -> std::sync::MutexGuard<'static, HashMap<String, Vec<PortForward>>> {
fn lock() -> std::sync::MutexGuard<'static, Registry> {
registry().lock().unwrap_or_else(|e| e.into_inner())
}
/// Why one mapping can't be forwarded to an AWS workspace, if it can't —
/// regardless of what else is configured. `None` when it is fine.
fn mapping_problem(m: &PortMapping) -> Option<String> {
/// The per-project lock every open and close holds, so two opens (a save on
/// every blur, a save during a start's open) never interleave.
fn project_lock(project_id: &str) -> Arc<tokio::sync::Mutex<()>> {
static L: std::sync::OnceLock<Mutex<HashMap<String, Arc<tokio::sync::Mutex<()>>>>> =
std::sync::OnceLock::new();
L.get_or_init(Default::default)
.lock()
.unwrap_or_else(|e| e.into_inner())
.entry(project_id.to_string())
.or_default()
.clone()
}
/// Why one mapping can't be forwarded to an AWS workspace, if it can't.
/// `None` when it is fine.
fn mapping_problem(m: &PortMapping, gateway_sidecar: bool) -> Option<String> {
if !m.protocol.eq_ignore_ascii_case("tcp") {
return Some(format!(
"{} → {} ({}): only TCP ports can be forwarded to an AWS workspace",
@@ -71,6 +120,12 @@ fn mapping_problem(m: &PortMapping) -> Option<String> {
super::ssm::AGENT_PORT
));
}
if gateway_sidecar && m.container_port == GATEWAY_SIDECAR_PORT {
return Some(format!(
"Container port {GATEWAY_SIDECAR_PORT} is the gateway sidecar's, which holds the \
model provider's key"
));
}
if RESERVED_HOST_PORTS.contains(&m.host_port) {
return Some(format!(
"Host port {} is reserved for the browser view ({}–{})",
@@ -88,20 +143,54 @@ fn incomplete(m: &PortMapping) -> bool {
m.host_port == 0 || m.container_port == 0
}
/// An AWS project's port mappings, checked on save: TCP only, no reserved
/// port (the browser view's two ranges, the agent's port), and no host port
/// twice. The first problem, worded for the user.
pub fn validate_aws_port_mappings(mappings: &[PortMapping]) -> Result<(), String> {
let mut seen = HashSet::new();
for m in mappings {
if incomplete(m) {
continue;
/// An AWS project's port mappings, checked in full: TCP only, no reserved
/// port (the browser view's two ranges, the agent's port, the gateway
/// sidecar's when it runs), and no host port twice. The first problem,
/// worded for the user.
pub fn validate_aws_port_mappings(mappings: &[PortMapping], gateway_sidecar: bool) -> Result<(), String> {
validate_changed(mappings, gateway_sidecar, |_| true)
}
/// [`validate_aws_port_mappings`] on a save, against what is stored:
/// `stored` is `None` when the project was not on AWS before (everything is
/// checked). Otherwise only rows the save adds or edits are — a stored row
/// this check would refuse (a UDP mapping kept from before) does not block
/// every unrelated save; it is skipped, with a warning, at each connect.
/// Turning the gateway sidecar on checks everything again.
pub fn validate_aws_port_mappings_update(
stored: Option<(&[PortMapping], bool)>,
mappings: &[PortMapping],
gateway_sidecar: bool,
) -> Result<(), String> {
match stored {
Some((old, old_gateway)) if old_gateway == gateway_sidecar => {
validate_changed(mappings, gateway_sidecar, |m| !old.contains(m))
}
if let Some(problem) = mapping_problem(m) {
return Err(problem);
_ => validate_aws_port_mappings(mappings, gateway_sidecar),
}
}
fn validate_changed(
mappings: &[PortMapping],
gateway_sidecar: bool,
changed: impl Fn(&PortMapping) -> bool,
) -> Result<(), String> {
let mut seen: HashMap<u16, bool> = HashMap::new();
for m in mappings.iter().filter(|m| !incomplete(m)) {
let is_changed = changed(m);
if is_changed {
if let Some(problem) = mapping_problem(m, gateway_sidecar) {
return Err(problem);
}
}
if !seen.insert(m.host_port) {
return Err(format!("Host port {} is mapped twice", m.host_port));
match seen.get(&m.host_port) {
Some(&other_changed) if is_changed || other_changed => {
return Err(format!("Host port {} is mapped twice", m.host_port))
}
Some(_) => {}
None => {
seen.insert(m.host_port, is_changed);
}
}
}
Ok(())
@@ -117,28 +206,47 @@ fn still_connected(project_id: &str, target: &ContainerTarget) -> bool {
}
}
/// Open `mappings` for `project_id` into `target` (its connected workspace),
/// replacing the set it had. One result per mapping, in order: a busy host
/// port, a host port mapped twice or a refused mapping is an error for that
/// mapping only, and the others still open.
///
/// If the project's tunnel is no longer `target` once they are bound (a Stop
/// or a newer connect raced this), nothing is kept.
/// Open `mappings` for `project_id` into `target` (its connected workspace).
/// See [`open_latest`]; the gateway sidecar is taken as off. Test helper.
#[cfg(test)]
pub async fn open_for(
project_id: &str,
target: &ContainerTarget,
mappings: &[PortMapping],
) -> Vec<PortForwardResult> {
// The old set first, so a re-attach can bind the same host ports again.
close(project_id).await;
let mappings = mappings.to_vec();
open_latest(project_id, target, move || Some((mappings, false))).await
}
let mut opened = Vec::new();
let mut results = Vec::new();
/// Open the project's mappings into `target`, replacing the set it had.
/// `read` gives the mappings and whether the gateway sidecar runs, and is
/// called **under the project's lock** — so the last of several overlapping
/// opens applies what is stored last. `None` from it closes the set.
///
/// One result per mapping, in order: a refused mapping, a host port mapped
/// twice or a busy host port is an error for that mapping only, and the
/// others still open. A mapping already open into the same target is kept,
/// live connections and all. If the project's tunnel is no longer `target`
/// once they are bound (a Stop or a newer connect raced this), nothing is
/// kept.
pub async fn open_latest(
project_id: &str,
target: &ContainerTarget,
read: impl FnOnce() -> Option<(Vec<PortMapping>, bool)> + Send,
) -> Vec<PortForwardResult> {
let plock = project_lock(project_id);
let _held = plock.lock().await;
let Some((mappings, gateway_sidecar)) = read() else {
close_unlocked(project_id).await;
return Vec::new();
};
// What can be forwarded, in order; the first row wins a host port.
let mut results: Vec<PortForwardResult> = Vec::new();
let mut wanted: Vec<(usize, u16, u16)> = Vec::new();
let mut taken = HashSet::new();
for m in mappings {
let error = if incomplete(m) {
continue;
} else if let Some(problem) = mapping_problem(m) {
for m in mappings.iter().filter(|m| !incomplete(m)) {
let error = if let Some(problem) = mapping_problem(m, gateway_sidecar) {
Some(problem)
} else if !taken.insert(m.host_port) {
Some(format!(
@@ -146,40 +254,66 @@ pub async fn open_for(
m.host_port, m.container_port, m.host_port
))
} else {
match PortForward::bind_mapping(target.clone(), m.host_port, m.container_port, PortFamily::V4)
.await
{
Ok(fwd) => {
if let Some(w) = &fwd.ipv6_warning {
log::warn!("Project {project_id}: port mapping {}: {w}", m.host_port);
}
log::info!(
"Project {project_id}: forwarding localhost:{} → workspace port {}",
m.host_port,
m.container_port
);
opened.push(fwd);
None
}
Err(e) if e.kind() == std::io::ErrorKind::AddrInUse => Some(format!(
"{} → {}: host port {} is already in use on this machine",
m.host_port, m.container_port, m.host_port
)),
Err(e) => Some(format!(
"{} → {}: could not listen on host port {}: {e}",
m.host_port, m.container_port, m.host_port
)),
}
wanted.push((results.len(), m.host_port, m.container_port));
None
};
results.push(PortForwardResult { mapping: m.clone(), error });
}
let previous = lock().insert(project_id.to_string(), opened);
shutdown_all(previous.unwrap_or_default()).await;
// Keep what is already open into this target and still wanted; free
// every other host port first, so a changed mapping can rebind it.
let previous = lock().remove(project_id);
let mut kept: HashMap<(u16, u16), PortForward> = HashMap::new();
let mut dropped = Vec::new();
if let Some(prev) = previous {
let same_target = prev.target_id == target.id();
for f in prev.forwards {
let key = (f.port, f.container_port);
if same_target && wanted.iter().any(|&(_, h, c)| (h, c) == key) {
kept.insert(key, f);
} else {
dropped.push(f);
}
}
}
shutdown_all(dropped).await;
let mut opened = Vec::new();
for (i, host_port, container_port) in wanted {
if let Some(f) = kept.remove(&(host_port, container_port)) {
opened.push(f);
continue;
}
match PortForward::bind_mapping(target.clone(), host_port, container_port, PortFamily::V4).await {
Ok(fwd) => {
if let Some(w) = &fwd.ipv6_warning {
log::warn!("Project {project_id}: port mapping {host_port}: {w}");
}
log::info!(
"Project {project_id}: forwarding localhost:{host_port} → workspace port {container_port}"
);
opened.push(fwd);
}
Err(e) => {
results[i].error = Some(if e.kind() == std::io::ErrorKind::AddrInUse {
format!(
"{host_port} → {container_port}: host port {host_port} is already in use on this machine"
)
} else {
format!("{host_port} → {container_port}: could not listen on host port {host_port}: {e}")
});
}
}
}
lock().insert(
project_id.to_string(),
OpenSet { target_id: target.id().to_string(), forwards: opened },
);
// Checked *after* the set is registered: a tunnel close that ran before
// this sees it gone here; one that runs after removes it itself.
if !still_connected(project_id, target) {
close(project_id).await;
close_unlocked(project_id).await;
for r in results.iter_mut().filter(|r| r.error.is_none()) {
r.error = Some("the workspace disconnected".into());
}
@@ -193,32 +327,44 @@ async fn shutdown_all(set: Vec<PortForward>) {
}
/// Close the project's forwards (its ports are free when this returns).
/// Waits for an open in progress.
pub async fn close(project_id: &str) {
let plock = project_lock(project_id);
let _held = plock.lock().await;
close_unlocked(project_id).await;
}
async fn close_unlocked(project_id: &str) {
let set = lock().remove(project_id);
if let Some(set) = set {
shutdown_all(set).await;
shutdown_all(set.forwards).await;
}
}
/// Close every project's forwards. The shutdown path's step (via
/// `tunnels::close_all`).
/// `tunnels::close_all`). An open in progress finds the tunnel gone and
/// closes its own set.
pub async fn close_all() {
let all: Vec<PortForward> = lock().drain().flat_map(|(_, v)| v).collect();
let all: Vec<PortForward> = lock().drain().flat_map(|(_, v)| v.forwards).collect();
shutdown_all(all).await;
}
/// After a connect (or a save of the mappings while connected): open an
/// AWS project's mappings into its connected workspace, replacing the set it
/// had. The warning detail for the mappings that failed, if any did; `None`
/// as well for a local project (Docker publishes its own) or a workspace
/// that is not (or no longer) connected — nothing to warn about there.
pub async fn open_for_project(project: &crate::models::Project) -> Option<String> {
if !project.is_aws() {
return None;
}
/// AWS project's mappings, **as stored when the open runs**, into its
/// connected workspace. The warning detail for the mappings that failed, if
/// any did; `None` as well for a local project (Docker publishes its own) or
/// a workspace that is not (or no longer) connected.
pub async fn open_for_project(store: &ProjectsStore, project_id: &str) -> Option<String> {
let project = store.get(project_id).filter(|p| p.is_aws())?;
let target = ContainerTarget::Agent(super::tunnels::get_endpoint(&project.id)?);
let results = open_for(&project.id, &target, &project.port_mappings).await;
if !still_connected(&project.id, &target) {
let results = open_latest(project_id, &target, || {
store.get(project_id).filter(|p| p.is_aws()).map(|p| {
let gateway = p.aws.as_ref().is_some_and(|a| a.gateway_sidecar);
(p.port_mappings, gateway)
})
})
.await;
if !still_connected(project_id, &target) {
return None;
}
failure_detail(&results)
@@ -227,7 +373,10 @@ pub async fn open_for_project(project: &crate::models::Project) -> Option<String
/// The host ports open for the project.
#[cfg(test)]
pub fn open_ports(project_id: &str) -> Vec<u16> {
lock().get(project_id).map(|v| v.iter().map(|f| f.port).collect()).unwrap_or_default()
let mut ports: Vec<u16> =
lock().get(project_id).map(|v| v.forwards.iter().map(|f| f.port).collect()).unwrap_or_default();
ports.sort_unstable();
ports
}
/// The `workspace-warning` detail for the mappings that failed, if any did.
+132 -6
View File
@@ -3,6 +3,7 @@
//! asked to run is recorded, never run.
use super::*;
use crate::storage::projects_store::ProjectsStore;
use crate::host::agent::AgentEndpoint;
use crate::host::tunnels;
use axum::extract::ws::{Message as WsMessage, WebSocket, WebSocketUpgrade};
@@ -243,14 +244,139 @@ async fn a_task_death_closes_the_mappings_but_a_stale_report_does_not() {
#[test]
fn an_aws_project_saves_only_tcp_unreserved_unique_mappings() {
assert!(validate_aws_port_mappings(&[tcp(3000, 3000), tcp(8081, 8080), tcp(0, 0), tcp(5000, 0)]).is_ok());
let v = |m: &[PortMapping]| validate_aws_port_mappings(m, false);
assert!(v(&[tcp(3000, 3000), tcp(8081, 8080), tcp(0, 0), tcp(5000, 0), tcp(4001, 4000)]).is_ok());
let udp = PortMapping { host_port: 5353, container_port: 5353, protocol: "udp".into() };
let e = validate_aws_port_mappings(&[udp]).unwrap_err();
let e = v(&[udp]).unwrap_err();
assert!(e.contains("only TCP") && e.contains("UDP"), "{e}");
assert!(validate_aws_port_mappings(&[tcp(3000, 3000), tcp(3000, 4000)]).unwrap_err().contains("mapped twice"));
assert!(validate_aws_port_mappings(&[tcp(4000, 39321)]).is_err());
assert!(validate_aws_port_mappings(&[tcp(4000, 47900)]).is_err());
assert!(validate_aws_port_mappings(&[tcp(47820, 4000)]).is_err());
assert!(v(&[tcp(3000, 3000), tcp(3000, 4000)]).unwrap_err().contains("mapped twice"));
assert!(v(&[tcp(4000, 39321)]).is_err());
assert!(v(&[tcp(4000, 47900)]).is_err());
assert!(v(&[tcp(47820, 4000)]).is_err());
}
#[test]
fn the_gateway_sidecars_port_is_refused_while_the_project_runs_it() {
// A mapping reaches the workspace's *loopback*, where the sidecar holds
// the provider key behind 127.0.0.1:4000.
let m = [tcp(4001, GATEWAY_SIDECAR_PORT)];
assert!(validate_aws_port_mappings(&m, false).is_ok());
let e = validate_aws_port_mappings(&m, true).unwrap_err();
assert!(e.contains("gateway sidecar"), "{e}");
// Turning the sidecar on re-checks rows that were already stored.
assert!(validate_aws_port_mappings_update(Some((&m, false)), &m, true).is_err());
assert!(validate_aws_port_mappings_update(Some((&m, false)), &m, false).is_ok());
}
#[test]
fn a_stored_row_that_would_be_refused_does_not_block_other_saves() {
let udp = PortMapping { host_port: 5353, container_port: 5353, protocol: "udp".into() };
let stored = vec![udp.clone(), tcp(3000, 3000)];
// Unchanged, plus a new valid row: fine.
let saved = vec![udp.clone(), tcp(3000, 3000), tcp(8080, 8080)];
assert!(validate_aws_port_mappings_update(Some((&stored, false)), &saved, false).is_ok());
// A new UDP row, or an edit to one, is refused.
let edited = PortMapping { host_port: 5354, ..udp.clone() };
let e = validate_aws_port_mappings_update(Some((&stored, false)), &[edited, tcp(3000, 3000)], false);
assert!(e.unwrap_err().contains("only TCP"));
// A new row taking a stored row's host port is refused.
let dup = vec![udp.clone(), tcp(3000, 3000), tcp(3000, 4000)];
assert!(validate_aws_port_mappings_update(Some((&stored, false)), &dup, false)
.unwrap_err()
.contains("mapped twice"));
// Newly on AWS: everything is checked.
assert!(validate_aws_port_mappings_update(None, &stored, false).is_err());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn overlapping_opens_converge_on_the_last_stored_set_without_false_errors() {
// The editor saves on every blur; each save opens. Each open re-reads
// the stored mappings under the project's lock, so overlapping opens
// neither fight over a host port nor leave an older save's set behind.
let p = pid("concurrent");
let (target, _, _) = connected_agent(&p).await;
let (a, b, c) = (free_port(), free_port(), free_port());
let sets = [
vec![tcp(a, 8080), tcp(b, 8081)],
vec![tcp(b, 8081), tcp(c, 8082)],
vec![tcp(a, 8085), tcp(c, 8082)],
vec![tcp(b, 8090), tcp(c, 8082)],
];
let stored = Arc::new(std::sync::Mutex::new(sets[0].clone()));
let go = Arc::new(tokio::sync::Barrier::new(8));
let mut opens = Vec::new();
for i in 0..8 {
let (p2, t2, s2, go2) = (p.clone(), target.clone(), stored.clone(), go.clone());
let mine = sets[i % sets.len()].clone();
opens.push(tokio::spawn(async move {
go2.wait().await;
open_latest(&p2, &t2, move || {
let mut s = s2.lock().unwrap();
*s = mine; // this save lands, then its open reads the store
Some((s.clone(), false))
})
.await
}));
}
for o in opens {
let results = o.await.unwrap();
assert!(results.iter().all(|r| r.error.is_none()), "{results:?}");
}
let last = stored.lock().unwrap().clone();
let mut want: Vec<u16> = last.iter().map(|m| m.host_port).collect();
want.sort_unstable();
assert_eq!(open_ports(&p), want, "the registry holds the last stored set");
tunnels::close(&p).await;
}
#[tokio::test]
async fn a_save_keeps_unchanged_forwards_and_their_live_connections() {
let p = pid("diff");
let (target, fake, _) = connected_agent(&p).await;
let (a, b) = (free_port(), free_port());
open_for(&p, &target, &[tcp(a, 8080)]).await;
let mut client = TcpStream::connect(("127.0.0.1", a)).await.unwrap();
client.write_all(b"one").await.unwrap();
let mut buf = [0u8; 3];
client.read_exact(&mut buf).await.unwrap();
let r = open_for(&p, &target, &[tcp(a, 8080), tcp(b, 8081)]).await;
assert!(r.iter().all(|r| r.error.is_none()), "{r:?}");
// The same connection still carries bytes: its forward was kept.
client.write_all(b"two").await.unwrap();
client.read_exact(&mut buf).await.unwrap();
assert_eq!(&buf, b"two");
assert_eq!(fake.cmds.lock().unwrap().len(), 1, "no new attach for the kept connection");
// A changed container port rebinds the same host port.
let r = open_for(&p, &target, &[tcp(a, 9090)]).await;
assert!(r[0].error.is_none(), "{:?}", r[0].error);
assert_eq!(round_trip(a, b"x").await, b"x");
assert_eq!(fake.cmds.lock().unwrap().last().unwrap()[2], "TCP:127.0.0.1:9090");
assert!(port_is_free(b));
tunnels::close(&p).await;
}
#[tokio::test]
async fn a_stored_project_is_read_when_the_open_runs() {
let p = pid("store");
let dir = tempfile::tempdir().unwrap();
let mut project = crate::models::Project::new("x".into(), vec![]);
project.id = p.clone();
project.host_id = Some("h1".into());
project.aws_placement =
Some(crate::models::AwsPlacement { region: "us-west-2".into(), environment: "dev".into() });
project.aws = Some(Default::default());
let (port, busy) = (free_port(), std::net::TcpListener::bind("127.0.0.1:0").unwrap());
let busy_port = busy.local_addr().unwrap().port();
project.port_mappings = vec![tcp(port, 8080), tcp(busy_port, 8081)];
let store = ProjectsStore::over_file_for_test(dir.path().join("projects.json"), vec![project]);
assert_eq!(open_for_project(&store, &p).await, None, "not connected: nothing, no warning");
let (_target, _, _) = connected_agent(&p).await;
let warning = open_for_project(&store, &p).await.expect("the busy port is reported");
assert!(warning.contains(&busy_port.to_string()) && warning.contains("already in use"), "{warning}");
assert_eq!(open_ports(&p), vec![port]);
tunnels::close(&p).await;
}
#[tokio::test]
@@ -164,8 +164,9 @@ export default function AccessSection({
</span>
{isAwsProject(project) ? (
<p className="mt-0.5 mb-2 text-xs text-[var(--text-secondary)] leading-snug">
Forward workspace ports to this machine while it is connected. TCP only.{" "}
{AWS_PORTS_LOCALHOST_ONLY}.
Forward workspace ports to this machine while it is connected. TCP only. A
connection carrying traffic counts as activity, so it keeps the workspace from its
idle stop. {AWS_PORTS_LOCALHOST_ONLY}.
</p>
) : (
<p className="mt-0.5 mb-2 text-xs text-[var(--text-secondary)] leading-snug">