rustberg 0.0.2

A production-grade, cross-platform, single-binary Apache Iceberg REST Catalog
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
//! Authorization system for fine-grained access control.
//!
//! This module provides a policy-based authorization system supporting
//! both Role-Based Access Control (RBAC) and Attribute-Based Access Control (ABAC).

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

use super::error::{AuthError, Result};
use super::principal::Principal;

// ============================================================================
// Resource and Action Types
// ============================================================================

/// Types of resources that can be protected.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ResourceType {
    /// The catalog itself
    Catalog,
    /// A namespace within the catalog
    Namespace,
    /// A table within a namespace
    Table,
    /// A view within a namespace
    View,
    /// A snapshot of a table
    Snapshot,
    /// A branch or tag reference
    Reference,
    /// API key management
    ApiKey,
    /// System administration
    System,
}

impl std::fmt::Display for ResourceType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ResourceType::Catalog => write!(f, "catalog"),
            ResourceType::Namespace => write!(f, "namespace"),
            ResourceType::Table => write!(f, "table"),
            ResourceType::View => write!(f, "view"),
            ResourceType::Snapshot => write!(f, "snapshot"),
            ResourceType::Reference => write!(f, "reference"),
            ResourceType::ApiKey => write!(f, "api_key"),
            ResourceType::System => write!(f, "system"),
        }
    }
}

/// Actions that can be performed on resources.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Action {
    /// Read/get a resource
    Read,
    /// Create a new resource
    Create,
    /// Update an existing resource
    Update,
    /// Delete a resource
    Delete,
    /// List resources
    List,
    /// Manage (admin-level access)
    Manage,
    /// Grant/revoke access to others
    Grant,
}

impl std::fmt::Display for Action {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Action::Read => write!(f, "read"),
            Action::Create => write!(f, "create"),
            Action::Update => write!(f, "update"),
            Action::Delete => write!(f, "delete"),
            Action::List => write!(f, "list"),
            Action::Manage => write!(f, "manage"),
            Action::Grant => write!(f, "grant"),
        }
    }
}

/// A specific resource instance being accessed.
#[derive(Debug, Clone)]
pub struct Resource {
    /// Type of the resource.
    pub resource_type: ResourceType,
    /// Tenant the resource belongs to.
    pub tenant_id: String,
    /// Optional catalog name (for multi-catalog deployments).
    pub catalog: Option<String>,
    /// Optional namespace path.
    pub namespace: Option<Vec<String>>,
    /// Optional table/view name.
    pub name: Option<String>,
    /// Additional attributes for ABAC.
    pub attributes: HashMap<String, String>,
}

impl Resource {
    /// Creates a new catalog resource.
    pub fn catalog(tenant_id: impl Into<String>) -> Self {
        Self {
            resource_type: ResourceType::Catalog,
            tenant_id: tenant_id.into(),
            catalog: None,
            namespace: None,
            name: None,
            attributes: HashMap::new(),
        }
    }

    /// Creates a new namespace resource.
    pub fn namespace(
        tenant_id: impl Into<String>,
        namespace: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        Self {
            resource_type: ResourceType::Namespace,
            tenant_id: tenant_id.into(),
            catalog: None,
            namespace: Some(namespace.into_iter().map(|s| s.into()).collect()),
            name: None,
            attributes: HashMap::new(),
        }
    }

    /// Creates a new table resource.
    pub fn table(
        tenant_id: impl Into<String>,
        namespace: impl IntoIterator<Item = impl Into<String>>,
        table_name: impl Into<String>,
    ) -> Self {
        Self {
            resource_type: ResourceType::Table,
            tenant_id: tenant_id.into(),
            catalog: None,
            namespace: Some(namespace.into_iter().map(|s| s.into()).collect()),
            name: Some(table_name.into()),
            attributes: HashMap::new(),
        }
    }

