mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
Added the list_topics and list_nodes role flags, Filtered the nodes listed by the roles
- `list_topics` / `list_nodes`: listing answers every topic, every node with messages, without reading any (`query` gives both). - Without them, listing nodes only answers those with messages in the topics the roles may read (every node until now). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -669,14 +669,15 @@ fn list(kind: NameKind) -> i32 {
|
||||
}
|
||||
|
||||
/// Lists the topics with messages that this client may push to or read
|
||||
/// (every one if it may query): how many, -1 on failure. Then
|
||||
/// `BakaShrot_GetNamesList()`.
|
||||
/// (every one if its roles have `list_topics` or `query`): how many, -1 on
|
||||
/// failure. Then `BakaShrot_GetNamesList()`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn BakaShrot_ListTopics() -> i32 {
|
||||
list(NameKind::Topic)
|
||||
}
|
||||
|
||||
/// Lists every node with messages: how many, -1 on failure. Then
|
||||
/// Lists the nodes with messages in the topics this client may read (every
|
||||
/// one if its roles have `list_nodes` or `query`): how many, -1 on failure. Then
|
||||
/// `BakaShrot_GetNamesList()`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn BakaShrot_ListNodes() -> i32 {
|
||||
|
||||
@@ -413,9 +413,10 @@ impl Client {
|
||||
}
|
||||
}
|
||||
|
||||
/// The topics with messages that this client may push to or read
|
||||
/// (every one if it may query), or every node with messages.
|
||||
/// `truncated` if some didn't fit.
|
||||
/// The topics with messages that this client may push to or read, or
|
||||
/// the nodes with messages in the topics it may read; every one if its
|
||||
/// roles have `list_topics` / `list_nodes`, or `query`. `truncated` if
|
||||
/// some didn't fit.
|
||||
pub fn list_names(&mut self, kind: NameKind) -> Result<NamesList> {
|
||||
match self.request(ListNames { kind }.into())? {
|
||||
Packet::NamesList(names) => Ok(names),
|
||||
|
||||
@@ -56,6 +56,10 @@ push_topics = ["sensors.*"] # always as its own node (its name)
|
||||
[roles.dashboard]
|
||||
read_topics = ["*"]
|
||||
query = true # read-only SQL, reads every topic
|
||||
# Listing answers the topics it may push to or read, and the nodes of the
|
||||
# topics it may read. These answer every one (query does too):
|
||||
# list_topics = true
|
||||
# list_nodes = true
|
||||
|
||||
# Peers are roles too: what's replicated with a server whose certificate
|
||||
# has this role, whichever end dials.
|
||||
|
||||
@@ -61,6 +61,14 @@ pub struct Role {
|
||||
/// Whether it may run read-only SQL, which reads every topic.
|
||||
#[serde(default)]
|
||||
pub query: bool,
|
||||
/// Whether listing topics answers every topic with messages, not only
|
||||
/// those it may push to or read.
|
||||
#[serde(default)]
|
||||
pub list_topics: bool,
|
||||
/// Whether listing nodes answers every node with messages, not only
|
||||
/// those of the topics it may read.
|
||||
#[serde(default)]
|
||||
pub list_nodes: bool,
|
||||
/// Replication: the topics sent to it.
|
||||
#[serde(default)]
|
||||
pub send_topics: NameList,
|
||||
@@ -109,6 +117,17 @@ impl Permissions {
|
||||
self.roles.iter().any(|role| role.query)
|
||||
}
|
||||
|
||||
/// Whether listing topics answers every one: `list_topics`, or
|
||||
/// `query` (which reads them all anyway).
|
||||
pub fn lists_every_topic(&self) -> bool {
|
||||
self.roles.iter().any(|role| role.list_topics || role.query)
|
||||
}
|
||||
|
||||
/// Whether listing nodes answers every one: `list_nodes`, or `query`.
|
||||
pub fn lists_every_node(&self) -> bool {
|
||||
self.roles.iter().any(|role| role.list_nodes || role.query)
|
||||
}
|
||||
|
||||
/// Whether `node`'s messages in `topic` are sent to it.
|
||||
pub fn sends(&self, topic: &str, node: &str) -> bool {
|
||||
self.roles
|
||||
|
||||
@@ -164,7 +164,19 @@ accept_nodes = ["garage"]
|
||||
assert!(both.can_read("Logs"));
|
||||
assert!(!both.can_read("sensors.garage"));
|
||||
assert!(!both.can_query());
|
||||
assert!(config.permissions(&["admin"]).unwrap().can_query());
|
||||
assert!(!both.lists_every_topic() && !both.lists_every_node());
|
||||
let admin = config.permissions(&["admin"]).unwrap();
|
||||
assert!(admin.can_query());
|
||||
// A query reads them all anyway.
|
||||
assert!(admin.lists_every_topic() && admin.lists_every_node());
|
||||
let listing = Config::parse(&with(
|
||||
"[roles.a]\nlist_topics = true\n[roles.b]\nlist_nodes = true\n",
|
||||
))
|
||||
.unwrap();
|
||||
let a = listing.permissions(&["a"]).unwrap();
|
||||
assert!(a.lists_every_topic() && !a.lists_every_node() && !a.can_query());
|
||||
let b = listing.permissions(&["b", "a"]).unwrap();
|
||||
assert!(b.lists_every_topic() && b.lists_every_node());
|
||||
|
||||
// Replication: each role's topics with that role's nodes, not mixed.
|
||||
let peer = config.permissions(&["peer", "backup"]).unwrap();
|
||||
|
||||
@@ -473,20 +473,41 @@ async fn get_messages(shared: &Arc<Shared>, peer: &Peer, get: GetMessages) -> Pa
|
||||
}
|
||||
|
||||
/// Topics: those with messages (pushed here or replicated) that it may
|
||||
/// push to or read, every one if it may query (queries read everything).
|
||||
/// Nodes: every one with messages.
|
||||
/// push to or read; every one with `list_topics` or `query`. Nodes: those
|
||||
/// with messages in the topics it may read; every one with `list_nodes`
|
||||
/// or `query`.
|
||||
async fn list_names(shared: &Arc<Shared>, peer: &Peer, list: ListNames) -> Packet {
|
||||
let permissions = &peer.permissions;
|
||||
let found = match list.kind {
|
||||
NameKind::Topic => shared.read(|reader| reader.topics()).await,
|
||||
NameKind::Node => shared.read(|reader| reader.nodes()).await,
|
||||
NameKind::Topic => shared
|
||||
.read(|reader| reader.topics())
|
||||
.await
|
||||
.map(|mut topics| {
|
||||
if !permissions.lists_every_topic() {
|
||||
topics
|
||||
.retain(|topic| permissions.can_push(topic) || permissions.can_read(topic));
|
||||
}
|
||||
topics
|
||||
}),
|
||||
NameKind::Node if permissions.lists_every_node() => {
|
||||
shared.read(|reader| reader.nodes()).await
|
||||
}
|
||||
NameKind::Node => shared.read(|reader| reader.heads()).await.map(|heads| {
|
||||
let mut nodes: Vec<String> = heads
|
||||
.into_iter()
|
||||
.filter(|head| permissions.can_read(&head.topic))
|
||||
.map(|head| head.node)
|
||||
.collect();
|
||||
// As `reader.nodes()` answers them: in order, none twice.
|
||||
nodes.sort_by_cached_key(|node| node.to_ascii_lowercase());
|
||||
nodes.dedup_by(|a, b| names_equal(a, b));
|
||||
nodes
|
||||
}),
|
||||
};
|
||||
let mut names = match found {
|
||||
Ok(names) => names,
|
||||
Err(refused) => return refused.into(),
|
||||
};
|
||||
if list.kind == NameKind::Topic && !peer.permissions.can_query() {
|
||||
names.retain(|topic| peer.permissions.can_push(topic) || peer.permissions.can_read(topic));
|
||||
}
|
||||
let truncated = names.len() > MAX_NAMES_LIST_ENTRIES;
|
||||
names.truncate(MAX_NAMES_LIST_ENTRIES);
|
||||
NamesList {
|
||||
|
||||
@@ -41,6 +41,10 @@ read_topics = ["logs"]
|
||||
[roles.analyst]
|
||||
query = true
|
||||
|
||||
[roles.explorer]
|
||||
list_topics = true
|
||||
list_nodes = true
|
||||
|
||||
[roles.peer]
|
||||
send_topics = ["*"]
|
||||
accept_topics = ["*"]
|
||||
@@ -604,8 +608,24 @@ fn names() {
|
||||
assert_eq!(names(&mut sensor, NameKind::Topic), ["logs", "sensors"]);
|
||||
assert_eq!(names(&mut screen, NameKind::Topic), ["logs"]);
|
||||
assert_eq!(names(&mut analyst, NameKind::Topic), ["logs", "sensors"]);
|
||||
// Nodes: every one with messages.
|
||||
assert_eq!(names(&mut screen, NameKind::Node), ["garage", "kitchen"]);
|
||||
// Nodes: those of the topics it may read; every one for a query role.
|
||||
assert!(names(&mut sensor, NameKind::Node).is_empty());
|
||||
assert_eq!(names(&mut screen, NameKind::Node), ["kitchen"]);
|
||||
assert_eq!(names(&mut analyst, NameKind::Node), ["garage", "kitchen"]);
|
||||
// `list_topics` and `list_nodes`: every one, without reading any.
|
||||
let mut explorer = running
|
||||
.connect(&running.issue("browser", &["explorer"]))
|
||||
.unwrap();
|
||||
assert_eq!(names(&mut explorer, NameKind::Topic), ["logs", "sensors"]);
|
||||
assert_eq!(names(&mut explorer, NameKind::Node), ["garage", "kitchen"]);
|
||||
assert_eq!(
|
||||
code(
|
||||
explorer
|
||||
.get_messages("logs", "kitchen", None, 1)
|
||||
.unwrap_err()
|
||||
),
|
||||
ErrorCode::NotAllowed.code()
|
||||
);
|
||||
}
|
||||
|
||||
/// The query parts of `OldSource/Tests/BigMessages-Test.ps1`.
|
||||
|
||||
@@ -101,8 +101,8 @@ enum Command {
|
||||
/// standard input's.
|
||||
sql: String,
|
||||
},
|
||||
/// Lists the topics with messages that this client may use (every one
|
||||
/// if it may query) and the nodes with messages.
|
||||
/// Lists the topics this client may use and the nodes of those it may
|
||||
/// read; every one if its roles have list_topics / list_nodes, or query.
|
||||
List {
|
||||
/// What to list.
|
||||
#[arg(value_enum, default_value_t = What::All)]
|
||||
|
||||
Reference in New Issue
Block a user