commy 0.4.1

A hierarchical, multi-tenant shared memory coordination system for Windows enabling secure, efficient data sharing between multiple processes via WebSocket and direct memory-mapping
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
//! Permission management for multi-tenant access control
//! 
//! This module defines the permission model for COMMY:
//! - Per-(Client, Tenant) permission tracking
//! - Fine-grained resource-level permissions
//! - Permission inheritance and delegation

use serde::{Deserialize, Serialize};
use std::collections::HashSet;

/// A granular permission in COMMY
/// 
/// Permissions are hierarchical and resource-specific
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum Permission {
    /// Tenant-level permissions
    TenantAdmin,
    TenantRead,
    TenantWrite,
    
    /// Service-level permissions
    ServiceCreate,
    ServiceDelete,
    ServiceAdmin,
    ServiceRead,
    ServiceWrite,
    
    /// Variable-level permissions
    VariableRead,
    VariableWrite,
    VariableDelete,
    
    /// Client management permissions
    ClientKick,
    ClientPermissionGrant,
    ClientPermissionRevoke,
    
    /// Custom permission
    Custom(String),
}

/// A set of permissions for a client within a tenant
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct PermissionSet {
    permissions: HashSet<Permission>,
}

impl PermissionSet {
    /// Create a new empty permission set
    pub fn new() -> Self {
        Self {
            permissions: HashSet::new(),
        }
    }

    /// Create a permission set with read-only access
    pub fn read_only() -> Self {
        let mut set = Self::new();
        set.grant(Permission::TenantRead);
        set.grant(Permission::ServiceRead);
        set.grant(Permission::VariableRead);
        set
    }

    /// Create a permission set with read-write access
    pub fn read_write() -> Self {
        let mut set = Self::read_only();
        set.grant(Permission::TenantWrite);
        set.grant(Permission::ServiceWrite);
        set.grant(Permission::VariableWrite);
        set
    }

    /// Create a permission set with full admin access
    pub fn admin() -> Self {
        let mut set = Self::new();
        set.grant(Permission::TenantAdmin);
        set.grant(Permission::ServiceAdmin);
        set.grant(Permission::ClientKick);
        set.grant(Permission::ClientPermissionGrant);
        set.grant(Permission::ClientPermissionRevoke);
        set
    }

    /// Grant a permission
    pub fn grant(&mut self, permission: Permission) {
        self.permissions.insert(permission);
    }

    /// Revoke a permission
    pub fn revoke(&mut self, permission: &Permission) {
        self.permissions.remove(permission);
    }

    /// Check if a permission is granted
    pub fn has_permission(&self, permission: &Permission) -> bool {
        self.permissions.contains(permission)
    }

    /// Check if any of multiple permissions are granted
    pub fn has_any(&self, permissions: &[Permission]) -> bool {
        permissions.iter().any(|p| self.has_permission(p))
    }

    /// Check if all of multiple permissions are granted
    pub fn has_all(&self, permissions: &[Permission]) -> bool {
        permissions.iter().all(|p| self.has_permission(p))
    }

    /// Get all permissions
    pub fn all_permissions(&self) -> Vec<Permission> {
        self.permissions.iter().cloned().collect()
    }

    /// Check if this permission set has admin privileges
    pub fn is_admin(&self) -> bool {
        self.has_permission(&Permission::TenantAdmin)
    }
    
    /// Check if permission set is empty
    pub fn is_empty(&self) -> bool {
        self.permissions.is_empty()
    }
    
    /// Compute intersection of two permission sets
    /// Returns a new set containing only permissions present in both
    pub fn intersection(&self, other: &PermissionSet) -> PermissionSet {
        let intersected: std::collections::HashSet<_> = self.permissions
            .intersection(&other.permissions)
            .cloned()
            .collect();
        
        PermissionSet {
            permissions: intersected,
        }
    }
    
    /// Compute union of two permission sets
    /// Returns a new set containing permissions from either set
    pub fn union(&self, other: &PermissionSet) -> PermissionSet {
        let unioned: std::collections::HashSet<_> = self.permissions
            .union(&other.permissions)
            .cloned()
            .collect();
        
        PermissionSet {
            permissions: unioned,
        }
    }
    
