Skip to main content

ma_core/acl/
mod.rs

1//! Capability-based access control for ma identities.
2//!
3//! An [`AclMap`] maps principal strings to [`CapabilityEntry`] values.
4//! Deny always wins over allow; a wildcard deny closes access to everyone.
5//!
6//! # Capabilities
7//!
8//! Capabilities are plain strings. Built-in system capabilities:
9//!
10//! | Capability | Meaning |
11//! |------------|---------|
12//! | `"rpc"`    | Send RPC messages via `/ma/rpc/0.0.1` |
13//! | `"ipfs"`   | Publish DID documents via `/ma/ipfs/0.0.1` |
14//! | `"read"`   | Read entities, config, and namespace contents |
15//! | `"create"` | Create new namespaces or entities |
16//! | `"update"` | Update existing namespaces or entities |
17//! | `"delete"` | Delete namespaces or entities |
18//! | `"*"`      | Wildcard — grants **all** capabilities at this level |
19//!
20//! Entity and namespace ACLs may also use arbitrary capability strings that
21//! correspond to verb names or sub-namespace names.
22//!
23//! # Key forms in an [`AclMap`]
24//!
25//! Keys are **principal strings** — exactly one of:
26//!
27//! | Form | Meaning |
28//! |------|---------|
29//! | `"*"` | Wildcard — matches any caller |
30//! | `"did:ma:<identity>"` | Bare DID (no fragment) |
31//! | `"#<local>"` | Local entity identifier |
32//! | `"+<handle>.<path>"` | Named group of principals (unlimited depth) |
33//!
34//! # YAML format
35//!
36//! ```yaml
37//! acl:
38//!   "*": [rpc, create]          # everyone: RPC + create
39//!   "did:ma:alice": ["*"]        # alice: all capabilities
40//!   "did:ma:bob": [rpc, read]   # bob: restricted
41//!   "did:ma:eve":               # null / absent → explicit deny
42//!   "+carlotta.friends": [rpc]         # group: all members get rpc
43//!   "+alice.project4.admins": ["*"]  # deep path: project4 admins get all caps
44//!   "+alice.enemies":                # group: all members denied
45//! ```
46//!
47//! # Example
48//!
49//! ```rust
50//! # use ma_core::{AclMap, CapabilityEntry, check_cap, CAP_RPC};
51//! let mut acl = AclMap::new();
52//! acl.insert("*".to_string(), CapabilityEntry::from_caps(["rpc"]));
53//! acl.insert("did:ma:Qmevil".to_string(), CapabilityEntry::Deny);
54//! assert!(check_cap(&acl, "did:ma:Qmgood", CAP_RPC).is_ok());
55//! assert!(check_cap(&acl, "did:ma:Qmevil", CAP_RPC).is_err());
56//! ```
57
58use std::collections::{BTreeSet, HashMap};
59
60use serde::{Deserialize, Deserializer, Serialize, Serializer};
61
62#[cfg(feature = "acl")]
63use crate::{Error, Result};
64
65// ── Capability constants ───────────────────────────────────────────────────────
66
67/// Deliver messages to an endpoint's inbox (`/ma/inbox/0.0.1`).
68pub const CAP_INBOX: &str = "inbox";
69/// Send RPC messages via `/ma/rpc/0.0.1`.
70pub const CAP_RPC: &str = "rpc";
71/// Publish DID documents via `/ma/ipfs/0.0.1`.
72pub const CAP_IPFS: &str = "ipfs";
73/// Read entities, config, and namespace contents.
74pub const CAP_READ: &str = "read";
75/// Create new namespaces or entities.
76pub const CAP_CREATE: &str = "create";
77/// Update existing namespaces or entities.
78pub const CAP_UPDATE: &str = "update";
79/// Delete namespaces or entities.
80pub const CAP_DELETE: &str = "delete";
81
82// ── Group-principal prefix ─────────────────────────────────────────────────────
83
84/// Sigil that marks a group principal in an [`AclMap`] key.
85///
86/// A group key has the form `+<handle>.<path>` where `<handle>` is the owning
87/// identity's handle and `<path>` is a dot-separated path of arbitrary depth
88/// into that handle's group tree (e.g. `+alice.project4.admins`).
89pub const GROUP_PREFIX: &str = "+";
90
91// ── CapabilityEntry ────────────────────────────────────────────────────────────
92
93/// Capability set for a principal in an [`AclMap`].
94///
95/// Serialises as:
96/// - `null` → [`Deny`](CapabilityEntry::Deny)
97/// - YAML sequence → [`Allow`](CapabilityEntry::Allow)
98#[derive(Debug, Clone, PartialEq, Eq)]
99pub enum CapabilityEntry {
100    /// Explicit deny. Wins over any wildcard allow for the same principal.
101    Deny,
102    /// Allow the listed capabilities. `["*"]` grants all capabilities.
103    Allow(BTreeSet<String>),
104}
105
106impl CapabilityEntry {
107    /// Construct an `Allow` entry from an iterator of capability name strings.
108    pub fn from_caps<I, S>(caps: I) -> Self
109    where
110        I: IntoIterator<Item = S>,
111        S: Into<String>,
112    {
113        Self::Allow(caps.into_iter().map(Into::into).collect())
114    }
115
116    /// Return `true` if this entry grants `cap`.
117    /// `"*"` in the capability set grants any capability.
118    pub fn has(&self, cap: &str) -> bool {
119        match self {
120            Self::Deny => false,
121            Self::Allow(caps) => caps.contains(cap) || caps.contains("*"),
122        }
123    }
124
125    /// Return `true` if this is an explicit deny.
126    pub fn is_deny(&self) -> bool {
127        matches!(self, Self::Deny)
128    }
129}
130
131impl Serialize for CapabilityEntry {
132    fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
133        match self {
134            Self::Deny => serializer.serialize_none(),
135            Self::Allow(caps) => {
136                use serde::ser::SerializeSeq;
137                let mut seq = serializer.serialize_seq(Some(caps.len()))?;
138                for cap in caps {
139                    seq.serialize_element(cap)?;
140                }
141                seq.end()
142            }
143        }
144    }
145}
146
147impl<'de> Deserialize<'de> for CapabilityEntry {
148    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
149        #[derive(Deserialize)]
150        #[serde(untagged)]
151        enum Raw {
152            #[allow(dead_code)]
153            Str(String),
154            Seq(Vec<String>),
155        }
156        let opt: Option<Raw> = Option::deserialize(deserializer)?;
157        match opt {
158            None => Ok(Self::Deny),
159            Some(Raw::Seq(v)) if v.is_empty() => Ok(Self::Deny),
160            Some(Raw::Seq(v)) => Ok(Self::Allow(v.into_iter().collect())),
161            Some(Raw::Str(_)) => Err(serde::de::Error::custom(
162                "invalid ACL entry: use a YAML sequence for Allow or null for Deny",
163            )),
164        }
165    }
166}
167
168// ── AclMap ─────────────────────────────────────────────────────────────────────
169
170/// Capability-based access control map.
171///
172/// Keys are principal strings — exactly one of:
173/// - `"*"` — wildcard
174/// - `"did:ma:<identity>"` — bare DID, no fragment
175/// - `"#<local>"` — local entity identifier
176/// - `"+<handle>.<path>"` — named group of principals (unlimited depth)
177///
178/// DID-URLs with fragments (`did:ma:foo#bar`) are **not** valid keys;
179/// use [`is_valid_acl_key`] to validate before inserting.
180pub type AclMap = HashMap<String, CapabilityEntry>;
181
182// ── check_cap ──────────────────────────────────────────────────────────────────
183
184/// Check whether `caller` has capability `cap` in `acl`.
185///
186/// 1. Normalise `caller` (strip fragment from DID-URLs).
187/// 2. Look up the normalised caller directly — if a principal entry, apply and stop.
188/// 3. Fall back to the `"*"` wildcard principal entry.
189/// 4. Explicit deny → `Err`; capability absent → `Err`; no entry → `Err`.
190///
191/// Group principals (`+<handle>.<path>`) are **not** resolved here;
192/// they are expanded by the runtime's async `check_full`.
193///
194/// A `"*"` item inside an `Allow` set grants **all** capabilities.
195#[cfg(feature = "acl")]
196pub fn check_cap(acl: &AclMap, caller: &str, cap: &str) -> Result<()> {
197    let normalized = normalize_principal(caller);
198    if let Some(direct) = acl.get(normalized) {
199        match direct {
200            CapabilityEntry::Deny => {
201                return Err(Error::Acl(format!("operation denied for {caller}")));
202            }
203            CapabilityEntry::Allow(caps) if caps.contains(cap) || caps.contains("*") => {
204                return Ok(());
205            }
206            CapabilityEntry::Allow(_) => {
207                return Err(Error::Acl(format!(
208                    "capability '{cap}' denied for {caller}"
209                )));
210            }
211        }
212    }
213
214    match acl.get("*") {
215        None => Err(Error::Acl(format!("no ACL entry for {caller}"))),
216        Some(CapabilityEntry::Deny) => Err(Error::Acl(format!("operation denied for {caller}"))),
217        Some(CapabilityEntry::Allow(caps)) if caps.contains(cap) || caps.contains("*") => Ok(()),
218        Some(CapabilityEntry::Allow(_)) => Err(Error::Acl(format!(
219            "capability '{cap}' denied for {caller}"
220        ))),
221    }
222}
223
224/// Return `true` if `key` is a valid [`AclMap`] key.
225///
226/// Valid keys: `"*"`, `"did:ma:<id>"`, `"#<local>"`, `"+<handle>.<path>"`
227/// (where `<path>` is dot-separated with unlimited depth),
228/// and arbitrary capability/verb name strings (non-empty).
229pub fn is_valid_acl_key(key: &str) -> bool {
230    !key.is_empty()
231}
232
233/// Return `true` if `key` is a principal key (identifies *who*).
234pub fn is_principal_key(key: &str) -> bool {
235    key == "*"
236        || (key.starts_with("did:") && !key.contains('#'))
237        || (key.starts_with('#') && key.len() > 1)
238        || is_valid_group_key(key)
239}
240
241/// Return `true` if `key` is a valid group principal.
242///
243/// Form: `+<handle>.<path>` where `<handle>` is non-empty and `<path>` is a
244/// non-empty dot-separated string of arbitrary depth
245/// (e.g. `+alice.admins`, `+alice.project4.admins`).
246fn is_valid_group_key(key: &str) -> bool {
247    if let Some(rest) = key.strip_prefix(GROUP_PREFIX) {
248        if let Some(dot) = rest.find('.') {
249            let handle = &rest[..dot];
250            let path = &rest[dot + 1..];
251            return !handle.is_empty() && !path.is_empty();
252        }
253    }
254    false
255}
256
257/// Validate all keys in an [`AclMap`], returning a descriptive error for the
258/// first invalid key found.
259///
260/// Call this immediately after loading an ACL from YAML or any external source.
261#[cfg(feature = "acl")]
262pub fn validate_acl_map(acl: &AclMap) -> Result<()> {
263    for key in acl.keys() {
264        if !is_valid_acl_key(key) {
265            return Err(Error::Acl(format!(
266                "invalid ACL key {key:?}: key must be non-empty"
267            )));
268        }
269    }
270    Ok(())
271}
272
273/// Normalise a caller identity for [`AclMap`] lookup.
274///
275/// - `did:ma:foo#bar` → `did:ma:foo` (strips fragment from DID-URLs)
276/// - `#local` → `#local` (local entity, passed through)
277/// - `*` → `*` (wildcard, passed through)
278pub fn normalize_principal(did: &str) -> &str {
279    if did.starts_with("did:") {
280        if let Some(pos) = did.find('#') {
281            return &did[..pos];
282        }
283    }
284    did
285}
286
287// ── Tests ──────────────────────────────────────────────────────────────────────
288
289#[cfg(test)]
290mod tests {
291    use super::*;
292
293    fn allow(caps: &[&str]) -> CapabilityEntry {
294        CapabilityEntry::from_caps(caps.iter().copied())
295    }
296
297    fn m(entries: &[(&str, CapabilityEntry)]) -> AclMap {
298        entries
299            .iter()
300            .map(|(k, v)| (k.to_string(), v.clone()))
301            .collect()
302    }
303
304    #[test]
305    fn wildcard_rpc_allows_rpc() {
306        let acl = m(&[("*", allow(&[CAP_RPC]))]);
307        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_ok());
308    }
309
310    #[test]
311    fn wildcard_rpc_denies_ipfs() {
312        let acl = m(&[("*", allow(&[CAP_RPC]))]);
313        assert!(check_cap(&acl, "did:ma:alice", CAP_IPFS).is_err());
314    }
315
316    #[test]
317    fn explicit_deny_wins_over_wildcard_allow() {
318        let acl = m(&[
319            ("*", allow(&[CAP_RPC, CAP_IPFS])),
320            ("did:ma:bandit", CapabilityEntry::Deny),
321        ]);
322        assert!(check_cap(&acl, "did:ma:bandit", CAP_RPC).is_err());
323    }
324
325    #[test]
326    fn exact_match_restricts_below_wildcard() {
327        let acl = m(&[
328            ("*", allow(&[CAP_RPC, CAP_IPFS])),
329            ("did:ma:bob", allow(&[CAP_RPC])),
330        ]);
331        assert!(check_cap(&acl, "did:ma:bob", CAP_RPC).is_ok());
332        assert!(check_cap(&acl, "did:ma:bob", CAP_IPFS).is_err());
333    }
334
335    #[test]
336    fn did_url_caller_is_normalized() {
337        let acl = m(&[("did:ma:alice", allow(&[CAP_RPC, CAP_IPFS]))]);
338        assert!(check_cap(&acl, "did:ma:alice#sign", CAP_RPC).is_ok());
339    }
340
341    #[test]
342    fn no_entry_default_deny() {
343        assert!(check_cap(&AclMap::new(), "did:ma:anyone", CAP_RPC).is_err());
344    }
345
346    #[test]
347    fn wildcard_deny_blocks_all() {
348        let acl = m(&[("*", CapabilityEntry::Deny)]);
349        assert!(check_cap(&acl, "did:ma:anyone", CAP_RPC).is_err());
350    }
351
352    #[test]
353    fn local_entity_key_allowed() {
354        let acl = m(&[("#agent", allow(&[CAP_RPC]))]);
355        assert!(check_cap(&acl, "#agent", CAP_RPC).is_ok());
356        assert!(check_cap(&acl, "#other", CAP_RPC).is_err());
357    }
358
359    #[test]
360    fn arbitrary_capability_works() {
361        let acl = m(&[("did:ma:alice", allow(&["emote", "reply"]))]);
362        assert!(check_cap(&acl, "did:ma:alice", "emote").is_ok());
363        assert!(check_cap(&acl, "did:ma:alice", "reply").is_ok());
364        assert!(check_cap(&acl, "did:ma:alice", "admin").is_err());
365    }
366
367    #[test]
368    fn wildcard_cap_grants_all_capabilities() {
369        let acl = m(&[("did:ma:alice", allow(&["*"]))]);
370        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_ok());
371        assert!(check_cap(&acl, "did:ma:alice", CAP_IPFS).is_ok());
372        assert!(check_cap(&acl, "did:ma:alice", "emote").is_ok());
373        assert!(check_cap(&acl, "did:ma:alice", "admin").is_ok());
374    }
375
376    #[test]
377    fn owner_capability_is_just_a_string() {
378        let acl = m(&[("did:ma:alice", allow(&["owner"]))]);
379        assert!(check_cap(&acl, "did:ma:alice", "owner").is_ok());
380        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_err());
381    }
382
383    #[test]
384    fn normalize_strips_fragment() {
385        assert_eq!(normalize_principal("did:ma:foo#bar"), "did:ma:foo");
386        assert_eq!(normalize_principal("did:ma:foo"), "did:ma:foo");
387        assert_eq!(normalize_principal("#local"), "#local");
388        assert_eq!(normalize_principal("*"), "*");
389    }
390
391    #[test]
392    fn explicit_deny_without_wildcard() {
393        // A bare Deny entry with no wildcard still denies.
394        let acl = m(&[("did:ma:bandit", CapabilityEntry::Deny)]);
395        assert!(check_cap(&acl, "did:ma:bandit", CAP_RPC).is_err());
396        // Others get default deny too (no wildcard).
397        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_err());
398    }
399
400    #[test]
401    fn multiple_caps_in_single_entry() {
402        let acl = m(&[("did:ma:alice", allow(&[CAP_RPC, CAP_IPFS, CAP_READ]))]);
403        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_ok());
404        assert!(check_cap(&acl, "did:ma:alice", CAP_IPFS).is_ok());
405        assert!(check_cap(&acl, "did:ma:alice", CAP_READ).is_ok());
406        assert!(check_cap(&acl, "did:ma:alice", CAP_CREATE).is_err());
407        assert!(check_cap(&acl, "did:ma:alice", CAP_DELETE).is_err());
408    }
409
410    #[test]
411    fn direct_entry_restricts_even_when_wildcard_is_broader() {
412        // Wildcard gives everyone rpc+ipfs, but bob only gets rpc.
413        // Direct entry wins and caps don't accumulate from wildcard.
414        let acl = m(&[
415            ("*", allow(&[CAP_RPC, CAP_IPFS])),
416            ("did:ma:bob", allow(&[CAP_RPC])),
417        ]);
418        assert!(check_cap(&acl, "did:ma:bob", CAP_RPC).is_ok());
419        assert!(check_cap(&acl, "did:ma:bob", CAP_IPFS).is_err());
420        // Alice (no direct entry) still gets both from wildcard.
421        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_ok());
422        assert!(check_cap(&acl, "did:ma:alice", CAP_IPFS).is_ok());
423    }
424
425    #[test]
426    fn group_principal_allowed() {
427        // "+group" keys are not resolved by check_cap; they pass through.
428        // Resolution happens in the runtime's async check_full.
429        let acl = m(&[("*", allow(&[CAP_RPC]))]);
430        assert!(check_cap(&acl, "did:ma:anyone", CAP_RPC).is_ok());
431    }
432
433    #[test]
434    fn valid_acl_keys() {
435        assert!(is_valid_acl_key("*"));
436        assert!(is_valid_acl_key("did:ma:Qmfoo"));
437        assert!(is_valid_acl_key("#agent"));
438        assert!(is_valid_acl_key("+alice.venner"));
439        assert!(is_valid_acl_key("+runtime.admins"));
440        assert!(is_valid_acl_key("fortune"));
441        assert!(is_valid_acl_key("admin"));
442        assert!(is_valid_acl_key("emote"));
443        assert!(!is_valid_acl_key(""));
444    }
445
446    #[cfg(feature = "acl")]
447    #[test]
448    fn capability_serde_roundtrip() {
449        let acl: AclMap = [
450            (
451                "*".to_string(),
452                CapabilityEntry::from_caps(["rpc", "create"]),
453            ),
454            ("did:ma:bandit".to_string(), CapabilityEntry::Deny),
455        ]
456        .into_iter()
457        .collect();
458        let yaml = serde_yaml::to_string(&acl).unwrap();
459        let roundtrip: AclMap = serde_yaml::from_str(&yaml).unwrap();
460        assert_eq!(acl, roundtrip);
461    }
462
463    #[cfg(feature = "acl")]
464    #[test]
465    fn yaml_null_deserializes_to_deny() {
466        let yaml = "'did:ma:x': ~\n'*':\n- rpc\n- create\n";
467        let acl: AclMap = serde_yaml::from_str(yaml).unwrap();
468        assert_eq!(acl.get("did:ma:x"), Some(&CapabilityEntry::Deny));
469        assert_eq!(
470            acl.get("*"),
471            Some(&CapabilityEntry::from_caps(["rpc", "create"]))
472        );
473    }
474}