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/// Read or modify ACL documents.
84///
85/// Separates ACL-administration rights from general CRUD access.
86/// A principal with `acl` capability can read and update ACL documents
87/// without necessarily having access to the resources those ACLs protect.
88pub const CAP_ACL: &str = "acl";
89
90// ── Group-principal prefix ─────────────────────────────────────────────────────
91
92/// Sigil that marks a group principal in an [`AclMap`] key.
93///
94/// A group key has the form `+<handle>.<path>` where `<handle>` is the owning
95/// identity's handle and `<path>` is a dot-separated path of arbitrary depth
96/// into that handle's group tree (e.g. `+alice.project4.admins`).
97pub const GROUP_PREFIX: &str = "+";
98
99/// Local-entity wildcard principal.
100///
101/// The bare `"#"` key in an [`AclMap`] matches **any** caller whose principal
102/// starts with `"#"` — i.e. any entity running on the same runtime instance.
103///
104/// It sits between the specific `"#<name>"` form and the global `"*"` wildcard
105/// in lookup priority:
106///
107/// ```text
108/// // specific entity  > local wildcard  > global wildcard
109/// //   "#fortune"         "#"                 "*"
110/// ```
111///
112/// The runtime pre-normalises intra-runtime callers from their full DID-URL form
113/// (`did:ma:<our_did>#fortune`) to the bare fragment form (`#fortune`) before
114/// the ACL lookup, so that these keys resolve correctly.
115///
116/// ## YAML example
117///
118/// ```yaml
119/// acl:
120///   "#": [handle_cast]        # any local entity on this runtime may call
121///   "did:ma:alice": ["*"]     # remote alice: all caps
122///   "*":                      # everyone else: deny
123/// ```
124pub const LOCAL_ENTITY_WILDCARD: &str = "#";
125
126// ── CapabilityEntry ────────────────────────────────────────────────────────────
127
128/// Capability set for a principal in an [`AclMap`].
129///
130/// Serialises as:
131/// - `null` → [`Deny`](CapabilityEntry::Deny)
132/// - YAML sequence → [`Allow`](CapabilityEntry::Allow)
133#[derive(Debug, Clone, PartialEq, Eq)]
134pub enum CapabilityEntry {
135    /// Explicit deny. Wins over any wildcard allow for the same principal.
136    Deny,
137    /// Allow the listed capabilities. `["*"]` grants all capabilities.
138    Allow(BTreeSet<String>),
139}
140
141impl CapabilityEntry {
142    /// Construct an `Allow` entry from an iterator of capability name strings.
143    pub fn from_caps<I, S>(caps: I) -> Self
144    where
145        I: IntoIterator<Item = S>,
146        S: Into<String>,
147    {
148        Self::Allow(caps.into_iter().map(Into::into).collect())
149    }
150
151    /// Return `true` if this entry grants `cap`.
152    /// `"*"` in the capability set grants any capability.
153    pub fn has(&self, cap: &str) -> bool {
154        match self {
155            Self::Deny => false,
156            Self::Allow(caps) => caps.contains(cap) || caps.contains("*"),
157        }
158    }
159
160    /// Return `true` if this is an explicit deny.
161    pub fn is_deny(&self) -> bool {
162        matches!(self, Self::Deny)
163    }
164}
165
166impl Serialize for CapabilityEntry {
167    fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
168        match self {
169            Self::Deny => serializer.serialize_none(),
170            Self::Allow(caps) => {
171                use serde::ser::SerializeSeq;
172                let mut seq = serializer.serialize_seq(Some(caps.len()))?;
173                for cap in caps {
174                    seq.serialize_element(cap)?;
175                }
176                seq.end()
177            }
178        }
179    }
180}
181
182impl<'de> Deserialize<'de> for CapabilityEntry {
183    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
184        #[derive(Deserialize)]
185        #[serde(untagged)]
186        enum Raw {
187            #[allow(dead_code)]
188            Str(String),
189            Seq(Vec<String>),
190        }
191        let opt: Option<Raw> = Option::deserialize(deserializer)?;
192        match opt {
193            None => Ok(Self::Deny),
194            Some(Raw::Seq(v)) if v.is_empty() => Ok(Self::Deny),
195            Some(Raw::Seq(v)) => Ok(Self::Allow(v.into_iter().collect())),
196            Some(Raw::Str(_)) => Err(serde::de::Error::custom(
197                "invalid ACL entry: use a YAML sequence for Allow or null for Deny",
198            )),
199        }
200    }
201}
202
203// ── AclMap ─────────────────────────────────────────────────────────────────────
204
205/// Capability-based access control map.
206///
207/// Keys are principal strings — exactly one of:
208/// - `"*"` — wildcard
209/// - `"did:ma:<identity>"` — bare DID, no fragment
210/// - `"#<local>"` — local entity identifier
211/// - `"+<handle>.<path>"` — named group of principals (unlimited depth)
212///
213/// DID-URLs with fragments (`did:ma:foo#bar`) are **not** valid keys;
214/// use [`is_valid_acl_key`] to validate before inserting.
215pub type AclMap = HashMap<String, CapabilityEntry>;
216
217// ── check_cap ──────────────────────────────────────────────────────────────────
218
219/// Check whether `caller` has capability `cap` in `acl`.
220///
221/// 1. Normalise `caller` (strip fragment from DID-URLs).
222/// 2. Look up the normalised caller directly — if a principal entry, apply and stop.
223/// 3. Fall back to the `"*"` wildcard principal entry.
224/// 4. Explicit deny → `Err`; capability absent → `Err`; no entry → `Err`.
225///
226/// Group principals (`+<handle>.<path>`) are **not** resolved here;
227/// they are expanded by the runtime's async `check_full`.
228///
229/// A `"*"` item inside an `Allow` set grants **all** capabilities.
230/// Evaluate a single [`CapabilityEntry`] for `cap` and `caller`.
231///
232/// Returns `Ok(())` when the entry grants `cap`, or the appropriate `Err`.
233#[cfg(feature = "acl")]
234fn apply_entry(entry: &CapabilityEntry, cap: &str, caller: &str) -> Result<()> {
235    match entry {
236        CapabilityEntry::Deny => Err(Error::Acl(format!("operation denied for {caller}"))),
237        CapabilityEntry::Allow(caps) if caps.contains(cap) || caps.contains("*") => Ok(()),
238        CapabilityEntry::Allow(_) => Err(Error::Acl(format!(
239            "capability '{cap}' denied for {caller}"
240        ))),
241    }
242}
243
244#[cfg(feature = "acl")]
245pub fn check_cap(acl: &AclMap, caller: &str, cap: &str) -> Result<()> {
246    let normalized = normalize_principal(caller);
247
248    // 1. Direct principal match (highest priority).
249    if let Some(direct) = acl.get(normalized) {
250        return apply_entry(direct, cap, caller);
251    }
252
253    // 2. Local-entity wildcard "#" — matches any `#`-prefixed caller.
254    if normalized.starts_with('#') {
255        if let Some(local_wild) = acl.get(LOCAL_ENTITY_WILDCARD) {
256            return apply_entry(local_wild, cap, caller);
257        }
258    }
259
260    // 3. Global wildcard "*" (lowest priority).
261    match acl.get("*") {
262        None => Err(Error::Acl(format!("no ACL entry for {caller}"))),
263        Some(entry) => apply_entry(entry, cap, caller),
264    }
265}
266
267/// Return `true` if `key` is a valid [`AclMap`] key.
268///
269/// Valid keys: `"*"`, `"did:ma:<id>"`, `"#<local>"`, `"+<handle>.<path>"`
270/// (where `<path>` is dot-separated with unlimited depth),
271/// and arbitrary capability/verb name strings (non-empty).
272pub fn is_valid_acl_key(key: &str) -> bool {
273    !key.is_empty()
274}
275
276/// Return `true` if `key` is a principal key (identifies *who*).
277///
278/// Valid principal key forms:
279/// - `"*"` — global wildcard
280/// - `"did:ma:<id>"` — bare DID (no fragment)
281/// - `"#"` — local-entity wildcard (any entity on this runtime)
282/// - `"#<name>"` — specific local entity by fragment name
283/// - `"+<handle>.<path>"` — group principal
284pub fn is_principal_key(key: &str) -> bool {
285    key == "*"
286        || (key.starts_with("did:") && !key.contains('#'))
287        || key.starts_with('#')   // covers both "#" (local wildcard) and "#name" (specific)
288        || is_valid_group_key(key)
289}
290
291/// Return `true` if `key` is a valid group principal.
292///
293/// Form: `+<handle>.<path>` where `<handle>` is non-empty and `<path>` is a
294/// non-empty dot-separated string of arbitrary depth
295/// (e.g. `+alice.admins`, `+alice.project4.admins`).
296fn is_valid_group_key(key: &str) -> bool {
297    if let Some(rest) = key.strip_prefix(GROUP_PREFIX) {
298        if let Some(dot) = rest.find('.') {
299            let handle = &rest[..dot];
300            let path = &rest[dot + 1..];
301            return !handle.is_empty() && !path.is_empty();
302        }
303    }
304    false
305}
306
307/// Validate all keys in an [`AclMap`], returning a descriptive error for the
308/// first invalid key found.
309///
310/// Call this immediately after loading an ACL from YAML or any external source.
311#[cfg(feature = "acl")]
312pub fn validate_acl_map(acl: &AclMap) -> Result<()> {
313    for key in acl.keys() {
314        if !is_valid_acl_key(key) {
315            return Err(Error::Acl(format!(
316                "invalid ACL key {key:?}: key must be non-empty"
317            )));
318        }
319    }
320    Ok(())
321}
322
323/// Normalise a caller identity for [`AclMap`] lookup.
324///
325/// - `did:ma:foo#bar` → `did:ma:foo` (strips fragment from **remote** DID-URLs)
326/// - `#local` → `#local` (already-normalised local entity, passed through)
327/// - `#` → `#` (local-entity wildcard, passed through)
328/// - `*` → `*` (global wildcard, passed through)
329///
330/// **Note:** Callers from the same runtime arrive as `did:ma:<our_did>#fragment`.
331/// The runtime is responsible for converting those to `#fragment` *before*
332/// calling this function (or [`check_cap`]) so that `"#<name>"` and `"#"`
333/// ACL keys resolve correctly.  This function only strips fragments from
334/// foreign DID-URLs; it does not know which DID belongs to the local runtime.
335pub fn normalize_principal(did: &str) -> &str {
336    if did.starts_with("did:") {
337        if let Some(pos) = did.find('#') {
338            return &did[..pos];
339        }
340    }
341    did
342}
343
344// ── Tests ──────────────────────────────────────────────────────────────────────
345
346#[cfg(test)]
347mod tests {
348    use super::*;
349
350    fn allow(caps: &[&str]) -> CapabilityEntry {
351        CapabilityEntry::from_caps(caps.iter().copied())
352    }
353
354    fn m(entries: &[(&str, CapabilityEntry)]) -> AclMap {
355        entries
356            .iter()
357            .map(|(k, v)| (k.to_string(), v.clone()))
358            .collect()
359    }
360
361    #[test]
362    fn wildcard_rpc_allows_rpc() {
363        let acl = m(&[("*", allow(&[CAP_RPC]))]);
364        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_ok());
365    }
366
367    #[test]
368    fn wildcard_rpc_denies_ipfs() {
369        let acl = m(&[("*", allow(&[CAP_RPC]))]);
370        assert!(check_cap(&acl, "did:ma:alice", CAP_IPFS).is_err());
371    }
372
373    #[test]
374    fn explicit_deny_wins_over_wildcard_allow() {
375        let acl = m(&[
376            ("*", allow(&[CAP_RPC, CAP_IPFS])),
377            ("did:ma:bandit", CapabilityEntry::Deny),
378        ]);
379        assert!(check_cap(&acl, "did:ma:bandit", CAP_RPC).is_err());
380    }
381
382    #[test]
383    fn exact_match_restricts_below_wildcard() {
384        let acl = m(&[
385            ("*", allow(&[CAP_RPC, CAP_IPFS])),
386            ("did:ma:bob", allow(&[CAP_RPC])),
387        ]);
388        assert!(check_cap(&acl, "did:ma:bob", CAP_RPC).is_ok());
389        assert!(check_cap(&acl, "did:ma:bob", CAP_IPFS).is_err());
390    }
391
392    #[test]
393    fn did_url_caller_is_normalized() {
394        let acl = m(&[("did:ma:alice", allow(&[CAP_RPC, CAP_IPFS]))]);
395        assert!(check_cap(&acl, "did:ma:alice#sign", CAP_RPC).is_ok());
396    }
397
398    #[test]
399    fn no_entry_default_deny() {
400        assert!(check_cap(&AclMap::new(), "did:ma:anyone", CAP_RPC).is_err());
401    }
402
403    #[test]
404    fn wildcard_deny_blocks_all() {
405        let acl = m(&[("*", CapabilityEntry::Deny)]);
406        assert!(check_cap(&acl, "did:ma:anyone", CAP_RPC).is_err());
407    }
408
409    #[test]
410    fn local_entity_key_allowed() {
411        let acl = m(&[("#agent", allow(&[CAP_RPC]))]);
412        assert!(check_cap(&acl, "#agent", CAP_RPC).is_ok());
413        assert!(check_cap(&acl, "#other", CAP_RPC).is_err());
414    }
415
416    #[test]
417    fn arbitrary_capability_works() {
418        let acl = m(&[("did:ma:alice", allow(&["emote", "reply"]))]);
419        assert!(check_cap(&acl, "did:ma:alice", "emote").is_ok());
420        assert!(check_cap(&acl, "did:ma:alice", "reply").is_ok());
421        assert!(check_cap(&acl, "did:ma:alice", "admin").is_err());
422    }
423
424    #[test]
425    fn wildcard_cap_grants_all_capabilities() {
426        let acl = m(&[("did:ma:alice", allow(&["*"]))]);
427        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_ok());
428        assert!(check_cap(&acl, "did:ma:alice", CAP_IPFS).is_ok());
429        assert!(check_cap(&acl, "did:ma:alice", "emote").is_ok());
430        assert!(check_cap(&acl, "did:ma:alice", "admin").is_ok());
431    }
432
433    #[test]
434    fn owner_capability_is_just_a_string() {
435        let acl = m(&[("did:ma:alice", allow(&["owner"]))]);
436        assert!(check_cap(&acl, "did:ma:alice", "owner").is_ok());
437        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_err());
438    }
439
440    #[test]
441    fn normalize_strips_fragment() {
442        assert_eq!(normalize_principal("did:ma:foo#bar"), "did:ma:foo");
443        assert_eq!(normalize_principal("did:ma:foo"), "did:ma:foo");
444        assert_eq!(normalize_principal("#local"), "#local");
445        assert_eq!(normalize_principal("#"), "#");
446        assert_eq!(normalize_principal("*"), "*");
447    }
448
449    // ── Local-entity wildcard ("#") tests ─────────────────────────────────────
450
451    #[test]
452    fn local_wildcard_allows_any_hash_prefixed_caller() {
453        // "#" matches any #-prefixed caller when there is no specific entry.
454        let acl = m(&[("#", allow(&[CAP_RPC]))]);
455        assert!(check_cap(&acl, "#fortune", CAP_RPC).is_ok());
456        assert!(check_cap(&acl, "#scheduler", CAP_RPC).is_ok());
457        assert!(check_cap(&acl, "#any_entity", CAP_RPC).is_ok());
458    }
459
460    #[test]
461    fn local_wildcard_does_not_match_remote_callers() {
462        // Remote DIDs do not start with '#', so "#" should not grant them.
463        let acl = m(&[("#", allow(&[CAP_RPC]))]);
464        assert!(check_cap(&acl, "did:ma:remote", CAP_RPC).is_err());
465    }
466
467    #[test]
468    fn specific_local_entity_wins_over_local_wildcard() {
469        // "#fortune" is more specific: it restricts below the "#" wildcard.
470        let acl = m(&[
471            ("#", allow(&[CAP_RPC, CAP_IPFS])),
472            ("#fortune", allow(&[CAP_RPC])), // only rpc, not ipfs
473        ]);
474        assert!(check_cap(&acl, "#fortune", CAP_RPC).is_ok());
475        assert!(check_cap(&acl, "#fortune", CAP_IPFS).is_err());
476        // Other local entities still get rpc+ipfs from the wildcard.
477        assert!(check_cap(&acl, "#other", CAP_IPFS).is_ok());
478    }
479
480    #[test]
481    fn local_wildcard_deny_blocks_all_local_entities() {
482        let acl = m(&[
483            ("#", CapabilityEntry::Deny),
484            ("*", allow(&[CAP_RPC])), // global wildcard would allow, but # deny wins
485        ]);
486        assert!(check_cap(&acl, "#fortune", CAP_RPC).is_err());
487        assert!(check_cap(&acl, "#any", CAP_RPC).is_err());
488        // Remote callers are unaffected by the "#" deny.
489        assert!(check_cap(&acl, "did:ma:remote", CAP_RPC).is_ok());
490    }
491
492    #[test]
493    fn specific_local_entity_allow_overrides_local_wildcard_deny() {
494        // "#fortune" explicit allow wins over "#" deny for that entity.
495        let acl = m(&[
496            ("#", CapabilityEntry::Deny),
497            ("#fortune", allow(&[CAP_RPC])),
498        ]);
499        assert!(check_cap(&acl, "#fortune", CAP_RPC).is_ok());
500        assert!(check_cap(&acl, "#other", CAP_RPC).is_err());
501    }
502
503    #[test]
504    fn global_wildcard_not_triggered_for_hash_caller_when_local_wildcard_present() {
505        // When "#" deny is set, "*" allow must NOT override it for local callers.
506        let acl = m(&[("#", CapabilityEntry::Deny), ("*", allow(&[CAP_RPC]))]);
507        // Local entity is denied by "#" — must not fall through to "*".
508        assert!(check_cap(&acl, "#fortune", CAP_RPC).is_err());
509    }
510
511    #[test]
512    fn local_wildcard_is_key_form_valid() {
513        assert!(is_principal_key("#"));
514        assert!(is_principal_key("#fortune"));
515        assert!(is_principal_key("*"));
516        assert!(is_principal_key("did:ma:alice"));
517    }
518
519    #[test]
520    fn explicit_deny_without_wildcard() {
521        // A bare Deny entry with no wildcard still denies.
522        let acl = m(&[("did:ma:bandit", CapabilityEntry::Deny)]);
523        assert!(check_cap(&acl, "did:ma:bandit", CAP_RPC).is_err());
524        // Others get default deny too (no wildcard).
525        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_err());
526    }
527
528    #[test]
529    fn multiple_caps_in_single_entry() {
530        let acl = m(&[("did:ma:alice", allow(&[CAP_RPC, CAP_IPFS, CAP_READ]))]);
531        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_ok());
532        assert!(check_cap(&acl, "did:ma:alice", CAP_IPFS).is_ok());
533        assert!(check_cap(&acl, "did:ma:alice", CAP_READ).is_ok());
534        assert!(check_cap(&acl, "did:ma:alice", CAP_CREATE).is_err());
535        assert!(check_cap(&acl, "did:ma:alice", CAP_DELETE).is_err());
536    }
537
538    #[test]
539    fn direct_entry_restricts_even_when_wildcard_is_broader() {
540        // Wildcard gives everyone rpc+ipfs, but bob only gets rpc.
541        // Direct entry wins and caps don't accumulate from wildcard.
542        let acl = m(&[
543            ("*", allow(&[CAP_RPC, CAP_IPFS])),
544            ("did:ma:bob", allow(&[CAP_RPC])),
545        ]);
546        assert!(check_cap(&acl, "did:ma:bob", CAP_RPC).is_ok());
547        assert!(check_cap(&acl, "did:ma:bob", CAP_IPFS).is_err());
548        // Alice (no direct entry) still gets both from wildcard.
549        assert!(check_cap(&acl, "did:ma:alice", CAP_RPC).is_ok());
550        assert!(check_cap(&acl, "did:ma:alice", CAP_IPFS).is_ok());
551    }
552
553    #[test]
554    fn group_principal_allowed() {
555        // "+group" keys are not resolved by check_cap; they pass through.
556        // Resolution happens in the runtime's async check_full.
557        let acl = m(&[("*", allow(&[CAP_RPC]))]);
558        assert!(check_cap(&acl, "did:ma:anyone", CAP_RPC).is_ok());
559    }
560
561    #[test]
562    fn valid_acl_keys() {
563        assert!(is_valid_acl_key("*"));
564        assert!(is_valid_acl_key("did:ma:Qmfoo"));
565        assert!(is_valid_acl_key("#agent"));
566        assert!(is_valid_acl_key("+alice.venner"));
567        assert!(is_valid_acl_key("+runtime.admins"));
568        assert!(is_valid_acl_key("fortune"));
569        assert!(is_valid_acl_key("admin"));
570        assert!(is_valid_acl_key("emote"));
571        assert!(!is_valid_acl_key(""));
572    }
573
574    #[cfg(feature = "acl")]
575    #[test]
576    fn capability_serde_roundtrip() {
577        let acl: AclMap = [
578            (
579                "*".to_string(),
580                CapabilityEntry::from_caps(["rpc", "create"]),
581            ),
582            ("did:ma:bandit".to_string(), CapabilityEntry::Deny),
583        ]
584        .into_iter()
585        .collect();
586        let yaml = serde_yaml::to_string(&acl).unwrap();
587        let roundtrip: AclMap = serde_yaml::from_str(&yaml).unwrap();
588        assert_eq!(acl, roundtrip);
589    }
590
591    #[cfg(feature = "acl")]
592    #[test]
593    fn yaml_null_deserializes_to_deny() {
594        let yaml = "'did:ma:x': ~\n'*':\n- rpc\n- create\n";
595        let acl: AclMap = serde_yaml::from_str(yaml).unwrap();
596        assert_eq!(acl.get("did:ma:x"), Some(&CapabilityEntry::Deny));
597        assert_eq!(
598            acl.get("*"),
599            Some(&CapabilityEntry::from_caps(["rpc", "create"]))
600        );
601    }
602}