Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/openshell-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ futures = { workspace = true }
openshell-server = { path = "../openshell-server", features = ["test-support"] }
rcgen = { workspace = true }
reqwest = { workspace = true }
russh = "0.65.0"
serde_json = { workspace = true }
temp-env = "0.3"
tokio-stream = { workspace = true }
Expand Down
67 changes: 60 additions & 7 deletions crates/openshell-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use openshell_cli::completers;
use openshell_cli::run;
use openshell_cli::tls::TlsOptions;
use openshell_core::proto::{GpuResourceRequirements, ServiceAuthorizationMode};
use openshell_core::ssh_host_key::{HostKeyCheck, HostKeyExpectation, KnownHostsPin};

/// Resolved gateway context: name + gateway endpoint.
struct GatewayContext {
Expand Down Expand Up @@ -664,10 +665,12 @@ enum Commands {
/// Two mutually exclusive modes:
///
/// **Token mode** (used internally by `sandbox connect`):
/// `openshell ssh-proxy --gateway <url> --sandbox <name> --token <token>`
/// `openshell ssh-proxy --gateway <url> --sandbox <name> --token <token>
/// --host-key <fingerprint> --known-hosts <file>`
///
/// **Name mode** (for use in `~/.ssh/config`):
/// `openshell ssh-proxy --gateway <name> --name <sandbox-name>`
/// `openshell ssh-proxy --gateway <name> --name <sandbox-name>
/// --known-hosts <file>`
#[command(hide = true, help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
SshProxy {
/// Gateway URL (e.g., <https://gw.example.com:443/proxy/connect>).
Expand All @@ -694,6 +697,17 @@ enum Commands {
/// Sandbox name. Used in name mode.
#[arg(long)]
name: Option<String>,

/// SSH host key the sandbox must present: the fingerprint the gateway
/// published, or `unverified`. Required in token mode; name mode asks
/// the gateway instead.
#[arg(long, requires = "known_hosts", conflicts_with = "name")]
host_key: Option<HostKeyExpectation>,

/// Known-hosts file in which the verified host key is pinned for `ssh`.
/// Required in both modes.
#[arg(long)]
known_hosts: Option<PathBuf>,
},
}

Expand Down Expand Up @@ -3723,7 +3737,7 @@ async fn run_async() -> Result<()> {
}
SandboxCommands::SshConfig { name } => {
let name = resolve_sandbox_name(name, &ctx.name, &cli.workspace)?;
run::print_ssh_config(&ctx.name, &name, &cli.workspace);
run::print_ssh_config(&ctx.name, &name, &cli.workspace)?;
}
SandboxCommands::Provider(command) => match command {
SandboxProviderCommands::List {
Expand Down Expand Up @@ -4179,19 +4193,38 @@ async fn run_async() -> Result<()> {
server,
gateway_name,
name,
host_key,
known_hosts,
}) => {
match (gateway, sandbox, token, server, gateway_name, name) {
// Token mode (existing behavior): pre-created session credentials.
(Some(gw), Some(sandbox), Some(tok), _, gateway_name_opt, _) => {
let (Some(expectation), Some(known_hosts)) = (host_key, known_hosts) else {
eprintln!(
"openshell: this SSH connection was started by an OpenShell client that predates host-key verification and is refused.\n\
Restart that client (the openshell command or openshell term) and connect again."
);
std::process::exit(1);
};
let host_key = KnownHostsPin::at(known_hosts, &sandbox, &cli.workspace)
.and_then(|pin| HostKeyCheck::for_relay(expectation, pin))
.map_err(|err| miette::miette!("{err}"))?;
let mut effective_tls = match gateway_name_opt {
Some(ref g) => tls.with_gateway_name(g),
None => tls,
};
if let Some(ref g) = gateway_name_opt {
apply_auth(&mut effective_tls, g)?;
}
run::sandbox_ssh_proxy(&gw, &sandbox, &cli.workspace, &tok, &effective_tls)
.await?;
run::sandbox_ssh_proxy(
&gw,
&sandbox,
&cli.workspace,
&tok,
&effective_tls,
host_key,
)
.await?;
}
// Name mode with --gateway-name: resolve endpoint from metadata.
(_, _, _, server_override, Some(g), Some(n)) => {
Expand All @@ -4209,11 +4242,27 @@ async fn run_async() -> Result<()> {
};
let mut tls = tls.with_gateway_name(&g);
apply_auth(&mut tls, &g)?;
run::sandbox_ssh_proxy_by_name(&endpoint, &n, &tls, &cli.workspace).await?;
run::sandbox_ssh_proxy_by_name(
&endpoint,
Some(&g),
&n,
&tls,
&cli.workspace,
known_hosts,
)
.await?;
}
// Legacy name mode with --server only (no --gateway-name).
(_, _, _, Some(srv), None, Some(n)) => {
run::sandbox_ssh_proxy_by_name(&srv, &n, &tls, &cli.workspace).await?;
run::sandbox_ssh_proxy_by_name(
&srv,
None,
&n,
&tls,
&cli.workspace,
known_hosts,
)
.await?;
}
_ => {
return Err(miette::miette!(
Expand Down Expand Up @@ -5131,6 +5180,10 @@ mod tests {
"tok-abc",
"--gateway-name",
"my-gateway",
"--host-key",
"SHA256:ZkAslGjFiUHdGf/WUL8rQvkib4PTvQatUV0OUQSncCA",
"--known-hosts",
"/home/u/.config/openshell/known_hosts/my-gateway/openshell-my-box.team-a",
])
.expect("TUI proxy command flags must be accepted by the CLI");
assert_eq!(cli.workspace, "team-a");
Expand Down
Loading
Loading