1
0
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:
2026-11-16 01:40:01 +01:00
parent cf8264b21d
commit c86dc8798e
14 changed files with 122 additions and 31 deletions
+4 -3
View File
@@ -227,13 +227,14 @@ ptrdiff_t BakaShrot_GetMessageNodeW(uint16_t *buffer, ptrdiff_t size);
/** /**
* Lists the topics with messages that this client may push to or read * 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 * (every one if its roles have `list_topics` or `query`): how many, -1 on
* `BakaShrot_GetNamesList()`. * failure. Then `BakaShrot_GetNamesList()`.
*/ */
int32_t BakaShrot_ListTopics(void); int32_t BakaShrot_ListTopics(void);
/** /**
* 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()`. * `BakaShrot_GetNamesList()`.
*/ */
int32_t BakaShrot_ListNodes(void); int32_t BakaShrot_ListNodes(void);
+2 -2
View File
@@ -542,11 +542,11 @@ class Client:
time.sleep(interval) time.sleep(interval)
def list_topics(self) -> NamesList: def list_topics(self) -> NamesList:
"""The topics with messages that the client may push to or read (every one if it may query).""" """The topics with messages that the client may push to or read (every one with list_topics or query)."""
return self._list(self._dll.ListTopics, "List topics") return self._list(self._dll.ListTopics, "List topics")
def list_nodes(self) -> NamesList: def list_nodes(self) -> NamesList:
"""Every node with messages.""" """The nodes with messages in the topics the client may read (every one with list_nodes or query)."""
return self._list(self._dll.ListNodes, "List nodes") return self._list(self._dll.ListNodes, "List nodes")
def _list(self, function, what: str) -> NamesList: def _list(self, function, what: str) -> NamesList:
+4 -3
View File
@@ -669,14 +669,15 @@ fn list(kind: NameKind) -> i32 {
} }
/// Lists the topics with messages that this client may push to or read /// 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 /// (every one if its roles have `list_topics` or `query`): how many, -1 on
/// `BakaShrot_GetNamesList()`. /// failure. Then `BakaShrot_GetNamesList()`.
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_ListTopics() -> i32 { pub extern "C" fn BakaShrot_ListTopics() -> i32 {
list(NameKind::Topic) 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()`. /// `BakaShrot_GetNamesList()`.
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_ListNodes() -> i32 { pub extern "C" fn BakaShrot_ListNodes() -> i32 {
+4 -3
View File
@@ -413,9 +413,10 @@ impl Client {
} }
} }
/// The topics with messages that this client may push to or read /// The topics with messages that this client may push to or read, or
/// (every one if it may query), or every node with messages. /// the nodes with messages in the topics it may read; every one if its
/// `truncated` if some didn't fit. /// roles have `list_topics` / `list_nodes`, or `query`. `truncated` if
/// some didn't fit.
pub fn list_names(&mut self, kind: NameKind) -> Result<NamesList> { pub fn list_names(&mut self, kind: NameKind) -> Result<NamesList> {
match self.request(ListNames { kind }.into())? { match self.request(ListNames { kind }.into())? {
Packet::NamesList(names) => Ok(names), Packet::NamesList(names) => Ok(names),
+4
View File
@@ -56,6 +56,10 @@ push_topics = ["sensors.*"] # always as its own node (its name)
[roles.dashboard] [roles.dashboard]
read_topics = ["*"] read_topics = ["*"]
query = true # read-only SQL, reads every topic 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 # Peers are roles too: what's replicated with a server whose certificate
# has this role, whichever end dials. # has this role, whichever end dials.
+19
View File
@@ -61,6 +61,14 @@ pub struct Role {
/// Whether it may run read-only SQL, which reads every topic. /// Whether it may run read-only SQL, which reads every topic.
#[serde(default)] #[serde(default)]
pub query: bool, 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. /// Replication: the topics sent to it.
#[serde(default)] #[serde(default)]
pub send_topics: NameList, pub send_topics: NameList,
@@ -109,6 +117,17 @@ impl Permissions {
self.roles.iter().any(|role| role.query) 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. /// Whether `node`'s messages in `topic` are sent to it.
pub fn sends(&self, topic: &str, node: &str) -> bool { pub fn sends(&self, topic: &str, node: &str) -> bool {
self.roles self.roles
+13 -1
View File
@@ -164,7 +164,19 @@ accept_nodes = ["garage"]
assert!(both.can_read("Logs")); assert!(both.can_read("Logs"));
assert!(!both.can_read("sensors.garage")); assert!(!both.can_read("sensors.garage"));
assert!(!both.can_query()); 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. // Replication: each role's topics with that role's nodes, not mixed.
let peer = config.permissions(&["peer", "backup"]).unwrap(); let peer = config.permissions(&["peer", "backup"]).unwrap();
+28 -7
View File
@@ -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 /// Topics: those with messages (pushed here or replicated) that it may
/// push to or read, every one if it may query (queries read everything). /// push to or read; every one with `list_topics` or `query`. Nodes: those
/// Nodes: every one with messages. /// 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 { async fn list_names(shared: &Arc<Shared>, peer: &Peer, list: ListNames) -> Packet {
let permissions = &peer.permissions;
let found = match list.kind { let found = match list.kind {
NameKind::Topic => shared.read(|reader| reader.topics()).await, NameKind::Topic => shared
NameKind::Node => shared.read(|reader| reader.nodes()).await, .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 { let mut names = match found {
Ok(names) => names, Ok(names) => names,
Err(refused) => return refused.into(), 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; let truncated = names.len() > MAX_NAMES_LIST_ENTRIES;
names.truncate(MAX_NAMES_LIST_ENTRIES); names.truncate(MAX_NAMES_LIST_ENTRIES);
NamesList { NamesList {
+22 -2
View File
@@ -41,6 +41,10 @@ read_topics = ["logs"]
[roles.analyst] [roles.analyst]
query = true query = true
[roles.explorer]
list_topics = true
list_nodes = true
[roles.peer] [roles.peer]
send_topics = ["*"] send_topics = ["*"]
accept_topics = ["*"] accept_topics = ["*"]
@@ -604,8 +608,24 @@ fn names() {
assert_eq!(names(&mut sensor, NameKind::Topic), ["logs", "sensors"]); assert_eq!(names(&mut sensor, NameKind::Topic), ["logs", "sensors"]);
assert_eq!(names(&mut screen, NameKind::Topic), ["logs"]); assert_eq!(names(&mut screen, NameKind::Topic), ["logs"]);
assert_eq!(names(&mut analyst, NameKind::Topic), ["logs", "sensors"]); assert_eq!(names(&mut analyst, NameKind::Topic), ["logs", "sensors"]);
// Nodes: every one with messages. // Nodes: those of the topics it may read; every one for a query role.
assert_eq!(names(&mut screen, NameKind::Node), ["garage", "kitchen"]); 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`. /// The query parts of `OldSource/Tests/BigMessages-Test.ps1`.
+2 -2
View File
@@ -101,8 +101,8 @@ enum Command {
/// standard input's. /// standard input's.
sql: String, sql: String,
}, },
/// Lists the topics with messages that this client may use (every one /// Lists the topics this client may use and the nodes of those it may
/// if it may query) and the nodes with messages. /// read; every one if its roles have list_topics / list_nodes, or query.
List { List {
/// What to list. /// What to list.
#[arg(value_enum, default_value_t = What::All)] #[arg(value_enum, default_value_t = What::All)]
+6
View File
@@ -37,11 +37,17 @@ What a role gives:
| `push_topics` | none | pushing to these topics, always as its own node | | `push_topics` | none | pushing to these topics, always as its own node |
| `read_topics` | none | reading these topics' messages | | `read_topics` | none | reading these topics' messages |
| `query` | `false` | read-only SQL, which reads every topic | | `query` | `false` | read-only SQL, which reads every topic |
| `list_topics` | `false` | listing topics answers every one with messages, not only those it may push to or read |
| `list_nodes` | `false` | listing nodes answers every one with messages, not only those of the topics it may read |
| `send_topics` | none | replication: these topics sent to it | | `send_topics` | none | replication: these topics sent to it |
| `accept_topics` | none | replication: these topics taken from it | | `accept_topics` | none | replication: these topics taken from it |
| `send_nodes` | `["*"]` | replication: only these nodes' messages sent | | `send_nodes` | `["*"]` | replication: only these nodes' messages sent |
| `accept_nodes` | `["*"]` | replication: only these nodes' messages taken | | `accept_nodes` | `["*"]` | replication: only these nodes' messages taken |
`list_topics` and `list_nodes` (2026-11-16) only give names, for a tool
that browses a server: reading a topic is still `read_topics`. `query`
gives both, it reads everything anyway.
No `AllowRead` / `AllowWrite`: a list that's empty allows nothing. No No `AllowRead` / `AllowWrite`: a list that's empty allows nothing. No
default topic: a push always names its topic (protocol 7 has none). default topic: a push always names its topic (protocol 7 has none).
+5 -5
View File
@@ -120,11 +120,11 @@ Reproduce one before fixing it, and move it up (or drop it) once known.
taken from anyone. Listing topics also only shows those with taken from anyone. Listing topics also only shows those with
messages: a client can't ask what it may push to. Fine for the messages: a client can't ask what it may push to. Fine for the
threat model; names in `push_topics` where the set is closed. threat model; names in `push_topics` where the set is closed.
- **Every client sees every node's name.** `ListNames` for nodes - **Whether a node exists can still be probed.** Listing nodes is
answers every node with messages (`list_names`, `reader.nodes()`), filtered by the roles since 2026-11-16 (`list_nodes`), but
whatever the client's roles read, to push-only clients too. Topics `GetMessages` answers `UnknownName` for a node without messages
are filtered by the roles. Fix: only the nodes of the topics it may read, or none anywhere and none for one with messages in another topic
without `read_topics` or `query`. (`reader.rs`, `messages`). Fix: none in both cases.
- **A CRL file that isn't there.** The server takes it as nothing - **A CRL file that isn't there.** The server takes it as nothing
revoked (`load_revocations`, `bakashrot-server/src/lib.rs`), the revoked (`load_revocations`, `bakashrot-server/src/lib.rs`), the
`bakashrot` tool refuses to start when `client.toml`'s `crl` names a `bakashrot` tool refuses to start when `client.toml`'s `crl` names a
+2 -2
View File
@@ -201,8 +201,8 @@ the same ids with different messages, undetected.
(`MESSAGES_BYTES`, a bigger message alone), `more` if others follow. (`MESSAGES_BYTES`, a bigger message alone), `more` if others follow.
- **`ListNames`:** topics are those with messages (pushed here or - **`ListNames`:** topics are those with messages (pushed here or
replicated), those the roles may push to or read (every one with replicated), those the roles may push to or read (every one with
`query`, which reads them all anyway); nodes are every one with `list_topics` or `query`); nodes are those with messages in the
messages. `truncated` past 65,536. topics the roles may read (every one with `list_nodes` or `query`). `truncated` past 65,536.
- **`Query`:** the roles must allow it (`NotAllowed`); read-only SQL, - **`Query`:** the roles must allow it (`NotAllowed`); read-only SQL,
refused with SQLite's message (`QueryFailed`, with a hint to add refused with SQLite's message (`QueryFailed`, with a hint to add
`is_json = 1` on "malformed JSON"), over 10,000 rows or 16 MB `is_json = 1` on "malformed JSON"), over 10,000 rows or 16 MB
+7 -1
View File
@@ -168,7 +168,13 @@ Also decided (2026-11-13):
peer is read like any other, and a push to a topic nobody names is peer is read like any other, and a push to a topic nobody names is
`NotAllowed` (`UnknownName` is only for nodes now). Before: `topics` `NotAllowed` (`UnknownName` is only for nodes now). Before: `topics`
since 2026-11-13, `replicated_topics` to filter on edge nodes (now since 2026-11-13, `replicated_topics` to filter on edge nodes (now
their peers' `send_topics`). their peers' `send_topics`);
- **`list_topics` and `list_nodes`** (2026-11-16), two booleans of a
role, for a tool that browses a server: listing answers every topic,
every node with messages. Without them (or `query`, which gives
both): the topics it may push to or read, the nodes of the topics
it may read (every node until then). Names only, reading is still
`read_topics`.
- **Schema v9, not compatible with v8** (2026-11-13): old databases aren't - **Schema v9, not compatible with v8** (2026-11-13): old databases aren't
opened, their data isn't carried over (the real-life tests start empty). opened, their data isn't carried over (the real-life tests start empty).
`docs/storage.md`: `docs/storage.md`: