rustfs-iam 1.0.0

Identity and Access Management (IAM) for RustFS, providing user management, roles, and permissions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{
    collections::{HashMap, HashSet},
    ops::{Deref, DerefMut},
    ptr,
    sync::{Arc, Mutex, Weak},
};

use arc_swap::{ArcSwap, Guard};
use rustfs_policy::{
    auth::UserIdentity,
    policy::{Args, PolicyDoc},
};
use time::OffsetDateTime;
use tokio::sync::Mutex as AsyncMutex;
use tracing::warn;

use crate::store::{GroupInfo, MappedPolicy};

/// Immutable IAM cache snapshot published atomically by [`Cache`].
///
/// Readers should load one `CacheState` and read all related maps from it. Writers
/// must go through `Cache`/`LockedCache` so multi-map updates publish as one state.
#[derive(Clone)]
pub(crate) struct CacheState {
    pub(crate) policy_docs: Arc<CacheEntity<PolicyDoc>>,
    pub(crate) users: Arc<CacheEntity<UserIdentity>>,
    pub(crate) user_policies: Arc<CacheEntity<MappedPolicy>>,
    pub(crate) sts_accounts: Arc<CacheEntity<UserIdentity>>,
    pub(crate) sts_policies: Arc<CacheEntity<MappedPolicy>>,
    pub(crate) groups: Arc<CacheEntity<GroupInfo>>,
    pub(crate) user_group_memberships: Arc<CacheEntity<HashSet<String>>>,
    pub(crate) group_policies: Arc<CacheEntity<MappedPolicy>>,
}

impl Default for CacheState {
    fn default() -> Self {
        Self {
            policy_docs: Arc::new(CacheEntity::default()),
            users: Arc::new(CacheEntity::default()),
            user_policies: Arc::new(CacheEntity::default()),
            sts_accounts: Arc::new(CacheEntity::default()),
            sts_policies: Arc::new(CacheEntity::default()),
            groups: Arc::new(CacheEntity::default()),
            user_group_memberships: Arc::new(CacheEntity::default()),
            group_policies: Arc::new(CacheEntity::default()),
        }
    }
}

pub struct Cache {
    state: ArcSwap<CacheState>,
    write_lock: Mutex<()>,
    service_account_mutation_lock: AsyncMutex<()>,
    sts_account_mutation_locks: Arc<StsMutationLockRegistry>,
}

struct StsMutationLockRegistry {
    locks: Mutex<HashMap<String, Weak<AsyncMutex<StsMutationLockState>>>>,
}

pub(crate) struct StsMutationLockState {
    access_key: String,
    registry: Weak<StsMutationLockRegistry>,
    lock: Weak<AsyncMutex<StsMutationLockState>>,
}

impl Drop for StsMutationLockState {
    fn drop(&mut self) {
        let Some(registry) = self.registry.upgrade() else {
            return;
        };
        let mut locks = registry.locks.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
        if locks.get(&self.access_key).is_some_and(|current| current.ptr_eq(&self.lock)) {
            locks.remove(&self.access_key);
        }
    }
}

impl Default for Cache {
    fn default() -> Self {
        Self {
            state: ArcSwap::new(Arc::new(CacheState::default())),
            write_lock: Mutex::new(()),
            service_account_mutation_lock: AsyncMutex::new(()),
            sts_account_mutation_locks: Arc::new(StsMutationLockRegistry {
                locks: Mutex::new(HashMap::new()),
            }),
        }
    }
}

pub(crate) type CacheSnapshot = Guard<Arc<CacheState>>;

impl Cache {
    pub(crate) fn service_account_mutation_lock(&self) -> &AsyncMutex<()> {
        &self.service_account_mutation_lock
    }

