Skip to content

Commit fda257d

Browse files
committed
chore: serialize as filter directly from serde, fix check_acl_config
1 parent 211643c commit fda257d

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ foxmq.d/
66

77
# These are test keys and must not be used anywhere else.
88
!/tests/dmq
9+
.env

src/config/acl.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
1-
use std::path::Path;
1+
use std::{path::Path, str::FromStr};
22

33
use tashi_collections::HashMap;
44

5-
use crate::mqtt::trie::Filter;
5+
use crate::mqtt::trie::{Filter, TopicName};
66

7-
#[derive(serde::Deserialize, serde::Serialize, Default)]
7+
#[derive(serde::Deserialize, Default)]
88
pub struct AclConfig {
9+
#[serde(default)]
910
pub permissions: HashMap<String, TopicsConfig>,
1011
}
1112

12-
#[derive(serde::Deserialize, serde::Serialize)]
13+
#[derive(serde::Deserialize, Debug)]
1314
pub struct TopicsConfig {
1415
pub topic: Vec<TopicPermissions>,
1516
}
1617

17-
#[derive(serde::Deserialize, serde::Serialize)]
18+
#[derive(serde::Deserialize, Debug)]
1819
pub struct TopicPermissions {
19-
pub filter: String,
20+
#[serde(deserialize_with = "from_str")]
21+
pub filter: Filter,
2022
pub allowed: Vec<TransactionType>,
23+
#[serde(default)]
2124
pub denied: Vec<TransactionType>,
2225
}
2326

24-
#[derive(serde::Deserialize, serde::Serialize, PartialEq, Eq)]
27+
fn from_str<'de, D>(deserializer: D) -> Result<Filter, D::Error>
28+
where
29+
D: serde::Deserializer<'de>,
30+
{
31+
let s: String = serde::Deserialize::deserialize(deserializer)?;
32+
33+
Filter::from_str(&s).map_err(serde::de::Error::custom)
34+
}
35+
36+
#[derive(serde::Deserialize, PartialEq, Eq, Debug)]
37+
#[serde(rename_all = "lowercase")]
2538
pub enum TransactionType {
2639
Subscribe,
2740
Publish,
@@ -38,13 +51,14 @@ impl AclConfig {
3851
pub fn check_acl_config(
3952
&self,
4053
topics_config: Option<&TopicsConfig>,
41-
filter: &Filter,
54+
topic_name: &str,
4255
transaction_type: TransactionType,
4356
) -> bool {
4457
// Allows everything if no topics config was found.
4558
topics_config.map_or(true, |perms| {
46-
!perms.topic.iter().any(|k| {
47-
k.allowed.iter().any(|k| *k == transaction_type) && filter.matches_topic(&k.filter)
59+
perms.topic.iter().any(|k| {
60+
k.allowed.iter().any(|k| *k == transaction_type)
61+
&& k.filter.matches_topic(topic_name)
4862
})
4963
})
5064
}

src/mqtt/router.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::cmp;
22
use std::collections::BTreeMap;
33
use std::num::NonZeroU32;
44
use std::ops::{Index, IndexMut};
5-
use std::str::FromStr;
65
use std::sync::{Arc, OnceLock};
76
use std::time::{Instant, SystemTime};
87

@@ -702,7 +701,7 @@ fn handle_subscribe(state: &mut RouterState, client_idx: ClientIndex, request: S
702701
.and_then(|(filter, props)| {
703702
if !state.acl.check_acl_config(
704703
permissions,
705-
&filter,
704+
filter.as_str(),
706705
crate::config::acl::TransactionType::Subscribe,
707706
) {
708707
Err(SubscribeReasonCode::NotAuthorized)?
@@ -941,13 +940,11 @@ fn dispatch(state: &mut RouterState, publish: Arc<PublishTrasaction>, origin: Pu
941940
return;
942941
};
943942

944-
let topic_filter = Filter::from(&topic);
945-
946943
let topics_config = state.acl.get_topics_acl_config(&client.user);
947944

948945
if !state.acl.check_acl_config(
949946
topics_config,
950-
&topic_filter,
947+
&publish.topic,
951948
crate::config::acl::TransactionType::Publish,
952949
) {
953950
return;

0 commit comments

Comments
 (0)