Add crash-safe passive Windows certificate renewal
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -7,6 +7,7 @@ use rmm_agent_core::{
|
||||
jobs, ledger,
|
||||
reconcile::{self, SyncClock},
|
||||
};
|
||||
use rmm_agent_windows::certificate_rotation::{ProtectedKeyFiles, RotationStore};
|
||||
use rmm_agent_windows::{CAPABILITIES, job_binding::readonly_binding, journal::Journal, native};
|
||||
use rmm_domain::jobs::{Action, ExecutionPermitPayload};
|
||||
use rmm_protocol::*;
|
||||
@@ -411,6 +412,8 @@ struct Agent {
|
||||
identity: EnrollmentResponse,
|
||||
ca: Vec<u8>,
|
||||
journal: Journal,
|
||||
certificates: RotationStore,
|
||||
key_files: ProtectedKeyFiles,
|
||||
boot: String,
|
||||
instance: String,
|
||||
started: Instant,
|
||||
@@ -1213,7 +1216,11 @@ impl Agent {
|
||||
let msi = job.action.windows_msi().cloned();
|
||||
let address = self.config.connect_address;
|
||||
let ca = self.ca.clone();
|
||||
let state = self.config.state_directory.clone();
|
||||
let tls_identity = self.certificates.current(
|
||||
self.journal.connection(),
|
||||
&self.key_files,
|
||||
Utc::now().timestamp(),
|
||||
)?;
|
||||
let request_for_stage = request.clone();
|
||||
let binding_for_stage = binding.clone();
|
||||
let worker = tokio::spawn(async move {
|
||||
@@ -1225,9 +1232,12 @@ impl Agent {
|
||||
"MSI_DOWNLOAD_FENCED"
|
||||
);
|
||||
verified_window(&binding_for_stage, &request_for_stage, &clock)?;
|
||||
let cert = protected_read(&state.join("device.pem"), 32768)?;
|
||||
let key = protected_read(&state.join("device.key"), 16384)?;
|
||||
let tls = crate::https_channel::mtls_client(address, &ca, &cert, &key)?;
|
||||
let tls = crate::https_channel::mtls_client(
|
||||
address,
|
||||
&ca,
|
||||
&tls_identity.certificate_pem,
|
||||
&tls_identity.private_key_pem,
|
||||
)?;
|
||||
let response = tls
|
||||
.get(format!(
|
||||
"https://{}:{}/agent/v1/artifacts/{}?job_id={}&attempt_id={}",
|
||||
@@ -1493,6 +1503,15 @@ impl Agent {
|
||||
result
|
||||
}
|
||||
async fn session_inner(&mut self, stop: &AtomicBool) -> Result<()> {
|
||||
// Server commit may have fenced Hello even if its HTTP response was
|
||||
// lost. Recover the durable original CSR before requiring any channel.
|
||||
if self.certificates.has_pending(self.journal.connection())? {
|
||||
ensure!(
|
||||
!self.config.enable_jobs,
|
||||
"ROTATION_PENDING_REQUIRES_PASSIVE_RECOVERY"
|
||||
);
|
||||
self.renew_certificate().await?;
|
||||
}
|
||||
self.boot = native::boot_id().await?;
|
||||
let advertised = self.capabilities();
|
||||
let hello = self.envelope(
|
||||
@@ -1502,16 +1521,11 @@ impl Agent {
|
||||
capabilities: advertised.clone(),
|
||||
}),
|
||||
)?;
|
||||
let identity = crate::certificate_rotation::DeviceIdentity {
|
||||
certificate_pem: protected_read(
|
||||
&self.config.state_directory.join("device.pem"),
|
||||
32768,
|
||||
)?,
|
||||
private_key_pem: protected_read(
|
||||
&self.config.state_directory.join("device.key"),
|
||||
16384,
|
||||
)?,
|
||||
};
|
||||
let identity = self.certificates.current(
|
||||
self.journal.connection(),
|
||||
&self.key_files,
|
||||
Utc::now().timestamp(),
|
||||
)?;
|
||||
let mode = crate::wire_channel::Mode::parse(&self.config.transport)?;
|
||||
let mut channel = crate::wire_channel::connect(
|
||||
mode,
|
||||
@@ -1557,6 +1571,7 @@ impl Agent {
|
||||
let mut replay = Instant::now() - Duration::from_secs(10);
|
||||
let mut inventory = Instant::now();
|
||||
let mut update_at = Instant::now();
|
||||
let mut rotation_at = Instant::now();
|
||||
let mut heartbeat = Instant::now() - Duration::from_secs(60);
|
||||
let mut heartbeat_after = Duration::from_secs(30);
|
||||
let mut tick = tokio::time::interval(Duration::from_secs(1));
|
||||
@@ -1642,6 +1657,21 @@ impl Agent {
|
||||
if stop.load(Ordering::SeqCst) {
|
||||
continue;
|
||||
}
|
||||
// This slice renews only passive clients. Job admission and helper
|
||||
// lifecycle are not extended by certificate maintenance.
|
||||
if !self.config.enable_jobs && rotation_at.elapsed() >= Duration::from_secs(30) {
|
||||
rotation_at = Instant::now();
|
||||
let now = Utc::now().timestamp();
|
||||
if self
|
||||
.certificates
|
||||
.due(self.journal.connection(), &self.key_files, now)?
|
||||
{
|
||||
self.renew_certificate().await?;
|
||||
// The server fences the previous connection generation.
|
||||
// A new Hello/Sync uses the newly selected pair.
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
if heartbeat.elapsed() >= heartbeat_after {
|
||||
let payload = Payload::Heartbeat(Heartbeat {
|
||||
instance_id: self.instance.clone(),
|
||||
@@ -1677,6 +1707,38 @@ impl Agent {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
async fn renew_certificate(&mut self) -> Result<()> {
|
||||
ensure!(!self.config.enable_jobs, "PASSIVE_RENEWAL_ONLY");
|
||||
self.close_gate();
|
||||
let request = self.certificates.prepare(
|
||||
self.journal.connection_mut(),
|
||||
&self.key_files,
|
||||
Utc::now().timestamp(),
|
||||
)?;
|
||||
let client = crate::https_channel::mtls_client(
|
||||
self.config.connect_address,
|
||||
&self.ca,
|
||||
&request.identity.certificate_pem,
|
||||
&request.identity.private_key_pem,
|
||||
)?;
|
||||
let response = client
|
||||
.post(format!(
|
||||
"https://{}:{}/agent/v1/certificates/renew",
|
||||
crate::https_channel::server_name()?,
|
||||
self.config.connect_address.port()
|
||||
))
|
||||
.json(&serde_json::json!({"csr_pem":request.csr_pem}))
|
||||
.send()
|
||||
.await?;
|
||||
let bytes = crate::https_channel::bounded_body(response, 65536, "application/json").await?;
|
||||
self.certificates.install(
|
||||
self.journal.connection_mut(),
|
||||
&self.key_files,
|
||||
&request,
|
||||
&bytes,
|
||||
Utc::now().timestamp(),
|
||||
)
|
||||
}
|
||||
}
|
||||
fn service_run(stop: &'static AtomicBool) -> Result<()> {
|
||||
let arguments: Vec<String> = std::env::args().skip(1).collect();
|
||||
@@ -1719,6 +1781,11 @@ fn service_run(stop: &'static AtomicBool) -> Result<()> {
|
||||
import_legacy_inventory(&mut journal)?;
|
||||
}
|
||||
ensure!(!config.enable_jobs || binding.is_some(),"ENABLED_JOBS_REQUIRE_BINDING");
|
||||
let certificates=RotationStore::new(Uuid::parse_str(&identity.agent_id)?,binding.as_ref().map_or(1,|b|b.enrollment_generation),ca.clone())?;
|
||||
let key_files=ProtectedKeyFiles::open(&config.state_directory)?;
|
||||
certificates.initialize(journal.connection_mut(),&key_files,&protected_read(&config.state_directory.join("device.pem"),32768)?,Utc::now().timestamp())?;
|
||||
// SQLite and sidecars inherit the same service-SID-only state ACL.
|
||||
for child in fs::read_dir(&config.state_directory)? { let child=child?;native::private_path(&child.path(),child.file_type()?.is_dir())?; }
|
||||
let (ledger,ledger_file)=match &binding {Some(binding)=>{let (db,file)=open_ledger(&config.state_directory,binding)?;(Some(db),Some(file))},None=>(None,None)};
|
||||
let clock=JobClock::new(config.enable_jobs);
|
||||
let sync=if config.enable_jobs {binding.clone().map(|binding|reconcile::ClientSync::new(binding).with_helper_identity(reconcile::HelperIdentity::System))} else {None};
|
||||
@@ -1728,6 +1795,8 @@ fn service_run(stop: &'static AtomicBool) -> Result<()> {
|
||||
identity,
|
||||
ca,
|
||||
journal,
|
||||
certificates,
|
||||
key_files,
|
||||
boot,
|
||||
instance: Uuid::new_v4().to_string(),
|
||||
started: Instant::now(), binding, ledger, _ledger_file:ledger_file, clock, sync,
|
||||
|
||||
Reference in New Issue
Block a user