    pub(crate) fn sts_account_mutation_lock(&self, access_key: &str) -> Arc<AsyncMutex<StsMutationLockState>> {
        let mut locks = self
            .sts_account_mutation_locks
            .locks
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        if let Some(lock) = locks.get(access_key).and_then(Weak::upgrade) {
            return lock;
        }

        let registry = Arc::downgrade(&self.sts_account_mutation_locks);
        let access_key_owned = access_key.to_string();
        let lock = Arc::new_cyclic(|lock| {
            AsyncMutex::new(StsMutationLockState {
                access_key: access_key_owned,
                registry,
                lock: lock.clone(),
            })
        });
        locks.insert(access_key.to_string(), Arc::downgrade(&lock));
        lock
    }

    pub(crate) fn snapshot(&self) -> CacheSnapshot {
        self.state.load()
    }

    pub(crate) fn with_write_lock<R>(&self, f: impl FnOnce(&mut LockedCache) -> R) -> R {
        let _guard = self.write_lock.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
        let current = self.state.load_full();
        let mut locked = LockedCache {
            state: CacheState::clone(&current),
            current_ptr: Arc::as_ptr(&current),
            dirty: false,
        };
        let ret = f(&mut locked);
        if locked.dirty {
            self.state.store(Arc::new(locked.state));
        }
        ret
    }

    pub fn add_or_update_policy_doc(&self, key: &str, value: &PolicyDoc, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.add_or_update_policy_doc(key, value, t));
    }

    pub fn add_or_update_user(&self, key: &str, value: &UserIdentity, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.add_or_update_user(key, value, t));
    }

    pub fn add_or_update_user_policy(&self, key: &str, value: &MappedPolicy, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.add_or_update_user_policy(key, value, t));
    }

    pub fn add_or_update_sts_account(&self, key: &str, value: &UserIdentity, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.add_or_update_sts_account(key, value, t));
    }

    pub fn add_or_update_sts_policy(&self, key: &str, value: &MappedPolicy, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.add_or_update_sts_policy(key, value, t));
    }

    pub fn add_or_update_group(&self, key: &str, value: &GroupInfo, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.add_or_update_group(key, value, t));
    }

    pub fn add_or_update_user_group_membership(&self, key: &str, value: &HashSet<String>, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.add_or_update_user_group_membership(key, value, t));
    }

    pub fn add_or_update_group_policy(&self, key: &str, value: &MappedPolicy, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.add_or_update_group_policy(key, value, t));
    }

    pub fn delete_policy_doc(&self, key: &str, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.delete_policy_doc(key, t));
    }

    pub fn delete_user(&self, key: &str, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.delete_user(key, t));
    }

    pub fn delete_user_policy(&self, key: &str, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.delete_user_policy(key, t));
    }

    pub fn delete_sts_account(&self, key: &str, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.delete_sts_account(key, t));
    }

    pub fn delete_sts_policy(&self, key: &str, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.delete_sts_policy(key, t));
    }

    pub fn delete_group(&self, key: &str, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.delete_group(key, t));
    }

    pub fn delete_group_policy(&self, key: &str, t: OffsetDateTime) {
        self.with_write_lock(|cache| cache.delete_group_policy(key, t));
    }
}

pub(crate) struct LockedCache {
    state: CacheState,
    current_ptr: *const CacheState,
    dirty: bool,
}

impl LockedCache {
    pub(crate) fn state(&self) -> &CacheState {
        &self.state
    }

    pub(crate) fn matches_snapshot(&self, snapshot: &CacheSnapshot) -> bool {
        ptr::eq(self.current_ptr, Arc::as_ptr(snapshot))
    }

    fn exec<T: Clone>(target: &mut Arc<CacheEntity<T>>, t: OffsetDateTime, mut op: impl FnMut(&mut CacheEntity<T>)) -> bool {
        if target.load_time >= t {
            return false;
        }

        let mut new = CacheEntity::clone(target);
        op(&mut new);
        *target = Arc::new(new);
        true
    }

    fn replaced<T>(value: CacheEntity<T>) -> Arc<CacheEntity<T>> {
        Arc::new(value.update_load_time())
    }