    /// Creates a new view resource.
    pub fn view(
        tenant_id: impl Into<String>,
        namespace: impl IntoIterator<Item = impl Into<String>>,
        view_name: impl Into<String>,
    ) -> Self {
        Self {
            resource_type: ResourceType::View,
            tenant_id: tenant_id.into(),
            catalog: None,
            namespace: Some(namespace.into_iter().map(|s| s.into()).collect()),
            name: Some(view_name.into()),
            attributes: HashMap::new(),
        }
    }

    /// Sets an attribute on the resource.
    pub fn with_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.attributes.insert(key.into(), value.into());
        self
    }

    /// Returns the full resource path as a string.
    pub fn path(&self) -> String {
        let mut parts = vec![self.tenant_id.clone()];

        if let Some(catalog) = &self.catalog {
            parts.push(catalog.clone());
        }

        if let Some(namespace) = &self.namespace {
            parts.extend(namespace.clone());
        }

        if let Some(name) = &self.name {
            parts.push(name.clone());
        }

        parts.join("/")
    }
}

// ============================================================================
// Authorization Context
// ============================================================================

/// Context for making authorization decisions.
#[derive(Debug, Clone)]
pub struct AuthzContext {
    /// The principal making the request.
    pub principal: Principal,
    /// The resource being accessed.
    pub resource: Resource,
    /// The action being performed.
    pub action: Action,
    /// Additional context attributes.
    pub context: HashMap<String, String>,
}

impl AuthzContext {
    /// Creates a new authorization context.
    pub fn new(principal: Principal, resource: Resource, action: Action) -> Self {
        Self {
            principal,
            resource,
            action,
            context: HashMap::new(),
        }
    }

    /// Adds a context attribute.
    pub fn with_context(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.context.insert(key.into(), value.into());
        self
    }
}

// ============================================================================
// Authorization Decision
// ============================================================================

/// Result of an authorization check.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AuthzDecision {
    /// Access is allowed.
    Allow,
    /// Access is denied with a reason.
    Deny(String),
}

impl AuthzDecision {
    /// Returns true if access is allowed.
    pub fn is_allowed(&self) -> bool {
        matches!(self, AuthzDecision::Allow)
    }

    /// Returns true if access is denied.
    pub fn is_denied(&self) -> bool {
        matches!(self, AuthzDecision::Deny(_))
    }

    /// Converts to a Result, returning Err with AuthError::Forbidden if denied.
    pub fn into_result(self) -> Result<()> {
        match self {
            AuthzDecision::Allow => Ok(()),
            AuthzDecision::Deny(reason) => Err(AuthError::Forbidden(reason)),
        }
    }
}

// ============================================================================
// Authorizer Trait
// ============================================================================

/// Trait for authorization implementations.
#[async_trait]
pub trait Authorizer: Send + Sync {
    /// Checks if the given context is authorized.
    async fn authorize(&self, ctx: &AuthzContext) -> AuthzDecision;

    /// Checks authorization and returns a Result.
    ///
    /// This method also emits audit log events for authorization denials.
    async fn check(&self, ctx: &AuthzContext) -> Result<()> {
        let decision = self.authorize(ctx).await;

        if decision.is_denied() {
            // Emit audit log for authorization denial
            use super::audit::log_authz_denied;
            log_authz_denied(
                ctx.principal.id(),
                ctx.principal.tenant_id(),
                &ctx.resource.resource_type.to_string(),
                &ctx.resource.path(),
                &ctx.action.to_string(),
            );
        }

        decision.into_result()
    }
}

// ============================================================================
// AllowAllAuthorizer
// ============================================================================

/// Authorizer that allows all requests.
///
/// **WARNING**: For development/testing only.
pub struct AllowAllAuthorizer;

#[async_trait]
impl Authorizer for AllowAllAuthorizer {
    async fn authorize(&self, _ctx: &AuthzContext) -> AuthzDecision {
        AuthzDecision::Allow
    }
}

