Add versioned script library, reconcile permit refusals and compact helper captures
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
-- Keep the serialized snapshot visible to existing readers and repair tools.
|
||||||
|
-- Splitting the original bytes preserves signatures, including unknown fields.
|
||||||
|
ALTER TABLE helper_sync_captures RENAME TO helper_capture_headers;
|
||||||
|
ALTER TABLE helper_capture_headers ADD COLUMN tail_id INTEGER REFERENCES helper_capture_tails(id);
|
||||||
|
CREATE TABLE helper_capture_tails(id INTEGER PRIMARY KEY, tail BLOB NOT NULL UNIQUE);
|
||||||
|
CREATE VIEW helper_sync_captures AS
|
||||||
|
SELECT h.checkpoint,
|
||||||
|
CASE WHEN h.tail_id IS NULL THEN h.snapshot
|
||||||
|
ELSE CAST(h.snapshot || (SELECT tail FROM helper_capture_tails WHERE id=h.tail_id) AS BLOB)
|
||||||
|
END AS snapshot, h.fingerprint, h.captured_at
|
||||||
|
FROM helper_capture_headers h;
|
||||||
|
CREATE TRIGGER insert_helper_capture INSTEAD OF INSERT ON helper_sync_captures BEGIN
|
||||||
|
INSERT INTO helper_capture_tails(tail)
|
||||||
|
SELECT substr(NEW.snapshot,instr(NEW.snapshot,CAST(',"attempts":' AS BLOB)))
|
||||||
|
WHERE instr(NEW.snapshot,CAST(',"attempts":' AS BLOB))>0
|
||||||
|
ON CONFLICT(tail) DO NOTHING;
|
||||||
|
INSERT INTO helper_capture_headers(checkpoint,snapshot,fingerprint,captured_at,tail_id)
|
||||||
|
SELECT NEW.checkpoint,CASE WHEN split=0 THEN NEW.snapshot ELSE substr(NEW.snapshot,1,split-1) END,
|
||||||
|
NEW.fingerprint,NEW.captured_at,
|
||||||
|
CASE WHEN split>0 THEN (SELECT id FROM helper_capture_tails WHERE tail=substr(NEW.snapshot,split)) END
|
||||||
|
FROM (SELECT instr(NEW.snapshot,CAST(',"attempts":' AS BLOB)) AS split);
|
||||||
|
END;
|
||||||
|
CREATE TRIGGER delete_helper_capture INSTEAD OF DELETE ON helper_sync_captures BEGIN
|
||||||
|
DELETE FROM helper_capture_headers WHERE checkpoint=OLD.checkpoint;
|
||||||
|
END;
|
||||||
@@ -1038,6 +1038,20 @@ fn initialize(db: &Connection, binding: &Binding) -> Result<()> {
|
|||||||
CREATE TABLE IF NOT EXISTS helper_sync_rows(grant_id TEXT PRIMARY KEY,revision INTEGER NOT NULL,fingerprint TEXT NOT NULL);
|
CREATE TABLE IF NOT EXISTS helper_sync_rows(grant_id TEXT PRIMARY KEY,revision INTEGER NOT NULL,fingerprint TEXT NOT NULL);
|
||||||
CREATE TABLE IF NOT EXISTS helper_sync_captures(checkpoint INTEGER PRIMARY KEY,snapshot BLOB NOT NULL,fingerprint TEXT NOT NULL,captured_at TEXT NOT NULL);
|
CREATE TABLE IF NOT EXISTS helper_sync_captures(checkpoint INTEGER PRIMARY KEY,snapshot BLOB NOT NULL,fingerprint TEXT NOT NULL,captured_at TEXT NOT NULL);
|
||||||
CREATE TABLE IF NOT EXISTS helper_sync_decisions(decision_id TEXT PRIMARY KEY,checkpoint INTEGER NOT NULL,response BLOB NOT NULL,request BLOB NOT NULL,fingerprint TEXT NOT NULL);")?;
|
CREATE TABLE IF NOT EXISTS helper_sync_decisions(decision_id TEXT PRIMARY KEY,checkpoint INTEGER NOT NULL,response BLOB NOT NULL,request BLOB NOT NULL,fingerprint TEXT NOT NULL);")?;
|
||||||
|
let legacy_captures: bool = db.query_row(
|
||||||
|
"SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE type='table' AND name='helper_sync_captures')",
|
||||||
|
[], |row| row.get(0),
|
||||||
|
)?;
|
||||||
|
if legacy_captures {
|
||||||
|
// initialize runs under the snapshot transaction; savepoint also makes
|
||||||
|
// the schema change atomic for callers that initialize independently.
|
||||||
|
db.execute_batch("SAVEPOINT helper_capture_schema")?;
|
||||||
|
if let Err(error) = db.execute_batch(include_str!("helper_capture_storage.sql")) {
|
||||||
|
db.execute_batch("ROLLBACK TO helper_capture_schema; RELEASE helper_capture_schema")?;
|
||||||
|
return Err(error.into());
|
||||||
|
}
|
||||||
|
db.execute_batch("RELEASE helper_capture_schema")?;
|
||||||
|
}
|
||||||
let binding_hash = hash(&serde_json::to_vec(binding)?);
|
let binding_hash = hash(&serde_json::to_vec(binding)?);
|
||||||
db.execute(
|
db.execute(
|
||||||
"INSERT OR IGNORE INTO helper_sync_meta(id,journal_id,binding_sha256) VALUES(1,?1,?2)",
|
"INSERT OR IGNORE INTO helper_sync_meta(id,journal_id,binding_sha256) VALUES(1,?1,?2)",
|
||||||
|
|||||||
@@ -260,6 +260,30 @@ pub fn recover_interrupted_outcome_with(
|
|||||||
Ok(Some(saved))
|
Ok(Some(saved))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A refusal carries no execution authority. An unbound or ambiguous refusal
|
||||||
|
/// remains local uncertainty until the signed reconciliation resolves it.
|
||||||
|
pub fn denied_request(db: &Connection, offer: &JobOffer, permit: &ExecutionPermit) -> Result<()> {
|
||||||
|
ensure!(
|
||||||
|
!permit.denial_code.is_empty() && permit.payload_bytes.is_empty() && permit.signature.is_empty(),
|
||||||
|
"INVALID_PERMIT_DENIAL"
|
||||||
|
);
|
||||||
|
let Some(request) = &permit.unreserved_request else {
|
||||||
|
return interrupted_request(db, offer);
|
||||||
|
};
|
||||||
|
let job: JobPayload = serde_json::from_slice(&offer.payload_bytes)?;
|
||||||
|
ensure!(
|
||||||
|
request.job_id == offer.job_id && request.attempt_id == offer.attempt_id
|
||||||
|
&& request.job_payload_hash == offer.job_payload_hash
|
||||||
|
&& request.assignment_version == job.assignment_version,
|
||||||
|
"PERMIT_DENIAL_BINDING"
|
||||||
|
);
|
||||||
|
ensure!(db.execute(
|
||||||
|
"UPDATE job_attempts SET state='accepted' WHERE job=?1 AND attempt=?2 AND state='requesting' AND grant_id=''",
|
||||||
|
params![offer.job_id, offer.attempt_id],
|
||||||
|
)? == 1, "PERMIT_DENIAL_STATE");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn interrupted_request(db: &Connection, offer: &JobOffer) -> Result<()> {
|
pub fn interrupted_request(db: &Connection, offer: &JobOffer) -> Result<()> {
|
||||||
ensure!(
|
ensure!(
|
||||||
db.execute(
|
db.execute(
|
||||||
|
|||||||
@@ -353,7 +353,24 @@ fn portable_acceptance_requires_platform_capability_and_exact_redelivery() {
|
|||||||
assert!(jobs::accept(&db, &binding, &changed, |_| true).is_err());
|
assert!(jobs::accept(&db, &binding, &changed, |_| true).is_err());
|
||||||
jobs::reserve_request(&db, &offer).unwrap();
|
jobs::reserve_request(&db, &offer).unwrap();
|
||||||
assert!(jobs::reserve_request(&db, &offer).is_err());
|
assert!(jobs::reserve_request(&db, &offer).is_err());
|
||||||
assert_eq!(jobs::transition(&db, &offer, "unknown", "").unwrap(), 1);
|
let mut denied = rmm_protocol::ExecutionPermit {
|
||||||
|
denial_code: "RESOURCE_LOCKED".to_owned(),
|
||||||
|
unreserved_request: Some(rmm_protocol::ExecutionPermitRequest {
|
||||||
|
job_id: offer.job_id.clone(),
|
||||||
|
attempt_id: Uuid::new_v4().to_string(),
|
||||||
|
job_payload_hash: offer.job_payload_hash.clone(),
|
||||||
|
assignment_version: binding.assignment_version,
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
assert!(jobs::denied_request(&db, &offer, &denied).is_err());
|
||||||
|
assert!(jobs::requesting(&db).unwrap().is_some());
|
||||||
|
denied.unreserved_request.as_mut().unwrap().attempt_id = offer.attempt_id.clone();
|
||||||
|
jobs::denied_request(&db, &offer, &denied).unwrap();
|
||||||
|
jobs::reserve_request(&db, &offer).unwrap();
|
||||||
|
// An older server, or a refusal after a prior grant, warrants uncertainty.
|
||||||
|
denied.unreserved_request = None;
|
||||||
|
jobs::denied_request(&db, &offer, &denied).unwrap();
|
||||||
assert!(jobs::next(&db).unwrap().is_none());
|
assert!(jobs::next(&db).unwrap().is_none());
|
||||||
assert!(jobs::transition(&db, &offer, "running", "").is_err());
|
assert!(jobs::transition(&db, &offer, "running", "").is_err());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -430,6 +430,7 @@ mod tests {
|
|||||||
max_parallel: 1,
|
max_parallel: 1,
|
||||||
halt_after_failures: 1,
|
halt_after_failures: 1,
|
||||||
recipe_references: vec![],
|
recipe_references: vec![],
|
||||||
|
script_revision: None,
|
||||||
artifact_references: vec![],
|
artifact_references: vec![],
|
||||||
};
|
};
|
||||||
let scope = ProbeScope::from_plan(&plan, Uuid::new_v4()).context("fixed Echo plan")?;
|
let scope = ProbeScope::from_plan(&plan, Uuid::new_v4()).context("fixed Echo plan")?;
|
||||||
|
|||||||
@@ -339,6 +339,7 @@ mod tests {
|
|||||||
max_parallel: 1,
|
max_parallel: 1,
|
||||||
halt_after_failures: 1,
|
halt_after_failures: 1,
|
||||||
recipe_references: vec![],
|
recipe_references: vec![],
|
||||||
|
script_revision: None,
|
||||||
artifact_references: vec![],
|
artifact_references: vec![],
|
||||||
};
|
};
|
||||||
let scope = ProbeScope::from_plan(&plan, Uuid::new_v4()).context("fixture fixed scope")?;
|
let scope = ProbeScope::from_plan(&plan, Uuid::new_v4()).context("fixture fixed scope")?;
|
||||||
|
|||||||
@@ -1764,12 +1764,8 @@ impl Agent {
|
|||||||
}
|
}
|
||||||
let offer = jobs::requesting(self.journal.connection())?.context("UNSOLICITED_PERMIT")?;
|
let offer = jobs::requesting(self.journal.connection())?.context("UNSOLICITED_PERMIT")?;
|
||||||
if !permit.denial_code.is_empty() {
|
if !permit.denial_code.is_empty() {
|
||||||
self.record(
|
jobs::denied_request(self.journal.connection(), &offer, &permit)?;
|
||||||
generation,
|
self.close_gate();
|
||||||
&offer,
|
|
||||||
"",
|
|
||||||
Outcome::unknown("PERMIT_DENIED_RECONCILIATION_REQUIRED"),
|
|
||||||
)?;
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
if self.clock.reading().is_err() {
|
if self.clock.reading().is_err() {
|
||||||
@@ -2238,6 +2234,7 @@ impl Agent {
|
|||||||
self.queue_update_observation(generation)?;
|
self.queue_update_observation(generation)?;
|
||||||
self.replay(&channel, generation).await?;
|
self.replay(&channel, generation).await?;
|
||||||
let mut needs_sync = self.sync.is_some();
|
let mut needs_sync = self.sync.is_some();
|
||||||
|
let mut permit_after = Instant::now();
|
||||||
if self.sync.is_some() && self.running.is_none() {
|
if self.sync.is_some() && self.running.is_none() {
|
||||||
if self.start_sync(&channel, generation).await.is_err() {
|
if self.start_sync(&channel, generation).await.is_err() {
|
||||||
self.close_gate();
|
self.close_gate();
|
||||||
@@ -2285,7 +2282,7 @@ impl Agent {
|
|||||||
if self.sync.is_some() && !self.prepared && !self.helper_control_busy_for_sync() && rmm_agent_windows::job_readiness::refresh_due(needs_sync,sync_at.elapsed(),self.sync.as_ref().and_then(|s|s.validity_deadline()),self.clock.reading().ok().map(|r|r.upper)) {
|
if self.sync.is_some() && !self.prepared && !self.helper_control_busy_for_sync() && rmm_agent_windows::job_readiness::refresh_due(needs_sync,sync_at.elapsed(),self.sync.as_ref().and_then(|s|s.validity_deadline()),self.clock.reading().ok().map(|r|r.upper)) {
|
||||||
if self.start_sync(&channel,generation).await.is_err(){self.close_gate();needs_sync=true;}else{needs_sync=false;}sync_at=Instant::now();
|
if self.start_sync(&channel,generation).await.is_err(){self.close_gate();needs_sync=true;}else{needs_sync=false;}sync_at=Instant::now();
|
||||||
}
|
}
|
||||||
if !needs_sync && !self.prepared && self.ready(generation) && jobs::events(self.journal.connection())?.is_empty() && self.journal.count()?==0 {
|
if Instant::now() >= permit_after && !needs_sync && !self.prepared && self.ready(generation) && jobs::events(self.journal.connection())?.is_empty() && self.journal.count()?==0 {
|
||||||
// A transiently stale clock skips this tick; it must not end the session.
|
// A transiently stale clock skips this tick; it must not end the session.
|
||||||
if let Ok(now)=self.clock.now() && let Some(offer)=jobs::next_at(self.journal.connection(),now)? {
|
if let Ok(now)=self.clock.now() && let Some(offer)=jobs::next_at(self.journal.connection(),now)? {
|
||||||
let job=self.binding.as_ref().context("JOB_BINDING_REQUIRED")?.job(&offer.payload_bytes)?;
|
let job=self.binding.as_ref().context("JOB_BINDING_REQUIRED")?.job(&offer.payload_bytes)?;
|
||||||
@@ -2320,7 +2317,13 @@ impl Agent {
|
|||||||
let fresh=jobs::accept(&tx,&binding,&offer,|_|supported)?;
|
let fresh=jobs::accept(&tx,&binding,&offer,|_|supported)?;
|
||||||
jobs::event(&tx,&accepted)?;tx.commit()?;needs_sync|=fresh;
|
jobs::event(&tx,&accepted)?;tx.commit()?;needs_sync|=fresh;
|
||||||
},
|
},
|
||||||
Some(Payload::ExecutionPermit(permit))=>self.permit(generation,permit)?,
|
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)?;
|
||||||
|
},
|
||||||
Some(Payload::CancelRequest(cancel))=>{
|
Some(Payload::CancelRequest(cancel))=>{
|
||||||
let row:Option<(Vec<u8>,String,String)>=self.journal.connection().query_row("SELECT offer,state,grant_id FROM job_attempts WHERE job=?1 AND attempt=?2",rusqlite::params![cancel.job_id,cancel.attempt_id],|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?))).optional()?;
|
let row:Option<(Vec<u8>,String,String)>=self.journal.connection().query_row("SELECT offer,state,grant_id FROM job_attempts WHERE job=?1 AND attempt=?2",rusqlite::params![cancel.job_id,cancel.attempt_id],|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?))).optional()?;
|
||||||
if let Some((bytes,state,grant))=row {
|
if let Some((bytes,state,grant))=row {
|
||||||
|
|||||||
+32
-11
@@ -353,13 +353,8 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
|||||||
anyhow::bail!("UNSOLICITED_PERMIT")
|
anyhow::bail!("UNSOLICITED_PERMIT")
|
||||||
};
|
};
|
||||||
if !permit.denial_code.is_empty() {
|
if !permit.denial_code.is_empty() {
|
||||||
// A denial is not proof that an earlier reserved grant never existed.
|
jobs::denied_request(&self.db, &offer, &permit)?;
|
||||||
self.record(
|
self.close_gate();
|
||||||
generation,
|
|
||||||
&offer,
|
|
||||||
"",
|
|
||||||
rmm_privileged_helper::Outcome::unknown("PERMIT_DENIED_RECONCILIATION_REQUIRED"),
|
|
||||||
)?;
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
ensure!(
|
ensure!(
|
||||||
@@ -621,7 +616,7 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
|||||||
let hello = self.envelope(
|
let hello = self.envelope(
|
||||||
0,
|
0,
|
||||||
Payload::ClientHello(ClientHello {
|
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(),
|
capabilities: advertised.clone(),
|
||||||
}),
|
}),
|
||||||
)?;
|
)?;
|
||||||
@@ -655,6 +650,7 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
|||||||
self.queue_update_observation(generation)?;
|
self.queue_update_observation(generation)?;
|
||||||
self.replay(&ws, generation).await?;
|
self.replay(&ws, generation).await?;
|
||||||
let mut needs_sync = false;
|
let mut needs_sync = false;
|
||||||
|
let mut permit_after = Instant::now();
|
||||||
if self.sync.is_some()
|
if self.sync.is_some()
|
||||||
&& let Err(error) = self.start_sync(&ws, generation).await
|
&& let Err(error) = self.start_sync(&ws, generation).await
|
||||||
{
|
{
|
||||||
@@ -721,7 +717,13 @@ impl<C: reconcile::SyncClock> Agent<C> {
|
|||||||
tx.commit()?;
|
tx.commit()?;
|
||||||
needs_sync|=fresh;self.replay(&ws, generation).await?;
|
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)) => {
|
Some(Payload::CancelRequest(cancel)) => {
|
||||||
let row: Option<(Vec<u8>, String, String)> = {
|
let row: Option<(Vec<u8>, String, String)> = {
|
||||||
use rusqlite::OptionalExtension;
|
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 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 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)? {
|
&& self.clock.trusted() && let Some(offer) = jobs::next(&self.db)? {
|
||||||
let job = self.binding.as_ref().context("binding")?.job(&offer.payload_bytes)?;
|
let job = self.binding.as_ref().context("binding")?.job(&offer.payload_bytes)?;
|
||||||
if Utc::now() >= job.start_before {
|
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
|
// Reserve before send. A lost response must never cause
|
||||||
// a second permit request for this attempt.
|
// 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)?;
|
jobs::reserve_request(&self.db, &offer)?;
|
||||||
let request = self.envelope(generation, Payload::ExecutionPermitRequest(ExecutionPermitRequest {
|
let request = self.envelope(generation, Payload::ExecutionPermitRequest(ExecutionPermitRequest {
|
||||||
job_id: offer.job_id, attempt_id: offer.attempt_id,
|
job_id: offer.job_id, attempt_id: offer.attempt_id,
|
||||||
@@ -974,6 +987,14 @@ async fn main() -> Result<()> {
|
|||||||
Ok::<_, anyhow::Error>(binding)
|
Ok::<_, anyhow::Error>(binding)
|
||||||
})
|
})
|
||||||
.transpose()?;
|
.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 certificate_dir = dir.join("certificates");
|
||||||
let certificates = certificate_rotation::RotationStore::open(
|
let certificates = certificate_rotation::RotationStore::open(
|
||||||
&certificate_dir,
|
&certificate_dir,
|
||||||
@@ -1022,7 +1043,7 @@ async fn main() -> Result<()> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
let mode =
|
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 {
|
let mut agent = Agent {
|
||||||
address,
|
address,
|
||||||
ca,
|
ca,
|
||||||
|
|||||||
@@ -134,6 +134,7 @@ fn fixture(platform: Platform, full: bool) -> serde_json::Value {
|
|||||||
max_parallel: 1,
|
max_parallel: 1,
|
||||||
halt_after_failures: 1,
|
halt_after_failures: 1,
|
||||||
recipe_references: Vec::new(),
|
recipe_references: Vec::new(),
|
||||||
|
script_revision: None,
|
||||||
artifact_references: m
|
artifact_references: m
|
||||||
.artifacts
|
.artifacts
|
||||||
.iter()
|
.iter()
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ fn main() {
|
|||||||
let job_bytes = encode(&job).unwrap();
|
let job_bytes = encode(&job).unwrap();
|
||||||
let job_hash = hash(&job_bytes);
|
let job_hash = hash(&job_bytes);
|
||||||
let script_sha256 = hash(script.as_bytes());
|
let script_sha256 = hash(script.as_bytes());
|
||||||
let plan = PlanPayload { schema_version: 1, plan_id: Uuid::from_u128(6+index as u128*10), tenant_id: tenant, principal: "simulated-dev-script-console".into(), reason: format!("Codec fixture: {name}; no native execution"), jobs: vec![PlannedJob { payload: job, job_payload_hash: job_hash.clone(), script_sha256: Some(script_sha256.clone()) }], max_parallel: 1, halt_after_failures: 1, recipe_references: vec![], artifact_references: vec![] };
|
let plan = PlanPayload { schema_version: 1, plan_id: Uuid::from_u128(6+index as u128*10), tenant_id: tenant, principal: "simulated-dev-script-console".into(), reason: format!("Codec fixture: {name}; no native execution"), jobs: vec![PlannedJob { payload: job, job_payload_hash: job_hash.clone(), script_sha256: Some(script_sha256.clone()) }], max_parallel: 1, halt_after_failures: 1, recipe_references: vec![], script_revision: None, artifact_references: vec![] };
|
||||||
let bytes = encode(&plan).unwrap();
|
let bytes = encode(&plan).unwrap();
|
||||||
json!({"name":name,"plan_payload_bytes":bytes,"plan_hash":hash(&bytes),"job_payload_bytes":job_bytes,"job_payload_hash":job_hash,"script_sha256":script_sha256})
|
json!({"name":name,"plan_payload_bytes":bytes,"plan_hash":hash(&bytes),"job_payload_bytes":job_bytes,"job_payload_hash":job_hash,"script_sha256":script_sha256})
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|||||||
@@ -674,6 +674,16 @@ pub struct PlanPayload {
|
|||||||
pub recipe_references: Vec<RecipeReference>,
|
pub recipe_references: Vec<RecipeReference>,
|
||||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||||
pub artifact_references: Vec<JobArtifactReference>,
|
pub artifact_references: Vec<JobArtifactReference>,
|
||||||
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
|
pub script_revision: Option<ScriptRevisionReference>,
|
||||||
|
}
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
pub struct ScriptRevisionReference {
|
||||||
|
pub revision_id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
pub version: i32,
|
||||||
|
pub script_sha256: String,
|
||||||
}
|
}
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
|
|||||||
@@ -114,6 +114,8 @@ pub struct ExecutionPermit {
|
|||||||
pub signature: Vec<u8>,
|
pub signature: Vec<u8>,
|
||||||
#[prost(string, tag = "3")]
|
#[prost(string, tag = "3")]
|
||||||
pub denial_code: String,
|
pub denial_code: String,
|
||||||
|
#[prost(message, optional, tag = "4")]
|
||||||
|
pub unreserved_request: Option<ExecutionPermitRequest>,
|
||||||
}
|
}
|
||||||
#[derive(Clone, PartialEq, Message)]
|
#[derive(Clone, PartialEq, Message)]
|
||||||
pub struct JobStateChanged {
|
pub struct JobStateChanged {
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ pub async fn revoke_device(
|
|||||||
if n == 0 {
|
if n == 0 {
|
||||||
return Err(Error(StatusCode::NOT_FOUND, "NOT_FOUND"));
|
return Err(Error(StatusCode::NOT_FOUND, "NOT_FOUND"));
|
||||||
}
|
}
|
||||||
sqlx::query("UPDATE jobs SET cancel_requested=true WHERE asset_id=$1 AND state NOT IN ('succeeded','failed','cancelled','expired')").bind(asset).execute(&mut *tx).await?;
|
sqlx::query("UPDATE jobs SET cancel_requested=true WHERE asset_id=$1 AND state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown')").bind(asset).execute(&mut *tx).await?;
|
||||||
audit(
|
audit(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
&p,
|
&p,
|
||||||
@@ -100,7 +100,7 @@ pub async fn revoke_plan(
|
|||||||
.bind(op)
|
.bind(op)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
sqlx::query("UPDATE jobs SET cancel_requested=true,state=CASE WHEN NOT start_reserved AND state='queued' THEN 'cancelled' ELSE state END WHERE operation_id=$1 AND state NOT IN ('succeeded','failed','cancelled','expired')").bind(op).execute(&mut *tx).await?;
|
sqlx::query("UPDATE jobs SET cancel_requested=true,state=CASE WHEN NOT start_reserved AND state='queued' THEN 'cancelled' ELSE state END WHERE operation_id=$1 AND state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown')").bind(op).execute(&mut *tx).await?;
|
||||||
audit(
|
audit(
|
||||||
&mut tx,
|
&mut tx,
|
||||||
&p,
|
&p,
|
||||||
|
|||||||
@@ -526,6 +526,7 @@ pub async fn permit(
|
|||||||
payload_bytes: vec![],
|
payload_bytes: vec![],
|
||||||
signature: vec![],
|
signature: vec![],
|
||||||
denial_code: code.into(),
|
denial_code: code.into(),
|
||||||
|
unreserved_request: None,
|
||||||
};
|
};
|
||||||
let row=sqlx::query("SELECT j.*,a.authorization_id,a.revoked_at,a.principal AS approver,p.plan_hash,p.payload AS plan_payload,r.approval_state,i.enrollment_generation,i.assignment_version,i.asset_id AS bound_asset,i.capabilities,i.revoked_at AS device_revoked,i.connection_generation,t.changes_stopped FROM jobs j JOIN operations o ON o.tenant_id=j.tenant_id AND o.operation_id=j.operation_id JOIN authorizations a ON a.tenant_id=o.tenant_id AND a.authorization_id=o.authorization_id JOIN approval_requests r ON r.tenant_id=j.tenant_id AND r.operation_id=j.operation_id JOIN operation_plans p ON p.tenant_id=j.tenant_id AND p.plan_id=r.plan_id JOIN agent_installations i ON i.tenant_id=j.tenant_id AND i.agent_id=j.agent_id JOIN tenants t ON t.tenant_id=j.tenant_id WHERE j.job_id=$1 AND j.attempt_id=$2 AND j.agent_id=$3 FOR UPDATE OF j").bind(Uuid::parse_str(&r.job_id)?).bind(Uuid::parse_str(&r.attempt_id)?).bind(agent).fetch_optional(&mut *tx).await?;
|
let row=sqlx::query("SELECT j.*,a.authorization_id,a.revoked_at,a.principal AS approver,p.plan_hash,p.payload AS plan_payload,r.approval_state,i.enrollment_generation,i.assignment_version,i.asset_id AS bound_asset,i.capabilities,i.revoked_at AS device_revoked,i.connection_generation,t.changes_stopped FROM jobs j JOIN operations o ON o.tenant_id=j.tenant_id AND o.operation_id=j.operation_id JOIN authorizations a ON a.tenant_id=o.tenant_id AND a.authorization_id=o.authorization_id JOIN approval_requests r ON r.tenant_id=j.tenant_id AND r.operation_id=j.operation_id JOIN operation_plans p ON p.tenant_id=j.tenant_id AND p.plan_id=r.plan_id JOIN agent_installations i ON i.tenant_id=j.tenant_id AND i.agent_id=j.agent_id JOIN tenants t ON t.tenant_id=j.tenant_id WHERE j.job_id=$1 AND j.attempt_id=$2 AND j.agent_id=$3 FOR UPDATE OF j").bind(Uuid::parse_str(&r.job_id)?).bind(Uuid::parse_str(&r.attempt_id)?).bind(agent).fetch_optional(&mut *tx).await?;
|
||||||
let Some(row) = row else {
|
let Some(row) = row else {
|
||||||
@@ -584,6 +585,13 @@ pub async fn permit(
|
|||||||
{
|
{
|
||||||
return Ok(denied("BINDING_MISMATCH"));
|
return Ok(denied("BINDING_MISMATCH"));
|
||||||
}
|
}
|
||||||
|
// The locked row belongs to this request and has no start reservation.
|
||||||
|
// Echo the binding only after both checks, so a client can safely return
|
||||||
|
// this particular offer to scheduling after a fresh signed reconciliation.
|
||||||
|
let denied = |code: &str| ExecutionPermit {
|
||||||
|
unreserved_request: Some(r.clone()),
|
||||||
|
..denied(code)
|
||||||
|
};
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
if !payload
|
if !payload
|
||||||
.action
|
.action
|
||||||
@@ -702,7 +710,7 @@ pub async fn permit(
|
|||||||
)
|
)
|
||||||
.fetch_one(&mut *tx)
|
.fetch_one(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
let concurrent:i64=sqlx::query_scalar("SELECT count(*) FROM jobs WHERE start_reserved AND state NOT IN ('succeeded','failed','cancelled','expired')").fetch_one(&mut *tx).await?;
|
let concurrent:i64=sqlx::query_scalar("SELECT count(*) FROM jobs WHERE start_reserved AND state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown')").fetch_one(&mut *tx).await?;
|
||||||
if hourly >= i64::from(limits.0) || concurrent >= i64::from(limits.1) {
|
if hourly >= i64::from(limits.0) || concurrent >= i64::from(limits.1) {
|
||||||
return Ok(denied("QUOTA_EXCEEDED"));
|
return Ok(denied("QUOTA_EXCEEDED"));
|
||||||
}
|
}
|
||||||
@@ -710,7 +718,7 @@ pub async fn permit(
|
|||||||
dependency_group, ..
|
dependency_group, ..
|
||||||
} = &payload.action
|
} = &payload.action
|
||||||
{
|
{
|
||||||
let busy:bool=sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM jobs WHERE start_reserved AND state NOT IN ('succeeded','failed','cancelled','expired') AND (convert_from(payload,'UTF8')::jsonb->'action'->>'dependency_group'=$1 OR convert_from(payload,'UTF8')::jsonb->'action'->>'action'='reboot'))").bind(dependency_group).fetch_one(&mut *tx).await?;
|
let busy:bool=sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM jobs WHERE start_reserved AND state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown') AND (convert_from(payload,'UTF8')::jsonb->'action'->>'dependency_group'=$1 OR convert_from(payload,'UTF8')::jsonb->'action'->>'action'='reboot'))").bind(dependency_group).fetch_one(&mut *tx).await?;
|
||||||
if busy {
|
if busy {
|
||||||
return Ok(denied("RESOURCE_LOCKED"));
|
return Ok(denied("RESOURCE_LOCKED"));
|
||||||
}
|
}
|
||||||
@@ -727,11 +735,11 @@ pub async fn permit(
|
|||||||
.bind(row.get::<Uuid, _>("operation_id"))
|
.bind(row.get::<Uuid, _>("operation_id"))
|
||||||
.fetch_one(&mut *tx)
|
.fetch_one(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
let active:i64=sqlx::query_scalar("SELECT count(*) FROM jobs WHERE operation_id=$1 AND start_reserved AND state NOT IN ('succeeded','failed','cancelled','expired')").bind(row.get::<Uuid,_>("operation_id")).fetch_one(&mut *tx).await?;
|
let active:i64=sqlx::query_scalar("SELECT count(*) FROM jobs WHERE operation_id=$1 AND start_reserved AND state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown')").bind(row.get::<Uuid,_>("operation_id")).fetch_one(&mut *tx).await?;
|
||||||
if failures >= i64::from(plan.halt_after_failures) || active >= i64::from(plan.max_parallel) {
|
if failures >= i64::from(plan.halt_after_failures) || active >= i64::from(plan.max_parallel) {
|
||||||
return Ok(denied("ROLLOUT_PAUSED"));
|
return Ok(denied("ROLLOUT_PAUSED"));
|
||||||
}
|
}
|
||||||
let locked:bool=sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM jobs WHERE agent_id=$1 AND start_reserved AND state NOT IN ('succeeded','failed','cancelled','expired'))").bind(agent).fetch_one(&mut *tx).await?;
|
let locked:bool=sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM jobs WHERE agent_id=$1 AND start_reserved AND state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown'))").bind(agent).fetch_one(&mut *tx).await?;
|
||||||
if locked {
|
if locked {
|
||||||
return Ok(denied("RESOURCE_LOCKED"));
|
return Ok(denied("RESOURCE_LOCKED"));
|
||||||
}
|
}
|
||||||
@@ -776,6 +784,7 @@ pub async fn permit(
|
|||||||
payload_bytes: bytes,
|
payload_bytes: bytes,
|
||||||
signature,
|
signature,
|
||||||
denial_code: String::new(),
|
denial_code: String::new(),
|
||||||
|
unreserved_request: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub async fn next(
|
pub async fn next(
|
||||||
|
|||||||
+23
-18
@@ -53,7 +53,11 @@ pub fn scope(p: &Principal, tenant: Uuid, write: bool) -> Result<()> {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
async fn principal(state: &crate::State, h: &HeaderMap, write: bool) -> Result<Principal> {
|
pub(crate) async fn principal(
|
||||||
|
state: &crate::State,
|
||||||
|
h: &HeaderMap,
|
||||||
|
write: bool,
|
||||||
|
) -> Result<Principal> {
|
||||||
state
|
state
|
||||||
.auth
|
.auth
|
||||||
.principal(state, h, write)
|
.principal(state, h, write)
|
||||||
@@ -420,7 +424,7 @@ pub async fn plan_request(
|
|||||||
reason: input.reason,
|
reason: input.reason,
|
||||||
artifact_ids: Vec::new(),
|
artifact_ids: Vec::new(),
|
||||||
};
|
};
|
||||||
plan_resolved(state, p, tenant, resolved, Some(updates)).await
|
plan_resolved(state, p, tenant, resolved, Some(updates), None).await
|
||||||
}
|
}
|
||||||
fn validate_variants(variants: &[Variant]) -> Result<()> {
|
fn validate_variants(variants: &[Variant]) -> Result<()> {
|
||||||
if variants.is_empty() || variants.len() > 2 {
|
if variants.is_empty() || variants.len() > 2 {
|
||||||
@@ -531,9 +535,9 @@ pub async fn plan(
|
|||||||
"AGENT_UPDATE_RELEASE_ID_REQUIRED",
|
"AGENT_UPDATE_RELEASE_ID_REQUIRED",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
plan_resolved(state, p, tenant, input, None).await
|
plan_resolved(state, p, tenant, input, None, None).await
|
||||||
}
|
}
|
||||||
async fn plan_resolved(
|
pub(crate) async fn plan_resolved(
|
||||||
state: &crate::State,
|
state: &crate::State,
|
||||||
p: &Principal,
|
p: &Principal,
|
||||||
tenant: Uuid,
|
tenant: Uuid,
|
||||||
@@ -541,6 +545,7 @@ async fn plan_resolved(
|
|||||||
updates: Option<
|
updates: Option<
|
||||||
std::collections::BTreeMap<Uuid, rmm_domain::agent_update::ResolvedAgentUpdate>,
|
std::collections::BTreeMap<Uuid, rmm_domain::agent_update::ResolvedAgentUpdate>,
|
||||||
>,
|
>,
|
||||||
|
script_revision: Option<ScriptRevisionReference>,
|
||||||
) -> Result<Value> {
|
) -> Result<Value> {
|
||||||
scope(p, tenant, true)?;
|
scope(p, tenant, true)?;
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
@@ -716,6 +721,7 @@ async fn plan_resolved(
|
|||||||
halt_after_failures: input.rollout.halt_after_failures,
|
halt_after_failures: input.rollout.halt_after_failures,
|
||||||
recipe_references,
|
recipe_references,
|
||||||
artifact_references,
|
artifact_references,
|
||||||
|
script_revision,
|
||||||
};
|
};
|
||||||
crate::windows_msi_catalog::validate_plan_bindings(&payload)?;
|
crate::windows_msi_catalog::validate_plan_bindings(&payload)?;
|
||||||
crate::agent_updates::validate_plan_bindings(&payload)?;
|
crate::agent_updates::validate_plan_bindings(&payload)?;
|
||||||
@@ -1085,11 +1091,7 @@ pub async fn operation(
|
|||||||
"needs_attention"
|
"needs_attention"
|
||||||
} else if c("succeeded") == n {
|
} else if c("succeeded") == n {
|
||||||
"succeeded"
|
"succeeded"
|
||||||
} else if c("succeeded")
|
} else if c("succeeded") + c("failed") + c("cancelled") + c("expired") + c("closed_unknown")
|
||||||
+ c("failed")
|
|
||||||
+ c("cancelled")
|
|
||||||
+ c("expired")
|
|
||||||
+ c("closed_unknown")
|
|
||||||
== n
|
== n
|
||||||
{
|
{
|
||||||
if c("succeeded") > 0 {
|
if c("succeeded") > 0 {
|
||||||
@@ -1240,13 +1242,12 @@ pub async fn close_unknown(
|
|||||||
}
|
}
|
||||||
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant).await?;
|
let mut tx = rmm_persistence::tenant_tx(&state.pool, tenant).await?;
|
||||||
crate::permissions::require_principal(&mut tx, p, "plans:approve").await?;
|
crate::permissions::require_principal(&mut tx, p, "plans:approve").await?;
|
||||||
let row = sqlx::query(
|
let row =
|
||||||
"SELECT attempt_id,state,revision,result FROM jobs WHERE job_id=$1 FOR UPDATE",
|
sqlx::query("SELECT attempt_id,state,revision,result FROM jobs WHERE job_id=$1 FOR UPDATE")
|
||||||
)
|
.bind(id)
|
||||||
.bind(id)
|
.fetch_optional(&mut *tx)
|
||||||
.fetch_optional(&mut *tx)
|
.await?
|
||||||
.await?
|
.ok_or_else(missing)?;
|
||||||
.ok_or_else(missing)?;
|
|
||||||
if row.get::<Uuid, _>("attempt_id") != input.attempt_id
|
if row.get::<Uuid, _>("attempt_id") != input.attempt_id
|
||||||
|| row.get::<i64, _>("revision") != input.revision
|
|| row.get::<i64, _>("revision") != input.revision
|
||||||
{
|
{
|
||||||
@@ -1286,9 +1287,11 @@ pub async fn close_unknown(
|
|||||||
.bind(tenant)
|
.bind(tenant)
|
||||||
.bind(Uuid::new_v4())
|
.bind(Uuid::new_v4())
|
||||||
.bind(&p.name)
|
.bind(&p.name)
|
||||||
.bind(json!({"job_id":id,"attempt_id":input.attempt_id,"grant_id":grant,"closure_id":closure,
|
.bind(
|
||||||
|
json!({"job_id":id,"attempt_id":input.attempt_id,"grant_id":grant,"closure_id":closure,
|
||||||
"original_revision":input.revision,"reason":input.reason,"session_hash":session,
|
"original_revision":input.revision,"reason":input.reason,"session_hash":session,
|
||||||
"approval_origin":p.approval_origin(),"testsimulation":p.testsimulation}))
|
"approval_origin":p.approval_origin(),"testsimulation":p.testsimulation}),
|
||||||
|
)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
tx.commit().await?;
|
tx.commit().await?;
|
||||||
@@ -1569,6 +1572,8 @@ mod windows_contract_tests {
|
|||||||
crate::job_signer::JobSigner::load_or_create(&temp.join("key.pem")).unwrap(),
|
crate::job_signer::JobSigner::load_or_create(&temp.join("key.pem")).unwrap(),
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
crate::script_library::tests::revision_plan_contract(&state, &principal, assets[0], assets[1])
|
||||||
|
.await;
|
||||||
let mut request = input();
|
let mut request = input();
|
||||||
request.targets.asset_ids = assets.clone();
|
request.targets.asset_ids = assets.clone();
|
||||||
let view = plan(&state, &principal, tenant, request.clone())
|
let view = plan(&state, &principal, tenant, request.clone())
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ mod pagination;
|
|||||||
mod permissions;
|
mod permissions;
|
||||||
mod root_policy;
|
mod root_policy;
|
||||||
mod scheduler;
|
mod scheduler;
|
||||||
|
mod script_library;
|
||||||
mod synchronization;
|
mod synchronization;
|
||||||
mod tls;
|
mod tls;
|
||||||
mod update_artifact_store;
|
mod update_artifact_store;
|
||||||
@@ -205,6 +206,7 @@ async fn main() -> Result<()> {
|
|||||||
.route("/api/v1/tenants/{tenant}/devices", get(api::devices_page))
|
.route("/api/v1/tenants/{tenant}/devices", get(api::devices_page))
|
||||||
.route("/api/v1/tenants/{tenant}/devices/{asset}", get(api::device));
|
.route("/api/v1/tenants/{tenant}/devices/{asset}", get(api::device));
|
||||||
routes = jobs::routes(routes);
|
routes = jobs::routes(routes);
|
||||||
|
routes = script_library::routes(routes);
|
||||||
routes = controls::routes(routes);
|
routes = controls::routes(routes);
|
||||||
routes = catalog::routes(routes);
|
routes = catalog::routes(routes);
|
||||||
routes = agent_updates::routes(routes);
|
routes = agent_updates::routes(routes);
|
||||||
|
|||||||
@@ -72,6 +72,19 @@ struct Cancel {
|
|||||||
#[derive(Deserialize, schemars::JsonSchema)]
|
#[derive(Deserialize, schemars::JsonSchema)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
struct Empty {}
|
struct Empty {}
|
||||||
|
#[derive(Deserialize, schemars::JsonSchema)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
struct PublishScript {
|
||||||
|
tenant_id: Uuid,
|
||||||
|
input: crate::script_library::Publish,
|
||||||
|
}
|
||||||
|
#[derive(Deserialize, schemars::JsonSchema)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
struct PlanScript {
|
||||||
|
tenant_id: Uuid,
|
||||||
|
revision_id: Uuid,
|
||||||
|
input: crate::script_library::Selection,
|
||||||
|
}
|
||||||
fn tool<T: schemars::JsonSchema>(name: &'static str, description: &'static str) -> Tool {
|
fn tool<T: schemars::JsonSchema>(name: &'static str, description: &'static str) -> Tool {
|
||||||
let schema = schemars::schema_for!(T).to_value();
|
let schema = schemars::schema_for!(T).to_value();
|
||||||
let map = schema.as_object().cloned().unwrap_or_default();
|
let map = schema.as_object().cloned().unwrap_or_default();
|
||||||
@@ -91,6 +104,18 @@ fn tools() -> Vec<Tool> {
|
|||||||
"inventory.get_device",
|
"inventory.get_device",
|
||||||
"Read stored inventory of one authorized device.",
|
"Read stored inventory of one authorized device.",
|
||||||
),
|
),
|
||||||
|
tool::<Devices>(
|
||||||
|
"scripts.list",
|
||||||
|
"Read tenant script revisions, including the exact script and execution identity.",
|
||||||
|
),
|
||||||
|
tool::<PublishScript>(
|
||||||
|
"scripts.publish",
|
||||||
|
"Save an immutable script revision. Use expected_version zero for a new name or the current version to publish an edit.",
|
||||||
|
),
|
||||||
|
tool::<PlanScript>(
|
||||||
|
"scripts.plan",
|
||||||
|
"Plan the selected immutable script revision against fixed assets. Submit the returned plan for WebUI approval via operations.submit.",
|
||||||
|
),
|
||||||
tool::<Devices>(
|
tool::<Devices>(
|
||||||
"updates.list_releases",
|
"updates.list_releases",
|
||||||
"Read immutable signed agent release metadata for the authorized tenant; never approves or applies an update.",
|
"Read immutable signed agent release metadata for the authorized tenant; never approves or applies an update.",
|
||||||
@@ -208,6 +233,28 @@ impl Adapter {
|
|||||||
let i: crate::monitoring::LogQuery = parse(args)?;
|
let i: crate::monitoring::LogQuery = parse(args)?;
|
||||||
crate::monitoring::query(&self.state, &p, i).await
|
crate::monitoring::query(&self.state, &p, i).await
|
||||||
}
|
}
|
||||||
|
"scripts.list" => {
|
||||||
|
let i: Devices = parse(args)?;
|
||||||
|
crate::script_library::list(
|
||||||
|
&self.state,
|
||||||
|
&p,
|
||||||
|
i.tenant_id,
|
||||||
|
crate::pagination::PageQuery {
|
||||||
|
cursor: i.cursor,
|
||||||
|
limit: i.limit,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
"scripts.publish" => {
|
||||||
|
let i: PublishScript = parse(args)?;
|
||||||
|
crate::script_library::publish(&self.state, &p, i.tenant_id, i.input).await
|
||||||
|
}
|
||||||
|
"scripts.plan" => {
|
||||||
|
let i: PlanScript = parse(args)?;
|
||||||
|
crate::script_library::plan(&self.state, &p, i.tenant_id, i.revision_id, i.input)
|
||||||
|
.await
|
||||||
|
}
|
||||||
"operations.plan" => {
|
"operations.plan" => {
|
||||||
let i: Plan = parse(args)?;
|
let i: Plan = parse(args)?;
|
||||||
crate::jobs::plan_request(&self.state, &p, i.tenant_id, i.input).await
|
crate::jobs::plan_request(&self.state, &p, i.tenant_id, i.input).await
|
||||||
|
|||||||
@@ -0,0 +1,359 @@
|
|||||||
|
//! Tenant-owned script revisions. Selection materializes an ordinary immutable
|
||||||
|
//! script plan; submitting and approving it use the existing job lifecycle.
|
||||||
|
use crate::{
|
||||||
|
auth::Principal,
|
||||||
|
jobs::{self, Error, PlanInput, Rollout, Schedule, Targets},
|
||||||
|
pagination::{Cursor, PageQuery},
|
||||||
|
};
|
||||||
|
use axum::{
|
||||||
|
Json, Router,
|
||||||
|
extract::{Path, Query, State},
|
||||||
|
http::{HeaderMap, StatusCode},
|
||||||
|
routing::{get, post},
|
||||||
|
};
|
||||||
|
use rmm_domain::jobs::{
|
||||||
|
Action, PrivilegedScriptProfile, ScriptRevisionReference, WindowsExecutionProfile, hash,
|
||||||
|
};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use serde_json::{Value, json};
|
||||||
|
use sqlx::Row;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Clone, Deserialize, schemars::JsonSchema)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
pub struct Publish {
|
||||||
|
pub name: String,
|
||||||
|
pub expected_version: i32,
|
||||||
|
pub interpreter: String,
|
||||||
|
pub execution_identity: String,
|
||||||
|
pub script_utf8: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, schemars::JsonSchema)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
pub struct Selection {
|
||||||
|
pub targets: Targets,
|
||||||
|
pub schedule: Schedule,
|
||||||
|
pub rollout: Rollout,
|
||||||
|
pub reason: String,
|
||||||
|
pub max_runtime_seconds: u32,
|
||||||
|
pub max_output_bytes: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn script(interpreter: &str, identity: &str, body: String) -> Result<Action, Error> {
|
||||||
|
let action = Action::RunScript {
|
||||||
|
interpreter: interpreter.to_owned(),
|
||||||
|
script_utf8: body,
|
||||||
|
arguments: vec![],
|
||||||
|
expected_stdout: None,
|
||||||
|
windows_execution_profile: (interpreter == "powershell")
|
||||||
|
.then_some(WindowsExecutionProfile::PowerShell51RemoteSigned),
|
||||||
|
privileged_script_profile: (identity != "agent_service")
|
||||||
|
.then_some(PrivilegedScriptProfile::FullMaintenanceV1),
|
||||||
|
};
|
||||||
|
if !action.validate()
|
||||||
|
|| !action.valid_execution_identity(identity)
|
||||||
|
|| !matches!(identity, "agent_service" | "root" | "system")
|
||||||
|
{
|
||||||
|
return Err(Error(StatusCode::BAD_REQUEST, "INVALID_SCRIPT_REVISION"));
|
||||||
|
}
|
||||||
|
Ok(action)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view(r: &sqlx::postgres::PgRow) -> Value {
|
||||||
|
let body: String = r.get("script_utf8");
|
||||||
|
json!({"revision_id":r.get::<Uuid,_>("revision_id"),"name":r.get::<String,_>("name"),
|
||||||
|
"version":r.get::<i32,_>("version"),"interpreter":r.get::<String,_>("interpreter"),
|
||||||
|
"execution_identity":r.get::<String,_>("execution_identity"),"script_sha256":hash(body.as_bytes()),
|
||||||
|
"script_utf8":body,"created_at":r.get::<chrono::DateTime<chrono::Utc>,_>("created_at"),
|
||||||
|
"created_by":r.get::<String,_>("created_by")})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn list(
|
||||||
|
s: &crate::State,
|
||||||
|
p: &Principal,
|
||||||
|
tenant: Uuid,
|
||||||
|
q: PageQuery,
|
||||||
|
) -> Result<Value, Error> {
|
||||||
|
jobs::scope(p, tenant, false)?;
|
||||||
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, tenant).await?;
|
||||||
|
crate::permissions::require_principal(&mut tx, p, "operations:read").await?;
|
||||||
|
let mut page = Cursor::parse(s, p, "script-library:revision-id", q)
|
||||||
|
.map_err(|s| Error(s, "VALIDATION_ERROR"))?;
|
||||||
|
let rows=sqlx::query("SELECT * FROM script_revisions WHERE revision_id>$1 AND created_at<=$2 ORDER BY revision_id LIMIT $3")
|
||||||
|
.bind(page.last).bind(page.as_of).bind(i64::from(page.limit)+1).fetch_all(&mut *tx).await?;
|
||||||
|
let shown = rows
|
||||||
|
.iter()
|
||||||
|
.take(usize::from(page.limit))
|
||||||
|
.map(view)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
let truncated = rows.len() > shown.len();
|
||||||
|
let next = if truncated {
|
||||||
|
Some(
|
||||||
|
page.next(s, rows[shown.len() - 1].get("revision_id"))
|
||||||
|
.map_err(|s| Error(s, "CURSOR_ERROR"))?,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
tx.commit().await?;
|
||||||
|
Ok(
|
||||||
|
json!({"items":shown,"as_of":page.as_of,"next_cursor":next,"truncated":truncated,"completeness":if truncated {"partial"} else {"complete"}}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn publish(
|
||||||
|
s: &crate::State,
|
||||||
|
p: &Principal,
|
||||||
|
tenant: Uuid,
|
||||||
|
input: Publish,
|
||||||
|
) -> Result<Value, Error> {
|
||||||
|
jobs::scope(p, tenant, true)?;
|
||||||
|
if input.name.trim() != input.name
|
||||||
|
|| input.name.is_empty()
|
||||||
|
|| input.name.chars().count() > 120
|
||||||
|
|| input.name.chars().any(char::is_control)
|
||||||
|
|| !(0..i32::MAX).contains(&input.expected_version)
|
||||||
|
{
|
||||||
|
return Err(Error(StatusCode::BAD_REQUEST, "INVALID_SCRIPT_REVISION"));
|
||||||
|
}
|
||||||
|
script(
|
||||||
|
&input.interpreter,
|
||||||
|
&input.execution_identity,
|
||||||
|
input.script_utf8.clone(),
|
||||||
|
)?;
|
||||||
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, tenant).await?;
|
||||||
|
crate::permissions::require_principal(&mut tx, p, "operations:plan").await?;
|
||||||
|
crate::permissions::require_principal(&mut tx, p, "scripts:execute").await?;
|
||||||
|
// Same predecessor implies the same next version. The unique constraint
|
||||||
|
// serializes concurrent publishers without a mutable "latest" row.
|
||||||
|
let version = input.expected_version + 1;
|
||||||
|
let inserted=sqlx::query("INSERT INTO script_revisions(tenant_id,name,version,interpreter,execution_identity,script_utf8,created_by)
|
||||||
|
SELECT $1,$2,$3,$4,$5,$6,$7 WHERE COALESCE((SELECT max(version) FROM script_revisions WHERE name=$2),0)=$8
|
||||||
|
ON CONFLICT (tenant_id,name,version) DO NOTHING RETURNING *")
|
||||||
|
.bind(tenant).bind(&input.name).bind(version).bind(&input.interpreter)
|
||||||
|
.bind(&input.execution_identity).bind(&input.script_utf8).bind(&p.name)
|
||||||
|
.bind(input.expected_version).fetch_optional(&mut *tx).await?;
|
||||||
|
let row=match inserted{
|
||||||
|
Some(r)=>r,
|
||||||
|
None=>sqlx::query("SELECT * FROM script_revisions WHERE name=$1 AND version=$2 AND interpreter=$3 AND execution_identity=$4 AND script_utf8=$5 AND created_by=$6")
|
||||||
|
.bind(input.name).bind(version).bind(input.interpreter).bind(input.execution_identity)
|
||||||
|
.bind(input.script_utf8).bind(&p.name).fetch_optional(&mut *tx).await?
|
||||||
|
.ok_or(Error(StatusCode::CONFLICT,"SCRIPT_REVISION_CONFLICT"))?
|
||||||
|
};
|
||||||
|
tx.commit().await?;
|
||||||
|
Ok(view(&row))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn plan(
|
||||||
|
s: &crate::State,
|
||||||
|
p: &Principal,
|
||||||
|
tenant: Uuid,
|
||||||
|
id: Uuid,
|
||||||
|
input: Selection,
|
||||||
|
) -> Result<Value, Error> {
|
||||||
|
jobs::scope(p, tenant, true)?;
|
||||||
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, tenant).await?;
|
||||||
|
crate::permissions::require_principal(&mut tx, p, "operations:plan").await?;
|
||||||
|
crate::permissions::require_principal(&mut tx, p, "scripts:execute").await?;
|
||||||
|
let row = sqlx::query("SELECT * FROM script_revisions WHERE revision_id=$1")
|
||||||
|
.bind(id)
|
||||||
|
.fetch_optional(&mut *tx)
|
||||||
|
.await?
|
||||||
|
.ok_or(Error(StatusCode::NOT_FOUND, "NOT_FOUND"))?;
|
||||||
|
tx.commit().await?;
|
||||||
|
let body: String = row.get("script_utf8");
|
||||||
|
let reference = ScriptRevisionReference {
|
||||||
|
revision_id: id,
|
||||||
|
name: row.get("name"),
|
||||||
|
version: row.get("version"),
|
||||||
|
script_sha256: hash(body.as_bytes()),
|
||||||
|
};
|
||||||
|
let identity: String = row.get("execution_identity");
|
||||||
|
let action = script(row.get("interpreter"), &identity, body)?;
|
||||||
|
let plan = PlanInput {
|
||||||
|
targets: input.targets,
|
||||||
|
action,
|
||||||
|
script_variants: vec![],
|
||||||
|
execution_identity: identity,
|
||||||
|
schedule: input.schedule,
|
||||||
|
max_runtime_seconds: input.max_runtime_seconds,
|
||||||
|
max_output_bytes: input.max_output_bytes,
|
||||||
|
rollout: input.rollout,
|
||||||
|
reason: input.reason,
|
||||||
|
artifact_ids: vec![],
|
||||||
|
};
|
||||||
|
jobs::plan_resolved(s, p, tenant, plan, None, Some(reference)).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn routes(r: Router<crate::State>) -> Router<crate::State> {
|
||||||
|
r.route(
|
||||||
|
"/api/v1/tenants/{tenant}/script-revisions",
|
||||||
|
get(list_http).post(publish_http),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/api/v1/tenants/{tenant}/script-revisions/{revision}/plan",
|
||||||
|
post(plan_http),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
async fn list_http(
|
||||||
|
State(s): State<crate::State>,
|
||||||
|
h: HeaderMap,
|
||||||
|
Path(t): Path<Uuid>,
|
||||||
|
Query(q): Query<PageQuery>,
|
||||||
|
) -> Result<Json<Value>, Error> {
|
||||||
|
let p = jobs::principal(&s, &h, false).await?;
|
||||||
|
Ok(Json(list(&s, &p, t, q).await?))
|
||||||
|
}
|
||||||
|
async fn publish_http(
|
||||||
|
State(s): State<crate::State>,
|
||||||
|
h: HeaderMap,
|
||||||
|
Path(t): Path<Uuid>,
|
||||||
|
Json(i): Json<Publish>,
|
||||||
|
) -> Result<Json<Value>, Error> {
|
||||||
|
let p = jobs::principal(&s, &h, true).await?;
|
||||||
|
Ok(Json(publish(&s, &p, t, i).await?))
|
||||||
|
}
|
||||||
|
async fn plan_http(
|
||||||
|
State(s): State<crate::State>,
|
||||||
|
h: HeaderMap,
|
||||||
|
Path((t, id)): Path<(Uuid, Uuid)>,
|
||||||
|
Json(i): Json<Selection>,
|
||||||
|
) -> Result<Json<Value>, Error> {
|
||||||
|
let p = jobs::principal(&s, &h, true).await?;
|
||||||
|
Ok(Json(plan(&s, &p, t, id, i).await?))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub(crate) mod tests {
|
||||||
|
use super::*;
|
||||||
|
use chrono::{Duration, Utc};
|
||||||
|
|
||||||
|
/// Shared isolated PostgreSQL fixture in tests/windows_plan_database.py.
|
||||||
|
pub(crate) async fn revision_plan_contract(
|
||||||
|
s: &crate::State,
|
||||||
|
p: &Principal,
|
||||||
|
linux: Uuid,
|
||||||
|
windows: Uuid,
|
||||||
|
) {
|
||||||
|
let tenant = p.tenant;
|
||||||
|
let initial = list(s, p, tenant, PageQuery::default()).await.unwrap();
|
||||||
|
let template = &initial["items"][0];
|
||||||
|
assert_eq!(template["name"], "Windows-Updates installieren");
|
||||||
|
assert_eq!(template["interpreter"], "powershell");
|
||||||
|
let native = script(
|
||||||
|
"powershell",
|
||||||
|
"system",
|
||||||
|
template["script_utf8"].as_str().unwrap().into(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert!(native.full_maintenance());
|
||||||
|
|
||||||
|
let v1 = Publish {
|
||||||
|
name: "Agentversion".into(),
|
||||||
|
expected_version: 0,
|
||||||
|
interpreter: "posix_sh".into(),
|
||||||
|
execution_identity: "agent_service".into(),
|
||||||
|
script_utf8: "printf '1\\n'\n".into(),
|
||||||
|
};
|
||||||
|
let first = publish(s, p, tenant, v1.clone()).await.unwrap();
|
||||||
|
let retry = publish(s, p, tenant, v1.clone()).await.unwrap();
|
||||||
|
assert_eq!(first, retry);
|
||||||
|
let id = Uuid::parse_str(first["revision_id"].as_str().unwrap()).unwrap();
|
||||||
|
let mut v2 = Publish {
|
||||||
|
script_utf8: "printf '2\\n'\n".into(),
|
||||||
|
..v1.clone()
|
||||||
|
};
|
||||||
|
assert_eq!(
|
||||||
|
publish(s, p, tenant, v2.clone()).await.unwrap_err().0,
|
||||||
|
StatusCode::CONFLICT
|
||||||
|
);
|
||||||
|
v2.expected_version = 1;
|
||||||
|
let second = publish(s, p, tenant, v2).await.unwrap();
|
||||||
|
assert_ne!(first["revision_id"], second["revision_id"]);
|
||||||
|
assert_eq!(second["version"], 2);
|
||||||
|
assert!(
|
||||||
|
list(s, p, Uuid::new_v4(), PageQuery::default())
|
||||||
|
.await
|
||||||
|
.is_err()
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut foreign = rmm_persistence::tenant_tx(&s.pool, Uuid::new_v4())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let visible: i64 =
|
||||||
|
sqlx::query_scalar("SELECT count(*) FROM script_revisions WHERE revision_id=$1")
|
||||||
|
.bind(id)
|
||||||
|
.fetch_one(&mut *foreign)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(visible, 0);
|
||||||
|
foreign.rollback().await.unwrap();
|
||||||
|
let mut own = rmm_persistence::tenant_tx(&s.pool, tenant).await.unwrap();
|
||||||
|
assert!(
|
||||||
|
sqlx::query(
|
||||||
|
"UPDATE script_revisions SET script_utf8='replacement' WHERE revision_id=$1"
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.execute(&mut *own)
|
||||||
|
.await
|
||||||
|
.is_err()
|
||||||
|
);
|
||||||
|
own.rollback().await.unwrap();
|
||||||
|
|
||||||
|
let selection = |asset| Selection {
|
||||||
|
targets: Targets {
|
||||||
|
asset_ids: vec![asset],
|
||||||
|
},
|
||||||
|
schedule: Schedule {
|
||||||
|
not_before: Utc::now(),
|
||||||
|
start_before: Utc::now() + Duration::minutes(30),
|
||||||
|
},
|
||||||
|
rollout: Rollout {
|
||||||
|
max_parallel: 1,
|
||||||
|
halt_after_failures: 1,
|
||||||
|
},
|
||||||
|
reason: "isolated library contract".into(),
|
||||||
|
max_runtime_seconds: 60,
|
||||||
|
max_output_bytes: 4096,
|
||||||
|
};
|
||||||
|
assert_eq!(
|
||||||
|
plan(s, p, tenant, id, selection(windows))
|
||||||
|
.await
|
||||||
|
.unwrap_err()
|
||||||
|
.1,
|
||||||
|
"UNSUPPORTED_CAPABILITY"
|
||||||
|
);
|
||||||
|
let planned = plan(s, p, tenant, id, selection(linux)).await.unwrap();
|
||||||
|
let plan_id = Uuid::parse_str(planned["plan_id"].as_str().unwrap()).unwrap();
|
||||||
|
let mut tx = rmm_persistence::tenant_tx(&s.pool, tenant).await.unwrap();
|
||||||
|
let bytes: Vec<u8> =
|
||||||
|
sqlx::query_scalar("SELECT payload FROM operation_plans WHERE plan_id=$1")
|
||||||
|
.bind(plan_id)
|
||||||
|
.fetch_one(&mut *tx)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
tx.commit().await.unwrap();
|
||||||
|
let bound: rmm_domain::jobs::PlanPayload = serde_json::from_slice(&bytes).unwrap();
|
||||||
|
let reference = bound.script_revision.unwrap();
|
||||||
|
assert_eq!(reference.revision_id, id);
|
||||||
|
assert_eq!(reference.version, 1);
|
||||||
|
assert_eq!(reference.script_sha256, hash(v1.script_utf8.as_bytes()));
|
||||||
|
assert!(
|
||||||
|
matches!(&bound.jobs[0].payload.action, Action::RunScript { script_utf8, .. } if script_utf8 == &v1.script_utf8)
|
||||||
|
);
|
||||||
|
let response = jobs::submit(
|
||||||
|
s,
|
||||||
|
p,
|
||||||
|
tenant,
|
||||||
|
plan_id,
|
||||||
|
jobs::Submit {
|
||||||
|
idempotency_key: "fixed-library".into(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(response["approval_state"], "pending_approval");
|
||||||
|
assert_eq!(response["job_count"], 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -330,6 +330,7 @@ pub(super) async fn test_fixed_echo(
|
|||||||
max_parallel: 1,
|
max_parallel: 1,
|
||||||
halt_after_failures: 1,
|
halt_after_failures: 1,
|
||||||
recipe_references: vec![],
|
recipe_references: vec![],
|
||||||
|
script_revision: None,
|
||||||
artifact_references: vec![],
|
artifact_references: vec![],
|
||||||
};
|
};
|
||||||
let scope = ProbeScope::from_plan(&plan, op).unwrap();
|
let scope = ProbeScope::from_plan(&plan, op).unwrap();
|
||||||
@@ -515,5 +516,5 @@ pub(super) async fn test_fixed_echo(
|
|||||||
tx.rollback().await.unwrap();
|
tx.rollback().await.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
crate::windows_echo_completion::test_completed_echo(admin,s,&p,&w).await;
|
crate::windows_echo_completion::test_completed_echo(admin, s, &p, &w).await;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -348,6 +348,7 @@ mod tests {
|
|||||||
max_parallel: 1,
|
max_parallel: 1,
|
||||||
halt_after_failures: 1,
|
halt_after_failures: 1,
|
||||||
recipe_references: vec![reference(&m)],
|
recipe_references: vec![reference(&m)],
|
||||||
|
script_revision: None,
|
||||||
artifact_references: vec![JobArtifactReference {
|
artifact_references: vec![JobArtifactReference {
|
||||||
job_id: job.job_id,
|
job_id: job.job_id,
|
||||||
attempt_id: job.attempt_id,
|
attempt_id: job.attempt_id,
|
||||||
|
|||||||
@@ -0,0 +1,89 @@
|
|||||||
|
CREATE TABLE script_revisions (
|
||||||
|
tenant_id uuid NOT NULL REFERENCES tenants,
|
||||||
|
revision_id uuid NOT NULL DEFAULT gen_random_uuid(),
|
||||||
|
name text NOT NULL CHECK (length(name) BETWEEN 1 AND 120),
|
||||||
|
version integer NOT NULL CHECK (version > 0),
|
||||||
|
interpreter text NOT NULL CHECK (interpreter IN ('powershell','posix_sh','bash')),
|
||||||
|
execution_identity text NOT NULL CHECK (execution_identity IN ('agent_service','root','system')),
|
||||||
|
script_utf8 text NOT NULL CHECK (octet_length(script_utf8) BETWEEN 1 AND 32768),
|
||||||
|
created_by text NOT NULL,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT now(),
|
||||||
|
PRIMARY KEY (tenant_id, revision_id),
|
||||||
|
UNIQUE (tenant_id, name, version),
|
||||||
|
CHECK (execution_identity = 'agent_service' OR
|
||||||
|
(execution_identity = 'system') = (interpreter = 'powershell'))
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Provision the same editable starting revision for existing and future tenants.
|
||||||
|
CREATE FUNCTION seed_windows_update_script(target_tenant uuid) RETURNS void
|
||||||
|
LANGUAGE sql SET search_path = pg_catalog, public AS $seed$
|
||||||
|
INSERT INTO public.script_revisions(tenant_id, name, version, interpreter, execution_identity, created_by, script_utf8)
|
||||||
|
VALUES (target_tenant, 'Windows-Updates installieren', 1, 'powershell', 'system', 'system:bootstrap', $ps$# Install available software updates from the configured Windows Update source.
|
||||||
|
# See https://learn.microsoft.com/windows/win32/wua_sdk/windows-update-agent-object-model
|
||||||
|
$ErrorActionPreference = 'Stop'
|
||||||
|
$session = New-Object -ComObject Microsoft.Update.Session
|
||||||
|
$session.ClientApplicationID = 'Rust Discovery'
|
||||||
|
$searcher = $session.CreateUpdateSearcher()
|
||||||
|
$search = $searcher.Search("IsInstalled=0 and IsHidden=0 and Type='Software'")
|
||||||
|
if ($search.ResultCode -ne 2) { throw "Update search returned $($search.ResultCode)" }
|
||||||
|
$failed = $false
|
||||||
|
$systemInfo = New-Object -ComObject Microsoft.Update.SystemInfo
|
||||||
|
if ($systemInfo.RebootRequired) {
|
||||||
|
Write-Output '{"reboot_required":true,"status":"restart_pending"}'
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
foreach ($update in $search.Updates) {
|
||||||
|
$result = [ordered]@{ id = $update.Identity.UpdateID; title = $update.Title; status = 'pending' }
|
||||||
|
try {
|
||||||
|
if ($update.InstallationBehavior.CanRequestUserInput -or !$update.EulaAccepted) {
|
||||||
|
$result.status = 'operator_input_required'
|
||||||
|
$failed = $true
|
||||||
|
} else {
|
||||||
|
$batch = New-Object -ComObject Microsoft.Update.UpdateColl
|
||||||
|
[void]$batch.Add($update)
|
||||||
|
if (!$update.IsDownloaded) {
|
||||||
|
$downloader = $session.CreateUpdateDownloader()
|
||||||
|
$downloader.Updates = $batch
|
||||||
|
$download = $downloader.Download().GetUpdateResult(0)
|
||||||
|
if ($download.ResultCode -ne 2) { throw "Download HRESULT $($download.HResult)" }
|
||||||
|
}
|
||||||
|
$installer = $session.CreateUpdateInstaller()
|
||||||
|
$installer.AllowSourcePrompts = $false
|
||||||
|
$installer.Updates = $batch
|
||||||
|
$install = $installer.Install().GetUpdateResult(0)
|
||||||
|
$result.status = [int]$install.ResultCode
|
||||||
|
$result.hresult = $install.HResult
|
||||||
|
$result.reboot_required = [bool]$install.RebootRequired
|
||||||
|
if ($install.ResultCode -ne 2) { $failed = $true }
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
$result.status = 'failed'
|
||||||
|
$result.error = $_.Exception.Message
|
||||||
|
$failed = $true
|
||||||
|
}
|
||||||
|
Write-Output ($result | ConvertTo-Json -Compress)
|
||||||
|
}
|
||||||
|
Write-Output (@{ found = $search.Updates.Count; reboot_required = [bool]$systemInfo.RebootRequired } | ConvertTo-Json -Compress)
|
||||||
|
if ($failed) { exit 1 }
|
||||||
|
$ps$);
|
||||||
|
$seed$;
|
||||||
|
REVOKE ALL ON FUNCTION seed_windows_update_script(uuid) FROM PUBLIC;
|
||||||
|
SELECT seed_windows_update_script(tenant_id) FROM tenants;
|
||||||
|
CREATE FUNCTION seed_tenant_script_library() RETURNS trigger LANGUAGE plpgsql
|
||||||
|
SET search_path = pg_catalog, public AS $$
|
||||||
|
BEGIN
|
||||||
|
PERFORM public.seed_windows_update_script(NEW.tenant_id);
|
||||||
|
RETURN NEW;
|
||||||
|
END
|
||||||
|
$$;
|
||||||
|
CREATE TRIGGER tenant_script_library AFTER INSERT ON tenants
|
||||||
|
FOR EACH ROW EXECUTE FUNCTION seed_tenant_script_library();
|
||||||
|
|
||||||
|
ALTER TABLE script_revisions ENABLE ROW LEVEL SECURITY;
|
||||||
|
ALTER TABLE script_revisions FORCE ROW LEVEL SECURITY;
|
||||||
|
CREATE POLICY tenant_scope ON script_revisions
|
||||||
|
USING (tenant_id::text = current_setting('app.tenant_id', true))
|
||||||
|
WITH CHECK (tenant_id::text = current_setting('app.tenant_id', true));
|
||||||
|
GRANT SELECT, INSERT ON script_revisions TO rmm_app;
|
||||||
|
CREATE TRIGGER immutable_content BEFORE UPDATE OR DELETE ON script_revisions
|
||||||
|
FOR EACH ROW EXECUTE FUNCTION immutable_plan();
|
||||||
+1
-1
@@ -52,7 +52,7 @@ message SyncResponse { bytes decision_bytes=1; bytes signature=2; }
|
|||||||
message JobOffer { string job_id=1; string attempt_id=2; string authorization_id=3; string plan_hash=4; string job_payload_hash=5; bytes payload_bytes=6; }
|
message JobOffer { string job_id=1; string attempt_id=2; string authorization_id=3; string plan_hash=4; string job_payload_hash=5; bytes payload_bytes=6; }
|
||||||
message JobAccepted { string job_id=1; string attempt_id=2; string job_payload_hash=3; string journal_proof=4; }
|
message JobAccepted { string job_id=1; string attempt_id=2; string job_payload_hash=3; string journal_proof=4; }
|
||||||
message ExecutionPermitRequest { string job_id=1; string attempt_id=2; string job_payload_hash=3; uint64 assignment_version=4; }
|
message ExecutionPermitRequest { string job_id=1; string attempt_id=2; string job_payload_hash=3; uint64 assignment_version=4; }
|
||||||
message ExecutionPermit { bytes payload_bytes=1; bytes signature=2; string denial_code=3; }
|
message ExecutionPermit { bytes payload_bytes=1; bytes signature=2; string denial_code=3; ExecutionPermitRequest unreserved_request=4; }
|
||||||
message JobStateChanged { string job_id=1; string attempt_id=2; uint64 revision=3; string state=4; string grant_id=5; }
|
message JobStateChanged { string job_id=1; string attempt_id=2; uint64 revision=3; string state=4; string grant_id=5; }
|
||||||
message JobResult { string job_id=1; string attempt_id=2; uint64 revision=3; string state=4; string grant_id=5; bytes stdout=6; bytes stderr=7; bool output_truncated=8; bool reboot_required=9; optional int32 exit_code=10; string evidence_json=11; string error_code=12; }
|
message JobResult { string job_id=1; string attempt_id=2; uint64 revision=3; string state=4; string grant_id=5; bytes stdout=6; bytes stderr=7; bool output_truncated=8; bool reboot_required=9; optional int32 exit_code=10; string evidence_json=11; string error_code=12; }
|
||||||
message CancelRequest { string job_id=1; string attempt_id=2; string reason=3; }
|
message CancelRequest { string job_id=1; string attempt_id=2; string reason=3; }
|
||||||
|
|||||||
+186
@@ -0,0 +1,186 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Build the project's Rust binaries from a Git revision in the existing builders."""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import fcntl
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import shlex
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tarfile
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
TARGETS = {
|
||||||
|
"server-arm64": (
|
||||||
|
"linux/arm64", "aarch64-unknown-linux-gnu",
|
||||||
|
"sha256:30ae3d90753bc39b0a98a897784c662aff9598ffab7a98a6298e8aa79145c2ec",
|
||||||
|
[("rmm-server", "rmm-server"), ("rmm-time-broker-linux", "rmm-time-broker-linux")],
|
||||||
|
),
|
||||||
|
"linux-x64": (
|
||||||
|
"linux/amd64", "x86_64-unknown-linux-gnu",
|
||||||
|
"sha256:9a76ad3744ebfdddae96ca10001ce8291c1b332fb54c8df5eea0e8e999ac6fc6",
|
||||||
|
[("rmm-agent", "rmm-agent"), ("rmm-privileged-helper", "rmm-privileged-helper"),
|
||||||
|
("rmm-updater-worker", "rmm-agent-updater")],
|
||||||
|
),
|
||||||
|
"windows-gnu-x64": (
|
||||||
|
"linux/amd64", "x86_64-pc-windows-gnu",
|
||||||
|
"sha256:1b2fd985d2c1fcc3545ace602405dc68fa899201bd8a73fa319ff96377203fa9",
|
||||||
|
[("rmm-agent-windows", "rmm-agent-windows"), ("rmm-agent-windows", "rmm-helper-windows")],
|
||||||
|
),
|
||||||
|
}
|
||||||
|
SOURCE_PATHS = ["Cargo.toml", "Cargo.lock", "rust-toolchain.toml", "crates", "proto",
|
||||||
|
"migrations", "tools/apt-transaction", "deploy/linux", "tests"]
|
||||||
|
|
||||||
|
|
||||||
|
def git(*args):
|
||||||
|
return subprocess.check_output(["git", *args], cwd=ROOT, text=True).strip()
|
||||||
|
|
||||||
|
|
||||||
|
def prepare(target, jobs, windows_updater=False):
|
||||||
|
platform, triple, image, packages = TARGETS[target]
|
||||||
|
packages = list(packages)
|
||||||
|
cargo = ["cargo", "build", "--release", "--locked", "--offline", "--jobs", str(jobs), "--target", triple]
|
||||||
|
suffix = ""
|
||||||
|
if target == "windows-gnu-x64":
|
||||||
|
suffix = ".exe"
|
||||||
|
cargo += ["--features", "rmm-agent-windows/system-helper"]
|
||||||
|
if windows_updater:
|
||||||
|
packages.append(("rmm-agent-windows", "rmm-updater-windows"))
|
||||||
|
for package in dict.fromkeys(package for package, _ in packages):
|
||||||
|
cargo += ["-p", package]
|
||||||
|
binaries = [binary + suffix for _, binary in packages]
|
||||||
|
for _, binary in packages:
|
||||||
|
cargo += ["--bin", binary]
|
||||||
|
|
||||||
|
return platform, triple, image, binaries, cargo
|
||||||
|
|
||||||
|
|
||||||
|
def build_archive(work, archive, revision, plan):
|
||||||
|
platform, triple, image, binaries, cargo = plan
|
||||||
|
source, cache = work / "src", work / "cargo"
|
||||||
|
command = ["docker", "run", "--rm", "--pull=never", "--platform", platform, "--network=none",
|
||||||
|
"--read-only", "--cap-drop=ALL", "--security-opt=no-new-privileges",
|
||||||
|
"--tmpfs", "/tmp:rw,exec", "--user", f"{os.getuid()}:{os.getgid()}",
|
||||||
|
"-v", f"{source}:/src:ro", "-v", f"{cache}:/target", "-w", "/src",
|
||||||
|
"-e", "CARGO_TARGET_DIR=/target", "--entrypoint", cargo[0], image, *cargo[1:]]
|
||||||
|
print(shlex.join(command), flush=True)
|
||||||
|
os.umask(0o077)
|
||||||
|
subprocess.run(["docker", "image", "inspect", image], check=True, stdout=subprocess.DEVNULL)
|
||||||
|
work.mkdir(parents=True, exist_ok=True)
|
||||||
|
with (work / "build.lock").open("a") as lock:
|
||||||
|
fcntl.flock(lock, fcntl.LOCK_EX)
|
||||||
|
source.mkdir(exist_ok=True)
|
||||||
|
cache.mkdir(exist_ok=True)
|
||||||
|
with tempfile.TemporaryDirectory(dir=work) as temporary:
|
||||||
|
staging = Path(temporary)
|
||||||
|
unpacked = staging / "src"
|
||||||
|
unpacked.mkdir()
|
||||||
|
subprocess.run(["tar", "-xf", archive, "-C", unpacked], check=True)
|
||||||
|
# Preserve unchanged inputs for Cargo. Changed inputs receive a fresh mtime
|
||||||
|
# even when switching back to an older commit with an older Git timestamp.
|
||||||
|
subprocess.run(["rsync", "-rlcp", "--delete", str(unpacked) + "/", source], check=True)
|
||||||
|
subprocess.run(command, check=True)
|
||||||
|
artifacts = staging / "artifacts"
|
||||||
|
artifacts.mkdir()
|
||||||
|
hashes = {}
|
||||||
|
for name in binaries:
|
||||||
|
path = artifacts / name
|
||||||
|
built = cache / triple / "release" / name
|
||||||
|
if built.resolve() != built or not built.is_file():
|
||||||
|
raise SystemExit(f"Missing regular build artifact: {name}")
|
||||||
|
shutil.copy2(built, path)
|
||||||
|
hashes[name] = hashlib.sha256(path.read_bytes()).hexdigest()
|
||||||
|
manifest = {"revision": revision, "target": triple, "builder": image, "sha256": hashes}
|
||||||
|
manifest_bytes = (json.dumps(manifest, indent=2) + "\n").encode()
|
||||||
|
(artifacts / "manifest.json").write_bytes(manifest_bytes)
|
||||||
|
destination = work / "artifacts" / revision / hashlib.sha256(manifest_bytes).hexdigest()
|
||||||
|
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
if destination.exists():
|
||||||
|
if ((destination / "manifest.json").read_bytes() != manifest_bytes
|
||||||
|
or any(hashlib.sha256((destination / n).read_bytes()).hexdigest() != h for n, h in hashes.items())):
|
||||||
|
raise SystemExit(f"Previous output was modified: {destination}")
|
||||||
|
else:
|
||||||
|
artifacts.rename(destination)
|
||||||
|
print(f"Artifacts: {destination}\n{json.dumps(hashes, indent=2)}")
|
||||||
|
|
||||||
|
|
||||||
|
def remote_build(archive, revision, jobs):
|
||||||
|
from dev_stage import ssh_prefix
|
||||||
|
|
||||||
|
# The only SSH payload is this build driver and the selected Git sources.
|
||||||
|
# The worker has no dependency on a checkout or on the notebook's profiles.
|
||||||
|
worker = Path(__file__).resolve()
|
||||||
|
checksums = [hashlib.sha256(p.read_bytes()).hexdigest() for p in (worker, archive)]
|
||||||
|
shell = """set -eu
|
||||||
|
umask 077
|
||||||
|
stage=$(mktemp -d)
|
||||||
|
trap 'rm -rf -- "$stage"' EXIT
|
||||||
|
cd "$stage"
|
||||||
|
tar -xf - build.py source.tar.gz
|
||||||
|
printf '%s build.py\\n%s source.tar.gz\\n' "$1" "$2" | sha256sum -c - >/dev/null
|
||||||
|
python3 -I build.py _server_worker "$3" "$4"
|
||||||
|
"""
|
||||||
|
with tempfile.TemporaryFile() as bundle:
|
||||||
|
with tarfile.open(fileobj=bundle, mode="w") as package:
|
||||||
|
package.add(worker, arcname="build.py")
|
||||||
|
package.add(archive, arcname="source.tar.gz")
|
||||||
|
bundle.seek(0)
|
||||||
|
command = shlex.join(["sh", "-c", shell, "build", *checksums, revision, str(jobs)])
|
||||||
|
result = subprocess.run([*ssh_prefix("server"), command], stdin=bundle)
|
||||||
|
if result.returncode:
|
||||||
|
raise SystemExit(f"ARM64 build via SSH failed (exit {result.returncode}).")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Build Rust binaries from a committed revision in the project's pinned Docker builders.",
|
||||||
|
epilog="Build hosts need tar, rsync and Docker. WebUI: npm ci and npm run build in web/. "
|
||||||
|
"Native Windows MSVC: use deploy/windows/Build-NativeMsvc.ps1 (-Mode Preflight, then -Mode Build).",
|
||||||
|
)
|
||||||
|
parser.add_argument("target", choices=TARGETS)
|
||||||
|
parser.add_argument("--ref", default="HEAD", help="Git commit or ref (default: HEAD). Uncommitted edits are excluded.")
|
||||||
|
parser.add_argument("--jobs", type=int, default=2, help="Maximum parallel Cargo jobs (default: 2).")
|
||||||
|
parser.add_argument("--ssh", action="store_true", help="Build server-arm64 on the configured server, in ~/rmm-dev-build/.")
|
||||||
|
parser.add_argument("--plan", action="store_true", help="Show the build plan without connecting, writing or building.")
|
||||||
|
args = parser.parse_args()
|
||||||
|
if args.jobs < 1:
|
||||||
|
parser.error("--jobs must be positive")
|
||||||
|
if args.ssh and args.target != "server-arm64":
|
||||||
|
parser.error("--ssh applies to server-arm64")
|
||||||
|
revision = git("rev-parse", "--verify", "--end-of-options", args.ref + "^{commit}")
|
||||||
|
windows_updater = args.target == "windows-gnu-x64" and git(
|
||||||
|
"ls-tree", "--name-only", revision, "crates/agent-windows/src/bin/rmm-updater-windows.rs")
|
||||||
|
plan = prepare(args.target, args.jobs, windows_updater)
|
||||||
|
platform, _, image, binaries, cargo = plan
|
||||||
|
host = "server via SSH" if args.ssh else "local"
|
||||||
|
print(f"{host}: {args.target} {revision}\n{platform} {image}\n{', '.join(binaries)}\n{shlex.join(cargo)}", flush=True)
|
||||||
|
if args.plan:
|
||||||
|
return
|
||||||
|
with tempfile.TemporaryDirectory() as temporary:
|
||||||
|
archive = Path(temporary) / "source.tar.gz"
|
||||||
|
subprocess.run(["git", "archive", "-o", archive, revision, "--", *SOURCE_PATHS], cwd=ROOT, check=True)
|
||||||
|
if args.ssh:
|
||||||
|
remote_build(archive, revision, args.jobs)
|
||||||
|
else:
|
||||||
|
build_archive(ROOT / "devstate" / "build" / args.target, archive, revision, plan)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
if sys.argv[1:2] == ["_server_worker"]:
|
||||||
|
revision, jobs_arg = sys.argv[2:]
|
||||||
|
if (len(revision) not in (40, 64) or any(c not in "0123456789abcdef" for c in revision)
|
||||||
|
or not jobs_arg.isascii() or not jobs_arg.isdecimal() or int(jobs_arg) < 1):
|
||||||
|
raise SystemExit("Invalid packed build request.")
|
||||||
|
work = Path.home() / "rmm-dev-build" / "server-arm64"
|
||||||
|
build_archive(work, Path("source.tar.gz").resolve(), revision, prepare("server-arm64", int(jobs_arg)))
|
||||||
|
else:
|
||||||
|
main()
|
||||||
|
except (OSError, subprocess.CalledProcessError) as error:
|
||||||
|
raise SystemExit(str(error)) from None
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Backfill lossless storage for old helper snapshots on a development guest."""
|
||||||
|
import argparse
|
||||||
|
from contextlib import closing
|
||||||
|
from pathlib import Path
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
|
def compact(db, schema, batch=32):
|
||||||
|
db.execute('PRAGMA foreign_keys=ON')
|
||||||
|
db.execute('BEGIN IMMEDIATE')
|
||||||
|
try:
|
||||||
|
if db.execute("SELECT type FROM sqlite_schema WHERE name='helper_sync_captures'").fetchone() == ('table',):
|
||||||
|
statement = ''
|
||||||
|
for line in schema.splitlines(keepends=True):
|
||||||
|
statement += line
|
||||||
|
if sqlite3.complete_statement(statement):
|
||||||
|
db.execute(statement)
|
||||||
|
statement = ''
|
||||||
|
db.commit()
|
||||||
|
except Exception:
|
||||||
|
db.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
|
cursor = -1
|
||||||
|
total = 0
|
||||||
|
while True:
|
||||||
|
db.execute('BEGIN IMMEDIATE')
|
||||||
|
try:
|
||||||
|
rows = db.execute(
|
||||||
|
"SELECT checkpoint,snapshot,fingerprint,captured_at FROM helper_capture_headers "
|
||||||
|
"WHERE checkpoint>? AND tail_id IS NULL ORDER BY checkpoint LIMIT ?", (cursor, batch)
|
||||||
|
).fetchall()
|
||||||
|
for checkpoint, raw, fingerprint, captured_at in rows:
|
||||||
|
cursor = checkpoint
|
||||||
|
if b',"attempts":' not in raw:
|
||||||
|
continue
|
||||||
|
db.execute("DELETE FROM helper_capture_headers WHERE checkpoint=?", (checkpoint,))
|
||||||
|
db.execute("INSERT INTO helper_sync_captures VALUES(?,?,?,?)", (checkpoint, raw, fingerprint, captured_at))
|
||||||
|
restored = db.execute("SELECT snapshot FROM helper_sync_captures WHERE checkpoint=?", (checkpoint,)).fetchone()[0]
|
||||||
|
if restored != raw:
|
||||||
|
raise ValueError('snapshot roundtrip failed')
|
||||||
|
total += 1
|
||||||
|
db.commit()
|
||||||
|
except Exception:
|
||||||
|
db.rollback()
|
||||||
|
raise
|
||||||
|
if len(rows) < batch:
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
parser.add_argument('ledger', type=Path)
|
||||||
|
parser.add_argument('--schema', type=Path, default=Path(__file__).resolve().parents[1] / 'crates/agent-core/src/helper_capture_storage.sql')
|
||||||
|
args = parser.parse_args()
|
||||||
|
with closing(sqlite3.connect(args.ledger.resolve(strict=True).as_uri()+'?mode=rw', uri=True, timeout=30)) as db:
|
||||||
|
db.execute('PRAGMA max_page_count=262144')
|
||||||
|
rows = compact(db, args.schema.read_text())
|
||||||
|
print(f"{rows} snapshots backfilled; {db.execute('PRAGMA freelist_count').fetchone()[0]} free pages")
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Vorhandene Entwicklungsartefakte per SSH in das Benutzerprofil des Ziels kopieren."""
|
||||||
|
import argparse
|
||||||
|
import base64
|
||||||
|
import hashlib
|
||||||
|
from pathlib import Path, PureWindowsPath
|
||||||
|
import re
|
||||||
|
import shlex
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
sys.path.insert(0, str(ROOT / "deploy"))
|
||||||
|
from vm_access_check import load_config
|
||||||
|
from vm_mvp_acceptance import protected_json
|
||||||
|
|
||||||
|
|
||||||
|
def ssh_prefix(target):
|
||||||
|
credentials = ROOT / "credentials"
|
||||||
|
config = load_config(credentials / ("linux-vm.conf" if target == "linux" else "server.conf"))
|
||||||
|
host = config["username"] + "@" + config["address"]
|
||||||
|
key = str(Path(config["ssh_key_path"]).expanduser())
|
||||||
|
base = ["ssh", "-F", "/dev/null", "-T"]
|
||||||
|
direct = [*base, "-p", config.get("port", "22"), "-i", key, "-o", "StrictHostKeyChecking=yes",
|
||||||
|
"-o", "BatchMode=yes", "-o", "ForwardAgent=no", "-o", "IdentitiesOnly=yes"]
|
||||||
|
if target != "windows":
|
||||||
|
return [*direct, "-o", "ConnectTimeout=10", host]
|
||||||
|
# Match the pinned access registered by deploy/windows/prepare_dev_access.py.
|
||||||
|
# Reconstruct the proxy from validated fields before using a profile containing a command.
|
||||||
|
jump = shlex.join([*direct, "-W", "127.0.0.1:22240", host])
|
||||||
|
prefix = [*base, "-i", key, "-o", "ProxyCommand=" + jump, "-o", "HostKeyAlias=rmm-windows-dev",
|
||||||
|
"-o", "UserKnownHostsFile=" + str(credentials / "windows-dev.known_hosts"),
|
||||||
|
"-o", "GlobalKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=yes",
|
||||||
|
"-o", "BatchMode=yes", "-o", "IdentitiesOnly=yes", "-o", "ForwardAgent=no",
|
||||||
|
"-o", "ConnectTimeout=10", "rmm-dev@127.0.0.1"]
|
||||||
|
if protected_json(credentials / "windows-dev-access.json").get("ssh_prefix") != prefix:
|
||||||
|
raise ValueError("Windows-Profil stimmt nicht mit dem registrierten SSH-Weg überein")
|
||||||
|
return prefix
|
||||||
|
|
||||||
|
|
||||||
|
UNIX = r'''
|
||||||
|
set -eu
|
||||||
|
umask 077
|
||||||
|
hash=$1; name=$2; mode=$3
|
||||||
|
root="$HOME/rmm-dev-stage"; dir="$root/$hash"; dst="$dir/$name"
|
||||||
|
for folder in "$root" "$dir"; do
|
||||||
|
if [ ! -e "$folder" ] && [ ! -L "$folder" ]; then
|
||||||
|
if [ "$mode" = probe ]; then echo missing; exit 0; fi
|
||||||
|
mkdir -m 700 "$folder" || [ -d "$folder" ]
|
||||||
|
fi
|
||||||
|
[ ! -L "$folder" ] && [ -d "$folder" ] &&
|
||||||
|
[ "$(stat -c '%u:%a' "$folder")" = "$(id -u):700" ] || exit 1
|
||||||
|
done
|
||||||
|
verify() {
|
||||||
|
[ ! -L "$1" ] && [ -f "$1" ] && [ "$(sha256sum < "$1" | cut -d' ' -f1)" = "$hash" ]
|
||||||
|
}
|
||||||
|
if [ -e "$dst" ] || [ -L "$dst" ]; then verify "$dst"; echo verified; exit 0; fi
|
||||||
|
if [ "$mode" = probe ]; then echo missing; exit 0; fi
|
||||||
|
part=$(mktemp "$dir/.upload.XXXXXXXX")
|
||||||
|
trap 'rm -f -- "$part"' EXIT
|
||||||
|
cat > "$part"
|
||||||
|
verify "$part"
|
||||||
|
ln -T -- "$part" "$dst" || verify "$dst"
|
||||||
|
verify "$dst"
|
||||||
|
echo verified
|
||||||
|
'''
|
||||||
|
|
||||||
|
WINDOWS = r'''
|
||||||
|
$ErrorActionPreference = 'Stop'
|
||||||
|
$root = Join-Path $env:USERPROFILE 'rmm-dev-stage'
|
||||||
|
$dir = Join-Path $root $hash
|
||||||
|
$dst = Join-Path $dir $name
|
||||||
|
foreach ($folder in @($root,$dir)) {
|
||||||
|
if (!(Test-Path -LiteralPath $folder)) {
|
||||||
|
if ($mode -eq 'probe') { 'missing'; exit 0 }
|
||||||
|
[void][IO.Directory]::CreateDirectory($folder)
|
||||||
|
}
|
||||||
|
$item = Get-Item -LiteralPath $folder -Force
|
||||||
|
if (!$item.PSIsContainer -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) {
|
||||||
|
throw 'Invalid staging directory'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function Verify-File($file) {
|
||||||
|
$item = Get-Item -LiteralPath $file -Force
|
||||||
|
if ($item.PSIsContainer -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -or
|
||||||
|
(Get-FileHash -LiteralPath $file -Algorithm SHA256).Hash -ne $hash) { throw 'Invalid artifact' }
|
||||||
|
}
|
||||||
|
if (Test-Path -LiteralPath $dst) { Verify-File $dst; 'verified'; exit 0 }
|
||||||
|
if ($mode -eq 'probe') { 'missing'; exit 0 }
|
||||||
|
$part = Join-Path $dir ('.upload.' + [Guid]::NewGuid())
|
||||||
|
$out = [IO.File]::Open($part, 'CreateNew', 'Write', 'None')
|
||||||
|
try {
|
||||||
|
try { [Console]::OpenStandardInput().CopyTo($out) } finally { $out.Dispose() }
|
||||||
|
Verify-File $part
|
||||||
|
try { [IO.File]::Move($part,$dst) } catch [IO.IOException] { Verify-File $dst }
|
||||||
|
Verify-File $dst
|
||||||
|
'verified'
|
||||||
|
} finally { [IO.File]::Delete($part) }
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def remote_command(target, digest, name, mode):
|
||||||
|
if target == "windows":
|
||||||
|
# name and digest have a restricted alphabet before PowerShell sees them.
|
||||||
|
script = f"$hash='{digest}'\n$name='{name}'\n$mode='{mode}'\n" + WINDOWS
|
||||||
|
encoded = base64.b64encode(script.encode("utf-16le")).decode("ascii")
|
||||||
|
return "powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand " + encoded
|
||||||
|
return shlex.join(["sh", "-c", UNIX, "dev-stage", digest, name, mode])
|
||||||
|
|
||||||
|
|
||||||
|
def transfer(prefix, command, file=None):
|
||||||
|
result = subprocess.run([*prefix, command], stdin=file or subprocess.DEVNULL,
|
||||||
|
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, timeout=600)
|
||||||
|
status = result.stdout.strip()
|
||||||
|
if result.returncode or status not in (b"missing", b"verified"):
|
||||||
|
raise RuntimeError("SSH-Verbindung, Stagingpfad oder SHA-256-Prüfung fehlgeschlagen")
|
||||||
|
return status
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__, epilog=(
|
||||||
|
"Ziel: rmm-dev-stage/<SHA-256>/<Dateiname> im SSH-Benutzerprofil. "
|
||||||
|
"Identische Dateien werden wiederverwendet. Dienste und Releases werden separat aktiviert."))
|
||||||
|
parser.add_argument("--plan", action="store_true", help="Nur lokale Hashes und Zielpfade anzeigen")
|
||||||
|
parser.add_argument("target", choices=("server", "linux", "windows"), help="Entwicklungsziel")
|
||||||
|
parser.add_argument("files", nargs="+", type=Path, metavar="DATEI", help="Vorhandene lokale Artefakte")
|
||||||
|
args = parser.parse_args()
|
||||||
|
for path in args.files:
|
||||||
|
name = path.name
|
||||||
|
if (not path.is_file() or not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_.-]{0,126}", name)
|
||||||
|
or name.endswith(".") or PureWindowsPath(name).is_reserved()):
|
||||||
|
parser.error("Reguläre Datei mit portablem Artefaktnamen erwartet")
|
||||||
|
if path.resolve().is_relative_to(ROOT / "credentials"):
|
||||||
|
parser.error("Zugangsprofile sind keine Entwicklungsartefakte")
|
||||||
|
prefix = None if args.plan else ssh_prefix(args.target)
|
||||||
|
for path in args.files:
|
||||||
|
with path.open("rb") as file:
|
||||||
|
hashed = hashlib.sha256()
|
||||||
|
for block in iter(lambda: file.read(1024 * 1024), b""):
|
||||||
|
hashed.update(block)
|
||||||
|
digest = hashed.hexdigest()
|
||||||
|
destination = f"{args.target}:rmm-dev-stage/{digest}/{path.name}"
|
||||||
|
if args.plan:
|
||||||
|
print("geplant", destination)
|
||||||
|
continue
|
||||||
|
if transfer(prefix, remote_command(args.target, digest, path.name, "probe")) == b"verified":
|
||||||
|
print("vorhanden und geprüft", destination)
|
||||||
|
continue
|
||||||
|
file.seek(0)
|
||||||
|
if transfer(prefix, remote_command(args.target, digest, path.name, "stage"), file) != b"verified":
|
||||||
|
raise RuntimeError("Zielhash nicht bestätigt")
|
||||||
|
print("bereitgestellt und geprüft", destination)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
main()
|
||||||
|
except (OSError, ValueError, RuntimeError, subprocess.TimeoutExpired) as error:
|
||||||
|
# Do not include the subprocess command (Windows SSH contains the private access path).
|
||||||
|
message = str(error) if isinstance(error, (ValueError, RuntimeError)) else type(error).__name__
|
||||||
|
sys.exit(f"Staging fehlgeschlagen: {message}")
|
||||||
+3
-1
@@ -1,3 +1,4 @@
|
|||||||
|
import { ScriptLibrary } from './script-library';
|
||||||
import { CatalogCheckNotice } from './catalog-check';
|
import { CatalogCheckNotice } from './catalog-check';
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import { createRoot } from 'react-dom/client';
|
import { createRoot } from 'react-dom/client';
|
||||||
@@ -35,6 +36,7 @@ export function ScriptPlanNotice({ plan }: { plan: RecordValue }) {
|
|||||||
const jobs = scriptPlanJobs(plan); if (!jobs.length) return null;
|
const jobs = scriptPlanJobs(plan); if (!jobs.length) return null;
|
||||||
return <article aria-label="Feste Skriptkonsole vor Genehmigung"><h3>Vollständige feste Skriptaufträge ({jobs.length})</h3><p>Diese Inhalte stammen aus den hashgeprüften gespeicherten Planbytes. Jeder Zielauftrag, Versuch und Skripthash ist fest gebunden.</p>
|
return <article aria-label="Feste Skriptkonsole vor Genehmigung"><h3>Vollständige feste Skriptaufträge ({jobs.length})</h3><p>Diese Inhalte stammen aus den hashgeprüften gespeicherten Planbytes. Jeder Zielauftrag, Versuch und Skripthash ist fest gebunden.</p>
|
||||||
{jobs.map(job => <article key={job.jobId}><h4>Asset {job.assetId}</h4><p>Agent: {job.agentId} · Job: {job.jobId} · Versuch: {job.attemptId}</p>
|
{jobs.map(job => <article key={job.jobId}><h4>Asset {job.assetId}</h4><p>Agent: {job.agentId} · Job: {job.jobId} · Versuch: {job.attemptId}</p>
|
||||||
|
{job.revision && <p>Skriptbibliothek · <strong>{job.revision.name} · Version {job.revision.version}</strong> · {job.revision.id}</p>}
|
||||||
<p>Interpreter: {job.interpreter} · Identität: <strong>{job.identity}</strong> · Privilegiertes Profil: <strong>{job.privilegedProfile === fullMaintenanceScriptProfile ? fullMaintenanceScriptProfile : 'bestehendes begrenztes Profil'}</strong> · Windowsprozess: {job.interpreter !== 'powershell' ? 'nicht zutreffend' : job.windowsProfile === 'powershell_51_remote_signed' ? 'Windows PowerShell5.1 RemoteSigned, nur Jobprozess' : 'geerbte Windowsrichtlinie / kein Windows-Prozessoverride'}</p>
|
<p>Interpreter: {job.interpreter} · Identität: <strong>{job.identity}</strong> · Privilegiertes Profil: <strong>{job.privilegedProfile === fullMaintenanceScriptProfile ? fullMaintenanceScriptProfile : 'bestehendes begrenztes Profil'}</strong> · Windowsprozess: {job.interpreter !== 'powershell' ? 'nicht zutreffend' : job.windowsProfile === 'powershell_51_remote_signed' ? 'Windows PowerShell5.1 RemoteSigned, nur Jobprozess' : 'geerbte Windowsrichtlinie / kein Windows-Prozessoverride'}</p>
|
||||||
<p>Frühester Start: {job.notBefore} · Startfrist: {job.startBefore} · Laufzeit: {job.runtimeSeconds}s · Ausgabe: höchstens {job.outputBytes} Bytes</p>
|
<p>Frühester Start: {job.notBefore} · Startfrist: {job.startBefore} · Laufzeit: {job.runtimeSeconds}s · Ausgabe: höchstens {job.outputBytes} Bytes</p>
|
||||||
<p>Skript: {job.scriptBytes} UTF-8-Bytes · SHA256: <code>{job.scriptHash}</code><br/>Jobpayload-SHA256: <code>{job.jobHash}</code></p>
|
<p>Skript: {job.scriptBytes} UTF-8-Bytes · SHA256: <code>{job.scriptHash}</code><br/>Jobpayload-SHA256: <code>{job.jobHash}</code></p>
|
||||||
@@ -271,7 +273,7 @@ function Workspace({ auth, logout }: { auth: Session; logout: () => void }) {
|
|||||||
}
|
}
|
||||||
function TenantWorkspace({ tenantId, auth }: { tenantId: string; auth: Session }) {
|
function TenantWorkspace({ tenantId, auth }: { tenantId: string; auth: Session }) {
|
||||||
const [selected, setSelected] = useState<string[]>([]), [planId, setPlan] = useState<string | null>(null), [operationId, setOperation] = useState<string | null>(null);
|
const [selected, setSelected] = useState<string[]>([]), [planId, setPlan] = useState<string | null>(null), [operationId, setOperation] = useState<string | null>(null);
|
||||||
return <><Devices tenantId={tenantId} auth={auth} selected={selected} setSelected={setSelected}/><Planner tenantId={tenantId} auth={auth} selected={selected} showPlan={setPlan}/><Catalog tenantId={tenantId} auth={auth}/><WindowsMsiCatalog tenantId={tenantId} auth={auth}/><AgentReleaseCatalog tenantId={tenantId} auth={auth}/><TenantControls tenantId={tenantId} auth={auth}/><Operations tenantId={tenantId} open={setOperation}/>{operationId && <OperationDetail key={operationId} tenantId={tenantId} id={operationId} auth={auth} openPlan={setPlan}/ >}{planId && <PlanDetail key={planId} tenantId={tenantId} id={planId} auth={auth} onOperation={setOperation}/>}</>;
|
return <><Devices tenantId={tenantId} auth={auth} selected={selected} setSelected={setSelected}/><ScriptLibrary tenantId={tenantId} auth={auth} selected={selected} showPlan={setPlan}/><Planner tenantId={tenantId} auth={auth} selected={selected} showPlan={setPlan}/><Catalog tenantId={tenantId} auth={auth}/><WindowsMsiCatalog tenantId={tenantId} auth={auth}/><AgentReleaseCatalog tenantId={tenantId} auth={auth}/><TenantControls tenantId={tenantId} auth={auth}/><Operations tenantId={tenantId} open={setOperation}/>{operationId && <OperationDetail key={operationId} tenantId={tenantId} id={operationId} auth={auth} openPlan={setPlan}/ >}{planId && <PlanDetail key={planId} tenantId={tenantId} id={planId} auth={auth} onOperation={setOperation}/>}</>;
|
||||||
}
|
}
|
||||||
function App() {
|
function App() {
|
||||||
const [auth, setAuth] = useState<Session | null>(null), [loading, setLoading] = useState(true), [error, setError] = useState<string | null>(null);
|
const [auth, setAuth] = useState<Session | null>(null), [loading, setLoading] = useState(true), [error, setError] = useState<string | null>(null);
|
||||||
|
|||||||
@@ -37,6 +37,20 @@ it('projects legacy/Linux root/Windows SYSTEM from the exact shared Rust golden
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('shows the immutable library revision and rejects a different catalog script hash', async () => {
|
||||||
|
const parsed = record(plan('legacy_linux_agent').plan_payload);
|
||||||
|
const job = (parsed.jobs as RecordValue[])[0]!;
|
||||||
|
const ref = { revision_id: '27068008-ae22-4bde-8e54-d81624523a93', name: 'Agentversion', version: 3, script_sha256: job.script_sha256 };
|
||||||
|
async function stored(hash: unknown) {
|
||||||
|
const raw = new TextEncoder().encode(JSON.stringify({ ...parsed, script_revision: { ...ref, script_sha256: hash } }));
|
||||||
|
return { plan_hash: await sha256(raw), plan_payload_bytes: Array.from(raw) };
|
||||||
|
}
|
||||||
|
const good = await stored(ref.script_sha256);
|
||||||
|
await verifyPlanIntegrity(good); await verifyScriptPlan(good);
|
||||||
|
expect(scriptPlanJobs(good)[0]?.revision).toEqual({ id: ref.revision_id, name: ref.name, version: 3 });
|
||||||
|
await expect(verifyScriptPlan(await stored('f'.repeat(64)))).rejects.toThrow('Bibliotheksversion');
|
||||||
|
});
|
||||||
|
|
||||||
it('rejects view, byte and fixed identity/budget/profile tampering before and after display', async () => {
|
it('rejects view, byte and fixed identity/budget/profile tampering before and after display', async () => {
|
||||||
for (const alias of ['payload', 'plan_payload', 'jobs', 'resolved_targets']) {
|
for (const alias of ['payload', 'plan_payload', 'jobs', 'resolved_targets']) {
|
||||||
const p = plan('linux_root_full'); await verifyScriptPlan(p); p[alias] = [];
|
const p = plan('linux_root_full'); await verifyScriptPlan(p); p[alias] = [];
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { canonicalJson, record, RecordValue, text, windowsScriptProfile, privile
|
|||||||
import { bytes, hashPattern, parseStrictJson, sha256 } from './safety';
|
import { bytes, hashPattern, parseStrictJson, sha256 } from './safety';
|
||||||
|
|
||||||
export interface ScriptPlanJob {
|
export interface ScriptPlanJob {
|
||||||
|
revision?: { id: string; name: string; version: number };
|
||||||
assetId: string; agentId: string; jobId: string; attemptId: string;
|
assetId: string; agentId: string; jobId: string; attemptId: string;
|
||||||
interpreter: string; identity: string; windowsProfile: string; privilegedProfile: string;
|
interpreter: string; identity: string; windowsProfile: string; privilegedProfile: string;
|
||||||
script: string; scriptHash: string; jobHash: string; scriptBytes: number;
|
script: string; scriptHash: string; jobHash: string; scriptBytes: number;
|
||||||
@@ -61,11 +62,19 @@ export async function verifyScriptPlan(plan: RecordValue): Promise<void> {
|
|||||||
if (!Array.isArray(parsed.jobs)) throw new Error('Vollständige feste Jobdefinitionen fehlen');
|
if (!Array.isArray(parsed.jobs)) throw new Error('Vollständige feste Jobdefinitionen fehlen');
|
||||||
const jobs = parsed.jobs.map(value => record(value));
|
const jobs = parsed.jobs.map(value => record(value));
|
||||||
validateViews(plan, parsed, jobs);
|
validateViews(plan, parsed, jobs);
|
||||||
|
let revision: ScriptPlanJob['revision'], revisionHash: string | undefined;
|
||||||
|
if (Object.hasOwn(parsed, 'script_revision')) {
|
||||||
|
const ref = record(parsed.script_revision);
|
||||||
|
exact(ref, ['revision_id','name','version','script_sha256']);
|
||||||
|
revision = { id: uuid(ref.revision_id), name: text(ref.name, 'Skriptname'), version: integer(ref.version, 1, 2147483647) };
|
||||||
|
revisionHash = digest(ref.script_sha256);
|
||||||
|
if (!jobs.length || jobs.some(job => !isScript(record(job.payload).action))) throw new Error('Bibliotheksversion benötigt feste Skriptjobs');
|
||||||
|
}
|
||||||
if (!jobs.some(job => isScript(record(job.payload).action))) {
|
if (!jobs.some(job => isScript(record(job.payload).action))) {
|
||||||
if (snapshot(plan) !== before) throw new Error('Plan wurde während der Prüfung verändert');
|
if (snapshot(plan) !== before) throw new Error('Plan wurde während der Prüfung verändert');
|
||||||
verified.set(plan, { snapshot: before, jobs: [] }); return;
|
verified.set(plan, { snapshot: before, jobs: [] }); return;
|
||||||
}
|
}
|
||||||
exact(parsed, ['schema_version', 'plan_id', 'tenant_id', 'principal', 'reason', 'jobs', 'max_parallel', 'halt_after_failures', ...(Object.hasOwn(parsed, 'recipe_references') ? ['recipe_references'] : []), ...(Object.hasOwn(parsed, 'artifact_references') ? ['artifact_references'] : [])]);
|
exact(parsed, ['schema_version', 'plan_id', 'tenant_id', 'principal', 'reason', 'jobs', 'max_parallel', 'halt_after_failures', ...(revision ? ['script_revision'] : []), ...(Object.hasOwn(parsed, 'recipe_references') ? ['recipe_references'] : []), ...(Object.hasOwn(parsed, 'artifact_references') ? ['artifact_references'] : [])]);
|
||||||
if (parsed.schema_version !== 1 || !jobs.length || jobs.length > 100) throw new Error('Skriptplan: unbekanntes Schema oder Zielbudget');
|
if (parsed.schema_version !== 1 || !jobs.length || jobs.length > 100) throw new Error('Skriptplan: unbekanntes Schema oder Zielbudget');
|
||||||
uuid(parsed.plan_id); uuid(parsed.tenant_id); text(parsed.principal, 'Antragsteller'); text(parsed.reason, 'Begründung');
|
uuid(parsed.plan_id); uuid(parsed.tenant_id); text(parsed.principal, 'Antragsteller'); text(parsed.reason, 'Begründung');
|
||||||
integer(parsed.max_parallel, 1, 10); integer(parsed.halt_after_failures, 1, Number.MAX_SAFE_INTEGER);
|
integer(parsed.max_parallel, 1, 10); integer(parsed.halt_after_failures, 1, Number.MAX_SAFE_INTEGER);
|
||||||
@@ -94,8 +103,9 @@ export async function verifyScriptPlan(plan: RecordValue): Promise<void> {
|
|||||||
const orderedAction = { action: 'run_script', interpreter, script_utf8: script, arguments: action.arguments, expected_stdout: action.expected_stdout, ...(hasWindowsProfile ? { windows_execution_profile: action.windows_execution_profile } : {}), ...(hasPrivilegedProfile ? { privileged_script_profile: action.privileged_script_profile } : {}) };
|
const orderedAction = { action: 'run_script', interpreter, script_utf8: script, arguments: action.arguments, expected_stdout: action.expected_stdout, ...(hasWindowsProfile ? { windows_execution_profile: action.windows_execution_profile } : {}), ...(hasPrivilegedProfile ? { privileged_script_profile: action.privileged_script_profile } : {}) };
|
||||||
const ordered = { schema_version: p.schema_version, tenant_id: p.tenant_id, asset_id: assetId, agent_id: agentId, enrollment_generation: p.enrollment_generation, assignment_version: p.assignment_version, job_id: jobId, attempt_id: attemptId, action: orderedAction, execution_identity: identity, not_before: notBefore, start_before: startBefore, max_runtime_seconds: runtimeSeconds, max_output_bytes: outputBytes };
|
const ordered = { schema_version: p.schema_version, tenant_id: p.tenant_id, asset_id: assetId, agent_id: agentId, enrollment_generation: p.enrollment_generation, assignment_version: p.assignment_version, job_id: jobId, attempt_id: attemptId, action: orderedAction, execution_identity: identity, not_before: notBefore, start_before: startBefore, max_runtime_seconds: runtimeSeconds, max_output_bytes: outputBytes };
|
||||||
const scriptHash = digest(job.script_sha256), jobHash = digest(job.job_payload_hash);
|
const scriptHash = digest(job.script_sha256), jobHash = digest(job.job_payload_hash);
|
||||||
|
if (revisionHash && revisionHash !== scriptHash) throw new Error('Skripthash widerspricht der Bibliotheksversion');
|
||||||
if (await sha256(scriptBytes) !== scriptHash || await sha256(new TextEncoder().encode(JSON.stringify(ordered))) !== jobHash) throw new Error('Skript-/Jobhash widerspricht exakten Rust-Payloadbytes');
|
if (await sha256(scriptBytes) !== scriptHash || await sha256(new TextEncoder().encode(JSON.stringify(ordered))) !== jobHash) throw new Error('Skript-/Jobhash widerspricht exakten Rust-Payloadbytes');
|
||||||
result.push({ assetId, agentId, jobId, attemptId, interpreter, identity, windowsProfile, privilegedProfile, script, scriptHash, jobHash, scriptBytes: scriptBytes.length, arguments: [...action.arguments] as string[], expectedStdout: action.expected_stdout as string | null, notBefore, startBefore, runtimeSeconds, outputBytes });
|
result.push({ ...(revision ? { revision } : {}), assetId, agentId, jobId, attemptId, interpreter, identity, windowsProfile, privilegedProfile, script, scriptHash, jobHash, scriptBytes: scriptBytes.length, arguments: [...action.arguments] as string[], expectedStdout: action.expected_stdout as string | null, notBefore, startBefore, runtimeSeconds, outputBytes });
|
||||||
}
|
}
|
||||||
if (snapshot(plan) !== before) throw new Error('Skriptplan wurde während der Hashprüfung verändert');
|
if (snapshot(plan) !== before) throw new Error('Skriptplan wurde während der Hashprüfung verändert');
|
||||||
verified.set(plan, { snapshot: before, jobs: result });
|
verified.set(plan, { snapshot: before, jobs: result });
|
||||||
|
|||||||
@@ -0,0 +1,110 @@
|
|||||||
|
import React, { FormEvent, useEffect, useState } from 'react';
|
||||||
|
import { objectId, page, record, request, Session, tenantPath, text } from './api';
|
||||||
|
import { allowed } from './safety';
|
||||||
|
|
||||||
|
interface Draft {
|
||||||
|
name: string;
|
||||||
|
interpreter: 'posix_sh' | 'bash' | 'powershell';
|
||||||
|
execution_identity: 'agent_service' | 'root' | 'system';
|
||||||
|
script_utf8: string;
|
||||||
|
}
|
||||||
|
interface Revision extends Draft { revision_id: string; version: number; script_sha256: string }
|
||||||
|
const blank: Draft = { name: '', interpreter: 'posix_sh', execution_identity: 'agent_service', script_utf8: '' };
|
||||||
|
|
||||||
|
function revision(input: unknown): Revision {
|
||||||
|
const row = record(input);
|
||||||
|
if ((row.interpreter !== 'posix_sh' && row.interpreter !== 'bash' && row.interpreter !== 'powershell') ||
|
||||||
|
(row.execution_identity !== 'agent_service' && row.execution_identity !== 'root' && row.execution_identity !== 'system') ||
|
||||||
|
typeof row.version !== 'number' || !Number.isSafeInteger(row.version) || row.version < 1) throw Error('Ungültige Skriptversion');
|
||||||
|
return { name: text(row.name, 'Name'), interpreter: row.interpreter, execution_identity: row.execution_identity,
|
||||||
|
script_utf8: text(row.script_utf8, 'Skript'), revision_id: objectId(row, 'revision'), version: row.version,
|
||||||
|
script_sha256: text(row.script_sha256, 'SHA256') };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ScriptLibrary({ tenantId, auth, selected, showPlan }: { tenantId: string; auth: Session; selected: string[]; showPlan: (id: string) => void }) {
|
||||||
|
const path = tenantPath(tenantId, '/script-revisions');
|
||||||
|
const canRead = allowed(auth, 'operations:read'), canPlan = allowed(auth, 'operations:plan') && allowed(auth, 'scripts:execute');
|
||||||
|
const [items, setItems] = useState<Revision[]>([]), [cursor, setCursor] = useState<string | null>(null);
|
||||||
|
const [chosen, setChosen] = useState(''), [draft, setDraft] = useState<Draft | null>(null), [base, setBase] = useState(0);
|
||||||
|
const [busy, setBusy] = useState(false), [error, setError] = useState(''), [notice, setNotice] = useState('');
|
||||||
|
const stored = items.find(row => row.revision_id === chosen);
|
||||||
|
useEffect(() => {
|
||||||
|
if (!canRead) return;
|
||||||
|
const controller = new AbortController();
|
||||||
|
setBusy(true);
|
||||||
|
request(path, { signal: controller.signal }).then(value => {
|
||||||
|
if (controller.signal.aborted) return;
|
||||||
|
const result = page(value), rows = result.items.map(revision);
|
||||||
|
setItems(rows); setCursor(result.next_cursor); setChosen(rows[0]?.revision_id ?? '');
|
||||||
|
}).catch(err => { if (!controller.signal.aborted) setError(String(err)); })
|
||||||
|
.finally(() => { if (!controller.signal.aborted) setBusy(false); });
|
||||||
|
return () => controller.abort();
|
||||||
|
}, [path, canRead]);
|
||||||
|
|
||||||
|
async function action(work: () => Promise<void>) {
|
||||||
|
setBusy(true); setError(''); setNotice('');
|
||||||
|
try { await work(); } catch (err) { setError(err instanceof Error ? err.message : String(err)); }
|
||||||
|
finally { setBusy(false); }
|
||||||
|
}
|
||||||
|
async function more() {
|
||||||
|
const result = page(await request(path + '?cursor=' + encodeURIComponent(cursor!)));
|
||||||
|
const rows = result.items.map(revision);
|
||||||
|
setItems(old => [...old, ...rows.filter(row => !old.some(saved => saved.revision_id === row.revision_id))]); setCursor(result.next_cursor);
|
||||||
|
}
|
||||||
|
async function save(e: FormEvent) {
|
||||||
|
e.preventDefault();
|
||||||
|
if (!draft) return;
|
||||||
|
await action(async () => {
|
||||||
|
const saved = revision(await request(path, { csrf: auth.csrf_token, body: { ...draft, expected_version: base } }));
|
||||||
|
setItems(old => [...old.filter(row => row.revision_id !== saved.revision_id), saved]); setChosen(saved.revision_id);
|
||||||
|
setDraft(null); setNotice(`Version ${saved.version} gespeichert.`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
function edit(row?: Revision) {
|
||||||
|
setBase(row?.version ?? 0);
|
||||||
|
setDraft(row ? { name: row.name, interpreter: row.interpreter, execution_identity: row.execution_identity, script_utf8: row.script_utf8 } : { ...blank });
|
||||||
|
setError(''); setNotice('');
|
||||||
|
}
|
||||||
|
async function plan(e: FormEvent<HTMLFormElement>) {
|
||||||
|
e.preventDefault();
|
||||||
|
if (!stored || !selected.length) return;
|
||||||
|
const form = new FormData(e.currentTarget);
|
||||||
|
await action(async () => {
|
||||||
|
const now = Date.now(), expires = now + Number(form.get('minutes')) * 60000;
|
||||||
|
const body = { targets: { asset_ids: selected }, reason: String(form.get('reason')),
|
||||||
|
schedule: { not_before: new Date(now).toISOString(), start_before: new Date(expires).toISOString() },
|
||||||
|
rollout: { max_parallel: 1, halt_after_failures: 1 }, max_runtime_seconds: Number(form.get('runtime')), max_output_bytes: 65536 };
|
||||||
|
const result = record(await request(path + '/' + encodeURIComponent(stored.revision_id) + '/plan', { csrf: auth.csrf_token, body }));
|
||||||
|
showPlan(objectId(result, 'plan')); setNotice('Plan zur Prüfung geöffnet.');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (!canRead) return null;
|
||||||
|
const limit = stored?.execution_identity === 'system' ? 120 : stored?.execution_identity === 'root' ? 300 : 3600;
|
||||||
|
return <section aria-label="Skriptbibliothek"><h2>Skriptbibliothek</h2>
|
||||||
|
{error && <p role="alert" className="error">{error}</p>}{notice && <p role="status">{notice}</p>}
|
||||||
|
{items.length > 0 && <label>Gespeicherte Skriptversion<select value={chosen} disabled={busy || !!draft} onChange={e => setChosen(e.target.value)}>
|
||||||
|
{[...items].sort((a,b) => a.name.localeCompare(b.name) || b.version - a.version).map(row => <option key={row.revision_id} value={row.revision_id}>{row.name} · v{row.version}</option>)}
|
||||||
|
</select></label>}
|
||||||
|
{cursor && <button disabled={busy || !!draft} onClick={() => void action(more)}>Weitere Versionen laden</button>}
|
||||||
|
{stored && !draft && <><p>{stored.interpreter === 'powershell' ? 'Windows PowerShell' : stored.interpreter === 'bash' ? 'Linux bash' : 'Linux sh'} · {stored.execution_identity} · SHA256 <code>{stored.script_sha256}</code></p>
|
||||||
|
<details><summary>Gespeicherten Skripttext anzeigen</summary><pre>{stored.script_utf8}</pre></details></>}
|
||||||
|
{canPlan && !draft && <div><button disabled={busy} onClick={() => edit()}>Neues Skript</button>
|
||||||
|
{stored && <button disabled={busy} onClick={() => edit(stored)}>Neue Version bearbeiten</button>}</div>}
|
||||||
|
{draft && <form onSubmit={save}><fieldset disabled={busy}><legend>{base ? `Neue Version von ${draft.name}` : 'Neues Skript'}</legend>
|
||||||
|
<label>Name<input required maxLength={120} readOnly={base > 0} value={draft.name} onChange={e => setDraft({...draft,name:e.target.value})}/></label>
|
||||||
|
<label>Interpreter<select value={draft.interpreter} onChange={e => setDraft({...draft,interpreter:e.target.value as Draft['interpreter'],execution_identity:'agent_service'})}>
|
||||||
|
<option value="posix_sh">Linux sh</option><option value="bash">Linux bash</option><option value="powershell">Windows PowerShell</option></select></label>
|
||||||
|
<label>Ausführungsidentität<select value={draft.execution_identity} onChange={e => setDraft({...draft,execution_identity:e.target.value as Draft['execution_identity']})}>
|
||||||
|
<option value="agent_service">agent_service</option>{draft.interpreter === 'powershell' ? <option value="system">system</option> : <option value="root">root</option>}</select></label>
|
||||||
|
<label>Skripttext<textarea required rows={9} value={draft.script_utf8} onChange={e => setDraft({...draft,script_utf8:e.target.value})}/></label>
|
||||||
|
<button>Version speichern</button><button type="button" onClick={() => setDraft(null)}>Abbrechen</button>
|
||||||
|
</fieldset></form>}
|
||||||
|
{canPlan && stored && !draft && <form key={stored.revision_id} onSubmit={plan}><fieldset disabled={busy}><legend>Auftrag für {stored.name} · v{stored.version} vorbereiten</legend>
|
||||||
|
<p>{selected.length} ausgewählte Geräte · Ein Gerät gleichzeitig · Stopp beim ersten Fehler · 65536 Byte Ausgabe je Gerät</p>
|
||||||
|
<label>Begründung<input name="reason" required maxLength={1024}/></label>
|
||||||
|
<label>Startfrist in Minuten<input name="minutes" type="number" min={1} max={1440} defaultValue={60} required/></label>
|
||||||
|
<label>Laufzeit je Gerät in Sekunden<input name="runtime" type="number" min={1} max={limit} defaultValue={Math.min(limit,120)} required/></label>
|
||||||
|
<button disabled={!selected.length}>Plan vorbereiten</button>
|
||||||
|
</fieldset></form>}
|
||||||
|
</section>;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user