    pub(crate) fn replace_policy_docs(&mut self, value: CacheEntity<PolicyDoc>) {
        self.state.policy_docs = Self::replaced(value);
        self.dirty = true;
    }

    pub(crate) fn replace_users(&mut self, value: CacheEntity<UserIdentity>) {
        self.state.users = Self::replaced(value);
        self.dirty = true;
    }

    pub(crate) fn replace_user_policies(&mut self, value: CacheEntity<MappedPolicy>) {
        self.state.user_policies = Self::replaced(value);
        self.dirty = true;
    }

    pub(crate) fn replace_sts_accounts(&mut self, value: CacheEntity<UserIdentity>) {
        self.state.sts_accounts = Self::replaced(value);
        self.dirty = true;
    }

    pub(crate) fn replace_sts_policies(&mut self, value: CacheEntity<MappedPolicy>) {
        self.state.sts_policies = Self::replaced(value);
        self.dirty = true;
    }

    pub(crate) fn replace_groups(&mut self, value: CacheEntity<GroupInfo>) {
        self.state.groups = Self::replaced(value);
        self.dirty = true;
    }

    pub(crate) fn replace_group_policies(&mut self, value: CacheEntity<MappedPolicy>) {
        self.state.group_policies = Self::replaced(value);
        self.dirty = true;
    }

    pub(crate) fn replace_user_group_memberships(&mut self, value: CacheEntity<HashSet<String>>) {
        self.state.user_group_memberships = Self::replaced(value);
        self.dirty = true;
    }

    pub(crate) fn add_or_update_policy_doc(&mut self, key: &str, value: &PolicyDoc, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.policy_docs, t, |map| {
            map.insert(key.to_string(), value.clone());
        });
    }

    pub(crate) fn add_or_update_user(&mut self, key: &str, value: &UserIdentity, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.users, t, |map| {
            map.insert(key.to_string(), value.clone());
        });
    }

    pub(crate) fn add_or_update_user_policy(&mut self, key: &str, value: &MappedPolicy, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.user_policies, t, |map| {
            map.insert(key.to_string(), value.clone());
        });
    }

    pub(crate) fn add_or_update_sts_account(&mut self, key: &str, value: &UserIdentity, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.sts_accounts, t, |map| {
            map.insert(key.to_string(), value.clone());
        });
    }

    pub(crate) fn add_or_update_sts_policy(&mut self, key: &str, value: &MappedPolicy, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.sts_policies, t, |map| {
            map.insert(key.to_string(), value.clone());
        });
    }

    pub(crate) fn add_or_update_group(&mut self, key: &str, value: &GroupInfo, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.groups, t, |map| {
            map.insert(key.to_string(), value.clone());
        });
    }

    pub(crate) fn add_or_update_user_group_membership(&mut self, key: &str, value: &HashSet<String>, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.user_group_memberships, t, |map| {
            map.insert(key.to_string(), value.clone());
        });
    }

    pub(crate) fn add_or_update_group_policy(&mut self, key: &str, value: &MappedPolicy, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.group_policies, t, |map| {
            map.insert(key.to_string(), value.clone());
        });
    }

    pub(crate) fn delete_policy_doc(&mut self, key: &str, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.policy_docs, t, |map| {
            map.remove(key);
        });
    }

    pub(crate) fn delete_user(&mut self, key: &str, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.users, t, |map| {
            map.remove(key);
        });
    }

    pub(crate) fn delete_user_policy(&mut self, key: &str, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.user_policies, t, |map| {
            map.remove(key);
        });
    }

    pub(crate) fn delete_user_group_membership(&mut self, key: &str, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.user_group_memberships, t, |map| {
            map.remove(key);
        });
    }

    pub(crate) fn delete_sts_account(&mut self, key: &str, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.sts_accounts, t, |map| {
            map.remove(key);
        });
    }

    pub(crate) fn delete_sts_policy(&mut self, key: &str, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.sts_policies, t, |map| {
            map.remove(key);
        });
    }

    pub(crate) fn delete_group(&mut self, key: &str, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.groups, t, |map| {
            map.remove(key);
        });
    }

    pub(crate) fn delete_group_policy(&mut self, key: &str, t: OffsetDateTime) {
        self.dirty |= Self::exec(&mut self.state.group_policies, t, |map| {
            map.remove(key);
        });
    }

    pub(crate) fn build_user_group_memberships(&mut self) {
        let groups = Arc::clone(&self.state.groups);
        let mut user_group_memberships = HashMap::new();
        for (group_name, group) in groups.iter() {
            for user_name in &group.members {
                user_group_memberships
                    .entry(user_name.clone())
                    .or_insert_with(HashSet::new)
                    .insert(group_name.clone());
            }
        }
        self.replace_user_group_memberships(CacheEntity::new(user_group_memberships));
    }
}