    /// Create a PermissionSet from auth-framework scopes
    pub fn from_scopes(scopes: &[String]) -> Self {
        let mut set = Self::new();
        
        for scope in scopes {
            match scope.as_str() {
                "read" => {
                    set.grant(Permission::TenantRead);
                    set.grant(Permission::ServiceRead);
                    set.grant(Permission::VariableRead);
                }
                "write" => {
                    set.grant(Permission::TenantWrite);
                    set.grant(Permission::ServiceWrite);
                    set.grant(Permission::VariableWrite);
                }
                "admin" => {
                    set.grant(Permission::TenantAdmin);
                    set.grant(Permission::ServiceAdmin);
                    set.grant(Permission::ClientKick);
                    set.grant(Permission::ClientPermissionGrant);
                    set.grant(Permission::ClientPermissionRevoke);
                }
                "tenant:read" => set.grant(Permission::TenantRead),
                "tenant:write" => set.grant(Permission::TenantWrite),
                "tenant:admin" => set.grant(Permission::TenantAdmin),
                "service:create" => set.grant(Permission::ServiceCreate),
                "service:delete" => set.grant(Permission::ServiceDelete),
                "service:admin" => set.grant(Permission::ServiceAdmin),
                "service:read" => set.grant(Permission::ServiceRead),
                "service:write" => set.grant(Permission::ServiceWrite),
                "variable:read" => set.grant(Permission::VariableRead),
                "variable:write" => set.grant(Permission::VariableWrite),
                "variable:delete" => set.grant(Permission::VariableDelete),
                "client:kick" => set.grant(Permission::ClientKick),
                "client:grant" => set.grant(Permission::ClientPermissionGrant),
                "client:revoke" => set.grant(Permission::ClientPermissionRevoke),
                custom => set.grant(Permission::Custom(custom.to_string())),
            }
        }
        
        set
    }
}

/// Per-(Client, Tenant) permission record
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ClientTenantPermissions {
    /// Client ID
    pub client_id: String,
    
    /// Tenant ID
    pub tenant_id: String,
    
    /// Permission set for this client in this tenant
    pub permissions: PermissionSet,
    
    /// When these permissions were granted
    pub granted_at: String,
    
    /// When these permissions expire (if any)
    pub expires_at: Option<String>,
}

impl ClientTenantPermissions {
    /// Create new client-tenant permissions
    pub fn new(client_id: String, tenant_id: String, permissions: PermissionSet) -> Self {
        Self {
            client_id,
            tenant_id,
            permissions,
            granted_at: chrono::Utc::now().to_rfc3339(),
            expires_at: None,
        }
    }