// ============================================================================
// DenyAllAuthorizer
// ============================================================================

/// Authorizer that denies all requests.
pub struct DenyAllAuthorizer;

#[async_trait]
impl Authorizer for DenyAllAuthorizer {
    async fn authorize(&self, _ctx: &AuthzContext) -> AuthzDecision {
        AuthzDecision::Deny("Access denied by default policy".into())
    }
}

// ============================================================================
// RbacAuthorizer
// ============================================================================

/// Role-Based Access Control authorizer.
///
/// Grants access based on role-to-permission mappings.
pub struct RbacAuthorizer {
    /// Maps role names to allowed (ResourceType, Action) pairs.
    role_permissions: HashMap<String, Vec<(ResourceType, Action)>>,
}

impl RbacAuthorizer {
    /// Creates a new RBAC authorizer with default admin role.
    pub fn new() -> Self {
        let mut role_permissions = HashMap::new();

        // Admin role has full access
        role_permissions.insert(
            "admin".to_string(),
            vec![
                (ResourceType::Catalog, Action::Manage),
                (ResourceType::Namespace, Action::Manage),
                (ResourceType::Table, Action::Manage),
                (ResourceType::View, Action::Manage),
                (ResourceType::Snapshot, Action::Manage),
                (ResourceType::Reference, Action::Manage),
                (ResourceType::ApiKey, Action::Manage),
                (ResourceType::System, Action::Manage),
            ],
        );

        // System role (internal operations)
        role_permissions.insert("system".to_string(), role_permissions["admin"].clone());

        // Reader role
        role_permissions.insert(
            "reader".to_string(),
            vec![
                (ResourceType::Catalog, Action::Read),
                (ResourceType::Namespace, Action::Read),
                (ResourceType::Namespace, Action::List),
                (ResourceType::Table, Action::Read),
                (ResourceType::Table, Action::List),
                (ResourceType::View, Action::Read),
                (ResourceType::View, Action::List),
                (ResourceType::Snapshot, Action::Read),
                (ResourceType::Snapshot, Action::List),
            ],
        );

        // Writer role (reader + write)
        role_permissions.insert(
            "writer".to_string(),
            vec![
                // Read permissions
                (ResourceType::Catalog, Action::Read),
                (ResourceType::Namespace, Action::Read),
                (ResourceType::Namespace, Action::List),
                (ResourceType::Table, Action::Read),
                (ResourceType::Table, Action::List),
                (ResourceType::View, Action::Read),
                (ResourceType::View, Action::List),
                (ResourceType::Snapshot, Action::Read),
                (ResourceType::Snapshot, Action::List),
                // Write permissions
                (ResourceType::Namespace, Action::Create),
                (ResourceType::Namespace, Action::Update),
                (ResourceType::Table, Action::Create),
                (ResourceType::Table, Action::Update),
                (ResourceType::View, Action::Create),
                (ResourceType::View, Action::Update),
                (ResourceType::Snapshot, Action::Create),
            ],
        );

        Self { role_permissions }
    }

    /// Adds a custom role with specified permissions.
    pub fn with_role(
        mut self,
        role: impl Into<String>,
        permissions: Vec<(ResourceType, Action)>,
    ) -> Self {
        self.role_permissions.insert(role.into(), permissions);
        self
    }

    /// Checks if the action is implied by Manage permission.
    fn is_manage_implied(action: &Action) -> bool {
        // Manage implies all other actions
        matches!(
            action,
            Action::Read
                | Action::Create
                | Action::Update
                | Action::Delete
                | Action::List
                | Action::Grant
        )
    }
}

