Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/id/benches/world_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,7 @@ impl loadgen::Target<ReplaySession> for ReplayTarget {
uses: None,
used: 0,
subject: None,
issued_at: None,
});
for seq in 1..=self.events {
journal.push(JournalEntry::Committed {
Expand Down
103 changes: 101 additions & 2 deletions pkgs/id/crates/id-cli/src/commands/world/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//! the live-only sections (connected members, program runs) are always
//! unavailable here, and it writes nothing.

use std::fmt::Write as _;
use std::fs::File;
use std::io::{BufRead, BufReader, Read};
use std::path::Path;
Expand Down Expand Up @@ -133,7 +134,33 @@ pub(super) fn render_text(report: &OwnerReport, since_secs: u64) -> String {
.map_or_else(|| "unknown".to_owned(), |n| n.to_string());
out.push(format!("forked from: {from} ({inherited} joins inherited)"));
}
out.push(format!("joins in window: {}", measured(&joins.in_window)));
out.push(format!(
"joins in window: {}{}",
measured(&joins.in_window),
if joins.in_window.value().is_some() && !joins.in_window_complete {
", incomplete: the window starts before join times were recorded"
} else {
""
}
));
out.push(format!(
"join times recorded since: {}{}",
measured(&joins.available_since),
if joins.untimed > 0 {
format!(" ({} joins have no time)", joins.untimed)
} else {
String::new()
}
));
out.push(format!(
"revokes in window: {}{}",
measured(&joins.revoked_in_window),
if joins.revoked_untimed > 0 && joins.revoked_in_window.value().is_some() {
format!(" ({} revokes have no time)", joins.revoked_untimed)
} else {
String::new()
}
));
out.push(format!(
"active members: {}",
measured(&report.members.active_count)
Expand Down Expand Up @@ -171,11 +198,18 @@ pub(super) fn render_text(report: &OwnerReport, since_secs: u64) -> String {
if join.revoked {
tags.push("revoked");
}
if tags.is_empty() {
let mut label = if tags.is_empty() {
join.participant_id.to_string()
} else {
format!("{} ({})", join.participant_id, tags.join(", "))
};
if let Some(at) = join.issued_at_unix_ms {
let _ = write!(label, " joined {at}");
}
if let Some(at) = join.revoked_at_unix_ms {
let _ = write!(label, " revoked {at}");
}
label
})
.collect();
out.push(format!("recent joins, newest first: {}", recent.join("; ")));
Expand Down Expand Up @@ -250,9 +284,74 @@ mod tests {
uses: None,
used: 0,
subject: None,
issued_at: None,
}
}

fn issued_at(id: u64, at: u64) -> JournalEntry {
let JournalEntry::Issued {
participant,
digest,
scopes,
parent,
expires_at,
uses,
used,
subject,
..
} = issued(id, None)
else {
unreachable!()
};
JournalEntry::Issued {
participant,
digest,
scopes,
parent,
expires_at,
uses,
used,
subject,
issued_at: Some(at),
}
}

#[test]
fn text_form_shows_join_and_revoke_times() {
let v = id_worlds::world::JOURNAL_VERSION_JOIN_TIMES;
let dir = world_dir(&[
JournalEntry::Created {
world_id: "w-cli".to_owned(),
version: v,
},
issued(1, None),
issued_at(2, 4_000),
JournalEntry::Revoked {
participant_id: 2,
by: None,
revoked_at: Some(4_500),
},
]);
let report = owner_report(dir.path(), 3, 5_000).unwrap();
let text = render_text(&report, 3);
assert!(text.contains("joins in window: 1, incomplete"), "{text}");
assert!(
text.contains("join times recorded since: 4000 (1 joins have no time)"),
"{text}"
);
assert!(text.contains("revokes in window: 1"), "{text}");
assert!(
text.contains("recent joins, newest first: 2 (revoked) joined 4000 revoked 4500; 1"),
"{text}"
);
let json = serde_json::to_value(&report).unwrap();
assert_eq!(json["joins"]["available_since"]["value"], 4_000);
assert_eq!(
json["joins"]["recent"][1]["issued_at_unix_ms"],
serde_json::Value::Null
);
}

#[test]
fn reports_a_local_world_offline() {
let dir = world_dir(&[created(), issued(1, None), issued(2, Some(1))]);
Expand Down
17 changes: 14 additions & 3 deletions pkgs/id/crates/id-worlds/src/world/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,12 +1168,17 @@ impl WorldActor {
let _ = reply.send(false);
return;
}
let revoked = self.core.revoke_tree_by(participant_id, by);
let revoked_at = Some(self.core.clock().now_ms());
let revoked = self.core.revoke_tree_at(participant_id, by, revoked_at);
let was_active = !revoked.is_empty();
if was_active {
// Fail-closed: surface a persist failure rather than
// reporting an audited success (design #835 phase 2).
let persisted = self.persist(JournalEntry::Revoked { participant_id, by });
let persisted = self.persist(JournalEntry::Revoked {
participant_id,
by,
revoked_at,
});
for id in revoked {
let _ = self.revocation_sender.send(id);
}
Expand Down Expand Up @@ -1211,7 +1216,10 @@ impl WorldActor {
);
Ok(false)
} else {
let revoked = self.core.revoke_tree_by(participant_id, Some(by));
let revoked_at = Some(self.core.clock().now_ms());
let revoked =
self.core
.revoke_tree_at(participant_id, Some(by), revoked_at);
let was_active = !revoked.is_empty();
if was_active {
// Fail-closed: the in-memory revocation and
Expand All @@ -1222,6 +1230,7 @@ impl WorldActor {
let persisted = self.persist(JournalEntry::Revoked {
participant_id,
by: Some(by),
revoked_at,
});
for id in revoked {
let _ = self.revocation_sender.send(id);
Expand Down Expand Up @@ -2600,6 +2609,7 @@ mod replication_feed_tests {
use proptest::prelude::*;
prop_oneof![
any::<u64>().prop_map(|participant_id| JournalEntry::Revoked {
revoked_at: None,
participant_id,
by: None,
}),
Expand Down Expand Up @@ -2635,6 +2645,7 @@ mod replication_feed_tests {
// the journal TOTAL still ranges past it via the entry count.
prop_oneof![
any::<u64>().prop_map(|participant_id| JournalEntry::Revoked {
revoked_at: None,
participant_id,
by: None,
}),
Expand Down
27 changes: 24 additions & 3 deletions pkgs/id/crates/id-worlds/src/world/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ impl WorldCore {
uses,
used,
subject,
issued_at,
} => {
let digest = hex_decode_32(&digest).context("malformed capability digest")?;
// #2972: participant 0 is the reserved host/system
Expand Down Expand Up @@ -699,13 +700,15 @@ impl WorldCore {
}
let mut ancestor = parent;
let mut revoked_by_ancestor = false;
let mut ancestor_revoked_at = None;
while let Some(ancestor_id) = ancestor {
let ancestor_record = core
.capabilities
.get(&ancestor_id)
.context("delegated capability ancestor disappeared during restore")?;
if ancestor_record.revoked {
revoked_by_ancestor = true;
ancestor_revoked_at = ancestor_record.revoked_at;
break;
}
ancestor = ancestor_record.parent;
Expand Down Expand Up @@ -744,16 +747,30 @@ impl WorldCore {
bounds: CapabilityBounds { expires_at, uses },
used,
subject,
issued_at,
revoked_at: ancestor_revoked_at,
},
);
if revoked_by_ancestor {
inherited_revocations.insert(issued_participant_id);
}
}
JournalEntry::Revoked { participant_id, by } => {
if !inherited_revocations.remove(&participant_id) {
JournalEntry::Revoked {
participant_id,
by,
revoked_at,
} => {
if inherited_revocations.remove(&participant_id) {
// Revoked with an ancestor before this checkpointed
// entry was read: keep the time the entry carries.
if let Some(record) = core.capabilities.get_mut(&participant_id) {
record.revoked_at = revoked_at;
}
} else {
ensure!(
!core.revoke_tree_by(participant_id, by).is_empty(),
!core
.revoke_tree_at(participant_id, by, revoked_at)
.is_empty(),
"journal revokes unknown or revoked participant {participant_id}"
);
}
Expand Down Expand Up @@ -1644,6 +1661,10 @@ impl WorldCore {
.capabilities
.get(&id)
.and_then(|record| record.revoked_by),
revoked_at: self
.capabilities
.get(&id)
.and_then(|record| record.revoked_at),
});
}
}
Expand Down
23 changes: 23 additions & 0 deletions pkgs/id/crates/id-worlds/src/world/core/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ impl WorldCore {
uses: record.bounds.uses,
used: record.used,
subject: record.subject.clone(),
issued_at: record.issued_at,
})
}

Expand Down Expand Up @@ -487,6 +488,7 @@ impl WorldCore {
let token = format!("{id}.{}", hex_encode(&secret));
let digest = token_digest(&token);
let participant = Participant { id, display_name };
let issued_at = Some(self.clock().now_ms());
self.capabilities.insert(
id,
CapabilityRecord {
Expand All @@ -499,6 +501,8 @@ impl WorldCore {
bounds,
used: 0,
subject,
issued_at,
revoked_at: None,
},
);
Ok((participant, JoinCapability(token)))
Expand Down Expand Up @@ -532,12 +536,31 @@ impl WorldCore {
/// who ordered it; `None` for the admin) on the revoked root so a
/// checkpoint keeps the audit actor (#11778).
pub fn revoke_tree_by(&mut self, participant_id: u64, by: Option<u64>) -> Vec<u64> {
self.revoke_tree_at(participant_id, by, None)
}

/// [`Self::revoke_tree_by`], also recording `at` (Unix ms) as the
/// revoke time of every capability it newly revokes (#11960), so the
/// owner report and a checkpoint keep when each one stopped working.
pub(crate) fn revoke_tree_at(
&mut self,
participant_id: u64,
by: Option<u64>,
at: Option<u64>,
) -> Vec<u64> {
let revoked = self.revoke_tree(participant_id);
if !revoked.is_empty()
&& let Some(record) = self.capabilities.get_mut(&participant_id)
{
record.revoked_by = by;
}
if at.is_some() {
for id in &revoked {
if let Some(record) = self.capabilities.get_mut(id) {
record.revoked_at = at;
}
}
}
revoked
}

Expand Down
28 changes: 27 additions & 1 deletion pkgs/id/crates/id-worlds/src/world/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ pub enum JournalEntry {
/// Account the capability acts for; its ceiling and friendships bound it.
#[serde(default, skip_serializing_if = "Option::is_none")]
subject: Option<String>,
/// Unix milliseconds at which the world issued the capability, from
/// the world clock (#11960). `None` for capabilities issued before
/// times were recorded; never inferred. Requires
/// [`JOURNAL_VERSION_JOIN_TIMES`].
#[serde(default, skip_serializing_if = "Option::is_none")]
issued_at: Option<u64>,
},
/// A capability was revoked, with every capability delegated from it.
/// Replay revokes the same tree.
Expand All @@ -239,6 +245,11 @@ pub enum JournalEntry {
/// from a moderator scope rather than the admin. `None` means admin.
#[serde(default, skip_serializing_if = "Option::is_none")]
by: Option<u64>,
/// Unix milliseconds at which the revoke took effect, from the world
/// clock (#11960). `None` for revokes recorded before times were.
/// Requires [`JOURNAL_VERSION_JOIN_TIMES`].
#[serde(default, skip_serializing_if = "Option::is_none")]
revoked_at: Option<u64>,
},
/// A builder-scoped participant installed a program. Journal-only: it is
/// the audit record for the builder authority, written alongside the
Expand Down Expand Up @@ -735,10 +746,18 @@ pub const JOURNAL_VERSION_PORTALS: u32 = 10;
/// journal at the header check.
pub const JOURNAL_VERSION_PROGRAM_BUNDLE: u32 = 11;

/// Journal version 12 (#11960): `Issued` entries carry `issued_at` and
/// `Revoked` entries carry `revoked_at`. The fields are optional, so a
/// version-11 reader would parse such an entry, drop the time and lose it
/// at its next checkpoint; an entry that carries a time therefore requires
/// this version, and an older binary refuses the journal at the header
/// check instead.
pub const JOURNAL_VERSION_JOIN_TIMES: u32 = 12;

/// Newest journal version this binary can read. Journals with a higher
/// header version are refused at the header check, before any entry is
/// applied.
pub const MAX_JOURNAL_VERSION: u32 = JOURNAL_VERSION_PROGRAM_BUNDLE;
pub const MAX_JOURNAL_VERSION: u32 = JOURNAL_VERSION_JOIN_TIMES;

/// The header version a journal must carry once it contains `entry`: version 2
/// for execution-profile entries, version 3 for region-handoff entries,
Expand Down Expand Up @@ -879,6 +898,13 @@ fn entry_kind_version(entry: &JournalEntry) -> u32 {
JOURNAL_VERSION_EXECUTION_PROFILE.max(budget_version(Some(profile)))
}
JournalEntry::BuilderInstalled { .. } => JOURNAL_VERSION_EXECUTION_PROFILE,
JournalEntry::Issued {
issued_at: Some(_), ..
}
| JournalEntry::Revoked {
revoked_at: Some(_),
..
} => JOURNAL_VERSION_JOIN_TIMES,
_ => JOURNAL_VERSION,
}
}
Expand Down
Loading