    /// Check if permissions have expired
    pub fn is_expired(&self) -> bool {
        if let Some(expires) = &self.expires_at {
            if let Ok(expiry) = chrono::DateTime::parse_from_rfc3339(expires) {
                return chrono::Utc::now() > expiry.with_timezone(&chrono::Utc);
            }
        }
        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_permission_set_hierarchy() {
        let mut perms = PermissionSet::new();
        
        // Initially empty
        assert!(!perms.has_permission(&Permission::TenantRead));
        
        // Grant permission
        perms.grant(Permission::TenantRead);
        assert!(perms.has_permission(&Permission::TenantRead));
        
        // Revoke permission
        perms.revoke(&Permission::TenantRead);
        assert!(!perms.has_permission(&Permission::TenantRead));
    }

    #[test]
    fn test_predefined_permission_sets() {
        let read_only = PermissionSet::read_only();
        assert!(read_only.has_permission(&Permission::TenantRead));
        assert!(!read_only.has_permission(&Permission::TenantWrite));
        
        let read_write = PermissionSet::read_write();
        assert!(read_write.has_permission(&Permission::TenantRead));
        assert!(read_write.has_permission(&Permission::TenantWrite));
        
        let admin = PermissionSet::admin();
        assert!(admin.is_admin());
        assert!(admin.has_permission(&Permission::ClientKick));
    }

    #[test]
    fn test_has_any_has_all() {
        let mut perms = PermissionSet::new();
        perms.grant(Permission::TenantRead);
        perms.grant(Permission::VariableRead);
        
        assert!(perms.has_any(&[Permission::TenantRead, Permission::TenantWrite]));
        assert!(!perms.has_all(&[Permission::TenantRead, Permission::TenantWrite]));
    }

    #[test]
    fn test_is_empty_and_all_permissions() {
        let empty = PermissionSet::new();
        assert!(empty.is_empty());
        assert!(empty.all_permissions().is_empty());

        let rw = PermissionSet::read_write();
        assert!(!rw.is_empty());
        assert!(!rw.all_permissions().is_empty());
    }

    #[test]
    fn test_is_admin() {
        let admin = PermissionSet::admin();
        assert!(admin.is_admin());

        let read_only = PermissionSet::read_only();
        assert!(!read_only.is_admin());
    }

    #[test]
    fn test_intersection() {
        let mut a = PermissionSet::new();
        a.grant(Permission::TenantRead);
        a.grant(Permission::VariableRead);

        let mut b = PermissionSet::new();
        b.grant(Permission::TenantRead);
        b.grant(Permission::ServiceWrite);

        let intersect = a.intersection(&b);
        assert!(intersect.has_permission(&Permission::TenantRead));
        assert!(!intersect.has_permission(&Permission::VariableRead));
        assert!(!intersect.has_permission(&Permission::ServiceWrite));
    }

    #[test]
    fn test_union() {
        let mut a = PermissionSet::new();
        a.grant(Permission::TenantRead);

        let mut b = PermissionSet::new();
        b.grant(Permission::ServiceWrite);

        let union = a.union(&b);
        assert!(union.has_permission(&Permission::TenantRead));
        assert!(union.has_permission(&Permission::ServiceWrite));
    }

    #[test]
    fn test_from_scopes_read() {
        let scopes = vec!["read".to_string()];
        let perms = PermissionSet::from_scopes(&scopes);
        assert!(perms.has_permission(&Permission::TenantRead));
        assert!(perms.has_permission(&Permission::ServiceRead));
        assert!(perms.has_permission(&Permission::VariableRead));
        assert!(!perms.has_permission(&Permission::TenantWrite));
    }

    #[test]
    fn test_from_scopes_write() {
        let scopes = vec!["write".to_string()];
        let perms = PermissionSet::from_scopes(&scopes);
        assert!(perms.has_permission(&Permission::TenantWrite));
        assert!(perms.has_permission(&Permission::VariableWrite));
    }

    #[test]
    fn test_from_scopes_admin() {
        let scopes = vec!["admin".to_string()];
        let perms = PermissionSet::from_scopes(&scopes);
        assert!(perms.is_admin());
        assert!(perms.has_permission(&Permission::ClientKick));
    }

    #[test]
    fn test_from_scopes_granular() {
        let scopes = vec![
            "service:create".to_string(),
            "variable:delete".to_string(),
            "client:kick".to_string(),
        ];
        let perms = PermissionSet::from_scopes(&scopes);
        assert!(perms.has_permission(&Permission::ServiceCreate));
        assert!(perms.has_permission(&Permission::VariableDelete));
        assert!(perms.has_permission(&Permission::ClientKick));
        assert!(!perms.has_permission(&Permission::TenantRead));
    }

    #[test]
    fn test_from_scopes_custom() {
        let scopes = vec!["my_custom_scope".to_string()];
        let perms = PermissionSet::from_scopes(&scopes);
        assert!(perms.has_permission(&Permission::Custom("my_custom_scope".to_string())));
    }

    #[test]
    fn test_client_tenant_permissions_is_expired_false_when_no_expiry() {
        let perms = PermissionSet::read_only();
        let ctp = ClientTenantPermissions::new(
            "client1".to_string(),
            "tenant1".to_string(),
            perms,
        );
        assert!(!ctp.is_expired());
    }

    #[test]
    fn test_client_tenant_permissions_is_expired_true_for_past_date() {
        let perms = PermissionSet::new();
        let mut ctp = ClientTenantPermissions::new(
            "c".to_string(),
            "t".to_string(),
            perms,
        );
        // Set expiry to a past date
        ctp.expires_at = Some("2000-01-01T00:00:00+00:00".to_string());
        assert!(ctp.is_expired());
    }

    #[test]
    fn test_client_tenant_permissions_new() {
        let perms = PermissionSet::read_only();
        let ctp = ClientTenantPermissions::new(
            "client_a".to_string(),
            "tenant_b".to_string(),
            perms,
        );
        assert_eq!(ctp.client_id, "client_a");
        assert_eq!(ctp.tenant_id, "tenant_b");
        assert!(ctp.permissions.has_permission(&Permission::TenantRead));
        assert!(ctp.expires_at.is_none());
        assert!(!ctp.is_expired());
    }

    #[test]
    fn test_from_scopes_write_does_not_grant_tenant_read() {
        let scopes = vec!["write".to_string()];
        let perms = PermissionSet::from_scopes(&scopes);
        assert!(
            !perms.has_permission(&Permission::TenantRead),
            "write scope must NOT implicitly grant TenantRead"
        );
        assert!(
            !perms.has_permission(&Permission::ServiceRead),
            "write scope must NOT implicitly grant ServiceRead"
        );
        assert!(
            !perms.has_permission(&Permission::VariableRead),
            "write scope must NOT implicitly grant VariableRead"
        );
        // Write grants ARE present
        assert!(perms.has_permission(&Permission::TenantWrite));
        assert!(perms.has_permission(&Permission::VariableWrite));
    }

    #[test]
    fn test_is_expired_with_unparseable_date_returns_false() {
        let mut ctp = ClientTenantPermissions::new(
            "c".to_string(),
            "t".to_string(),
            PermissionSet::new(),
        );
        ctp.expires_at = Some("not-a-valid-date".to_string());
        assert!(
            !ctp.is_expired(),
            "Unparseable expiry date must NOT be treated as expired"
        );
    }

    #[test]
    fn test_revoke_never_granted_permission_no_panic() {
        let mut perms = PermissionSet::new();
        // Revoking a permission that was never granted must not panic
        perms.revoke(&Permission::TenantAdmin);
        perms.revoke(&Permission::ServiceCreate);
        perms.revoke(&Permission::Custom("nonexistent_scope".to_string()));
        // No panic = test passes; verify state is still empty
        assert!(perms.is_empty());
    }
}