Skip to content

Commit 18979fe

Browse files
committed
ACL: Targets in ACL entries are NULLable
1 parent 4bb0831 commit 18979fe

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

rs-matter/src/acl.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
error::{Error, ErrorCode},
2323
fabric,
2424
interaction_model::messages::GenericPath,
25-
tlv::{self, FromTLV, TLVElement, TLVList, TLVWriter, TagType, ToTLV},
25+
tlv::{self, FromTLV, Nullable, TLVElement, TLVList, TLVWriter, TagType, ToTLV},
2626
transport::session::{Session, SessionMode, MAX_CAT_IDS_PER_NOC},
2727
utils::writebuf::WriteBuf,
2828
};
@@ -282,7 +282,15 @@ impl Target {
282282
}
283283

284284
type Subjects = [Option<u64>; SUBJECTS_PER_ENTRY];
285-
type Targets = [Option<Target>; TARGETS_PER_ENTRY];
285+
286+
type Targets = Nullable<[Option<Target>; TARGETS_PER_ENTRY]>;
287+
impl Targets {
288+
fn init_notnull() -> Self {
289+
const INIT_TARGETS: Option<Target> = None;
290+
Nullable::NotNull([INIT_TARGETS; TARGETS_PER_ENTRY])
291+
}
292+
}
293+
286294
#[derive(ToTLV, FromTLV, Clone, Debug, PartialEq)]
287295
#[tlvargs(start = 1)]
288296
pub struct AclEntry {
@@ -298,14 +306,12 @@ pub struct AclEntry {
298306
impl AclEntry {
299307
pub fn new(fab_idx: u8, privilege: Privilege, auth_mode: AuthMode) -> Self {
300308
const INIT_SUBJECTS: Option<u64> = None;
301-
const INIT_TARGETS: Option<Target> = None;
302-
303309
Self {
304310
fab_idx: Some(fab_idx),
305311
privilege,
306312
auth_mode,
307313
subjects: [INIT_SUBJECTS; SUBJECTS_PER_ENTRY],
308-
targets: [INIT_TARGETS; TARGETS_PER_ENTRY],
314+
targets: Targets::init_notnull(),
309315
}
310316
}
311317

@@ -324,12 +330,20 @@ impl AclEntry {
324330
}
325331

326332
pub fn add_target(&mut self, target: Target) -> Result<(), Error> {
333+
if self.targets.is_null() {
334+
self.targets = Targets::init_notnull();
335+
}
327336
let index = self
328337
.targets
338+
.as_ref()
339+
.notnull()
340+
.unwrap()
329341
.iter()
330342
.position(|s| s.is_none())
331343
.ok_or(ErrorCode::NoSpace)?;
332-
self.targets[index] = Some(target);
344+
345+
self.targets.as_mut().notnull().unwrap()[index] = Some(target);
346+
333347
Ok(())
334348
}
335349

@@ -358,12 +372,17 @@ impl AclEntry {
358372
fn match_access_desc(&self, object: &AccessDesc) -> bool {
359373
let mut allow = false;
360374
let mut entries_exist = false;
361-
for t in self.targets.iter().flatten() {
362-
entries_exist = true;
363-
if (t.endpoint.is_none() || t.endpoint == object.path.endpoint)
364-
&& (t.cluster.is_none() || t.cluster == object.path.cluster)
365-
{
366-
allow = true
375+
match self.targets.as_ref().notnull() {
376+
None => allow = true, // Allow if targets are NULL
377+
Some(targets) => {
378+
for t in targets.iter().flatten() {
379+
entries_exist = true;
380+
if (t.endpoint.is_none() || t.endpoint == object.path.endpoint)
381+
&& (t.cluster.is_none() || t.cluster == object.path.cluster)
382+
{
383+
allow = true
384+
}
385+
}
367386
}
368387
}
369388
if !entries_exist {

rs-matter/src/tlv/traits.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,28 @@ pub enum Nullable<T> {
265265
}
266266

267267
impl<T> Nullable<T> {
268+
pub fn as_mut(&mut self) -> Nullable<&mut T> {
269+
match self {
270+
Nullable::Null => Nullable::Null,
271+
Nullable::NotNull(t) => Nullable::NotNull(t),
272+
}
273+
}
274+
275+
pub fn as_ref(&self) -> Nullable<&T> {
276+
match self {
277+
Nullable::Null => Nullable::Null,
278+
Nullable::NotNull(t) => Nullable::NotNull(t),
279+
}
280+
}
281+
268282
pub fn is_null(&self) -> bool {
269283
match self {
270284
Nullable::Null => true,
271285
Nullable::NotNull(_) => false,
272286
}
273287
}
274288

275-
pub fn unwrap_notnull(self) -> Option<T> {
289+
pub fn notnull(self) -> Option<T> {
276290
match self {
277291
Nullable::Null => None,
278292
Nullable::NotNull(t) => Some(t),

0 commit comments

Comments
 (0)