Skip to content

Commit 44262c3

Browse files
committed
fix(cli): replace SSH config entries that accept any host key
Using --editor rewrote only the entry of the sandbox being connected, so the entries an earlier release wrote for other sandboxes kept accepting any host key. It now replaces each of them that is still, line for line, what that release wrote. An entry the user edited is left alone and keeps its warning. The TUI also puts "SSH host key NOT verified" first in its status line after an unverified session, because ssh takes the terminal right after the printed warning. Signed-off-by: Adrien Langou <alangou@nvidia.com>
1 parent a925b8f commit 44262c3

5 files changed

Lines changed: 119 additions & 6 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/openshell-cli/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ openshell-supervisor-middleware-builtins = { path = "../openshell-supervisor-mid
2727
serde = { workspace = true }
2828
serde_json = { workspace = true }
2929
serde_yml = { workspace = true }
30+
shell-words = { workspace = true }
3031
prost = { workspace = true }
3132
prost-types = { workspace = true }
3233

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

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2345,6 +2345,41 @@ fn host_line_matches(line: &str, alias: &str) -> bool {
23452345
.any(|token| token == alias)
23462346
}
23472347

2348+
/// What `render_ssh_config` wrote before host-key verification. With this
2349+
/// entry, `ssh` accepts any host key.
2350+
fn earlier_release_ssh_config(exe: &str, gateway: &str, name: &str, workspace: &str) -> String {
2351+
format!(
2352+
"Host {}\n User sandbox\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n GlobalKnownHostsFile /dev/null\n LogLevel ERROR\n ServerAliveInterval 15\n ServerAliveCountMax 3\n ProxyCommand {} ssh-proxy --gateway-name {} --name {} --workspace {}\n",
2353+
host_alias(name, workspace),
2354+
shell_escape(exe),
2355+
shell_escape(gateway),
2356+
shell_escape(name),
2357+
shell_escape(workspace),
2358+
)
2359+
}
2360+
2361+
/// Replaces each entry an earlier release wrote with the one this release
2362+
/// writes for the same sandbox. Only an entry that is still that text, line
2363+
/// for line, is replaced: one the user edited may hold options to keep. The
2364+
/// executable is not compared, because each release named its own path.
2365+
fn replace_earlier_release_entries(contents: &str) -> String {
2366+
let mut updated = contents.to_string();
2367+
let commands = contents
2368+
.lines()
2369+
.filter_map(|line| line.trim_start().strip_prefix("ProxyCommand "));
2370+
for command in commands {
2371+
// An entry that cannot be rendered stays, and warns when it is used.
2372+
if let Ok(args) = shell_words::split(command)
2373+
&& let [exe, _, _, gateway, _, name, _, workspace] = args.as_slice()
2374+
&& let Ok(entry) = render_ssh_config(gateway, name, workspace)
2375+
{
2376+
let earlier = earlier_release_ssh_config(exe, gateway, name, workspace);
2377+
updated = updated.replace(&earlier, &entry);
2378+
}
2379+
}
2380+
updated
2381+
}
2382+
23482383
fn upsert_host_block(contents: &str, alias: &str, block: &str) -> String {
23492384
let lines: Vec<&str> = contents.lines().collect();
23502385
let start = lines.iter().position(|line| host_line_matches(line, alias));
@@ -2394,7 +2429,7 @@ pub fn install_ssh_config(gateway: &str, name: &str, workspace: &str) -> Result<
23942429

23952430
let alias = host_alias(name, workspace);
23962431
let contents = fs::read_to_string(&managed_config).unwrap_or_default();
2397-
let updated = upsert_host_block(&contents, &alias, &block);
2432+
let updated = replace_earlier_release_entries(&upsert_host_block(&contents, &alias, &block));
23982433
fs::write(&managed_config, updated)
23992434
.into_diagnostic()
24002435
.wrap_err("failed to write OpenShell SSH config")?;
@@ -2921,6 +2956,56 @@ mod tests {
29212956
});
29222957
}
29232958

