amaters-server 0.2.1

AmateRS server binary
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//! Authorization module
//!
//! This module provides authorization services:
//! - Role-based access control (RBAC)
//! - Collection-level permissions
//! - Operation-level permissions (read/write/admin)
//! - Policy enforcement
//!
//! Security model:
//! - Deny by default (secure)
//! - Explicit permissions required
//! - Supports hierarchical roles

use crate::auth::Principal;
use crate::config::AuthorizationSettings;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::Path;
use std::sync::Arc;
use thiserror::Error;
use tracing::{debug, info};

/// Authorization errors
#[derive(Error, Debug)]
pub enum AuthzError {
    #[error("Permission denied: {0}")]
    PermissionDenied(String),

    #[error("Role not found: {0}")]
    RoleNotFound(String),

    #[error("Invalid permission: {0}")]
    InvalidPermission(String),

    #[error("Configuration error: {0}")]
    ConfigError(String),

    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),

    #[error("TOML error: {0}")]
    Toml(#[from] toml::de::Error),
}

pub type AuthzResult<T> = Result<T, AuthzError>;

/// Permission levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Hash)]
pub enum Permission {
    /// Read access
    Read,
    /// Write access (includes read)
    Write,
    /// Admin access (includes read and write)
    Admin,
}

impl Permission {
    /// Check if this permission includes another permission
    pub fn includes(&self, other: Permission) -> bool {
        matches!(
            (self, other),
            (Permission::Admin, _)
                | (Permission::Write, Permission::Read)
                | (Permission::Write, Permission::Write)
                | (Permission::Read, Permission::Read)
        )
    }

    /// Parse permission from string
    pub fn parse(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "read" => Some(Permission::Read),
            "write" => Some(Permission::Write),
            "admin" => Some(Permission::Admin),
            _ => None,
        }
    }
}

impl std::str::FromStr for Permission {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Permission::parse(s).ok_or(())
    }
}

impl std::fmt::Display for Permission {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Permission::Read => write!(f, "read"),
            Permission::Write => write!(f, "write"),
            Permission::Admin => write!(f, "admin"),
        }
    }
}

/// Authorization action
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Action {
    /// Read from collection
    Read,
    /// Write to collection
    Write,
    /// Delete from collection
    Delete,
    /// Create collection
    CreateCollection,
    /// Drop collection
    DropCollection,
    /// List collections
    ListCollections,
    /// Administrative operation
    Admin,
}

impl Action {
    /// Get the minimum permission level required for this action
    pub fn required_permission(&self) -> Permission {
        match self {
            Action::Read | Action::ListCollections => Permission::Read,
            Action::Write | Action::Delete => Permission::Write,
            Action::CreateCollection | Action::DropCollection | Action::Admin => Permission::Admin,
        }
    }
}

/// Resource being accessed
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Resource {
    /// Specific collection
    Collection(String),
    /// All collections
    AllCollections,
    /// Server administration
    Server,
}

/// Role definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Role {
    /// Role name
    pub name: String,
    /// Role description
    pub description: String,
    /// Permissions for this role
    pub permissions: Vec<PermissionRule>,
    /// Roles that this role inherits from
    #[serde(default)]
    pub inherits: Vec<String>,
}

/// Permission rule
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PermissionRule {
    /// Resource pattern (e.g., "collection:users", "collection:*", "server")
    pub resource: String,
    /// Actions allowed (read, write, admin)
    pub actions: Vec<String>,
}

/// Policy file format
#[derive(Debug, Serialize, Deserialize)]
struct PolicyFile {
    /// Role definitions
    roles: Vec<Role>,
}

/// Authorization service
pub struct Authorizer {
    config: Arc<AuthorizationSettings>,
    roles: HashMap<String, Role>,
    default_role: String,
    deny_by_default: bool,
}

impl Authorizer {
    /// Create a new authorizer
    pub fn new(config: AuthorizationSettings) -> AuthzResult<Self> {
        let config = Arc::new(config);

        // Load roles
        let roles = if let Some(ref roles_file) = config.roles_file {
            Self::load_roles(roles_file)?
        } else {
            Self::default_roles()
        };

        let deny_by_default = config.default_mode == "deny-by-default";

        Ok(Self {
            default_role: config.default_role.clone(),
            config,
            roles,
            deny_by_default,
        })
    }