impl CacheInner {
    #[inline]
    pub fn get_user(&self, user_name: &str) -> Option<&UserIdentity> {
        self.snapshot
            .users
            .get(user_name)
            .or_else(|| self.snapshot.sts_accounts.get(user_name))
    }

    // todo
    pub fn is_allowed_sts(&self, _args: &Args, _parent: &str) -> bool {
        warn!("policy cache STS check path is not implemented");
        false
    }

    // todo
    pub fn is_allowed_service_account(&self, _args: &Args, _parent: &str) -> bool {
        warn!("policy cache service account check path is not implemented");
        false
    }

    pub fn is_allowed(&self, _args: Args) -> bool {
        warn!("policy cache is_allowed check path is currently denied by default");
        false
    }

    pub fn policy_db_get(&self, _name: &str, _groups: &[String]) -> Vec<String> {
        warn!("policy cache policy_db_get is not implemented, returning empty policy set");
        vec![]
    }
}

#[derive(Clone)]
pub struct CacheEntity<T> {
    map: HashMap<String, T>,
    /// The time of the reload
    load_time: OffsetDateTime,
}

impl<T> Deref for CacheEntity<T> {
    type Target = HashMap<String, T>;
    fn deref(&self) -> &Self::Target {
        &self.map
    }
}

impl<T> DerefMut for CacheEntity<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.map
    }
}

impl<T> CacheEntity<T> {
    pub fn new(map: HashMap<String, T>) -> Self {
        Self {
            map,
            load_time: OffsetDateTime::UNIX_EPOCH,
        }
    }
}

impl<T> Default for CacheEntity<T> {
    fn default() -> Self {
        Self {
            map: HashMap::new(),
            load_time: OffsetDateTime::UNIX_EPOCH,
        }
    }
}

impl<T> CacheEntity<T> {
    pub fn update_load_time(mut self) -> Self {
        self.load_time = OffsetDateTime::now_utc();
        self
    }
}

pub struct CacheInner {
    snapshot: CacheSnapshot,
}

