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