    /// Load roles from file
    fn load_roles(path: &Path) -> AuthzResult<HashMap<String, Role>> {
        if !path.exists() {
            return Err(AuthzError::ConfigError(format!(
                "Roles file does not exist: {}",
                path.display()
            )));
        }

        let contents = fs::read_to_string(path)?;

        // Try JSON first, then TOML
        let policy: PolicyFile = if path.extension().and_then(|s| s.to_str()) == Some("json") {
            serde_json::from_str(&contents)?
        } else {
            toml::from_str(&contents)?
        };

        let mut roles = HashMap::new();
        for role in policy.roles {
            roles.insert(role.name.clone(), role);
        }

        info!("Loaded {} roles from {}", roles.len(), path.display());
        Ok(roles)
    }

    /// Get default built-in roles
    fn default_roles() -> HashMap<String, Role> {
        let mut roles = HashMap::new();

        // Admin role - full access
        roles.insert(
            "admin".to_string(),
            Role {
                name: "admin".to_string(),
                description: "Administrator with full access".to_string(),
                permissions: vec![PermissionRule {
                    resource: "*".to_string(),
                    actions: vec!["admin".to_string()],
                }],
                inherits: Vec::new(),
            },
        );

        // User role - read/write to own collections
        roles.insert(
            "user".to_string(),
            Role {
                name: "user".to_string(),
                description: "Regular user with read/write access".to_string(),
                permissions: vec![PermissionRule {
                    resource: "collection:*".to_string(),
                    actions: vec!["read".to_string(), "write".to_string()],
                }],
                inherits: Vec::new(),
            },
        );

        // Reader role - read-only access
        roles.insert(
            "reader".to_string(),
            Role {
                name: "reader".to_string(),
                description: "Read-only user".to_string(),
                permissions: vec![PermissionRule {
                    resource: "collection:*".to_string(),
                    actions: vec!["read".to_string()],
                }],
                inherits: Vec::new(),
            },
        );

        info!("Using default built-in roles");
        roles
    }

    /// Check if a principal is authorized to perform an action on a resource
    pub fn authorize(
        &self,
        principal: &Principal,
        action: &Action,
        resource: &Resource,
    ) -> AuthzResult<()> {
        if !self.config.enabled {
            // Authorization disabled - allow all
            return Ok(());
        }

        // Get the user's role
        let role_name = principal
            .get_attribute("role")
            .map(|s| s.as_str())
            .unwrap_or(&self.default_role);

        // Check if user has permission
        if self.has_permission(role_name, action, resource)? {
            debug!(
                "Authorized: user={} role={} action={:?} resource={:?}",
                principal.name, role_name, action, resource
            );
            Ok(())
        } else {
            Err(AuthzError::PermissionDenied(format!(
                "User '{}' with role '{}' not authorized to {:?} on {:?}",
                principal.name, role_name, action, resource
            )))
        }
    }

    /// Check if a role has permission to perform an action on a resource
    fn has_permission(
        &self,
        role_name: &str,
        action: &Action,
        resource: &Resource,
    ) -> AuthzResult<bool> {
        let role = self
            .roles
            .get(role_name)
            .ok_or_else(|| AuthzError::RoleNotFound(role_name.to_string()))?;

        // Get all permissions (including inherited)
        let permissions = self.collect_permissions(role)?;

        // Check if any permission matches
        let required_permission = action.required_permission();

        for rule in &permissions {
            if self.matches_resource(&rule.resource, resource) {
                // Check if rule grants required permission
                for action_str in &rule.actions {
                    if let Some(granted_permission) = Permission::parse(action_str) {
                        if granted_permission.includes(required_permission) {
                            return Ok(true);
                        }
                    }
                }
            }
        }

        // No matching permission found
        Ok(!self.deny_by_default)
    }

