Add reviewed Windows scope completion and idle certificate renewal; deploy completion API
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -0,0 +1,473 @@
|
||||
//! Explicit passive-certificate qualification on a separate fixture identity.
|
||||
//! Run only as a child of the existing LocalService agent, after WebUI approval.
|
||||
//! No original agent state, certificate, service configuration or key is read.
|
||||
#[cfg(windows)]
|
||||
mod native {
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use chrono::Utc;
|
||||
use prost::Message;
|
||||
use rcgen::{CertificateParams, DnType, KeyPair};
|
||||
use rmm_agent_windows::{
|
||||
certificate_rotation::{DeviceIdentity, ProtectedKeyFiles, RotationStore},
|
||||
journal::Journal,
|
||||
native,
|
||||
};
|
||||
use rmm_protocol::{
|
||||
ClientHello, EnrollmentRequest, EnrollmentResponse, Envelope, Heartbeat, MessageBatch,
|
||||
Payload,
|
||||
};
|
||||
use serde_json::{Value, json};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::{fs, io::Write, net::SocketAddr, path::Path, time::Duration};
|
||||
use uuid::Uuid;
|
||||
|
||||
const SERVER: &str = "discovery.ease-technologies.de";
|
||||
const PERMANENT_AGENT: &str = "63ea15b2-0643-403d-b5b4-cba1288dbea3";
|
||||
|
||||
fn hash(bytes: &[u8]) -> String {
|
||||
format!("{:x}", Sha256::digest(bytes))
|
||||
}
|
||||
fn read(path: &Path, maximum: usize) -> Result<Vec<u8>> {
|
||||
native::private_path(path, false)?;
|
||||
ensure!(
|
||||
fs::metadata(path)?.len() <= maximum as u64,
|
||||
"FIXTURE_FILE_LIMIT"
|
||||
);
|
||||
Ok(fs::read(path)?)
|
||||
}
|
||||
fn create(path: &Path, bytes: &[u8]) -> Result<()> {
|
||||
let mut file = fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(path)?;
|
||||
// The fixture directory must provide its inherited service DACL before
|
||||
// any private bytes are written, exactly like ProtectedKeyFiles.
|
||||
native::private_path(path, false)?;
|
||||
file.write_all(bytes)?;
|
||||
file.sync_all()?;
|
||||
Ok(())
|
||||
}
|
||||
fn client(ca: &[u8], identity: Option<&DeviceIdentity>) -> Result<reqwest::Client> {
|
||||
let address: SocketAddr = "88.99.80.202:443".parse()?;
|
||||
let mut builder = reqwest::Client::builder()
|
||||
.https_only(true)
|
||||
.tls_built_in_root_certs(false)
|
||||
.add_root_certificate(reqwest::Certificate::from_pem(ca)?)
|
||||
.resolve(SERVER, address)
|
||||
.no_proxy()
|
||||
.redirect(reqwest::redirect::Policy::none())
|
||||
.timeout(Duration::from_secs(10));
|
||||
if let Some(identity) = identity {
|
||||
let mut pair = identity.certificate_pem.clone();
|
||||
pair.extend_from_slice(&identity.private_key_pem);
|
||||
builder = builder.identity(reqwest::Identity::from_pem(&pair)?);
|
||||
}
|
||||
Ok(builder.build()?)
|
||||
}
|
||||
async fn body(mut response: reqwest::Response, content_type: &str) -> Result<Vec<u8>> {
|
||||
ensure!(response.status().as_u16() == 200, "FIXTURE_HTTP_STATUS");
|
||||
ensure!(
|
||||
response
|
||||
.headers()
|
||||
.get(reqwest::header::CONTENT_TYPE)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
== Some(content_type),
|
||||
"FIXTURE_CONTENT_TYPE"
|
||||
);
|
||||
let mut result = Vec::new();
|
||||
while let Some(chunk) = response.chunk().await? {
|
||||
ensure!(
|
||||
result.len().saturating_add(chunk.len()) <= 65536,
|
||||
"FIXTURE_RESPONSE_LIMIT"
|
||||
);
|
||||
result.extend_from_slice(&chunk);
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
fn certificate_hash(pem: &[u8]) -> Result<String> {
|
||||
let mut cursor = std::io::Cursor::new(pem);
|
||||
let mut certificates = rustls_pemfile::certs(&mut cursor);
|
||||
Ok(hash(
|
||||
certificates
|
||||
.next()
|
||||
.context("FIXTURE_CERTIFICATE")??
|
||||
.as_ref(),
|
||||
))
|
||||
}
|
||||
fn envelope(
|
||||
journal: &mut Journal,
|
||||
agent: &str,
|
||||
boot: &str,
|
||||
generation: u64,
|
||||
payload: Payload,
|
||||
) -> Result<Envelope> {
|
||||
Ok(Envelope {
|
||||
protocol_major: 1,
|
||||
protocol_minor: 0,
|
||||
message_id: Uuid::new_v4().to_string(),
|
||||
agent_id: agent.into(),
|
||||
enrollment_generation: 1,
|
||||
connection_generation: generation,
|
||||
boot_id: boot.into(),
|
||||
sequence: journal.next_sequence()?,
|
||||
observed_at: Utc::now().timestamp_millis(),
|
||||
payload: Some(payload),
|
||||
})
|
||||
}
|
||||
async fn send(client: &reqwest::Client, envelope: &Envelope) -> Result<Envelope> {
|
||||
let bytes = body(
|
||||
client
|
||||
.post(format!("https://{SERVER}/agent/v1/messages"))
|
||||
.header(reqwest::header::CONTENT_TYPE, "application/x-protobuf")
|
||||
.body(
|
||||
MessageBatch {
|
||||
protocol_major: 1,
|
||||
messages: vec![envelope.clone()],
|
||||
}
|
||||
.encode_to_vec(),
|
||||
)
|
||||
.send()
|
||||
.await?,
|
||||
"application/x-protobuf",
|
||||
)
|
||||
.await?;
|
||||
let replies = MessageBatch::decode(bytes.as_slice())?;
|
||||
ensure!(
|
||||
replies.protocol_major == 1 && replies.messages.len() == 1,
|
||||
"FIXTURE_EXACT_REPLY"
|
||||
);
|
||||
let reply = replies
|
||||
.messages
|
||||
.into_iter()
|
||||
.next()
|
||||
.context("FIXTURE_REPLY")?;
|
||||
ensure!(
|
||||
reply.protocol_major == 1
|
||||
&& reply.protocol_minor == 0
|
||||
&& reply.agent_id == envelope.agent_id
|
||||
&& reply.enrollment_generation == 1
|
||||
&& reply.boot_id == envelope.boot_id
|
||||
&& reply.sequence == envelope.sequence,
|
||||
"FIXTURE_REPLY_BINDING"
|
||||
);
|
||||
Ok(reply)
|
||||
}
|
||||
async fn connect(
|
||||
client: &reqwest::Client,
|
||||
journal: &mut Journal,
|
||||
agent: &str,
|
||||
boot: &str,
|
||||
) -> Result<u64> {
|
||||
let hello = envelope(
|
||||
journal,
|
||||
agent,
|
||||
boot,
|
||||
0,
|
||||
Payload::ClientHello(ClientHello {
|
||||
agent_version: "certificate-acceptance/1".into(),
|
||||
capabilities: vec!["heartbeat/1".into()],
|
||||
}),
|
||||
)?;
|
||||
let reply = send(client, &hello).await?;
|
||||
let Some(Payload::ServerHello(server)) = reply.payload else {
|
||||
anyhow::bail!("FIXTURE_HELLO_REQUIRED")
|
||||
};
|
||||
ensure!(
|
||||
server.connection_generation > 0
|
||||
&& reply.connection_generation == server.connection_generation,
|
||||
"FIXTURE_GENERATION"
|
||||
);
|
||||
let heartbeat = envelope(
|
||||
journal,
|
||||
agent,
|
||||
boot,
|
||||
server.connection_generation,
|
||||
Payload::Heartbeat(Heartbeat {
|
||||
instance_id: Uuid::new_v4().to_string(),
|
||||
uptime_seconds: 1,
|
||||
}),
|
||||
)?;
|
||||
let acknowledgement = send(client, &heartbeat).await?;
|
||||
ensure!(
|
||||
matches!(acknowledgement.payload, Some(Payload::Ack(ref a)) if a.message_id == heartbeat.message_id),
|
||||
"FIXTURE_HEARTBEAT_ACK"
|
||||
);
|
||||
ensure!(
|
||||
acknowledgement.connection_generation == server.connection_generation,
|
||||
"FIXTURE_ACK_GENERATION"
|
||||
);
|
||||
Ok(server.connection_generation)
|
||||
}
|
||||
async fn renew(root: &Path, phase: &mut &'static str) -> Result<Value> {
|
||||
*phase = "enrollment";
|
||||
create(
|
||||
&root.join("started.json"),
|
||||
b"{\"schema_version\":1,\"no_restart\":true}",
|
||||
)?;
|
||||
let ca = read(&root.join("ca.pem"), 32768)?;
|
||||
let key = KeyPair::generate()?;
|
||||
create(&root.join("device.key"), key.serialize_pem().as_bytes())?;
|
||||
let mut parameters = CertificateParams::default();
|
||||
parameters
|
||||
.distinguished_name
|
||||
.push(DnType::CommonName, "certificate-acceptance");
|
||||
let csr = parameters.serialize_request(&key)?.pem()?;
|
||||
let token = String::from_utf8(read(&root.join("enrollment-token"), 128)?)?
|
||||
.trim()
|
||||
.to_owned();
|
||||
ensure!(
|
||||
token.len() == 64 && token.bytes().all(|b| b.is_ascii_hexdigit()),
|
||||
"FIXTURE_ENROLLMENT_TOKEN"
|
||||
);
|
||||
let enrolled: EnrollmentResponse = serde_json::from_slice(
|
||||
&body(
|
||||
client(&ca, None)?
|
||||
.post(format!("https://{SERVER}/agent/v1/enroll"))
|
||||
.json(&EnrollmentRequest {
|
||||
token,
|
||||
csr_pem: csr,
|
||||
})
|
||||
.send()
|
||||
.await?,
|
||||
"application/json",
|
||||
)
|
||||
.await?,
|
||||
)?;
|
||||
let agent = Uuid::parse_str(&enrolled.agent_id)?;
|
||||
ensure!(
|
||||
!agent.is_nil()
|
||||
&& enrolled.agent_id != PERMANENT_AGENT
|
||||
&& !Uuid::parse_str(&enrolled.asset_id)?.is_nil(),
|
||||
"SEPARATE_FIXTURE_IDENTITY"
|
||||
);
|
||||
create(
|
||||
&root.join("device.pem"),
|
||||
enrolled.certificate_pem.as_bytes(),
|
||||
)?;
|
||||
create(&root.join("identity.json"), &serde_json::to_vec(&enrolled)?)?;
|
||||
fs::remove_file(root.join("enrollment-token"))?;
|
||||
let original_key_hash = hash(&read(&root.join("device.key"), 16384)?);
|
||||
let original_certificate_hash = certificate_hash(enrolled.certificate_pem.as_bytes())?;
|
||||
let store = RotationStore::new(agent, 1, ca.clone())?;
|
||||
let mut journal = Journal::open(root)?;
|
||||
journal.pin_identity(&enrolled.agent_id)?;
|
||||
let keys = ProtectedKeyFiles::open(root)?;
|
||||
store.initialize(
|
||||
journal.connection_mut(),
|
||||
&keys,
|
||||
enrolled.certificate_pem.as_bytes(),
|
||||
Utc::now().timestamp(),
|
||||
)?;
|
||||
ensure!(
|
||||
!store.due(journal.connection(), &keys, Utc::now().timestamp())?,
|
||||
"FRESH_CERTIFICATE_NOT_DUE"
|
||||
);
|
||||
let original = store.current(journal.connection(), &keys, Utc::now().timestamp())?;
|
||||
let old_client = client(&ca, Some(&original))?;
|
||||
let boot = native::boot_id().await?;
|
||||
let original_generation =
|
||||
connect(&old_client, &mut journal, &enrolled.agent_id, &boot).await?;
|
||||
*phase = "renewal_response_discard";
|
||||
let pending = store.prepare(journal.connection_mut(), &keys, Utc::now().timestamp())?;
|
||||
let csr_hash = hash(pending.csr_pem.as_bytes());
|
||||
let first = body(
|
||||
old_client
|
||||
.post(format!("https://{SERVER}/agent/v1/certificates/renew"))
|
||||
.json(&json!({"csr_pem":pending.csr_pem}))
|
||||
.send()
|
||||
.await?,
|
||||
"application/json",
|
||||
)
|
||||
.await?;
|
||||
let first_hash = hash(&first);
|
||||
// Deliberately do not install the first real server response. Close the
|
||||
// actual SQLite/key handles; a reopened journal must reuse the CSR.
|
||||
drop(pending);
|
||||
drop(first);
|
||||
drop(keys);
|
||||
drop(journal);
|
||||
*phase = "renewal_reopen";
|
||||
let mut journal = Journal::open(root)?;
|
||||
let keys = ProtectedKeyFiles::open(root)?;
|
||||
let pending = store.prepare(journal.connection_mut(), &keys, Utc::now().timestamp())?;
|
||||
ensure!(
|
||||
hash(pending.csr_pem.as_bytes()) == csr_hash,
|
||||
"EXACT_PENDING_CSR_AFTER_REOPEN"
|
||||
);
|
||||
let second = body(
|
||||
client(&ca, Some(&pending.identity))?
|
||||
.post(format!("https://{SERVER}/agent/v1/certificates/renew"))
|
||||
.json(&json!({"csr_pem":pending.csr_pem}))
|
||||
.send()
|
||||
.await?,
|
||||
"application/json",
|
||||
)
|
||||
.await?;
|
||||
ensure!(
|
||||
hash(&second) == first_hash,
|
||||
"EXACT_IDEMPOTENT_SERVER_RESPONSE"
|
||||
);
|
||||
store.install(
|
||||
journal.connection_mut(),
|
||||
&keys,
|
||||
&pending,
|
||||
&second,
|
||||
Utc::now().timestamp(),
|
||||
)?;
|
||||
ensure!(
|
||||
!store.has_pending(journal.connection())?,
|
||||
"RENEWAL_COMMIT_PENDING"
|
||||
);
|
||||
drop(pending);
|
||||
drop(keys);
|
||||
drop(journal);
|
||||
*phase = "new_certificate_connection";
|
||||
let mut journal = Journal::open(root)?;
|
||||
let keys = ProtectedKeyFiles::open(root)?;
|
||||
let current = store.current(journal.connection(), &keys, Utc::now().timestamp())?;
|
||||
let renewed_hash = certificate_hash(¤t.certificate_pem)?;
|
||||
ensure!(
|
||||
renewed_hash != original_certificate_hash,
|
||||
"NEW_CERTIFICATE_REQUIRED"
|
||||
);
|
||||
let renewed_generation = connect(
|
||||
&client(&ca, Some(¤t))?,
|
||||
&mut journal,
|
||||
&enrolled.agent_id,
|
||||
&boot,
|
||||
)
|
||||
.await?;
|
||||
ensure!(
|
||||
renewed_generation > original_generation,
|
||||
"RENEWAL_RECONNECTED_GENERATION"
|
||||
);
|
||||
ensure!(
|
||||
hash(&read(&root.join("device.key"), 16384)?) == original_key_hash
|
||||
&& certificate_hash(&read(&root.join("device.pem"), 32768)?)?
|
||||
== original_certificate_hash,
|
||||
"ORIGINAL_FIXTURE_FILES_PRESERVED"
|
||||
);
|
||||
Ok(
|
||||
json!({"schema_version":1,"verified":true,"identity_scope":"separate_fixture","agent_id":agent,
|
||||
"asset_id":enrolled.asset_id,"boot_id":boot,"execution_sid":"S-1-5-19","service_sid":native::service_sid()?,
|
||||
"original_certificate_sha256":original_certificate_hash,"renewed_certificate_sha256":renewed_hash,
|
||||
"csr_sha256":csr_hash,"response_sha256":first_hash,"same_csr_after_reopen":true,
|
||||
"same_server_response":true,"new_certificate_mtls_hello_and_heartbeat":true,
|
||||
"original_generation":original_generation,"renewed_generation":renewed_generation,
|
||||
"original_fixture_files_preserved":true,"permanent_identity_read":false,
|
||||
"scheduler_age_trigger_tested":false,"jobs_created":false,"completed_at":Utc::now()}),
|
||||
)
|
||||
}
|
||||
async fn revoked_read(root: &Path, phase: &mut &'static str) -> Result<Value> {
|
||||
*phase = "revoked_identity_denial";
|
||||
let before: Value =
|
||||
serde_json::from_slice(&read(&root.join("renewal-result.json"), 16384)?)?;
|
||||
ensure!(before["verified"] == true, "PRIOR_RENEWAL_REQUIRED");
|
||||
let enrolled: EnrollmentResponse =
|
||||
serde_json::from_slice(&read(&root.join("identity.json"), 32768)?)?;
|
||||
ensure!(
|
||||
enrolled.agent_id != PERMANENT_AGENT && before["agent_id"] == enrolled.agent_id,
|
||||
"SAME_SEPARATE_FIXTURE"
|
||||
);
|
||||
let ca = read(&root.join("ca.pem"), 32768)?;
|
||||
let store = RotationStore::new(Uuid::parse_str(&enrolled.agent_id)?, 1, ca.clone())?;
|
||||
let mut journal = Journal::open(root)?;
|
||||
let keys = ProtectedKeyFiles::open(root)?;
|
||||
let identity = store.current(journal.connection(), &keys, Utc::now().timestamp())?;
|
||||
ensure!(
|
||||
before["renewed_certificate_sha256"] == certificate_hash(&identity.certificate_pem)?,
|
||||
"SAME_RENEWED_CERTIFICATE"
|
||||
);
|
||||
let hello = envelope(
|
||||
&mut journal,
|
||||
&enrolled.agent_id,
|
||||
&native::boot_id().await?,
|
||||
0,
|
||||
Payload::ClientHello(ClientHello {
|
||||
agent_version: "certificate-acceptance/1".into(),
|
||||
capabilities: vec!["heartbeat/1".into()],
|
||||
}),
|
||||
)?;
|
||||
let response = client(&ca, Some(&identity))?
|
||||
.post(format!("https://{SERVER}/agent/v1/messages"))
|
||||
.header(reqwest::header::CONTENT_TYPE, "application/x-protobuf")
|
||||
.body(
|
||||
MessageBatch {
|
||||
protocol_major: 1,
|
||||
messages: vec![hello],
|
||||
}
|
||||
.encode_to_vec(),
|
||||
)
|
||||
.send()
|
||||
.await?;
|
||||
// A TLS or network failure is not accepted as proof of revocation.
|
||||
ensure!(
|
||||
response.status().as_u16() == 401,
|
||||
"EXACT_AUTHENTICATED_REVOCATION_DENIAL"
|
||||
);
|
||||
Ok(
|
||||
json!({"schema_version":1,"verified":true,"agent_id":enrolled.agent_id,
|
||||
"execution_sid":"S-1-5-19","http_status":401,"permanent_identity_read":false,
|
||||
"jobs_created":false,"completed_at":Utc::now()}),
|
||||
)
|
||||
}
|
||||
pub fn main() -> Result<()> {
|
||||
native::require_local_service()?;
|
||||
let arguments: Vec<_> = std::env::args().skip(1).collect();
|
||||
ensure!(arguments.len() == 2, "FIXTURE_ARGUMENTS");
|
||||
let id = Uuid::parse_str(&arguments[1])?;
|
||||
ensure!(
|
||||
!id.is_nil() && id.to_string() == arguments[1],
|
||||
"FIXTURE_CASE_UUID"
|
||||
);
|
||||
let root = std::path::PathBuf::from(r"C:\ProgramData\RustDiscovery\certificate-acceptance")
|
||||
.join(id.to_string());
|
||||
native::private_path(&root, true)?;
|
||||
let report = match arguments[0].as_str() {
|
||||
"renew" => root.join("renewal-result.json"),
|
||||
"revoked-read" => root.join("revocation-result.json"),
|
||||
_ => anyhow::bail!("FIXTURE_MODE"),
|
||||
};
|
||||
ensure!(!report.try_exists()?, "FIXTURE_REPORT_ALREADY_PRESENT");
|
||||
let mut phase = "initialization";
|
||||
let runtime = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()?;
|
||||
let result = runtime.block_on(async {
|
||||
if arguments[0] == "renew" {
|
||||
renew(&root, &mut phase).await
|
||||
} else {
|
||||
revoked_read(&root, &mut phase).await
|
||||
}
|
||||
});
|
||||
match result {
|
||||
Ok(value) => {
|
||||
create(&report, &serde_json::to_vec(&value)?)?;
|
||||
println!("{}", serde_json::to_string(&value)?);
|
||||
Ok(())
|
||||
}
|
||||
Err(_) => {
|
||||
// No network/library error text, credential or private key goes
|
||||
// into the approved job's output or its public receipt.
|
||||
let failure = json!({"schema_version":1,"verified":false,"phase":phase,
|
||||
"error_code":"CERTIFICATE_ACCEPTANCE_FAILED","restart_allowed":false});
|
||||
create(&report, &serde_json::to_vec(&failure)?)?;
|
||||
println!("{}", serde_json::to_string(&failure)?);
|
||||
anyhow::bail!("CERTIFICATE_ACCEPTANCE_FAILED")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn main() {
|
||||
if native::main().is_err() {
|
||||
eprintln!("CERTIFICATE_ACCEPTANCE_FAILED");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
fn main() {
|
||||
eprintln!("Native Windows LocalService fixture only");
|
||||
std::process::exit(1);
|
||||
}
|
||||
@@ -160,8 +160,134 @@ mod windows {
|
||||
ensure!(bytes.len() as u64 <= max, "public file changed");
|
||||
Ok(bytes)
|
||||
}
|
||||
fn complete_echo(apply: bool) -> Result<()> {
|
||||
let readers = [ipc::service_sid(SERVICE_NAME)?];
|
||||
let _root = PinnedFile::open(Path::new(ROOT), true, &readers, &[], false)?;
|
||||
let mut pin = PinnedFile::open(
|
||||
&Path::new(ROOT).join("binding.json"),
|
||||
false,
|
||||
&readers,
|
||||
&[],
|
||||
false,
|
||||
)?;
|
||||
let bytes = data(&mut pin.file, 8192)?;
|
||||
ensure!(hash(&bytes) == BINDING_SHA256, "changed job binding");
|
||||
let binding: Binding = serde_json::from_slice(&bytes)?;
|
||||
let (settings, _pins) = helper_system::read_settings()?;
|
||||
let mut config = PinnedFile::open(
|
||||
&Path::new(ROOT).join("config.json"),
|
||||
false,
|
||||
&readers,
|
||||
&[],
|
||||
false,
|
||||
)?;
|
||||
ensure!(
|
||||
config.sha256()? == PASSIVE && !settings.enable_effects && !settings.allow_reboot,
|
||||
"restore original passive pair first"
|
||||
);
|
||||
let stopped = both_stopped()?;
|
||||
ensure!(
|
||||
!apply || stopped,
|
||||
"both services must be stopped by the operator"
|
||||
);
|
||||
let mut locks = vec![];
|
||||
if stopped {
|
||||
for path in [
|
||||
Path::new(STATE).join("agent.lock"),
|
||||
Path::new(helper_system::STATE_ROOT).join("executor.lock"),
|
||||
] {
|
||||
let file = leaf(&path)?;
|
||||
file.try_lock_exclusive().context("service lock busy")?;
|
||||
locks.push(file);
|
||||
}
|
||||
ensure!(both_stopped()?, "service started during completion");
|
||||
}
|
||||
native::private_path(Path::new(STATE), true)?;
|
||||
for p in [AGENT_DB, LOCAL_DB] {
|
||||
native::private_path(Path::new(p), false)?;
|
||||
}
|
||||
let helper_sid = [ipc::service_sid(ipc::HELPER_SERVICE)?];
|
||||
let _helper_state = PinnedFile::open_read_shared(
|
||||
Path::new(helper_system::STATE_ROOT),
|
||||
true,
|
||||
&[],
|
||||
&helper_sid,
|
||||
)?;
|
||||
let helper_file =
|
||||
PinnedFile::open_read_shared(Path::new(HELPER_DB), false, &[], &helper_sid)?;
|
||||
let mut agent = Db::open(AGENT_DB, apply, stopped)?;
|
||||
let consumption = Db::open(LOCAL_DB, false, stopped)?;
|
||||
let helper = Db::open(HELPER_DB, false, stopped)?;
|
||||
let (ledger, lock, pin): (Vec<u8>, Vec<u8>, String) = helper.db.query_row(
|
||||
"SELECT ledger,executor_lock,binding_sha256 FROM windows_helper_files WHERE id=1",
|
||||
[],
|
||||
|r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
|
||||
)?;
|
||||
ensure!(
|
||||
serde_json::from_slice::<ipc::FileIdentity>(&ledger)? == helper_file.identity
|
||||
&& pin == ipc::binding_hash(&settings.binding)?,
|
||||
"helper journal or binding changed"
|
||||
);
|
||||
let witness = rmm_agent_windows::echo_completion::witness(
|
||||
&agent.db,
|
||||
&consumption.db,
|
||||
&helper.db,
|
||||
&binding,
|
||||
&settings.binding,
|
||||
)?;
|
||||
println!("{}", serde_json::to_string(&witness)?);
|
||||
if apply {
|
||||
ensure!(
|
||||
serde_json::from_slice::<ipc::FileIdentity>(&lock)?
|
||||
== ipc::file_identity(&locks[1])?,
|
||||
"helper lock identity changed"
|
||||
);
|
||||
let path =
|
||||
Path::new(r"C:\ProgramData\RustDiscoveryBootstrap\windows-echo-completion.json");
|
||||
let _folder = PinnedFile::open(
|
||||
path.parent().context("completion folder")?,
|
||||
true,
|
||||
&[],
|
||||
&[],
|
||||
false,
|
||||
)?;
|
||||
let mut file = PinnedFile::open(path, false, &[], &[], false)?;
|
||||
let receipt = data(&mut file.file, 131072)?;
|
||||
let boot = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()?
|
||||
.block_on(native::boot_id())?;
|
||||
let boot = uuid::Uuid::parse_str(&boot)?;
|
||||
ensure!(both_stopped()?, "service started during completion");
|
||||
let pair = rmm_agent_windows::echo_completion::LocalPair {
|
||||
consumption: &consumption.db,
|
||||
helper: &helper.db,
|
||||
binding: &binding,
|
||||
helper_binding: &settings.binding,
|
||||
};
|
||||
rmm_agent_windows::echo_completion::apply(
|
||||
&mut agent.db,
|
||||
pair,
|
||||
&receipt,
|
||||
boot,
|
||||
chrono::Utc::now(),
|
||||
)?;
|
||||
println!(
|
||||
"additional echo restriction retired; original history retained; ordinary signed pair sync and each WebUI job approval remain required"
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn run() -> Result<()> {
|
||||
let args: Vec<_> = std::env::args().skip(1).collect();
|
||||
if args.len() == 1
|
||||
&& matches!(
|
||||
args[0].as_str(),
|
||||
"echo-completion-read" | "echo-completion-apply"
|
||||
)
|
||||
{
|
||||
return complete_echo(args[0] == "echo-completion-apply");
|
||||
}
|
||||
ensure!(
|
||||
args.len() == 1
|
||||
&& matches!(
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
//! Read-only native updater OS-port preflight. No activation/capability proof.
|
||||
#[cfg(all(windows, feature = "system-helper"))]
|
||||
fn main() -> anyhow::Result<()> {
|
||||
use rmm_agent_core::helper_reconcile::BootSource;
|
||||
use rmm_agent_windows::{
|
||||
helper_system,
|
||||
update_native::{Budget, Role, files, services::Running},
|
||||
};
|
||||
use std::time::Duration;
|
||||
use uuid::Uuid;
|
||||
struct Boot(Uuid);
|
||||
impl BootSource for Boot {
|
||||
fn current_boot(&self) -> anyhow::Result<Uuid> {
|
||||
Ok(self.0)
|
||||
}
|
||||
}
|
||||
let runtime = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()?;
|
||||
let boot = Boot(
|
||||
runtime
|
||||
.block_on(rmm_agent_windows::native::boot_id())?
|
||||
.parse()?,
|
||||
);
|
||||
let budget = Budget::new(&boot, boot.0, Duration::from_secs(30))?;
|
||||
let (settings, _guards) = helper_system::read_settings()?;
|
||||
let mut running = Running::observe([settings.agent.clone(), settings.helper.clone()], &budget)?;
|
||||
let mut files = Vec::new();
|
||||
for role in [
|
||||
Role::Agent,
|
||||
Role::Helper,
|
||||
Role::Settings,
|
||||
Role::Pin,
|
||||
Role::TimeProfile,
|
||||
] {
|
||||
files.push(serde_json::json!({"role":role,"proof":files::observe(role, &budget)?}));
|
||||
}
|
||||
running.check(&budget)?;
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string(&serde_json::json!({
|
||||
"schema_version":1,"read_only":true,"boot_id":boot.0,
|
||||
"services":running.proofs(),"files":files,"native_apply_tested":false,
|
||||
"job_created":false,"capability_qualified":false
|
||||
}))?
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(not(all(windows, feature = "system-helper")))]
|
||||
fn main() {
|
||||
eprintln!("Windows with system-helper feature required; no test executed");
|
||||
std::process::exit(2);
|
||||
}
|
||||
Reference in New Issue
Block a user