impl Default for RbacAuthorizer {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Authorizer for RbacAuthorizer {
    async fn authorize(&self, ctx: &AuthzContext) -> AuthzDecision {
        // System principals always allowed
        if ctx.principal.is_system() {
            return AuthzDecision::Allow;
        }

        // Check each role the principal has
        for role in ctx.principal.roles() {
            if let Some(permissions) = self.role_permissions.get(role) {
                for (resource_type, action) in permissions {
                    // Check for exact match
                    if resource_type == &ctx.resource.resource_type && action == &ctx.action {
                        return AuthzDecision::Allow;
                    }

                    // Check if Manage permission implies the requested action
                    if resource_type == &ctx.resource.resource_type
                        && action == &Action::Manage
                        && Self::is_manage_implied(&ctx.action)
                    {
                        return AuthzDecision::Allow;
                    }
                }
            }
        }

        AuthzDecision::Deny(format!(
            "Principal '{}' does not have '{}' permission on '{}'",
            ctx.principal.id(),
            ctx.action,
            ctx.resource.resource_type
        ))
    }
}

// ============================================================================
// TenantIsolationAuthorizer
// ============================================================================

/// Authorizer that enforces tenant isolation.
///
/// Wraps another authorizer and ensures principals can only access
/// resources within their own tenant.
pub struct TenantIsolationAuthorizer {
    inner: Arc<dyn Authorizer>,
}

impl TenantIsolationAuthorizer {
    /// Creates a new tenant isolation authorizer.
    pub fn new(inner: Arc<dyn Authorizer>) -> Self {
        Self { inner }
    }
}

#[async_trait]
impl Authorizer for TenantIsolationAuthorizer {
    async fn authorize(&self, ctx: &AuthzContext) -> AuthzDecision {
        // System principals bypass tenant isolation
        if ctx.principal.is_system() {
            return self.inner.authorize(ctx).await;
        }

        // Check tenant match
        if ctx.principal.tenant_id() != ctx.resource.tenant_id {
            return AuthzDecision::Deny(format!(
                "Cross-tenant access denied: principal tenant '{}' cannot access resource in tenant '{}'",
                ctx.principal.tenant_id(),
                ctx.resource.tenant_id
            ));
        }

        // Delegate to inner authorizer
        self.inner.authorize(ctx).await
    }
}

// ============================================================================
// ChainAuthorizer
// ============================================================================

/// Authorizer that chains multiple authorizers.
///
/// All authorizers must allow for the request to be allowed.
pub struct ChainAuthorizer {
    authorizers: Vec<Arc<dyn Authorizer>>,
}

impl ChainAuthorizer {
    /// Creates a new chain authorizer.
    pub fn new(authorizers: Vec<Arc<dyn Authorizer>>) -> Self {
        Self { authorizers }
    }

