Add reviewed Windows scope completion and idle certificate renewal; deploy completion API
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
//! The caller must retain the existing exclusive agent-state journal lock.
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use rcgen::{CertificateParams, DnType, KeyPair};
|
||||
use rusqlite::{Connection, params};
|
||||
use rusqlite::{Connection, OptionalExtension, params};
|
||||
use rustls::{RootCertStore, pki_types::UnixTime, sign::CertifiedKey};
|
||||
use serde::Deserialize;
|
||||
use std::{io::Cursor, sync::Arc, time::Duration};
|
||||
@@ -61,7 +61,10 @@ impl RotationStore {
|
||||
) -> Result<()> {
|
||||
let tx = db.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
|
||||
tx.execute_batch("CREATE TABLE IF NOT EXISTS tls_versions(version TEXT PRIMARY KEY,certificate BLOB,csr TEXT);
|
||||
CREATE TABLE IF NOT EXISTS tls_rotation(id INTEGER PRIMARY KEY CHECK(id=1),schema_version INTEGER NOT NULL,agent TEXT NOT NULL,enrollment INTEGER NOT NULL,current_version TEXT NOT NULL REFERENCES tls_versions(version),pending_version TEXT REFERENCES tls_versions(version));")?;
|
||||
CREATE TABLE IF NOT EXISTS tls_rotation(id INTEGER PRIMARY KEY CHECK(id=1),schema_version INTEGER NOT NULL,agent TEXT NOT NULL,enrollment INTEGER NOT NULL,current_version TEXT NOT NULL REFERENCES tls_versions(version),pending_version TEXT REFERENCES tls_versions(version));
|
||||
CREATE TABLE IF NOT EXISTS tls_idle_admission(version TEXT PRIMARY KEY REFERENCES tls_versions(version),witness BLOB NOT NULL CHECK(length(witness) BETWEEN 1 AND 32768));
|
||||
CREATE TRIGGER IF NOT EXISTS tls_idle_no_update BEFORE UPDATE ON tls_idle_admission BEGIN SELECT RAISE(ABORT,'ROTATION_IDLE_IMMUTABLE');END;
|
||||
CREATE TRIGGER IF NOT EXISTS tls_idle_no_delete BEFORE DELETE ON tls_idle_admission BEGIN SELECT RAISE(ABORT,'ROTATION_IDLE_IMMUTABLE');END;")?;
|
||||
let present: bool = tx.query_row("SELECT EXISTS(SELECT 1 FROM tls_rotation)", [], |r| {
|
||||
r.get(0)
|
||||
})?;
|
||||
@@ -173,6 +176,20 @@ impl RotationStore {
|
||||
pub fn has_pending(&self, db: &Connection) -> Result<bool> {
|
||||
Ok(self.selector(db)?.1.is_some())
|
||||
}
|
||||
/// Retained local maintenance context, never readiness or job authority.
|
||||
/// Legacy passive requests deliberately have no active-idle admission.
|
||||
pub fn pending_idle_admission(&self, db: &Connection) -> Result<Option<Vec<u8>>> {
|
||||
let Some(version) = self.selector(db)?.1 else {
|
||||
return Ok(None);
|
||||
};
|
||||
Ok(db
|
||||
.query_row(
|
||||
"SELECT witness FROM tls_idle_admission WHERE version=?1",
|
||||
[version.to_string()],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.optional()?)
|
||||
}
|
||||
/// A crash before the transaction leaves only an unselected key file. A
|
||||
/// response loss reuses the committed CSR/key and still-valid old mTLS leaf.
|
||||
pub fn prepare(
|
||||
@@ -181,9 +198,37 @@ impl RotationStore {
|
||||
keys: &impl KeyFiles,
|
||||
now: i64,
|
||||
) -> Result<PendingRequest> {
|
||||
self.prepare_with_idle_admission(db, keys, now, None)
|
||||
}
|
||||
/// Commit a caller-verified idle witness in the SAME transaction as the
|
||||
/// pending CSR/selector. A resumed active request must retain exact bytes.
|
||||
pub fn prepare_with_idle_admission(
|
||||
&self,
|
||||
db: &mut Connection,
|
||||
keys: &impl KeyFiles,
|
||||
now: i64,
|
||||
admission: Option<&[u8]>,
|
||||
) -> Result<PendingRequest> {
|
||||
ensure!(
|
||||
admission.is_none_or(|bytes| !bytes.is_empty() && bytes.len() <= 32768),
|
||||
"ROTATION_IDLE_ADMISSION_LIMIT"
|
||||
);
|
||||
let identity = self.current(db, keys, now)?;
|
||||
let tx = db.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
|
||||
let (base, pending) = self.selector(&tx)?;
|
||||
if let (Some(version), Some(expected)) = (pending, admission) {
|
||||
let saved: Option<Vec<u8>> = tx
|
||||
.query_row(
|
||||
"SELECT witness FROM tls_idle_admission WHERE version=?1",
|
||||
[version.to_string()],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.optional()?;
|
||||
ensure!(
|
||||
saved.as_deref() == Some(expected),
|
||||
"ROTATION_IDLE_ADMISSION_CHANGED"
|
||||
);
|
||||
}
|
||||
let (version, csr_pem) = if let Some(version) = pending {
|
||||
ensure!(
|
||||
!version.is_nil() && version != base,
|
||||
@@ -231,6 +276,12 @@ impl RotationStore {
|
||||
"UPDATE tls_rotation SET pending_version=?1 WHERE id=1",
|
||||
[version.to_string()],
|
||||
)?;
|
||||
if let Some(witness) = admission {
|
||||
tx.execute(
|
||||
"INSERT INTO tls_idle_admission VALUES(?1,?2)",
|
||||
params![version.to_string(), witness],
|
||||
)?;
|
||||
}
|
||||
(version, csr)
|
||||
};
|
||||
tx.commit()?;
|
||||
@@ -299,6 +350,71 @@ impl RotationStore {
|
||||
}
|
||||
}
|
||||
|
||||
/// A local idle witness for certificate maintenance, not an execution grant.
|
||||
/// All accepted/unknown work and missing effect evidence block rotation. The
|
||||
/// caller retains both existing database guards while computing this digest.
|
||||
pub fn idle_history_sha256(journal: &Connection, ledger: &Connection) -> Result<String> {
|
||||
use rusqlite::types::ValueRef;
|
||||
use sha2::{Digest, Sha256};
|
||||
let safe: bool = journal.query_row("SELECT count(*)=1 AND min(last_decision IS NOT NULL AND pending_request IS NULL AND fault IS NULL AND generation>0) FROM reconcile_meta WHERE id=1", [], |r| r.get(0))?;
|
||||
let idle: bool = journal.query_row("SELECT NOT EXISTS(SELECT 1 FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired')) AND NOT EXISTS(SELECT 1 FROM job_events) AND NOT EXISTS(SELECT 1 FROM events)", [], |r| r.get(0))?;
|
||||
let resolved: bool = ledger.query_row("SELECT NOT EXISTS(SELECT 1 FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) WHERE c.result IS NULL OR e.start IS NULL OR e.outcome IS NULL) AND NOT EXISTS(SELECT 1 FROM execution_evidence e LEFT JOIN consumed c USING(grant_id) WHERE c.grant_id IS NULL)", [], |r| r.get(0))?;
|
||||
ensure!(
|
||||
safe && idle && resolved,
|
||||
"ROTATION_EFFECT_OR_CONTROL_UNRESOLVED"
|
||||
);
|
||||
let mut hash = Sha256::new();
|
||||
hash.update(b"rmm.windows.tls-idle.v1\0");
|
||||
for (db, sql) in [
|
||||
(journal, "SELECT seq FROM counter WHERE id=1"),
|
||||
(
|
||||
journal,
|
||||
"SELECT journal_id,checkpoint,high_sequence,generation,last_decision,pending_request,helper_id,helper_checkpoint,fault FROM reconcile_meta WHERE id=1",
|
||||
),
|
||||
(
|
||||
journal,
|
||||
"SELECT job,attempt,offer,state,revision,grant_id,cancel_requested FROM job_attempts ORDER BY job,attempt",
|
||||
),
|
||||
(
|
||||
ledger,
|
||||
"SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c JOIN execution_evidence e USING(grant_id) ORDER BY c.grant_id",
|
||||
),
|
||||
] {
|
||||
hash.update(sql.as_bytes());
|
||||
let mut query = db.prepare(sql)?;
|
||||
let columns = query.column_count();
|
||||
let mut rows = query.query([])?;
|
||||
let mut count = 0u64;
|
||||
while let Some(row) = rows.next()? {
|
||||
count += 1;
|
||||
ensure!(count <= 256, "ROTATION_HISTORY_LIMIT");
|
||||
hash.update([0xff]);
|
||||
for column in 0..columns {
|
||||
match row.get_ref(column)? {
|
||||
ValueRef::Null => hash.update([0]),
|
||||
ValueRef::Integer(value) => {
|
||||
hash.update([1]);
|
||||
hash.update(value.to_be_bytes());
|
||||
}
|
||||
ValueRef::Text(bytes) | ValueRef::Blob(bytes) => {
|
||||
ensure!(bytes.len() <= 1048576, "ROTATION_HISTORY_BLOB_LIMIT");
|
||||
hash.update([if matches!(row.get_ref(column)?, ValueRef::Text(_)) {
|
||||
2
|
||||
} else {
|
||||
3
|
||||
}]);
|
||||
hash.update((bytes.len() as u64).to_be_bytes());
|
||||
hash.update(bytes);
|
||||
}
|
||||
ValueRef::Real(_) => anyhow::bail!("ROTATION_HISTORY_TYPE"),
|
||||
}
|
||||
}
|
||||
}
|
||||
hash.update(count.to_be_bytes());
|
||||
}
|
||||
Ok(format!("{:x}", hash.finalize()))
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub struct ProtectedKeyFiles {
|
||||
root: std::path::PathBuf,
|
||||
@@ -474,6 +590,146 @@ mod tests {
|
||||
.unwrap()
|
||||
}
|
||||
#[test]
|
||||
fn active_idle_admission_is_atomic_retained_and_cannot_adopt_legacy_pending() {
|
||||
let (mut db, keys, store, _, _) = setup();
|
||||
let witness = b"exact independently checked idle context";
|
||||
db.execute_batch("CREATE TEMP TRIGGER deny_idle BEFORE INSERT ON tls_idle_admission BEGIN SELECT RAISE(ABORT,'TEST_IDLE_COMMIT_DENIED');END;").unwrap();
|
||||
assert!(
|
||||
store
|
||||
.prepare_with_idle_admission(&mut db, &keys, now(), Some(witness))
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(store.selector(&db).unwrap(), (Uuid::nil(), None));
|
||||
assert_eq!(
|
||||
db.query_row("SELECT count(*) FROM tls_versions", [], |r| r
|
||||
.get::<_, u64>(0))
|
||||
.unwrap(),
|
||||
1
|
||||
);
|
||||
db.execute_batch("DROP TRIGGER deny_idle").unwrap();
|
||||
let original = store
|
||||
.prepare_with_idle_admission(&mut db, &keys, now(), Some(witness))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
store.pending_idle_admission(&db).unwrap().as_deref(),
|
||||
Some(witness.as_slice())
|
||||
);
|
||||
assert!(
|
||||
store
|
||||
.prepare_with_idle_admission(&mut db, &keys, now(), Some(b"changed context"))
|
||||
.is_err()
|
||||
);
|
||||
let retained = store
|
||||
.prepare_with_idle_admission(&mut db, &keys, now(), Some(witness))
|
||||
.unwrap();
|
||||
assert_eq!(original.version, retained.version);
|
||||
assert_eq!(original.csr_pem, retained.csr_pem);
|
||||
assert!(
|
||||
db.execute(
|
||||
"UPDATE tls_idle_admission SET witness=?1",
|
||||
[b"replacement".as_slice()]
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert!(db.execute("DELETE FROM tls_idle_admission", []).is_err());
|
||||
let (mut legacy, legacy_keys, legacy_store, _, _) = setup();
|
||||
legacy_store
|
||||
.prepare(&mut legacy, &legacy_keys, now())
|
||||
.unwrap();
|
||||
assert!(
|
||||
legacy_store
|
||||
.pending_idle_admission(&legacy)
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
assert!(
|
||||
legacy_store
|
||||
.prepare_with_idle_admission(&mut legacy, &legacy_keys, now(), Some(witness))
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn idle_history_rejects_unfinished_jobs_missing_effect_evidence_and_changed_heads() {
|
||||
let root = std::env::temp_dir().join(format!("rmm-rotation-idle-{}", Uuid::new_v4()));
|
||||
std::fs::create_dir(&root).unwrap();
|
||||
let journal = crate::journal::Journal::open(&root).unwrap();
|
||||
let ledger = Connection::open_in_memory().unwrap();
|
||||
rmm_agent_core::ledger::initialize_ledger(&ledger).unwrap();
|
||||
assert!(idle_history_sha256(journal.connection(), &ledger).is_err());
|
||||
journal
|
||||
.connection()
|
||||
.execute(
|
||||
"UPDATE reconcile_meta SET generation=1,last_decision=?1 WHERE id=1",
|
||||
[Uuid::new_v4().to_string()],
|
||||
)
|
||||
.unwrap();
|
||||
let clean = idle_history_sha256(journal.connection(), &ledger).unwrap();
|
||||
journal.connection().execute("INSERT INTO job_attempts(job,attempt,offer,state) VALUES('fixture','attempt',x'01','accepted')",[]).unwrap();
|
||||
for state in [
|
||||
"queued",
|
||||
"accepted",
|
||||
"requesting",
|
||||
"running",
|
||||
"verifying",
|
||||
"unknown",
|
||||
"awaiting_reboot",
|
||||
] {
|
||||
journal
|
||||
.connection()
|
||||
.execute("UPDATE job_attempts SET state=?1", [state])
|
||||
.unwrap();
|
||||
assert!(
|
||||
idle_history_sha256(journal.connection(), &ledger).is_err(),
|
||||
"{state}"
|
||||
);
|
||||
}
|
||||
journal
|
||||
.connection()
|
||||
.execute("UPDATE job_attempts SET state='succeeded'", [])
|
||||
.unwrap();
|
||||
let finished = idle_history_sha256(journal.connection(), &ledger).unwrap();
|
||||
assert_ne!(clean, finished);
|
||||
ledger
|
||||
.execute(
|
||||
"INSERT INTO consumed VALUES('grant','agent',1,'fixture','attempt',NULL)",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
assert!(idle_history_sha256(journal.connection(), &ledger).is_err());
|
||||
ledger
|
||||
.execute("UPDATE consumed SET result=x'02'", [])
|
||||
.unwrap();
|
||||
assert!(idle_history_sha256(journal.connection(), &ledger).is_err());
|
||||
ledger
|
||||
.execute(
|
||||
"INSERT INTO execution_evidence VALUES('grant',x'01',x'02')",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
let resolved = idle_history_sha256(journal.connection(), &ledger).unwrap();
|
||||
journal
|
||||
.connection()
|
||||
.execute(
|
||||
"UPDATE reconcile_meta SET checkpoint=checkpoint+1 WHERE id=1",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
assert_ne!(
|
||||
resolved,
|
||||
idle_history_sha256(journal.connection(), &ledger).unwrap()
|
||||
);
|
||||
journal
|
||||
.connection()
|
||||
.execute(
|
||||
"UPDATE reconcile_meta SET pending_request='pending' WHERE id=1",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
assert!(idle_history_sha256(journal.connection(), &ledger).is_err());
|
||||
drop(journal);
|
||||
std::fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
#[test]
|
||||
fn response_loss_reuses_exact_pending_csr_and_private_key_stays_outside_sqlite() {
|
||||
let (mut db, keys, store, issuer, old) = setup();
|
||||
assert!(store.due(&db, &keys, now()).unwrap());
|
||||
|
||||
Reference in New Issue
Block a user