1
0
mirror of https://github.com/aziascreations/BakaShrot.git synced 2026-11-23 20:58:46 +01:00

Made the roles the only lists of topics, Refused PRAGMAs in queries

- Removed `[server] topics` and `[replication] replicated_topics`: a push
  is taken by its roles' `push_topics`, a replicated message by the
  peer's `accept_topics`; a topic exists from its first message.
- Topics that only came from a peer are read and listed like any other;
  `ListNames` lists the topics with messages, filtered by the roles.
- A push to a topic no role names is `NotAllowed`; `UnknownName` is only
  for nodes.
- Clients' queries: every PRAGMA refused as it's prepared (SQLite's
  authorizer), `PRAGMA query_only = 0` used to take effect.
- Noted the leads found by reading the code in `docs/known-issues.md`.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 01:28:24 +01:00
parent cef99102f9
commit cf8264b21d
34 changed files with 304 additions and 227 deletions
+5 -22
View File
@@ -1,15 +1,15 @@
//! The server's config: a TOML file, read and checked in one go.
//!
//! - [`Server`]: where it listens, its files, its topics, its database.
//! - [`Server`]: where it listens, its files, its database.
//! - [`Retention`]: how long each topic's messages are kept.
//! - [`Replication`]: the servers it sends to and takes from, what it
//! replicates, how often.
//! - [`Replication`]: the servers it sends to and takes from, how often.
//! - [`Role`]: what certificates with a role may do; [`Permissions`]: what
//! a certificate's roles allow together.
//!
//! Who connects is the certificate's (`bakashrot-pki`): its name is its
//! node, its roles are looked up here. Peers too: what's replicated with a
//! peer is its roles'. The file, every key and its default:
//! peer is its roles'. Topics aren't listed anywhere else: one exists
//! once a role let a message in. The file, every key and its default:
//! `docs/config.md`.
//!
//! Every value is checked as serde reads it, so a wrong one is refused with
@@ -80,9 +80,6 @@ pub struct Server {
/// The SQLite database; `bakashrot.sqlite` by default.
#[serde(default = "defaults::database")]
pub database: PathBuf,
/// The topics clients push to, by name.
#[serde(default, deserialize_with = "values::topic_names")]
pub topics: Vec<String>,
/// How hard a push is made sure to be on disk.
#[serde(default)]
pub synchronous: Synchronous,
@@ -100,13 +97,6 @@ pub struct Server {
pub retention_interval: Duration,
}
impl Server {
/// Whether `topic` is one of its topics.
pub fn has_topic(&self, topic: &str) -> bool {
self.topics.iter().any(|own| names_equal(own, topic))
}
}
/// How hard the database makes sure a push is on disk before answering it
/// (SQLite's `synchronous`).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
@@ -210,9 +200,6 @@ pub struct Replication {
/// roles' `accept_*`.
#[serde(deserialize_with = "values::hosts")]
pub accept_from: Vec<String>,
/// The only topics replicated at all, whatever the roles say (an edge
/// node's filter); none: no filter.
pub replicated_topics: Option<NameList>,
/// How long to wait before dialing a peer again; 10s by default.
#[serde(deserialize_with = "values::duration")]
pub retry_delay: Duration,
@@ -230,7 +217,6 @@ impl Default for Replication {
Replication {
push_to: Vec::new(),
accept_from: Vec::new(),
replicated_topics: None,
retry_delay: Duration::from_secs(10),
sync_interval: Duration::from_secs(5),
batch_size: 1000,
@@ -317,9 +303,6 @@ impl Config {
})
.cloned()
.collect();
(!roles.is_empty()).then(|| Permissions {
roles,
replicated_topics: self.replication.replicated_topics.clone(),
})
(!roles.is_empty()).then_some(Permissions { roles })
}
}
+9 -20
View File
@@ -13,7 +13,7 @@ impl TryFrom<Vec<String>> for NameList {
type Error = String;
fn try_from(list: Vec<String>) -> Result<Self, String> {
check_names(&list, true)?;
check_names(&list)?;
Ok(NameList(list))
}
}
@@ -77,12 +77,11 @@ pub struct Role {
}
/// What a certificate may do: everything one of its roles allows, each role
/// on its own (a role's topics go with that role's nodes), replication
/// within the server's `replicated_topics`.
/// on its own (a role's topics go with that role's nodes). The roles are
/// all there is: no other list of topics.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Permissions {
pub(crate) roles: Vec<Role>,
pub(crate) replicated_topics: Option<NameList>,
}
impl Permissions {
@@ -112,20 +111,16 @@ impl Permissions {
/// Whether `node`'s messages in `topic` are sent to it.
pub fn sends(&self, topic: &str, node: &str) -> bool {
self.replicated(topic)
&& self
.roles
.iter()
.any(|role| role.send_topics.matches(topic) && role.send_nodes.matches(node))
self.roles
.iter()
.any(|role| role.send_topics.matches(topic) && role.send_nodes.matches(node))
}
/// Whether `node`'s messages in `topic` are taken from it.
pub fn accepts(&self, topic: &str, node: &str) -> bool {
self.replicated(topic)
&& self
.roles
.iter()
.any(|role| role.accept_topics.matches(topic) && role.accept_nodes.matches(node))
self.roles
.iter()
.any(|role| role.accept_topics.matches(topic) && role.accept_nodes.matches(node))
}
/// The topics and the nodes taken from it, as `SyncState` lists them:
@@ -158,10 +153,4 @@ impl Permissions {
.iter()
.any(|role| !role.send_topics.is_empty() || !role.accept_topics.is_empty())
}
fn replicated(&self, topic: &str) -> bool {
self.replicated_topics
.as_ref()
.is_none_or(|topics| topics.matches(topic))
}
}
+5 -17
View File
@@ -2,7 +2,7 @@
//! addresses, names.
use bakashrot_proto::limits::{MAX_ATTACHMENT_SIZE, MAX_MESSAGES_PER_PACKET};
use bakashrot_proto::names::{is_valid_name, is_valid_name_or_pattern, names_equal};
use bakashrot_proto::names::{is_valid_name_or_pattern, names_equal};
use serde::de::Error as _;
use serde::{Deserialize, Deserializer};
use std::net::{Ipv6Addr, SocketAddr};
@@ -184,23 +184,11 @@ pub(crate) fn batch_size<'de, D: Deserializer<'de>>(reader: D) -> Result<u32, D:
Ok(size)
}
/// Topic names (no patterns), none twice.
pub(crate) fn topic_names<'de, D: Deserializer<'de>>(reader: D) -> Result<Vec<String>, D::Error> {
let names = Vec::<String>::deserialize(reader)?;
check_names(&names, false).map_err(D::Error::custom)?;
Ok(names)
}
/// Checks a list of names (`patterns`: or patterns), none twice.
pub(crate) fn check_names(list: &[String], patterns: bool) -> Result<(), String> {
/// Checks a list of names or patterns, none twice.
pub(crate) fn check_names(list: &[String]) -> Result<(), String> {
for (at, entry) in list.iter().enumerate() {
let (valid, what) = if patterns {
(is_valid_name_or_pattern(entry), "a name or a pattern")
} else {
(is_valid_name(entry), "a name")
};
if !valid {
return Err(format!("'{entry}' isn't {what} ({NAME_HELP})"));
if !is_valid_name_or_pattern(entry) {
return Err(format!("'{entry}' isn't a name or a pattern ({NAME_HELP})"));
}
if list[..at].iter().any(|before| names_equal(before, entry)) {
return Err(format!("'{entry}' is given twice"));