    /// Adds an authorizer to the chain.
    pub fn with(mut self, authorizer: Arc<dyn Authorizer>) -> Self {
        self.authorizers.push(authorizer);
        self
    }
}

#[async_trait]
impl Authorizer for ChainAuthorizer {
    async fn authorize(&self, ctx: &AuthzContext) -> AuthzDecision {
        for authorizer in &self.authorizers {
            let decision = authorizer.authorize(ctx).await;
            if decision.is_denied() {
                return decision;
            }
        }
        AuthzDecision::Allow
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::auth::principal::{AuthMethod, PrincipalBuilder, PrincipalType};

    fn test_principal(roles: Vec<&str>, tenant: &str) -> Principal {
        let mut builder = PrincipalBuilder::new(
            "test-user",
            "Test User",
            PrincipalType::User,
            tenant,
            AuthMethod::ApiKey,
        );
        for role in roles {
            builder = builder.with_role(role);
        }
        builder.build()
    }

    #[tokio::test]
    async fn test_allow_all_authorizer() {
        let authorizer = AllowAllAuthorizer;
        let principal = test_principal(vec![], "tenant-1");
        let resource = Resource::namespace("tenant-1", ["ns1"]);
        let ctx = AuthzContext::new(principal, resource, Action::Read);

        assert!(authorizer.authorize(&ctx).await.is_allowed());
    }

    #[tokio::test]
    async fn test_deny_all_authorizer() {
        let authorizer = DenyAllAuthorizer;
        let principal = test_principal(vec!["admin"], "tenant-1");
        let resource = Resource::namespace("tenant-1", ["ns1"]);
        let ctx = AuthzContext::new(principal, resource, Action::Read);

        assert!(authorizer.authorize(&ctx).await.is_denied());
    }

    #[tokio::test]
    async fn test_rbac_admin_role() {
        let authorizer = RbacAuthorizer::new();
        let principal = test_principal(vec!["admin"], "tenant-1");
        let resource = Resource::table("tenant-1", ["ns1"], "table1");
        let ctx = AuthzContext::new(principal, resource, Action::Delete);

        assert!(authorizer.authorize(&ctx).await.is_allowed());
    }

    #[tokio::test]
    async fn test_rbac_reader_role() {
        let authorizer = RbacAuthorizer::new();
        let principal = test_principal(vec!["reader"], "tenant-1");

        // Reader can read
        let resource = Resource::table("tenant-1", ["ns1"], "table1");
        let ctx = AuthzContext::new(principal.clone(), resource, Action::Read);
        assert!(authorizer.authorize(&ctx).await.is_allowed());

        // Reader cannot delete
        let resource = Resource::table("tenant-1", ["ns1"], "table1");
        let ctx = AuthzContext::new(principal, resource, Action::Delete);
        assert!(authorizer.authorize(&ctx).await.is_denied());
    }

    #[tokio::test]
    async fn test_tenant_isolation() {
        let inner = Arc::new(AllowAllAuthorizer);
        let authorizer = TenantIsolationAuthorizer::new(inner);

        // Same tenant - allowed
        let principal = test_principal(vec!["admin"], "tenant-1");
        let resource = Resource::namespace("tenant-1", ["ns1"]);
        let ctx = AuthzContext::new(principal, resource, Action::Read);
        assert!(authorizer.authorize(&ctx).await.is_allowed());

        // Different tenant - denied
        let principal = test_principal(vec!["admin"], "tenant-1");
        let resource = Resource::namespace("tenant-2", ["ns1"]);
        let ctx = AuthzContext::new(principal, resource, Action::Read);
        assert!(authorizer.authorize(&ctx).await.is_denied());
    }

    #[tokio::test]
    async fn test_system_principal_bypasses_isolation() {
        let inner = Arc::new(AllowAllAuthorizer);
        let authorizer = TenantIsolationAuthorizer::new(inner);

        let principal = Principal::system();
        let resource = Resource::namespace("any-tenant", ["ns1"]);
        let ctx = AuthzContext::new(principal, resource, Action::Delete);

        assert!(authorizer.authorize(&ctx).await.is_allowed());
    }

    #[tokio::test]
    async fn test_chain_authorizer() {
        let tenant_authz = Arc::new(TenantIsolationAuthorizer::new(Arc::new(AllowAllAuthorizer)));
        let rbac_authz = Arc::new(RbacAuthorizer::new());
        let chain = ChainAuthorizer::new(vec![tenant_authz, rbac_authz]);

        // Admin in correct tenant - allowed
        let principal = test_principal(vec!["admin"], "tenant-1");
        let resource = Resource::table("tenant-1", ["ns1"], "table1");
        let ctx = AuthzContext::new(principal, resource, Action::Delete);
        assert!(chain.authorize(&ctx).await.is_allowed());

        // Reader in correct tenant trying to delete - denied
        let principal = test_principal(vec!["reader"], "tenant-1");
        let resource = Resource::table("tenant-1", ["ns1"], "table1");
        let ctx = AuthzContext::new(principal, resource, Action::Delete);
        assert!(chain.authorize(&ctx).await.is_denied());
    }

    #[test]
    fn test_resource_path() {
        let resource = Resource::table("tenant-1", ["ns1", "ns2"], "my_table");
        assert_eq!(resource.path(), "tenant-1/ns1/ns2/my_table");
    }
}