Add an HTTPS installer for the OPNsense agent and show its command in the WebUI
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
//! HTTPS installation of the OPNsense agent. The program files are a fixed
|
||||
//! bundle in the release configuration (`opnsense/` next to the Linux bootstrap
|
||||
//! file). The public script holds no credentials; the bundle is served only
|
||||
//! against a valid one-time enrollment token, until enrollment consumes it.
|
||||
use super::*;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
/// Files the installer needs; `rmm-opnsense-jobs` (script worker) is optional.
|
||||
const REQUIRED: [&str; 3] = ["agent.pyz", "collect.pyz", "discovery_agent"];
|
||||
const OPTIONAL: [&str; 1] = ["rmm-opnsense-jobs"];
|
||||
const MAX_FILE_BYTES: u64 = 32 * 1024 * 1024;
|
||||
|
||||
#[derive(Deserialize, serde::Serialize, Clone)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(super) struct File {
|
||||
sha256: String,
|
||||
bytes: u64,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Manifest {
|
||||
schema_version: u32,
|
||||
version: String,
|
||||
files: BTreeMap<String, File>,
|
||||
}
|
||||
|
||||
fn bundle_dir() -> Result<std::path::PathBuf, Error> {
|
||||
std::env::var_os("RMM_AGENT_BOOTSTRAP_LINUX_FILE")
|
||||
.and_then(|slot| FsPath::new(&slot).parent().map(|p| p.join("opnsense")))
|
||||
.ok_or_else(|| fail("BOOTSTRAP_NOT_CONFIGURED"))
|
||||
}
|
||||
|
||||
fn manifest() -> Result<Manifest, Error> {
|
||||
let raw = protected_file(&bundle_dir()?.join("manifest.json"), 8192)?;
|
||||
let m: Manifest = serde_json::from_slice(&raw).map_err(|_| fail("OPNSENSE_BUNDLE_INVALID"))?;
|
||||
valid(&m).then_some(m).ok_or_else(|| fail("OPNSENSE_BUNDLE_INVALID"))
|
||||
}
|
||||
|
||||
fn valid(m: &Manifest) -> bool {
|
||||
m.schema_version == 1
|
||||
&& (1..=64).contains(&m.version.len())
|
||||
&& m.version.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b))
|
||||
&& REQUIRED.iter().all(|name| m.files.contains_key(*name))
|
||||
&& m.files.iter().all(|(name, f)| {
|
||||
(REQUIRED.contains(&name.as_str()) || OPTIONAL.contains(&name.as_str()))
|
||||
&& rmm_domain::agent_update::digest(&f.sha256)
|
||||
&& (1..=MAX_FILE_BYTES).contains(&f.bytes)
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether the enrollment dialog can offer the OPNsense command.
|
||||
pub(super) fn available() -> bool {
|
||||
manifest().is_ok()
|
||||
}
|
||||
|
||||
pub(crate) async fn current(State(s): State<crate::State>, headers: HeaderMap) -> Result<Response, Error> {
|
||||
let tenant = token_tenant(&s, &headers).await?;
|
||||
let m = manifest()?;
|
||||
// The device gateway is the same for every platform; the Linux slot names it.
|
||||
let gateway = config(&s, Platform::LinuxX64, tenant, None).await?.gateway_origin;
|
||||
let ca = s.signer.as_ref().ok_or_else(missing)?.ca.to_pem().map_err(|_| invalid())?;
|
||||
let trust = device_trust_data(&s)?;
|
||||
Ok(private_json(json!({"schema_version":1,"tenant_id":tenant,"version":m.version,"gateway_origin":gateway,
|
||||
"ca_pem":String::from_utf8(ca).map_err(|_| invalid())?,
|
||||
"job_signing_public_key_hex":trust["job_signing_public_key_hex"],"files":m.files})))
|
||||
}
|
||||
|
||||
pub(crate) async fn file(
|
||||
State(s): State<crate::State>,
|
||||
headers: HeaderMap,
|
||||
Path(name): Path<String>,
|
||||
) -> Result<Response, Error> {
|
||||
token_tenant(&s, &headers).await?;
|
||||
let expected = manifest()?.files.remove(&name).ok_or_else(missing)?;
|
||||
let raw = protected_file(&bundle_dir()?.join(&name), MAX_FILE_BYTES)?;
|
||||
if raw.len() as u64 != expected.bytes || hex::encode(openssl::sha::sha256(&raw)) != expected.sha256 {
|
||||
return Err(fail("OPNSENSE_BUNDLE_INVALID"));
|
||||
}
|
||||
Ok(([("content-type", "application/octet-stream"), ("cache-control", "private, no-store"),
|
||||
("x-content-type-options", "nosniff")], raw).into_response())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn bundle(files: &[(&str, u64)]) -> Manifest {
|
||||
Manifest { schema_version: 1, version: "0.1.0-opnsense.6".into(),
|
||||
files: files.iter().map(|(n, b)| (n.to_string(), File { sha256: "a".repeat(64), bytes: *b })).collect() }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bundle_needs_the_program_files_and_accepts_only_known_names() {
|
||||
let base = [("agent.pyz", 10), ("collect.pyz", 10), ("discovery_agent", 10)];
|
||||
assert!(valid(&bundle(&base)));
|
||||
assert!(valid(&bundle(&[base[0], base[1], base[2], ("rmm-opnsense-jobs", 10)])));
|
||||
assert!(!valid(&bundle(&base[..2])));
|
||||
assert!(!valid(&bundle(&[base[0], base[1], base[2], ("../ca.pem", 10)])));
|
||||
assert!(!valid(&bundle(&[base[0], base[1], ("discovery_agent", 0)])));
|
||||
assert!(!valid(&bundle(&[base[0], base[1], ("discovery_agent", MAX_FILE_BYTES + 1)])));
|
||||
let mut bad = bundle(&base);
|
||||
bad.version = "1.0 ; rm".into();
|
||||
assert!(!valid(&bad));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user