Add fresh device trust, installer recovery and scoped patch runtime
This commit is contained in:
@@ -54,6 +54,36 @@ pub enum PrivilegedScriptProfile {
|
||||
#[serde(rename = "full_maintenance_v1")]
|
||||
FullMaintenanceV1,
|
||||
}
|
||||
/// Bytes of the Windows Update Agent PowerShell template seeded by migration 0023.
|
||||
pub const WINDOWS_UPDATE_SCRIPT_UTF8: &str = include_str!("windows_update.ps1");
|
||||
pub const WINDOWS_UPDATE_SCRIPT_SHA256: &str =
|
||||
"0fa5f513a06b6cf902a3f9ac1989987642c181eedd6c305b3912771bd10f2ebf";
|
||||
pub const WINDOWS_UPDATE_MAX_SECONDS: u32 = 3600;
|
||||
pub const WINDOWS_UPDATE_LONG_CAPABILITY: &str = "script.windows_update.long/1";
|
||||
pub fn valid_windows_update_limit(
|
||||
limit: Option<u32>,
|
||||
profiles: Option<&[PrivilegedScriptProfile]>,
|
||||
) -> bool {
|
||||
limit.is_none_or(|seconds| {
|
||||
(121..=WINDOWS_UPDATE_MAX_SECONDS).contains(&seconds)
|
||||
&& profiles == Some(&[PrivilegedScriptProfile::FullMaintenanceV1][..])
|
||||
})
|
||||
}
|
||||
/// Only the built-in template's final ASCII JSON object describes the host's
|
||||
/// restart state. Earlier lines can include per-update results and native titles.
|
||||
pub fn windows_update_reboot_report(stdout: &[u8]) -> Option<bool> {
|
||||
let summary = stdout
|
||||
.rsplit(|byte| *byte == b'\n')
|
||||
.map(<[u8]>::trim_ascii)
|
||||
.find(|line| !line.is_empty())?;
|
||||
let json: serde_json::Value = serde_json::from_slice(summary).ok()?;
|
||||
let obj = json.as_object()?;
|
||||
let reboot = obj.get("reboot_required")?.as_bool()?;
|
||||
(obj.len() == 2
|
||||
&& (obj.get("found").is_some_and(|v| v.is_u64())
|
||||
|| (reboot && obj.get("status").and_then(|v| v.as_str()) == Some("restart_pending"))))
|
||||
.then_some(reboot)
|
||||
}
|
||||
pub const FULL_MAINTENANCE_CAPABILITY: &str = "script.full_maintenance/1";
|
||||
pub fn deserialize_privileged_script_profile<'de, D>(
|
||||
d: D,
|
||||
@@ -387,6 +417,20 @@ impl Action {
|
||||
}
|
||||
)
|
||||
}
|
||||
pub fn is_windows_update_template(&self) -> bool {
|
||||
matches!(self, Self::RunScript {
|
||||
interpreter, script_utf8, arguments, expected_stdout: None,
|
||||
windows_execution_profile: Some(WindowsExecutionProfile::PowerShell51RemoteSigned),
|
||||
privileged_script_profile: Some(PrivilegedScriptProfile::FullMaintenanceV1),
|
||||
} if interpreter == "powershell" && arguments.is_empty() && script_utf8 == WINDOWS_UPDATE_SCRIPT_UTF8)
|
||||
}
|
||||
pub fn windows_system_runtime_limit(&self, normal: u32, windows_update: Option<u32>) -> u32 {
|
||||
if self.is_windows_update_template() {
|
||||
windows_update.unwrap_or(normal)
|
||||
} else {
|
||||
normal
|
||||
}
|
||||
}
|
||||
pub fn valid_execution_identity(&self, identity: &str) -> bool {
|
||||
if let Self::UpdateAgent { update } = self {
|
||||
return update.execution_identity() == Some(identity);
|
||||
@@ -410,7 +454,7 @@ impl Action {
|
||||
|| (output_bytes <= 65536
|
||||
&& runtime_seconds
|
||||
<= if self.script_platform() == Some("windows") {
|
||||
120
|
||||
self.windows_system_runtime_limit(120, Some(WINDOWS_UPDATE_MAX_SECONDS))
|
||||
} else {
|
||||
300
|
||||
}))
|
||||
@@ -779,6 +823,29 @@ impl JobState {
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn windows_update_final_report_and_policy_are_exact() {
|
||||
assert_eq!(
|
||||
windows_update_reboot_report(
|
||||
b"ignored \xff\r\n{\"found\":1,\"reboot_required\":true}\r\n"
|
||||
),
|
||||
Some(true)
|
||||
);
|
||||
assert_eq!(
|
||||
windows_update_reboot_report(br#"{"reboot_required":false,"found":0}"#),
|
||||
Some(false)
|
||||
);
|
||||
assert_eq!(
|
||||
windows_update_reboot_report(br#"{"reboot_required":true,"status":"restart_pending"}"#),
|
||||
Some(true)
|
||||
);
|
||||
assert_eq!(
|
||||
windows_update_reboot_report(br#"{"id":"an update","reboot_required":true}"#),
|
||||
None
|
||||
);
|
||||
assert_eq!(windows_update_reboot_report(b"partial"), None);
|
||||
assert!(!valid_windows_update_limit(Some(3600), None));
|
||||
}
|
||||
#[test]
|
||||
fn windows_system_scm_is_explicit_and_preserves_linux_service_bytes() {
|
||||
let caps: Vec<String> = [
|
||||
"windows.base/1",
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
# Install available software updates from the configured Windows Update source.
|
||||
# See https://learn.microsoft.com/windows/win32/wua_sdk/windows-update-agent-object-model
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$session = New-Object -ComObject Microsoft.Update.Session
|
||||
$session.ClientApplicationID = 'Rust Discovery'
|
||||
$searcher = $session.CreateUpdateSearcher()
|
||||
$search = $searcher.Search("IsInstalled=0 and IsHidden=0 and Type='Software'")
|
||||
if ($search.ResultCode -ne 2) { throw "Update search returned $($search.ResultCode)" }
|
||||
$failed = $false
|
||||
$systemInfo = New-Object -ComObject Microsoft.Update.SystemInfo
|
||||
if ($systemInfo.RebootRequired) {
|
||||
Write-Output '{"reboot_required":true,"status":"restart_pending"}'
|
||||
exit 1
|
||||
}
|
||||
foreach ($update in $search.Updates) {
|
||||
$result = [ordered]@{ id = $update.Identity.UpdateID; title = $update.Title; status = 'pending' }
|
||||
try {
|
||||
if ($update.InstallationBehavior.CanRequestUserInput -or !$update.EulaAccepted) {
|
||||
$result.status = 'operator_input_required'
|
||||
$failed = $true
|
||||
} else {
|
||||
$batch = New-Object -ComObject Microsoft.Update.UpdateColl
|
||||
[void]$batch.Add($update)
|
||||
if (!$update.IsDownloaded) {
|
||||
$downloader = $session.CreateUpdateDownloader()
|
||||
$downloader.Updates = $batch
|
||||
$download = $downloader.Download().GetUpdateResult(0)
|
||||
if ($download.ResultCode -ne 2) { throw "Download HRESULT $($download.HResult)" }
|
||||
}
|
||||
$installer = $session.CreateUpdateInstaller()
|
||||
$installer.AllowSourcePrompts = $false
|
||||
$installer.Updates = $batch
|
||||
$install = $installer.Install().GetUpdateResult(0)
|
||||
$result.status = [int]$install.ResultCode
|
||||
$result.hresult = $install.HResult
|
||||
$result.reboot_required = [bool]$install.RebootRequired
|
||||
if ($install.ResultCode -ne 2) { $failed = $true }
|
||||
}
|
||||
} catch {
|
||||
$result.status = 'failed'
|
||||
$result.error = $_.Exception.Message
|
||||
$failed = $true
|
||||
}
|
||||
Write-Output ($result | ConvertTo-Json -Compress)
|
||||
}
|
||||
Write-Output (@{ found = $search.Updates.Count; reboot_required = [bool]$systemInfo.RebootRequired } | ConvertTo-Json -Compress)
|
||||
if ($failed) { exit 1 }
|
||||
Reference in New Issue
Block a user