mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
Added filtered name lists and the DLL's enumerations, Opened the query connection read-only, Bumped to 0.4.0
- Protocol 8: ListTopics / ListNodes became one ListNames [kind, topics, nodes], each filter a name or a pattern: the topics a node pushed to, the nodes that pushed to a topic, wildcards included. No storage change; the permissions are the lists' (may_list_topic, may_list_nodes_of) - The DLL: the JSON getters are suffixed AsJson (GetNamesListAsJson, GetQueryResultAsJson); ListNamesA/W replaces ListTopics / ListNodes. Beside them, Win32-like enumerations: EnumerateNames / EnumerateQuery, NextName / NextQueryRow (before the first item at first), getters, CloseEnum (enumerations.rs). They don't outlive their client: FreeClient closes them - C#: EnumerateTopics / EnumerateNodes / EnumerateQuery as IEnumerable (QueryRow for a query), fetched at each foreach, closed in finally - Queries: the connection opens the file read-only (SQLITE_OPEN_READ_ONLY), a fifth guard beside query_only, the read-only statement check, one statement only and the PRAGMA-denying authorizer; a unit test checks the handle is read-only - Python, PureBasic, the tool, docs, guides and revamp.md (step 16) updated; versions 0.4.0 everywhere Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,7 +7,7 @@ use aws_lc_rs::digest;
|
||||
use bakashrot_config::Permissions;
|
||||
use bakashrot_pki::Identity;
|
||||
use bakashrot_proto::limits::{MAX_MESSAGE_SIZE, MAX_NAMES_LIST_ENTRIES, MAX_PACKET_SIZE};
|
||||
use bakashrot_proto::names::names_equal;
|
||||
use bakashrot_proto::names::{name_matches, names_equal};
|
||||
use bakashrot_proto::packets::{
|
||||
Attachment, AttachmentPart, Continue, GetAttachment, GetMessages, ListNames, MessagePushed,
|
||||
Messages, NameKind, NamesList, PushMessage, Query, QueryResult, RequestRefused, Welcome,
|
||||
@@ -476,38 +476,50 @@ 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 with `list_topics` or `query`. Nodes: those
|
||||
/// with messages in the topics it may read; every one with `list_nodes`
|
||||
/// or `query`.
|
||||
/// The topics or nodes of the topic and node pairs with messages (pushed
|
||||
/// here or replicated, or that had some) that the filters match, among
|
||||
/// those it may see. A pair's topic must be one it may see when the answer
|
||||
/// is topics or `topics` filters them: one it may push to or read, any
|
||||
/// with `list_topics` or `query`. Its node, when the answer is nodes or
|
||||
/// `nodes` filters them: one in a topic it may read, any with
|
||||
/// `list_nodes` or `query`. Without filters: the topics it may see, the
|
||||
/// nodes it may.
|
||||
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
|
||||
.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
|
||||
let permissions = peer.permissions.clone();
|
||||
let kind = list.kind;
|
||||
// A pass over every pair: on the read's thread, off the async ones.
|
||||
let found = shared
|
||||
.read(move |reader| {
|
||||
let matches = |filter: &Option<String>, name: &str| {
|
||||
filter
|
||||
.as_deref()
|
||||
.is_none_or(|filter| name_matches(filter, name))
|
||||
};
|
||||
let check_topic = kind == NameKind::Topic || list.topics.is_some();
|
||||
let check_node = kind == NameKind::Node || list.nodes.is_some();
|
||||
let mut names: Vec<String> = reader
|
||||
.heads()?
|
||||
.into_iter()
|
||||
.filter(|head| permissions.can_read(&head.topic))
|
||||
.map(|head| head.node)
|
||||
.filter(|head| {
|
||||
matches(&list.topics, &head.topic)
|
||||
&& matches(&list.nodes, &head.node)
|
||||
&& (!check_topic || permissions.may_list_topic(&head.topic))
|
||||
&& (!check_node || permissions.may_list_nodes_of(&head.topic))
|
||||
})
|
||||
.map(|head| match kind {
|
||||
NameKind::Topic => head.topic,
|
||||
NameKind::Node => 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
|
||||
}),
|
||||
};
|
||||
// In order, none twice (names are without case).
|
||||
names.sort_unstable_by(|a, b| {
|
||||
let a = a.bytes().map(|c| c.to_ascii_lowercase());
|
||||
a.cmp(b.bytes().map(|c| c.to_ascii_lowercase()))
|
||||
});
|
||||
names.dedup_by(|a, b| names_equal(a, b));
|
||||
Ok(names)
|
||||
})
|
||||
.await;
|
||||
let mut names = match found {
|
||||
Ok(names) => names,
|
||||
Err(refused) => return refused.into(),
|
||||
@@ -515,7 +527,7 @@ async fn list_names(shared: &Arc<Shared>, peer: &Peer, list: ListNames) -> Packe
|
||||
let truncated = names.len() > MAX_NAMES_LIST_ENTRIES;
|
||||
names.truncate(MAX_NAMES_LIST_ENTRIES);
|
||||
NamesList {
|
||||
kind: list.kind,
|
||||
kind,
|
||||
truncated,
|
||||
names,
|
||||
}
|
||||
|
||||
@@ -596,12 +596,13 @@ fn names() {
|
||||
.connect(&running.issue("analyst-1", &["analyst"]))
|
||||
.unwrap();
|
||||
|
||||
let names = |client: &mut Client, kind| {
|
||||
let list = client.list_names(kind).unwrap();
|
||||
let filtered = |client: &mut Client, kind, topics, nodes| {
|
||||
let list = client.list_names(kind, topics, nodes).unwrap();
|
||||
assert_eq!(list.kind, kind);
|
||||
assert!(!list.truncated);
|
||||
list.names
|
||||
};
|
||||
let names = |client: &mut Client, kind| filtered(client, kind, None, None);
|
||||
// Topics: those it may push to or read; every one for a query role.
|
||||
assert_eq!(names(&mut sensor, NameKind::Topic), ["logs", "sensors"]);
|
||||
assert_eq!(names(&mut screen, NameKind::Topic), ["logs"]);
|
||||
@@ -624,6 +625,55 @@ fn names() {
|
||||
),
|
||||
ErrorCode::NotAllowed.code()
|
||||
);
|
||||
|
||||
// Filtered: the topics a node pushed to, the nodes that pushed to a
|
||||
// topic; names (without case) or patterns.
|
||||
sensor.push("sensors", "{}").unwrap();
|
||||
let (topic, node) = (NameKind::Topic, NameKind::Node);
|
||||
let every = &mut analyst;
|
||||
assert_eq!(
|
||||
filtered(every, topic, None, Some("kitchen")),
|
||||
["logs", "sensors"]
|
||||
);
|
||||
assert_eq!(
|
||||
filtered(every, topic, None, Some("KITCHEN")),
|
||||
["logs", "sensors"]
|
||||
);
|
||||
assert_eq!(filtered(every, topic, None, Some("g*")), ["sensors"]);
|
||||
assert!(filtered(every, topic, None, Some("nobody")).is_empty());
|
||||
assert_eq!(
|
||||
filtered(every, node, Some("sensors"), None),
|
||||
["garage", "kitchen"]
|
||||
);
|
||||
assert_eq!(filtered(every, node, Some("l?gs"), None), ["kitchen"]);
|
||||
assert_eq!(filtered(every, node, Some("*"), Some("k*")), ["kitchen"]);
|
||||
assert_eq!(
|
||||
filtered(every, topic, Some("s*"), Some("kitchen")),
|
||||
["sensors"]
|
||||
);
|
||||
// Only what it may see: the nodes of the topics it may read...
|
||||
assert_eq!(
|
||||
filtered(&mut screen, topic, None, Some("kitchen")),
|
||||
["logs"]
|
||||
);
|
||||
assert!(filtered(&mut screen, topic, None, Some("garage")).is_empty());
|
||||
assert_eq!(filtered(&mut screen, node, Some("*"), None), ["kitchen"]);
|
||||
// ...not of those it may only push to, the topics of those it may.
|
||||
assert!(filtered(&mut sensor, topic, None, Some("garage")).is_empty());
|
||||
assert_eq!(filtered(&mut sensor, topic, Some("s*"), None), ["sensors"]);
|
||||
assert_eq!(
|
||||
filtered(&mut explorer, topic, None, Some("garage")),
|
||||
["sensors"]
|
||||
);
|
||||
assert_eq!(
|
||||
filtered(&mut explorer, node, Some("logs"), None),
|
||||
["kitchen"]
|
||||
);
|
||||
// Neither a name nor a pattern: refused before it's sent.
|
||||
let error = analyst
|
||||
.list_names(NameKind::Node, Some("a b"), None)
|
||||
.unwrap_err();
|
||||
assert_eq!(error.code(), ErrorCode::InvalidArgument.code(), "{error}");
|
||||
}
|
||||
|
||||
/// Queries: allowed or not, results, refusals.
|
||||
@@ -1549,14 +1599,20 @@ fn replication_edges() {
|
||||
assert_eq!(got.messages[0].data, "m1");
|
||||
// (`old` had a message, past its age.)
|
||||
assert_eq!(
|
||||
reader.list_names(NameKind::Topic).unwrap().names,
|
||||
reader
|
||||
.list_names(NameKind::Topic, None, None)
|
||||
.unwrap()
|
||||
.names,
|
||||
["elsewhere", "logs", "old"]
|
||||
);
|
||||
let mut logs_only = running
|
||||
.connect(&running.issue("lounge", &["logs"]))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
logs_only.list_names(NameKind::Topic).unwrap().names,
|
||||
logs_only
|
||||
.list_names(NameKind::Topic, None, None)
|
||||
.unwrap()
|
||||
.names,
|
||||
["logs"]
|
||||
);
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user