2959+
#[test]
2960+
fn install_ssh_config_replaces_the_entries_an_earlier_release_wrote() {
2961+
let home = tempfile::tempdir().unwrap();
2962+
let xdg = tempfile::tempdir().unwrap();
2963+
let earlier = |gateway, name| {
2964+
earlier_release_ssh_config("/opt/open shell/openshell", gateway, name, "beta")
2965+
};
2966+
// Not replaced: an entry with an option the user added, and one from
2967+
// before workspaces.
2968+
let kept = [
2969+
earlier("my-gw", "edited").replace(" LogLevel", " ForwardAgent no\n LogLevel"),
2970+
earlier("my-gw", "old")
2971+
.replace("openshell-old.beta", "openshell-old")
2972+
.replace(" --workspace beta", ""),
2973+
]
2974+
.concat();
2975+
let added_after = " LocalForward 3000 localhost:3000\n";
2976+
2977+
Environment::new()
2978+
.set("HOME", home.path())
2979+
.set("XDG_CONFIG_HOME", xdg.path())
2980+
.run(|| {
2981+
let managed = openshell_ssh_config_path().unwrap();
2982+
fs::create_dir_all(managed.parent().unwrap()).unwrap();
2983+
let written = [
2984+
kept.as_str(),
2985+
// Copied from a release, so the text compared against stays true to it.
2986+
"Host openshell-one.beta\n User sandbox\n StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n GlobalKnownHostsFile /dev/null\n LogLevel ERROR\n ServerAliveInterval 15\n ServerAliveCountMax 3\n ProxyCommand '/opt/open shell/openshell' ssh-proxy --gateway-name 'my gw' --name one --workspace beta\n",
2987+
added_after,
2988+
&earlier("my-gw", "two"),
2989+
];
2990+
fs::write(&managed, written.concat()).unwrap();
2991+
2992+
install_ssh_config("my-gw", "demo", "default").unwrap();
2993+
2994+
let installed = fs::read_to_string(&managed).unwrap();
2995+
let expected = [
2996+
kept.as_str(),
2997+
&render_ssh_config("my gw", "one", "beta").unwrap(),
2998+
added_after,
2999+
&render_ssh_config("my-gw", "two", "beta").unwrap(),
3000+
"\n",
3001+
&render_ssh_config("my-gw", "demo", "default").unwrap(),
3002+
];
3003+
assert_eq!(installed, expected.concat());
3004+
install_ssh_config("my-gw", "demo", "default").unwrap();
3005+
assert_eq!(fs::read_to_string(&managed).unwrap(), installed);
3006+
});
3007+
}
3008+
29243009
#[test]
29253010
fn stale_ssh_config_warning_names_the_gateway_and_workspace_of_the_entry() {
29263011
assert_eq!(

‎crates/openshell-tui/src/lib.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ async fn handle_shell_connect(
10381038
disable_raw_mode().into_diagnostic()?;
10391039

10401040
// Step 8: Spawn SSH as child process and wait.
1041-
if let Some(warning) = unverified_warning {
1041+
if let Some(warning) = &unverified_warning {
10421042
eprintln!("{warning}");
10431043
}
10441044
let status = tokio::task::spawn_blocking(move || command.status()).await;
@@ -1056,6 +1056,11 @@ async fn handle_shell_connect(
10561056
app.status_text = format!("Disconnected from {sandbox_name}");
10571057
}
10581058
}
1059+
// `ssh` takes the terminal right after the warning above, so repeat it,
1060+
// first because the status line is cut at the terminal width.
1061+
if unverified_warning.is_some() {
1062+
app.status_text.insert_str(0, "SSH host key NOT verified; ");
1063+
}
10591064

10601065
// Step 9: Resume and draw the TUI before accepting new terminal input.
10611066
enable_raw_mode().into_diagnostic()?;
@@ -1178,7 +1183,7 @@ async fn handle_exec_command(
11781183
disable_raw_mode().into_diagnostic()?;
11791184

11801185
// Step 5: Run command — blocks until user Ctrl-C's or command exits.
1181-
if let Some(warning) = unverified_warning {
1186+
if let Some(warning) = &unverified_warning {
11821187
eprintln!("{warning}");
11831188
}
11841189
let status = tokio::task::spawn_blocking(move || ssh.status()).await;
@@ -1196,6 +1201,11 @@ async fn handle_exec_command(
11961201
app.status_text = format!("Command finished on {sandbox_name}");
11971202
}
11981203
}
1204+
// `ssh` takes the terminal right after the warning above, so repeat it,
1205+
// first because the status line is cut at the terminal width.
1206+
if unverified_warning.is_some() {
1207+
app.status_text.insert_str(0, "SSH host key NOT verified; ");
1208+
}
11991209

12001210
// Step 6: Resume TUI.
12011211
enable_raw_mode().into_diagnostic()?;

‎docs/how-it-works/sandboxes/overview.mdx‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -920,9 +920,25 @@ openshell sandbox ssh-config my-sandbox
920920
Append the output to `~/.ssh/config` or use `--editor` on `sandbox create`/`sandbox connect` for automatic setup.
921921
922922
SSH config entries that an earlier release generated accept any host key.
923-
They keep working and print a warning on every connection. Replace each pasted
924-
entry with new `openshell sandbox ssh-config` output, or use `--editor` on
925-
`sandbox connect` again for that sandbox.
923+
They keep working and print a warning on every connection. Using `--editor` on
924+
`sandbox create` or `sandbox connect` replaces that sandbox's entry, and every
925+
other `openshell-<sandbox>.<workspace>` entry in the OpenShell-managed SSH
926+
include file (`~/.config/openshell/ssh_config`) that you did not edit.
927+
928+
To repair an entry that still prints the warning:
929+
930+
- For an entry you edited, use `--editor` on `sandbox connect` for that
931+
sandbox, then add your options to the new entry.
932+
- For an entry named `openshell-<sandbox>`, with no workspace, delete it from
933+
the include file, then use `--editor` on `sandbox connect` for that sandbox.
934+
The new entry is named `openshell-<sandbox>.<workspace>`.
935+
- For an entry you pasted into `~/.ssh/config`, replace it with new
936+
`openshell sandbox ssh-config` output.
937+
938+
A replaced entry runs the `openshell` executable that replaced it. If you had
939+
changed that path, change it again. After a downgrade, replaced entries stop
940+
connecting, because earlier releases do not accept `--known-hosts`. Use
941+
`--editor` on `sandbox connect` with the downgraded CLI for each sandbox.
926942
927943
Unverified connections go ahead by default. Set
928944
`OPENSHELL_SSH_REQUIRE_HOST_KEY=1` to refuse every unverified connection, to a

0 commit comments

Comments
 (0)