chore: rustfmt the workspace and make clippy -D warnings pass

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 19:19:21 +07:00
parent d7fe99edd7
commit f13f0e6953
46 changed files with 6606 additions and 1496 deletions

View File

@@ -90,7 +90,16 @@ fn migrate_legacy(conn: &Connection) -> Result<()> {
let mut codes = 0;
let rows: Vec<(String, String, String, String, Option<String>, String)> = {
let mut stmt = conn.prepare("SELECT token, outpost_id, code, state, outpost_name, expires_at FROM wormhole_tokens")?;
let mapped = stmt.query_map([], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?, r.get(5)?)))?;
let mapped = stmt.query_map([], |r| {
Ok((
r.get(0)?,
r.get(1)?,
r.get(2)?,
r.get(3)?,
r.get(4)?,
r.get(5)?,
))
})?;
mapped.collect::<rusqlite::Result<_>>()?
};
for (token, outpost, code, state, name, expires) in rows {
@@ -106,7 +115,9 @@ fn migrate_legacy(conn: &Connection) -> Result<()> {
)?;
active += 1;
} else if state == "code_shown" {
let expires_at = chrono::DateTime::parse_from_rfc3339(&expires).map(|t| t.timestamp()).unwrap_or(0);
let expires_at = chrono::DateTime::parse_from_rfc3339(&expires)
.map(|t| t.timestamp())
.unwrap_or(0);
conn.execute(
"INSERT OR IGNORE INTO pairing_codes(code_digest,outpost_id,expires_at) VALUES (?1,?2,?3)",
params![digest("pairing-code", &code), outpost, expires_at],
@@ -139,7 +150,10 @@ fn migrate_legacy(conn: &Connection) -> Result<()> {
impl Db {
pub fn insert_pairing(&self, outpost_id: &str, code: &str, expires_at: i64) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute("DELETE FROM pairing_codes WHERE expires_at <= ?1 OR consumed_at IS NOT NULL", params![expires_at - 86_400])?;
conn.execute(
"DELETE FROM pairing_codes WHERE expires_at <= ?1 OR consumed_at IS NOT NULL",
params![expires_at - 86_400],
)?;
conn.execute(
"INSERT INTO pairing_codes(code_digest, outpost_id, expires_at) VALUES (?1, ?2, ?3)",
params![digest("pairing-code", code), outpost_id, expires_at],
@@ -150,7 +164,12 @@ impl Db {
/// Consume a pairing code and mint the upload token. The single UPDATE …
/// RETURNING is what makes a code usable once; the `Db` mutex additionally
/// serialises callers. The token exists in clear only in the return value.
pub fn claim_pairing(&self, code: &str, outpost_name: &str, now: i64) -> Result<Option<Issued>> {
pub fn claim_pairing(
&self,
code: &str,
outpost_name: &str,
now: i64,
) -> Result<Option<Issued>> {
let mut conn = self.conn.lock().unwrap();
let tx = conn.transaction()?;
let outpost: Option<String> = tx
@@ -162,7 +181,9 @@ impl Db {
|row| row.get(0),
)
.optional()?;
let Some(outpost) = outpost else { return Ok(None) };
let Some(outpost) = outpost else {
return Ok(None);
};
let token = new_token();
let expires_at = now + lifetime_secs();
tx.execute(
@@ -176,7 +197,12 @@ impl Db {
params![Uuid::new_v4().to_string(), outpost, digest("outpost-token", &token), now, expires_at],
)?;
tx.commit()?;
Ok(Some(Issued { token, outpost_id: outpost, expires_at, previous_valid_until: None }))
Ok(Some(Issued {
token,
outpost_id: outpost,
expires_at,
previous_valid_until: None,
}))
}
/// The Outpost a presented token belongs to, if it is active and unexpired.
@@ -196,7 +222,12 @@ impl Db {
/// working for `grace_secs` (0 = they stop immediately), so an operator has a
/// bounded window to put the new token on the agent. Returns `None` for an
/// unknown or revoked Outpost: it must be paired again.
pub fn rotate_credential(&self, outpost: &str, grace_secs: i64, now: i64) -> Result<Option<Issued>> {
pub fn rotate_credential(
&self,
outpost: &str,
grace_secs: i64,
now: i64,
) -> Result<Option<Issued>> {
let mut conn = self.conn.lock().unwrap();
let tx = conn.transaction()?;
let paired: bool = tx.query_row(
@@ -238,7 +269,10 @@ impl Db {
"UPDATE outpost_credentials SET state='revoked', revoked_at=?2 WHERE outpost_id=?1 AND state='active'",
params![outpost, now],
)?;
let known = tx.execute("UPDATE outposts SET pairing_state='revoked' WHERE id=?1", params![outpost])? == 1;
let known = tx.execute(
"UPDATE outposts SET pairing_state='revoked' WHERE id=?1",
params![outpost],
)? == 1;
// Anything that overwrites through, or reads from, this Outpost must stop working with it.
tx.execute(
"UPDATE orbit_relations SET active=0, deactivated_at=?2, deactivated_reason='outpost_revoked'
@@ -296,7 +330,9 @@ mod tests {
}
fn contains(haystack: &[u8], needle: &str) -> bool {
haystack.windows(needle.len()).any(|w| w == needle.as_bytes())
haystack
.windows(needle.len())
.any(|w| w == needle.as_bytes())
}
#[tokio::test]
@@ -304,14 +340,31 @@ mod tests {
let (_dir, db, path) = db().await;
let outpost = Uuid::new_v4().to_string();
db.insert_pairing(&outpost, "ABCDEFGH2345", 10_000).unwrap();
let issued = db.claim_pairing("ABCDEFGH2345", "agent", 100).unwrap().unwrap();
let issued = db
.claim_pairing("ABCDEFGH2345", "agent", 100)
.unwrap()
.unwrap();
assert_eq!(issued.token.len(), 64);
assert_eq!(db.upload_token_outpost(&issued.token, 101).unwrap().as_deref(), Some(outpost.as_str()));
assert_eq!(
db.upload_token_outpost(&issued.token, 101)
.unwrap()
.as_deref(),
Some(outpost.as_str())
);
drop(db);
let bytes = disk_bytes(&path);
assert!(contains(&bytes, "agent"), "sanity: the scan really reads the database file");
assert!(!contains(&bytes, &issued.token), "token in the database file");
assert!(!contains(&bytes, "ABCDEFGH2345"), "pairing code in the database file");
assert!(
contains(&bytes, "agent"),
"sanity: the scan really reads the database file"
);
assert!(
!contains(&bytes, &issued.token),
"token in the database file"
);
assert!(
!contains(&bytes, "ABCDEFGH2345"),
"pairing code in the database file"
);
}
#[tokio::test]
@@ -321,9 +374,15 @@ mod tests {
db.insert_pairing(&outpost, "CODE-ONE", 1_000).unwrap();
assert!(db.claim_pairing("WRONG", "a", 10).unwrap().is_none());
assert!(db.claim_pairing("CODE-ONE", "a", 10).unwrap().is_some());
assert!(db.claim_pairing("CODE-ONE", "a", 11).unwrap().is_none(), "replay");
assert!(
db.claim_pairing("CODE-ONE", "a", 11).unwrap().is_none(),
"replay"
);
db.insert_pairing(&outpost, "CODE-TWO", 1_000).unwrap();
assert!(db.claim_pairing("CODE-TWO", "a", 1_000).unwrap().is_none(), "expired at its deadline");
assert!(
db.claim_pairing("CODE-TWO", "a", 1_000).unwrap().is_none(),
"expired at its deadline"
);
}
#[tokio::test]
@@ -331,14 +390,19 @@ mod tests {
// The Db mutex serialises these, so this shows the statement is atomic
// (one consumer per code), not that SQLite arbitrates a race.
let (_dir, db, _) = db().await;
db.insert_pairing(&Uuid::new_v4().to_string(), "RACE-CODE", 1_000_000).unwrap();
db.insert_pairing(&Uuid::new_v4().to_string(), "RACE-CODE", 1_000_000)
.unwrap();
let handles: Vec<_> = (0..8)
.map(|_| {
let db = db.clone();
std::thread::spawn(move || db.claim_pairing("RACE-CODE", "a", 5).unwrap().is_some())
})
.collect();
let winners = handles.into_iter().map(|h| h.join().unwrap()).filter(|won| *won).count();
let winners = handles
.into_iter()
.map(|h| h.join().unwrap())
.filter(|won| *won)
.count();
assert_eq!(winners, 1);
}
@@ -348,12 +412,31 @@ mod tests {
let outpost = Uuid::new_v4().to_string();
db.insert_pairing(&outpost, "C", 1_000).unwrap();
let issued = db.claim_pairing("C", "agent", 10).unwrap().unwrap();
assert!(db.upload_token_outpost(&issued.token, issued.expires_at - 1).unwrap().is_some());
assert!(db.upload_token_outpost(&issued.token, issued.expires_at).unwrap().is_none(), "expiry");
assert!(db.upload_token_outpost(&issued.token, 20).unwrap().is_some());
assert!(db
.upload_token_outpost(&issued.token, issued.expires_at - 1)
.unwrap()
.is_some());
assert!(
db.upload_token_outpost(&issued.token, issued.expires_at)
.unwrap()
.is_none(),
"expiry"
);
assert!(db
.upload_token_outpost(&issued.token, 20)
.unwrap()
.is_some());
assert!(db.revoke_outpost(&outpost, 21).unwrap());
assert!(db.upload_token_outpost(&issued.token, 22).unwrap().is_none(), "revoked");
assert!(db.rotate_credential(&outpost, 60, 23).unwrap().is_none(), "a revoked Outpost must be paired again");
assert!(
db.upload_token_outpost(&issued.token, 22)
.unwrap()
.is_none(),
"revoked"
);
assert!(
db.rotate_credential(&outpost, 60, 23).unwrap().is_none(),
"a revoked Outpost must be paired again"
);
assert!(!db.revoke_outpost(&Uuid::new_v4().to_string(), 1).unwrap());
}
@@ -366,16 +449,38 @@ mod tests {
let second = db.rotate_credential(&outpost, 300, 100).unwrap().unwrap();
assert_eq!(second.previous_valid_until, Some(400));
assert_ne!(first.token, second.token);
assert!(db.upload_token_outpost(&first.token, 399).unwrap().is_some(), "old token inside the grace window");
assert!(db.upload_token_outpost(&first.token, 400).unwrap().is_none(), "and not after it");
assert!(db.upload_token_outpost(&second.token, 400).unwrap().is_some());
assert!(
db.upload_token_outpost(&first.token, 399)
.unwrap()
.is_some(),
"old token inside the grace window"
);
assert!(
db.upload_token_outpost(&first.token, 400)
.unwrap()
.is_none(),
"and not after it"
);
assert!(db
.upload_token_outpost(&second.token, 400)
.unwrap()
.is_some());
// grace 0 cuts the old token at once
let third = db.rotate_credential(&outpost, 0, 500).unwrap().unwrap();
assert_eq!(third.previous_valid_until, None);
assert!(db.upload_token_outpost(&second.token, 500).unwrap().is_none());
assert!(db.upload_token_outpost(&third.token, 500).unwrap().is_some());
assert!(db
.upload_token_outpost(&second.token, 500)
.unwrap()
.is_none());
assert!(db
.upload_token_outpost(&third.token, 500)
.unwrap()
.is_some());
// an unknown outpost cannot be rotated into existence
assert!(db.rotate_credential(&Uuid::new_v4().to_string(), 0, 1).unwrap().is_none());
assert!(db
.rotate_credential(&Uuid::new_v4().to_string(), 0, 1)
.unwrap()
.is_none());
}
/// A pre-M4 database: clear-text tokens and codes in `wormhole_tokens`.
@@ -403,31 +508,68 @@ mod tests {
let now = chrono::Utc::now().timestamp();
// the paired agent keeps working with its existing token
assert_eq!(
db.upload_token_outpost("LEGACY-ACTIVE-TOKEN-0001", now).unwrap().as_deref(),
db.upload_token_outpost("LEGACY-ACTIVE-TOKEN-0001", now)
.unwrap()
.as_deref(),
Some("11111111-1111-1111-1111-111111111111")
);
assert!(db.upload_token_outpost("LEGACY-ACTIVE-TOKEN-0001", now + lifetime_secs() + 10).unwrap().is_none());
assert!(db
.upload_token_outpost("LEGACY-ACTIVE-TOKEN-0001", now + lifetime_secs() + 10)
.unwrap()
.is_none());
// an unclaimed code can still be claimed once, but its pre-minted token is gone
let claimed = db.claim_pairing("LEGACYCODE2", "new-agent", now).unwrap().unwrap();
let claimed = db
.claim_pairing("LEGACYCODE2", "new-agent", now)
.unwrap()
.unwrap();
assert_ne!(claimed.token, "PREMINTED-TOKEN-0002");
assert!(db.upload_token_outpost("PREMINTED-TOKEN-0002", now).unwrap().is_none());
assert!(db.claim_pairing("LEGACYCODE3", "late-agent", now).unwrap().is_none(), "expired code stays expired");
assert!(db
.upload_token_outpost("PREMINTED-TOKEN-0002", now)
.unwrap()
.is_none());
assert!(
db.claim_pairing("LEGACYCODE3", "late-agent", now)
.unwrap()
.is_none(),
"expired code stays expired"
);
// the old table and every clear-text copy are gone, including freed pages
let exists: bool = db.conn.lock().unwrap()
.query_row("SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE name='wormhole_tokens')", [], |r| r.get(0)).unwrap();
let exists: bool = db
.conn
.lock()
.unwrap()
.query_row(
"SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE name='wormhole_tokens')",
[],
|r| r.get(0),
)
.unwrap();
assert!(!exists);
let state: String = db.conn.lock().unwrap()
.query_row("SELECT pairing_state FROM outposts WHERE id='11111111-1111-1111-1111-111111111111'", [], |r| r.get(0)).unwrap();
assert_eq!(state, "active");
drop(db);
let bytes = disk_bytes(&path);
assert!(contains(&bytes, "old-agent") || contains(&bytes, "new-agent"), "sanity: the scan really reads the database file");
for secret in ["LEGACY-ACTIVE-TOKEN-0001", "PREMINTED-TOKEN-0002", "PREMINTED-TOKEN-0003", "LEGACYCODE1", "LEGACYCODE2", "LEGACYCODE3"] {
assert!(
contains(&bytes, "old-agent") || contains(&bytes, "new-agent"),
"sanity: the scan really reads the database file"
);
for secret in [
"LEGACY-ACTIVE-TOKEN-0001",
"PREMINTED-TOKEN-0002",
"PREMINTED-TOKEN-0003",
"LEGACYCODE1",
"LEGACYCODE2",
"LEGACYCODE3",
] {
assert!(!contains(&bytes, secret), "{secret} still on disk");
}
// migrating again changes nothing
let again = Db::connect(&path).await.unwrap();
again.migrate().await.unwrap();
assert!(again.upload_token_outpost("LEGACY-ACTIVE-TOKEN-0001", now).unwrap().is_some());
assert!(again
.upload_token_outpost("LEGACY-ACTIVE-TOKEN-0001", now)
.unwrap()
.is_some());
}
}