Skip to content

Commit 6fe64e1

Browse files
committed
fix(cli): bind credentials to the selected gateway and protect saved SSH URLs
Signed-off-by: Adrien Langou <alangou@nvidia.com>
1 parent 1229e2d commit 6fe64e1

5 files changed

Lines changed: 129 additions & 64 deletions

File tree

‎crates/openshell-cli/src/main.rs‎

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2784,20 +2784,30 @@ fn run_main() -> Result<()> {
27842784
async fn run_async() -> Result<()> {
27852785
CompleteEnv::with_factory(Cli::command).complete();
27862786

2787-
let (cli, explicit_gateway) = {
2787+
let (cli, explicit_gateway, explicit_endpoint) = {
27882788
let mut args = Cli::command().get_matches();
27892789
let explicit_gateway =
27902790
args.value_source("gateway") == Some(clap::parser::ValueSource::CommandLine);
2791-
if explicit_gateway
2792-
&& args.value_source("gateway_endpoint") == Some(clap::parser::ValueSource::EnvVariable)
2793-
{
2794-
args.remove_one::<String>("gateway_endpoint");
2795-
}
2791+
let explicit_endpoint =
2792+
args.value_source("gateway_endpoint") == Some(clap::parser::ValueSource::CommandLine);
27962793
(
27972794
Cli::from_arg_matches_mut(&mut args).unwrap_or_else(|err| err.exit()),
27982795
explicit_gateway,
2796+
explicit_endpoint,
27992797
)
28002798
};
2799+
let current_gateway = || {
2800+
if let (Some(name), Some(endpoint)) = (&cli.gateway, &cli.gateway_endpoint)
2801+
&& explicit_gateway != explicit_endpoint
2802+
&& !get_gateway_metadata(name).is_some_and(|metadata| {
2803+
normalize_gateway_endpoint(&metadata.gateway_endpoint)
2804+
== normalize_gateway_endpoint(endpoint)
2805+
})
2806+
{
2807+
return resolve_gateway(&None, &cli.gateway_endpoint);
2808+
}
2809+
resolve_gateway(&cli.gateway, &cli.gateway_endpoint)
2810+
};
28012811

28022812
// Resolve colorization before anything is printed, so no command can emit
28032813
// escape sequences into a pipe.
@@ -2902,7 +2912,7 @@ async fn run_async() -> Result<()> {
29022912
run::gateway_select(name.as_deref(), &cli.gateway)?;
29032913
}
29042914
GatewayCommands::Info { output } => {
2905-
if let Ok(ctx) = resolve_gateway(&cli.gateway, &cli.gateway_endpoint) {
2915+
if let Ok(ctx) = current_gateway() {
29062916
let mut tls = tls.with_gateway_name(&ctx.name);
29072917
apply_auth(&mut tls, &ctx.name)?;
29082918
run::gateway_info(&ctx.name, &ctx.endpoint, &tls, output.as_str()).await?;
@@ -2938,7 +2948,7 @@ async fn run_async() -> Result<()> {
29382948
// -----------------------------------------------------------
29392949
Some(Commands::Status { output }) => {
29402950
// A gateway named on the command line must be registered.
2941-
let resolved = match resolve_gateway(&cli.gateway, &cli.gateway_endpoint) {
2951+
let resolved = match current_gateway() {
29422952
Err(err) if cli.gateway.is_some() => return Err(err),
29432953
resolved => resolved,
29442954
};
@@ -2975,7 +2985,7 @@ async fn run_async() -> Result<()> {
29752985
// Top-level current identity
29762986
// -----------------------------------------------------------
29772987
Some(Commands::Whoami { output }) => {
2978-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
2988+
let ctx = current_gateway()?;
29792989
let mut tls = tls.with_gateway_name(&ctx.name);
29802990
apply_auth(&mut tls, &ctx.name)?;
29812991
run::whoami(&ctx.endpoint, &tls, output.as_str()).await?;
@@ -3091,7 +3101,7 @@ async fn run_async() -> Result<()> {
30913101
target_host,
30923102
local,
30933103
} => {
3094-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
3104+
let ctx = current_gateway()?;
30953105
let mut tls = tls.with_gateway_name(&ctx.name);
30963106
apply_auth(&mut tls, &ctx.name)?;
30973107
let name = resolve_sandbox_name(name, &ctx.name, &cli.workspace)?;
@@ -3113,7 +3123,7 @@ async fn run_async() -> Result<()> {
31133123
background,
31143124
} => {
31153125
let spec = openshell_core::forward::ForwardSpec::parse(&port)?;
3116-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
3126+
let ctx = current_gateway()?;
31173127
let mut tls = tls.with_gateway_name(&ctx.name);
31183128
apply_auth(&mut tls, &ctx.name)?;
31193129
let name = resolve_sandbox_name(name, &ctx.name, &cli.workspace)?;
@@ -3144,7 +3154,7 @@ async fn run_async() -> Result<()> {
31443154
Some(Commands::Service {
31453155
command: Some(command),
31463156
}) => {
3147-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
3157+
let ctx = current_gateway()?;
31483158
let mut tls = tls.with_gateway_name(&ctx.name);
31493159
apply_auth(&mut tls, &ctx.name)?;
31503160
match command {
@@ -3208,7 +3218,7 @@ async fn run_async() -> Result<()> {
32083218
source,
32093219
level,
32103220
}) => {
3211-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
3221+
let ctx = current_gateway()?;
32123222
let mut tls = tls.with_gateway_name(&ctx.name);
32133223
apply_auth(&mut tls, &ctx.name)?;
32143224
let name = resolve_sandbox_name(name, &ctx.name, &cli.workspace)?;
@@ -3235,7 +3245,7 @@ async fn run_async() -> Result<()> {
32353245
Some(Commands::Policy {
32363246
command: Some(policy_cmd),
32373247
}) => {
3238-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
3248+
let ctx = current_gateway()?;
32393249
let mut tls = tls.with_gateway_name(&ctx.name);
32403250
apply_auth(&mut tls, &ctx.name)?;
32413251
match policy_cmd {
@@ -3397,7 +3407,7 @@ async fn run_async() -> Result<()> {
33973407
Some(Commands::Settings {
33983408
command: Some(settings_cmd),
33993409
}) => {
3400-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
3410+
let ctx = current_gateway()?;
34013411
let mut tls = tls.with_gateway_name(&ctx.name);
34023412
apply_auth(&mut tls, &ctx.name)?;
34033413

@@ -3476,7 +3486,7 @@ async fn run_async() -> Result<()> {
34763486
Some(Commands::Rule {
34773487
command: Some(draft_cmd),
34783488
}) => {
3479-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
3489+
let ctx = current_gateway()?;
34803490
let mut tls = tls.with_gateway_name(&ctx.name);
34813491
apply_auth(&mut tls, &ctx.name)?;
34823492
match draft_cmd {
@@ -3635,7 +3645,7 @@ async fn run_async() -> Result<()> {
36353645
|| expose.is_some();
36363646
let gpu_requirements: Option<GpuResourceRequirements> = gpu.map(Into::into);
36373647

3638-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
3648+
let ctx = current_gateway()?;
36393649
let endpoint = &ctx.endpoint;
36403650
let mut tls = tls.with_gateway_name(&ctx.name);
36413651
apply_auth(&mut tls, &ctx.name)?;
@@ -3685,7 +3695,7 @@ async fn run_async() -> Result<()> {
36853695
dest,
36863696
no_git_ignore,
36873697
} => {
3688-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
3698+
let ctx = current_gateway()?;
36893699
let mut tls = tls.with_gateway_name(&ctx.name);
36903700
apply_auth(&mut tls, &ctx.name)?;
36913701
let local = std::path::Path::new(&local_path);
@@ -3705,7 +3715,7 @@ async fn run_async() -> Result<()> {
37053715
sandbox_path,
37063716
dest,
37073717
} => {
3708-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
3718+
let ctx = current_gateway()?;
37093719
let mut tls = tls.with_gateway_name(&ctx.name);
37103720
apply_auth(&mut tls, &ctx.name)?;
37113721
let local_dest = dest.as_deref().unwrap_or(".");
@@ -3722,7 +3732,7 @@ async fn run_async() -> Result<()> {
37223732
eprintln!("{} Download complete", "✓".green().bold());
37233733
}
37243734
other => {
3725-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
3735+
let ctx = current_gateway()?;
37263736
let endpoint = &ctx.endpoint;
37273737
let mut tls = tls.with_gateway_name(&ctx.name);
37283738
apply_auth(&mut tls, &ctx.name)?;
@@ -4025,7 +4035,7 @@ async fn run_async() -> Result<()> {
40254035
Some(Commands::Workspace {
40264036
command: Some(command),
40274037
}) => {
4028-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
4038+
let ctx = current_gateway()?;
40294039
let endpoint = &ctx.endpoint;
40304040
let mut tls = tls.with_gateway_name(&ctx.name);
40314041
apply_auth(&mut tls, &ctx.name)?;
@@ -4095,7 +4105,7 @@ async fn run_async() -> Result<()> {
40954105
Some(Commands::Provider {
40964106
command: Some(command),
40974107
}) => {
4098-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
4108+
let ctx = current_gateway()?;
40994109
let endpoint = &ctx.endpoint;
41004110
let mut tls = tls.with_gateway_name(&ctx.name);
41014111
apply_auth(&mut tls, &ctx.name)?;
@@ -4289,14 +4299,14 @@ async fn run_async() -> Result<()> {
42894299
Some(Commands::Profile {
42904300
command: Some(command),
42914301
}) => {
4292-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
4302+
let ctx = current_gateway()?;
42934303
let endpoint = &ctx.endpoint;
42944304
let mut tls = tls.with_gateway_name(&ctx.name);
42954305
apply_auth(&mut tls, &ctx.name)?;
42964306
command.run(endpoint, &cli.workspace, &tls).await?;
42974307
}
42984308
Some(Commands::Term { theme }) => {
4299-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
4309+
let ctx = current_gateway()?;
43004310
let mut tls = tls.with_gateway_name(&ctx.name);
43014311
apply_auth(&mut tls, &ctx.name)?;
43024312
let channel = openshell_cli::tls::build_channel(&ctx.endpoint, &tls).await?;
@@ -4341,21 +4351,11 @@ async fn run_async() -> Result<()> {
43414351
} else {
43424352
// A saved profile resolves its own endpoint. A local --server
43434353
// can explicitly pair an endpoint with the chosen profile.
4344-
let (profile, endpoint) = if server.is_some() {
4345-
(
4346-
gateway_name.or_else(|| cli.gateway.filter(|_| explicit_gateway)),
4347-
server,
4348-
)
4349-
} else if gateway_name.is_some() {
4350-
(gateway_name, None)
4351-
} else if cli.gateway_endpoint.is_some() {
4352-
(
4353-
cli.gateway.filter(|_| explicit_gateway),
4354-
cli.gateway_endpoint,
4355-
)
4356-
} else {
4357-
(cli.gateway, cli.gateway_endpoint)
4358-
};
4354+
let use_global_endpoint =
4355+
gateway_name.is_none() && (!explicit_gateway || explicit_endpoint);
4356+
let endpoint =
4357+
server.or_else(|| cli.gateway_endpoint.filter(|_| use_global_endpoint));
4358+
let profile = gateway_name.or_else(|| cli.gateway.filter(|_| explicit_gateway));
43594359
let ctx = resolve_gateway(&profile, &endpoint)?;
43604360
let mut tls = tls.with_gateway_name(&ctx.name);
43614361
apply_auth(&mut tls, &ctx.name)?;
@@ -4402,7 +4402,7 @@ async fn run_async() -> Result<()> {
44024402
Some(Commands::Secret {
44034403
command: Some(command),
44044404
}) => {
4405-
let ctx = resolve_gateway(&cli.gateway, &cli.gateway_endpoint)?;
4405+
let ctx = current_gateway()?;
44064406
let endpoint = &ctx.endpoint;
44074407
let mut tls = tls.with_gateway_name(&ctx.name);
44084408
apply_auth(&mut tls, &ctx.name)?;

‎crates/openshell-cli/src/ssh.rs‎

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,27 +2236,32 @@ fn host_alias(name: &str, workspace: &str) -> String {
22362236

22372237
fn render_ssh_config(gateway: &str, name: &str, workspace: &str) -> Result<String> {
22382238
let exe = std::env::current_exe().into_diagnostic()?;
2239-
let selector = if let Some(authority) = gateway
2240-
.strip_prefix("https://")
2241-
.or_else(|| gateway.strip_prefix("http://"))
2242-
{
2243-
if authority
2244-
.strip_suffix('/')
2245-
.unwrap_or(authority)
2246-
.contains(['/', '@', '?', '#', '\\'])
2239+
let (selector, target) = match gateway.split_once("://") {
2240+
Some((scheme, rest))
2241+
if (scheme.eq_ignore_ascii_case("http") || scheme.eq_ignore_ascii_case("https"))
2242+
&& !rest.contains('@')
2243+
&& rest
2244+
.strip_suffix('/')
2245+
.unwrap_or(rest)
2246+
.parse::<hyper::http::uri::Authority>()
2247+
.is_ok() =>
22472248
{
2249+
(
2250+
"--server",
2251+
format!("{}://{rest}", scheme.to_ascii_lowercase()),
2252+
)
2253+
}
2254+
None if !gateway.contains(['/', '\r', '\n']) => ("--gateway-name", gateway.to_owned()),
2255+
_ => {
22482256
return Err(miette::miette!(
22492257
"A saved SSH configuration needs a named gateway profile for this private endpoint. Register the endpoint with `openshell gateway add <endpoint> --name <profile>`."
22502258
));
22512259
}
2252-
"--server"
2253-
} else {
2254-
"--gateway-name"
22552260
};
22562261
let proxy_cmd = format!(
22572262
"{} ssh-proxy {selector} {} --name {} --workspace {}",
22582263
shell_escape(&exe.to_string_lossy()).replace('%', "%%"),
2259-
shell_escape(gateway).replace('%', "%%"),
2264+
shell_escape(&target).replace('%', "%%"),
22602265
shell_escape(name).replace('%', "%%"),
22612266
shell_escape(workspace).replace('%', "%%"),
22622267
);
@@ -2944,9 +2949,8 @@ mod tests {
29442949

29452950
#[test]
29462951
fn render_ssh_config_includes_workspace_in_proxy_command() {
2947-
assert!(render_ssh_config("https://gw/private/..", "demo", "beta").is_err());
2948-
assert!(render_ssh_config("https://user:pw@gw/?key=value", "demo", "beta").is_err());
29492952
let config = render_ssh_config("my-gw", "demo", "beta").unwrap();
2953+
assert!(config.contains("--gateway-name my-gw"));
29502954
assert!(
29512955
config.contains("Host openshell-demo.beta"),
29522956
"host alias should be workspace-qualified: {config}"
@@ -2957,6 +2961,36 @@ mod tests {
29572961
);
29582962
}
29592963

2964+
#[test]
2965+
fn ssh_config_only_saves_direct_http_origins() {
2966+
for scheme in ["http", "https", "HTTP", "HtTpS"] {
2967+
for suffix in [
2968+
"user:pw@gw",
2969+
"gw/private/..",
2970+
"gw/?key=value",
2971+
"gw/#private",
2972+
"gw\\private",
2973+
] {
2974+
let endpoint = format!("{scheme}://{suffix}");
2975+
let err = render_ssh_config(&endpoint, "demo", "beta").unwrap_err();
2976+
assert!(!err.to_string().contains(&endpoint));
2977+
}
2978+
let endpoint = format!("{scheme}://[::1]:8443/");
2979+
let config = render_ssh_config(&endpoint, "demo", "beta").unwrap();
2980+
assert!(config.contains(&format!(
2981+
"--server {}",
2982+
shell_escape(&endpoint.to_ascii_lowercase())
2983+
)));
2984+
}
2985+
for endpoint in [
2986+
" https://user:pw@gw",
2987+
"unknown://user:pw@gw",
2988+
"https:/private",
2989+
] {
2990+
assert!(render_ssh_config(endpoint, "demo", "beta").is_err());
2991+
}
2992+
}
2993+
29602994
#[test]
29612995
fn host_alias_includes_workspace() {
29622996
assert_eq!(host_alias("demo", "default"), "openshell-demo.default");

0 commit comments

Comments
 (0)