112 lines
4.0 KiB
Rust
112 lines
4.0 KiB
Rust
//! mTLS public artifact prefetch for an already approved JobOffer. This runs
|
|
//! before the sixty-second startgrant, on its own task and bounded disk lanes.
|
|
use anyhow::{Result, ensure};
|
|
use reqwest::{Client, StatusCode};
|
|
use rmm_agent_windows::update_cache::{self, Download};
|
|
use rmm_domain::{
|
|
agent_update::ReleaseManifest,
|
|
jobs::JobPayload,
|
|
};
|
|
use std::time::{Duration, Instant};
|
|
const CHUNK: u64 = 512 * 1024;
|
|
fn metadata(
|
|
response: &reqwest::Response,
|
|
a: &rmm_domain::agent_update::ArtifactDescriptor,
|
|
offset: u64,
|
|
length: u64,
|
|
) -> Result<()> {
|
|
let partial = offset != 0 || length != a.bytes;
|
|
ensure!(
|
|
response.status()
|
|
== if partial {
|
|
StatusCode::PARTIAL_CONTENT
|
|
} else {
|
|
StatusCode::OK
|
|
},
|
|
"UPDATE_DOWNLOAD_STATUS"
|
|
);
|
|
let h = response.headers();
|
|
ensure!(
|
|
h.get("x-artifact-sha256").and_then(|v| v.to_str().ok()) == Some(a.sha256.as_str())
|
|
&& h.get("content-type").and_then(|v| v.to_str().ok())
|
|
== Some("application/octet-stream")
|
|
&& !h.contains_key("content-encoding")
|
|
&& h.get("content-length")
|
|
.and_then(|v| v.to_str().ok())
|
|
.and_then(|v| v.parse::<u64>().ok())
|
|
== Some(length),
|
|
"UPDATE_DOWNLOAD_METADATA"
|
|
);
|
|
if partial {
|
|
ensure!(
|
|
h.get("content-range").and_then(|v| v.to_str().ok())
|
|
== Some(format!("bytes {}-{}/{}", offset, offset + length - 1, a.bytes).as_str()),
|
|
"UPDATE_DOWNLOAD_RANGE"
|
|
);
|
|
} else {
|
|
ensure!(!h.contains_key("content-range"), "UPDATE_DOWNLOAD_RANGE");
|
|
}
|
|
Ok(())
|
|
}
|
|
pub async fn prefetch(
|
|
client: Client,
|
|
base: String,
|
|
job: JobPayload,
|
|
manifest: ReleaseManifest,
|
|
guard: impl Fn() -> Result<()> + Send + 'static,
|
|
) -> Result<()> {
|
|
ensure!(
|
|
manifest.validate() && manifest.platform == rmm_agent_windows::release_platform::WIRE,
|
|
"UPDATE_DOWNLOAD_MANIFEST"
|
|
);
|
|
let deadline = Instant::now() + Duration::from_secs(600);
|
|
for artifact in manifest.artifacts {
|
|
guard()?;
|
|
let descriptor = artifact.clone();
|
|
let exists = tokio::task::spawn_blocking(move || -> Result<bool> {
|
|
if update_cache::Source::open(&descriptor).is_ok() {
|
|
Ok(true)
|
|
} else {
|
|
Ok(false)
|
|
}
|
|
})
|
|
.await??;
|
|
if exists {
|
|
continue;
|
|
}
|
|
let descriptor = artifact.clone();
|
|
let mut stage = tokio::task::spawn_blocking(move || Download::begin(&descriptor)).await??;
|
|
while stage.offset() < artifact.bytes {
|
|
guard()?;
|
|
ensure!(Instant::now() < deadline, "UPDATE_DOWNLOAD_DEADLINE");
|
|
let offset = stage.offset();
|
|
let length = CHUNK.min(artifact.bytes - offset);
|
|
let mut response=client.get(format!("{base}/agent/v1/update-artifacts/{}?job_id={}&attempt_id={}&offset={offset}&length={length}",artifact.artifact_id,job.job_id,job.attempt_id)).send().await?;
|
|
metadata(&response, &artifact, offset, length)?;
|
|
let mut bytes = Vec::with_capacity(length as usize);
|
|
while let Some(chunk) = response.chunk().await? {
|
|
ensure!(
|
|
bytes.len() + chunk.len() <= length as usize,
|
|
"UPDATE_DOWNLOAD_BODY_LIMIT"
|
|
);
|
|
bytes.extend_from_slice(&chunk);
|
|
}
|
|
ensure!(
|
|
bytes.len() == length as usize,
|
|
"UPDATE_DOWNLOAD_BODY_LENGTH"
|
|
);
|
|
guard()?;
|
|
ensure!(Instant::now() < deadline, "UPDATE_DOWNLOAD_DEADLINE");
|
|
stage = tokio::task::spawn_blocking(move || -> Result<Download> {
|
|
stage.append(offset, &bytes)?;
|
|
Ok(stage)
|
|
})
|
|
.await??;
|
|
}
|
|
guard()?;
|
|
tokio::task::spawn_blocking(move || stage.finish()).await??;
|
|
}
|
|
guard()?;
|
|
Ok(())
|
|
}
|