Add versioned script library, reconcile permit refusals and compact helper captures
This commit is contained in:
+32
-11
@@ -353,13 +353,8 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
anyhow::bail!("UNSOLICITED_PERMIT")
|
||||
};
|
||||
if !permit.denial_code.is_empty() {
|
||||
// A denial is not proof that an earlier reserved grant never existed.
|
||||
self.record(
|
||||
generation,
|
||||
&offer,
|
||||
"",
|
||||
rmm_privileged_helper::Outcome::unknown("PERMIT_DENIED_RECONCILIATION_REQUIRED"),
|
||||
)?;
|
||||
jobs::denied_request(&self.db, &offer, &permit)?;
|
||||
self.close_gate();
|
||||
return Ok(());
|
||||
}
|
||||
ensure!(
|
||||
@@ -621,7 +616,7 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
let hello = self.envelope(
|
||||
0,
|
||||
Payload::ClientHello(ClientHello {
|
||||
agent_version: env!("CARGO_PKG_VERSION").into(),
|
||||
agent_version: env::var("RMM_INSTALLED_RELEASE_VERSION").unwrap_or_else(|_| env!("CARGO_PKG_VERSION").into()),
|
||||
capabilities: advertised.clone(),
|
||||
}),
|
||||
)?;
|
||||
@@ -655,6 +650,7 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
self.queue_update_observation(generation)?;
|
||||
self.replay(&ws, generation).await?;
|
||||
let mut needs_sync = false;
|
||||
let mut permit_after = Instant::now();
|
||||
if self.sync.is_some()
|
||||
&& let Err(error) = self.start_sync(&ws, generation).await
|
||||
{
|
||||
@@ -721,7 +717,13 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
tx.commit()?;
|
||||
needs_sync|=fresh;self.replay(&ws, generation).await?;
|
||||
}
|
||||
Some(Payload::ExecutionPermit(permit)) => self.permit(generation, permit, self.ready(generation,false))?,
|
||||
Some(Payload::ExecutionPermit(permit)) => {
|
||||
if !permit.denial_code.is_empty() {
|
||||
needs_sync = true;
|
||||
permit_after = Instant::now() + Duration::from_secs(30);
|
||||
}
|
||||
self.permit(generation, permit, self.ready(generation,false))?;
|
||||
},
|
||||
Some(Payload::CancelRequest(cancel)) => {
|
||||
let row: Option<(Vec<u8>, String, String)> = {
|
||||
use rusqlite::OptionalExtension;
|
||||
@@ -795,7 +797,7 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
if self.prepared.is_none() && self.sync.is_some() && (!busy||own_update) && health_task.is_none() && (needs_sync||sync_at.elapsed()>=Duration::from_secs(40)||!self.ready(generation,false)) {
|
||||
if let Err(error)=self.start_sync(&ws,generation).await{report_closed_gate(&error);self.close_gate();needs_sync=true;}else{needs_sync=false;}sync_at=Instant::now();
|
||||
}
|
||||
if !needs_sync && self.prepared.is_none() && self.ready(generation,false) && jobs::events(&self.db)?.is_empty() && self.binding.is_some()
|
||||
if Instant::now() >= permit_after && !needs_sync && self.prepared.is_none() && self.ready(generation,false) && jobs::events(&self.db)?.is_empty() && self.binding.is_some()
|
||||
&& self.clock.trusted() && let Some(offer) = jobs::next(&self.db)? {
|
||||
let job = self.binding.as_ref().context("binding")?.job(&offer.payload_bytes)?;
|
||||
if Utc::now() >= job.start_before {
|
||||
@@ -814,6 +816,17 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
||||
}
|
||||
// Reserve before send. A lost response must never cause
|
||||
// a second permit request for this attempt.
|
||||
// The root updater holds this lock exclusively while it checks
|
||||
// the durable journal and switches a release. Shared ownership
|
||||
// covers reservation so a grant cannot race an idle check.
|
||||
let _release_gate = if let Some(path) = env::var_os("RMM_RELEASE_LOCK") {
|
||||
let file = fs::File::open(path)?;
|
||||
match fs2::FileExt::try_lock_shared(&file) {
|
||||
Ok(()) => Some(file),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => continue,
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
} else { None };
|
||||
jobs::reserve_request(&self.db, &offer)?;
|
||||
let request = self.envelope(generation, Payload::ExecutionPermitRequest(ExecutionPermitRequest {
|
||||
job_id: offer.job_id, attempt_id: offer.attempt_id,
|
||||
@@ -974,6 +987,14 @@ async fn main() -> Result<()> {
|
||||
Ok::<_, anyhow::Error>(binding)
|
||||
})
|
||||
.transpose()?;
|
||||
if binding.is_none() && let Some(tenant) = env::var_os("RMM_ENROLLMENT_TENANT") {
|
||||
let identity: EnrollmentResponse = serde_json::from_slice(&fs::read(dir.join("identity.json"))?)?;
|
||||
binding = Some(rmm_privileged_helper::Binding {
|
||||
tenant_id: Uuid::parse_str(tenant.to_str().context("invalid enrollment tenant")?)?,
|
||||
agent_id: Uuid::parse_str(&identity.agent_id)?, asset_id: Uuid::parse_str(&identity.asset_id)?,
|
||||
enrollment_generation: 1, assignment_version: 1, public_key_hex: String::new(),
|
||||
});
|
||||
}
|
||||
let certificate_dir = dir.join("certificates");
|
||||
let certificates = certificate_rotation::RotationStore::open(
|
||||
&certificate_dir,
|
||||
@@ -1022,7 +1043,7 @@ async fn main() -> Result<()> {
|
||||
);
|
||||
}
|
||||
let mode =
|
||||
wire_channel::Mode::parse(&env::var("RMM_TRANSPORT").unwrap_or_else(|_| "auto".into()))?;
|
||||
wire_channel::Mode::parse(&env::var("RMM_TRANSPORT").unwrap_or_else(|_| "https".into()))?;
|
||||
let mut agent = Agent {
|
||||
address,
|
||||
ca,
|
||||
|
||||
Reference in New Issue
Block a user