    /// Collect all permissions for a role (including inherited)
    fn collect_permissions(&self, role: &Role) -> AuthzResult<Vec<PermissionRule>> {
        let mut permissions = role.permissions.clone();
        let mut visited = HashSet::new();
        visited.insert(role.name.clone());

        // Recursively collect inherited permissions
        for parent_name in &role.inherits {
            if visited.contains(parent_name) {
                // Circular inheritance detected
                continue;
            }

            let parent = self.roles.get(parent_name).ok_or_else(|| {
                AuthzError::ConfigError(format!("Parent role '{}' not found", parent_name))
            })?;

            let parent_permissions = self.collect_permissions(parent)?;
            permissions.extend(parent_permissions);
            visited.insert(parent_name.clone());
        }

        Ok(permissions)
    }

    /// Check if a resource pattern matches a specific resource
    fn matches_resource(&self, pattern: &str, resource: &Resource) -> bool {
        match (pattern, resource) {
            // Wildcard matches everything
            ("*", _) => true,

            // Collection patterns
            ("collection:*", Resource::Collection(_)) => true,
            ("collection:*", Resource::AllCollections) => true,

            // Specific collection
            (p, Resource::Collection(name)) if p.starts_with("collection:") => {
                let pattern_name = &p["collection:".len()..];
                pattern_name == name || pattern_name == "*"
            }

            // Server pattern
            ("server", Resource::Server) => true,

            _ => false,
        }
    }

    /// Get role information
    pub fn get_role(&self, role_name: &str) -> Option<&Role> {
        self.roles.get(role_name)
    }

    /// List all available roles
    pub fn list_roles(&self) -> Vec<&Role> {
        self.roles.values().collect()
    }

