feat: add reverse ssh sandbox targets

A sandbox behind NAT could not reach the room. A reverse target dials a tunnel-only sshd on the server, forwards localhost:22 to a loopback port, and the worker connects there.

The server assigns the port and writes one authorized_keys line per tunnel target. The key carries permitlisten for that port; the sshd allows remote forwarding only, with no shell or local forwarding. The tunnel private key is shown once and never stored; rotating it rewrites the file. The target list and setup dialog carry the command, and the docs include the sshd config and sidecar requirements.
This commit is contained in:
vhaudiquet committed 2026-11-29 19:25:14 +01:00
1 parent fc684115b5
commit d85f68a176
20 files changed
+893 -38

No files matched your search

+87
View File
@@ -26,6 +26,7 @@ impl Db {
conn.pragma_update(None, "foreign_keys", "ON")?;
conn.busy_timeout(std::time::Duration::from_secs(5))?;
conn.execute_batch(SCHEMA)?;
migrate(&conn)?;
Ok(Self {
conn: Arc::new(Mutex::new(conn)),
})
@@ -47,6 +48,43 @@ impl Db {
}
}
/// Adds columns that arrived after the first release. `CREATE TABLE IF
/// NOT EXISTS` leaves an existing table alone, so each column is checked
/// before the ALTER.
fn migrate(conn: &Connection) -> rusqlite::Result<()> {
if !has_column(conn, "sandbox_targets", "kind")? {
conn.execute(
"ALTER TABLE sandbox_targets ADD COLUMN kind TEXT NOT NULL DEFAULT 'direct'",
[],
)?;
}
if !has_column(conn, "sandbox_targets", "tunnel_public_key")? {
conn.execute(
"ALTER TABLE sandbox_targets ADD COLUMN tunnel_public_key TEXT",
[],
)?;
}
// A tunnel target owns one loopback port. Direct targets may share a
// port, so the index is partial.
conn.execute_batch(
"CREATE UNIQUE INDEX IF NOT EXISTS sandbox_tunnel_port
ON sandbox_targets(port) WHERE kind = 'tunnel';",
)?;
Ok(())
}
fn has_column(conn: &Connection, table: &str, column: &str) -> rusqlite::Result<bool> {
let mut stmt = conn.prepare(&format!("PRAGMA table_info({table})"))?;
let mut rows = stmt.query([])?;
while let Some(row) = rows.next()? {
let name: String = row.get(1)?;
if name == column {
return Ok(true);
}
}
Ok(false)
}
const SCHEMA: &str = r#"
CREATE TABLE IF NOT EXISTS meta (
key TEXT PRIMARY KEY,
@@ -100,6 +138,8 @@ CREATE TABLE IF NOT EXISTS sandbox_targets (
private_key_ciphertext TEXT NOT NULL,
public_key TEXT NOT NULL,
host_fingerprint TEXT,
kind TEXT NOT NULL DEFAULT 'direct',
tunnel_public_key TEXT,
created_at INTEGER NOT NULL
) STRICT;
@@ -165,3 +205,50 @@ CREATE INDEX IF NOT EXISTS members_room ON members(room_id);
CREATE INDEX IF NOT EXISTS events_room_seq ON events(room_id, seq);
CREATE INDEX IF NOT EXISTS auth_sessions_account ON auth_sessions(account_id);
"#;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_old_target_table_gains_the_tunnel_columns() {
let dir =
std::env::temp_dir().join(format!("rhema-db-migrate-{}", crate::util::random_token(6)));
std::fs::create_dir_all(&dir).expect("temp dir");
let path = dir.join("old.db");
{
let conn = Connection::open(&path).expect("old db");
conn.execute_batch(
"CREATE TABLE sandbox_targets (
id TEXT PRIMARY KEY,
account_id TEXT NOT NULL,
name TEXT NOT NULL,
host TEXT NOT NULL,
port INTEGER NOT NULL DEFAULT 22,
user_name TEXT NOT NULL,
workspace_root TEXT NOT NULL,
private_key_ciphertext TEXT NOT NULL,
public_key TEXT NOT NULL,
host_fingerprint TEXT,
created_at INTEGER NOT NULL
) STRICT;",
)
.expect("old schema");
}
let db = Db::open(&path).expect("open");
let conn = db.conn();
let mut stmt = conn
.prepare("PRAGMA table_info(sandbox_targets)")
.expect("pragma");
let columns: Vec<String> = stmt
.query_map([], |row| row.get(1))
.expect("rows")
.collect::<Result<_, _>>()
.expect("columns");
assert!(columns.iter().any(|column| column == "kind"));
assert!(columns.iter().any(|column| column == "tunnel_public_key"));
drop(stmt);
drop(conn);
std::fs::remove_dir_all(dir).ok();
}
}