impl From<&Cache> for CacheInner {
    fn from(value: &Cache) -> Self {
        Self {
            snapshot: value.snapshot(),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use futures::future::join_all;
    use rustfs_policy::auth::UserIdentity;
    use time::OffsetDateTime;

    use crate::cache::Cache;
    use crate::store::MappedPolicy;

    #[test]
    fn sts_mutation_locks_are_keyed_and_prune_unused_entries() {
        let cache = Cache::default();
        let first = cache.sts_account_mutation_lock("first");
        let same = cache.sts_account_mutation_lock("first");
        let different = cache.sts_account_mutation_lock("different");

        assert!(Arc::ptr_eq(&first, &same));
        assert!(!Arc::ptr_eq(&first, &different));
        drop(first);
        drop(same);
        drop(different);

        let _next = cache.sts_account_mutation_lock("next");
        let locks = cache
            .sts_account_mutation_locks
            .locks
            .lock()
            .expect("STS mutation lock registry mutex poisoned");
        assert!(!locks.contains_key("first"));
        assert!(!locks.contains_key("different"));
        assert!(locks.contains_key("next"));
    }

    #[tokio::test]
    async fn test_cache_entity_add() {
        let owner = Arc::new(Cache::default());

        let mut f = vec![];

        for (index, key) in (0..100).map(|x| x.to_string()).enumerate() {
            let owner = Arc::clone(&owner);
            f.push(async move {
                let user = UserIdentity {
                    version: index as i64,
                    ..Default::default()
                };
                owner.add_or_update_user(&key, &user, OffsetDateTime::now_utc());
            });
        }
        join_all(f).await;

        let cache = owner.snapshot();
        for (index, key) in (0..100).map(|x| x.to_string()).enumerate() {
            assert_eq!(cache.users.get(&key).map(|user| user.version), Some(index as i64));
        }
    }

    #[tokio::test]
    async fn test_cache_entity_update() {
        let owner = Arc::new(Cache::default());

        let mut f = vec![];

        for (index, key) in (0..100).map(|x| x.to_string()).enumerate() {
            let owner = Arc::clone(&owner);
            f.push(async move {
                let user = UserIdentity {
                    version: index as i64,
                    ..Default::default()
                };
                owner.add_or_update_user(&key, &user, OffsetDateTime::now_utc());
            });
        }
        join_all(f).await;

        let cache_load = owner.snapshot();
        for (index, key) in (0..100).map(|x| x.to_string()).enumerate() {
            assert_eq!(cache_load.users.get(&key).map(|user| user.version), Some(index as i64));
        }

        let mut f = vec![];

        for (index, key) in (0..100).map(|x| x.to_string()).enumerate() {
            let owner = Arc::clone(&owner);
            f.push(async move {
                let user = UserIdentity {
                    version: (index * 1000) as i64,
                    ..Default::default()
                };
                owner.add_or_update_user(&key, &user, OffsetDateTime::now_utc());
            });
        }
        join_all(f).await;

        let cache_load = owner.snapshot();
        for (index, key) in (0..100).map(|x| x.to_string()).enumerate() {
            assert_eq!(cache_load.users.get(&key).map(|user| user.version), Some((index * 1000) as i64));
        }
    }

    #[tokio::test]
    async fn test_cache_entity_delete() {
        let owner = Arc::new(Cache::default());

        let mut f = vec![];

        for (index, key) in (0..100).map(|x| x.to_string()).enumerate() {
            let owner = Arc::clone(&owner);
            f.push(async move {
                let user = UserIdentity {
                    version: index as i64,
                    ..Default::default()
                };
                owner.add_or_update_user(&key, &user, OffsetDateTime::now_utc());
            });
        }
        join_all(f).await;

        let cache_load = owner.snapshot();
        for (index, key) in (0..100).map(|x| x.to_string()).enumerate() {
            assert_eq!(cache_load.users.get(&key).map(|user| user.version), Some(index as i64));
        }
        drop(cache_load);

        let mut f = vec![];

        for key in (0..100).map(|x| x.to_string()) {
            let owner = Arc::clone(&owner);
            f.push(async move {
                owner.delete_user(&key, OffsetDateTime::now_utc());
            });
        }
        join_all(f).await;

        let cache_load = owner.snapshot();
        assert!(cache_load.users.is_empty());
    }

    #[tokio::test]
    async fn test_cache_snapshot_reads_one_published_state() {
        let cache = Cache::default();
        let before = cache.snapshot();
        let user = UserIdentity {
            version: 7,
            ..Default::default()
        };
        let policy = MappedPolicy::new("readwrite");

        cache.with_write_lock(|cache| {
            let now = OffsetDateTime::now_utc();
            cache.add_or_update_user("snapshot-user", &user, now);
            cache.add_or_update_user_policy("snapshot-user", &policy, now);
        });

        assert!(!before.users.contains_key("snapshot-user"));
        assert!(!before.user_policies.contains_key("snapshot-user"));

        let after = cache.snapshot();
        assert!(after.users.contains_key("snapshot-user"));
        assert!(after.user_policies.contains_key("snapshot-user"));
    }
}