    /// Check if authorization is enabled
    pub fn is_enabled(&self) -> bool {
        self.config.enabled
    }
}

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

    #[test]
    fn test_permission_includes() {
        assert!(Permission::Admin.includes(Permission::Read));
        assert!(Permission::Admin.includes(Permission::Write));
        assert!(Permission::Admin.includes(Permission::Admin));
        assert!(Permission::Write.includes(Permission::Read));
        assert!(Permission::Write.includes(Permission::Write));
        assert!(!Permission::Write.includes(Permission::Admin));
        assert!(Permission::Read.includes(Permission::Read));
        assert!(!Permission::Read.includes(Permission::Write));
        assert!(!Permission::Read.includes(Permission::Admin));
    }

    #[test]
    fn test_permission_from_str() {
        assert_eq!(Permission::parse("read"), Some(Permission::Read));
        assert_eq!(Permission::parse("write"), Some(Permission::Write));
        assert_eq!(Permission::parse("admin"), Some(Permission::Admin));
        assert_eq!(Permission::parse("invalid"), None);
    }

    #[test]
    fn test_action_required_permission() {
        assert_eq!(Action::Read.required_permission(), Permission::Read);
        assert_eq!(Action::Write.required_permission(), Permission::Write);
        assert_eq!(Action::Delete.required_permission(), Permission::Write);
        assert_eq!(
            Action::CreateCollection.required_permission(),
            Permission::Admin
        );
        assert_eq!(Action::Admin.required_permission(), Permission::Admin);
    }

    #[test]
    fn test_authorizer_creation() {
        let config = AuthorizationSettings {
            enabled: true,
            default_role: "user".to_string(),
            roles_file: None,
            policies_file: None,
            collection_permissions: true,
            default_mode: "deny-by-default".to_string(),
            audit_enabled: true,
            audit_log_path: None,
        };

        let authz = Authorizer::new(config).expect("Failed to create authorizer");
        assert!(authz.is_enabled());
        assert_eq!(authz.list_roles().len(), 3); // admin, user, reader
    }

    #[test]
    fn test_admin_role_authorization() {
        let config = AuthorizationSettings {
            enabled: true,
            default_role: "user".to_string(),
            roles_file: None,
            policies_file: None,
            collection_permissions: true,
            default_mode: "deny-by-default".to_string(),
            audit_enabled: true,
            audit_log_path: None,
        };

        let authz = Authorizer::new(config).expect("Failed to create authorizer");

        let principal = Principal::new(
            "admin1".to_string(),
            "Admin User".to_string(),
            AuthMethod::Jwt,
        )
        .with_attribute("role".to_string(), "admin".to_string());

        // Admin should have access to everything
        assert!(
            authz
                .authorize(
                    &principal,
                    &Action::Read,
                    &Resource::Collection("test".to_string())
                )
                .is_ok()
        );

        assert!(
            authz
                .authorize(
                    &principal,
                    &Action::Write,
                    &Resource::Collection("test".to_string())
                )
                .is_ok()
        );

        assert!(
            authz
                .authorize(&principal, &Action::CreateCollection, &Resource::Server)
                .is_ok()
        );
    }

    #[test]
    fn test_user_role_authorization() {
        let config = AuthorizationSettings {
            enabled: true,
            default_role: "user".to_string(),
            roles_file: None,
            policies_file: None,
            collection_permissions: true,
            default_mode: "deny-by-default".to_string(),
            audit_enabled: true,
            audit_log_path: None,
        };

        let authz = Authorizer::new(config).expect("Failed to create authorizer");

        let principal = Principal::new(
            "user1".to_string(),
            "Regular User".to_string(),
            AuthMethod::Jwt,
        )
        .with_attribute("role".to_string(), "user".to_string());

        // User should have read/write access to collections
        assert!(
            authz
                .authorize(
                    &principal,
                    &Action::Read,
                    &Resource::Collection("test".to_string())
                )
                .is_ok()
        );

        assert!(
            authz
                .authorize(
                    &principal,
                    &Action::Write,
                    &Resource::Collection("test".to_string())
                )
                .is_ok()
        );

        // User should NOT have admin access
        assert!(
            authz
                .authorize(&principal, &Action::CreateCollection, &Resource::Server)
                .is_err()
        );
    }

    #[test]
    fn test_reader_role_authorization() {
        let config = AuthorizationSettings {
            enabled: true,
            default_role: "user".to_string(),
            roles_file: None,
            policies_file: None,
            collection_permissions: true,
            default_mode: "deny-by-default".to_string(),
            audit_enabled: true,
            audit_log_path: None,
        };

        let authz = Authorizer::new(config).expect("Failed to create authorizer");

        let principal = Principal::new(
            "reader1".to_string(),
            "Read User".to_string(),
            AuthMethod::Jwt,
        )
        .with_attribute("role".to_string(), "reader".to_string());

        // Reader should have read access
        assert!(
            authz
                .authorize(
                    &principal,
                    &Action::Read,
                    &Resource::Collection("test".to_string())
                )
                .is_ok()
        );

        // Reader should NOT have write access
        assert!(
            authz
                .authorize(
                    &principal,
                    &Action::Write,
                    &Resource::Collection("test".to_string())
                )
                .is_err()
        );
    }

    #[test]
    fn test_authorization_disabled() {
        let config = AuthorizationSettings {
            enabled: false,
            default_role: "user".to_string(),
            roles_file: None,
            policies_file: None,
            collection_permissions: true,
            default_mode: "deny-by-default".to_string(),
            audit_enabled: true,
            audit_log_path: None,
        };

        let authz = Authorizer::new(config).expect("Failed to create authorizer");

        let principal = Principal::new(
            "user1".to_string(),
            "Test User".to_string(),
            AuthMethod::Jwt,
        );

        // When disabled, all operations should be allowed
        assert!(
            authz
                .authorize(&principal, &Action::Admin, &Resource::Server)
                .is_ok()
        );
    }

    #[test]
    fn test_resource_matching() {
        let config = AuthorizationSettings {
            enabled: true,
            default_role: "user".to_string(),
            roles_file: None,
            policies_file: None,
            collection_permissions: true,
            default_mode: "deny-by-default".to_string(),
            audit_enabled: true,
            audit_log_path: None,
        };

        let authz = Authorizer::new(config).expect("Failed to create authorizer");

        // Test wildcard pattern
        assert!(authz.matches_resource("*", &Resource::Collection("test".to_string())));
        assert!(authz.matches_resource("*", &Resource::Server));

        // Test collection pattern
        assert!(authz.matches_resource("collection:*", &Resource::Collection("test".to_string())));
        assert!(!authz.matches_resource("collection:*", &Resource::Server));

        // Test specific collection
        assert!(
            authz.matches_resource("collection:test", &Resource::Collection("test".to_string()))
        );
        assert!(!authz.matches_resource(
            "collection:test",
            &Resource::Collection("other".to_string())
        ));
    }
}