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