Skip to content
Merged
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
137 changes: 93 additions & 44 deletions desktop/src-tauri/src/docker_desktop_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,33 +254,33 @@ pub fn powershell_with_path(script: &str, variable: &str, path: &Path) -> std::p
command
}

/// The environment variable the all-users install reads the installer's command line from.
pub const INSTALLER_ARGUMENTS: &str = "OPENBOT_INSTALLER_ARGUMENTS";

/// The exit code that means the person said no to Windows' approval prompt: `ERROR_CANCELLED`, as
/// the WSL fix reads it (`windows::DECLINED`).
const ERROR_CANCELLED: i32 = 1223;
/// the WSL fix reads it.
const ERROR_CANCELLED: i32 = crate::windows::DECLINED;

/// The all-users install: Windows' approval prompt, then Docker's installer as an administrator,
/// waited for, its exit code PowerShell's. The installer's path is `INSTALLER_PATH`.
/// waited for, its exit code PowerShell's, and `ERROR_CANCELLED` for a No (`windows::runas_script`).
/// The installer's path is `INSTALLER_PATH` and its command line `INSTALLER_ARGUMENTS`.
pub fn elevated_install(installer: &Path, arguments: &[String]) -> std::process::Command {
let quoted: Vec<String> = arguments
.iter()
.map(|argument| format!("'{}'", argument.replace('\'', "''")))
.collect();
let start = format!(
"$p = Start-Process -FilePath $env:{INSTALLER_PATH} -ArgumentList {} -Verb RunAs -Wait \
-PassThru",
quoted.join(",")
let mut command = powershell_with_path(
&install_script(crate::windows::PROCESS_START),
INSTALLER_PATH,
installer,
);
powershell_with_path(&elevated_script(&start), INSTALLER_PATH, installer)
command.env(INSTALLER_ARGUMENTS, crate::windows::command_line(arguments));
command
}

/// `start`, which raises Windows' approval prompt and sets `$p`, exiting with `$p`'s exit code. A
/// no to the prompt exits `ERROR_CANCELLED`, found by its Win32 code in the error, never by its
/// words, which are in Windows' own language.
fn elevated_script(start: &str) -> String {
format!(
"$ErrorActionPreference = 'Stop'; try {{ {start}; exit $p.ExitCode }} catch {{ \
$e = $_.Exception; while ($e) {{ if ($e.NativeErrorCode -eq {ERROR_CANCELLED}) \
{{ exit {ERROR_CANCELLED} }}; $e = $e.InnerException }}; throw }}"
/// The script `elevated_install` runs, with `starting` raising the prompt (`windows::runas_script`).
fn install_script(starting: &str) -> String {
crate::windows::runas_script(
&format!("$env:{INSTALLER_PATH}"),
&format!("$env:{INSTALLER_ARGUMENTS}"),
false,
starting,
)
}

Expand Down Expand Up @@ -792,7 +792,7 @@ fn run_installer(mounted: &Mounted) -> Installed {
}

/// Whether `elevated_install` stopped because the person said no to Windows' approval prompt: it
/// then exits `ERROR_CANCELLED` (`elevated_script`). What PowerShell said is in Windows' own
/// then exits `ERROR_CANCELLED` (`windows::runas_script`). What PowerShell said is in Windows' own
/// language, so it is never read for this.
#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
fn closed_the_prompt(code: Option<i32>) -> bool {
Expand Down Expand Up @@ -1497,9 +1497,15 @@ Internal requirements count=1 size=180
.to_string_lossy()
.into_owned();
assert!(
script.contains("-ArgumentList 'install','--quiet' -Verb RunAs"),
script.contains("$start.Arguments = $env:OPENBOT_INSTALLER_ARGUMENTS"),
"{script}"
);
assert!(script.contains("$start.Verb = 'runas'"), "{script}");
let envs: Vec<_> = install.get_envs().collect();
assert!(envs.contains(&(
std::ffi::OsStr::new(INSTALLER_ARGUMENTS),
Some(std::ffi::OsStr::new("install --quiet"))
)));
}

/*
Expand Down Expand Up @@ -1746,41 +1752,84 @@ Internal requirements count=1 size=180
assert!(!closed_the_prompt(Some(1)));
assert!(!closed_the_prompt(Some(0)));
assert!(!closed_the_prompt(None));
// The script reads the Win32 code, never the words.
let script = elevated_script("$p = Start-Process x -Verb RunAs -Wait -PassThru");
// The script reads the Win32 code, never the words, and asks through the call that keeps
// the code (`windows::PROCESS_START`), not Start-Process, which loses it.
let script = install_script(crate::windows::PROCESS_START);
assert!(script.contains("NativeErrorCode -eq 1223"), "{script}");
assert!(script.contains("exit 1223"), "{script}");
assert!(
script.contains("$ErrorActionPreference = 'Stop'"),
"{script}"
);
assert!(!script.contains("canceled"), "{script}");
assert!(!script.contains("Start-Process"), "{script}");
}

/// The script `elevated_install` runs, in real Windows PowerShell, with `Start-Process` standing
/// in for by the error it throws when the person says no: a Win32 `ERROR_CANCELLED` inside the
/// `InvalidOperationException`, worded in German. It exits 1223. Any other error exits 1.
/// The script `elevated_install` runs, in real Windows PowerShell, answered No as Windows
/// answers it. With `Start-Process -Verb RunAs`, a No on Windows Server 2022 (PowerShell
/// 5.1.20348) came as an `InvalidOperationException` (HResult 0x80131509) with no inner
/// exception and no Win32 code, so the script exited 1 and the update went on to open Docker's
/// installer. It now asks through `Process.Start`, whose No carries `ERROR_CANCELLED`.
#[test]
#[cfg(windows)]
fn the_elevated_install_script_exits_1223_on_a_no_in_any_language() {
let run = |native: i32| {
let start = format!(
"throw [System.InvalidOperationException]::new('Dieser Befehl kann aufgrund des \
folgenden Fehlers nicht ausgeführt werden.', \
[System.ComponentModel.Win32Exception]::new({native}))"
fn the_elevated_install_script_exits_1223_on_a_no_as_windows_says_it() {
let script = format!(
"{}{}",
crate::windows::SAYS_NO_TYPE,
install_script(crate::windows::SAYS_NO)
);
let code = crate::windows::powershell_exit_for_test(
&script,
&[
(INSTALLER_PATH, r"C:\nowhere\Docker Desktop Installer.exe"),
(INSTALLER_ARGUMENTS, "install --quiet"),
],
);
assert_eq!(code, Some(ERROR_CANCELLED));
assert!(closed_the_prompt(code));
}

/// The real install command on real Windows, with `Start-Process` answering No as it did on
/// Windows Server 2022: the install never asks through it. The runner's Windows asks nobody,
/// so the program runs elevated and its exit code comes back; one that isn't there is a
/// failure, not a No.
#[test]
#[cfg(windows)]
fn the_elevated_install_runs_the_installer_and_hands_back_its_exit_code() {
let cmd = std::path::PathBuf::from(std::env::var("ComSpec").unwrap());
let ran = |program: &Path, arguments: &[&str]| {
let command = elevated_install(
program,
&arguments.iter().map(|a| a.to_string()).collect::<Vec<_>>(),
);
let output = powershell_with_path(
&elevated_script(&start),
INSTALLER_PATH,
Path::new(r"C:\nowhere\Docker Desktop Installer.exe"),
let script = command
.get_args()
.nth(3)
.unwrap()
.to_string_lossy()
.into_owned();
let envs: Vec<(String, String)> = command
.get_envs()
.filter_map(|(name, value)| {
Some((
name.to_string_lossy().into_owned(),
value?.to_string_lossy().into_owned(),
))
})
.collect();
let envs: Vec<(&str, &str)> =
envs.iter().map(|(n, v)| (n.as_str(), v.as_str())).collect();
crate::windows::powershell_exit_for_test(
&format!("{}{script}", crate::windows::START_PROCESS_SAID_NO),
&envs,
)
.output()
.unwrap();
output.status.code()
};
assert_eq!(run(ERROR_CANCELLED), Some(ERROR_CANCELLED));
let other = run(2);
assert_ne!(other, Some(ERROR_CANCELLED));
assert_ne!(other, Some(0));
assert_eq!(ran(cmd.as_path(), &["/c", "exit 7"]), Some(7));
let missing = ran(
Path::new(r"C:\nowhere\Docker Desktop Installer.exe"),
&["install"],
);
assert_ne!(missing, Some(0));
assert!(!closed_the_prompt(missing), "{missing:?}");
}
}
Loading
Loading