canic-core 0.26.9

Canic — a canister orchestration and management toolkit for the Internet Computer
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
//! Access expression model and evaluator.
//!
//! This module defines the expression tree and async predicate interface
//! used to compose access control without changing existing semantics.

mod evaluators;

use crate::{
    access::{self, AccessError, metrics::AccessMetrics},
    cdk::types::Principal,
    ids::{AccessMetricKind, EndpointCall},
    log,
    log::Topic,
};
use async_trait::async_trait;
use std::{future::Future, pin::Pin, sync::Arc};

///
/// AccessContext
///

#[derive(Clone, Debug)]
pub struct AccessContext {
    // Raw transport identity from msg_caller().
    pub caller: Principal,
    // Resolved app/auth subject (raw caller or delegated session subject).
    pub authenticated_caller: Principal,
    // Source of the resolved authenticated subject.
    pub identity_source: access::auth::AuthenticatedIdentitySource,
    pub call: EndpointCall,
}

impl AccessContext {
    #[must_use]
    pub const fn transport_caller(&self) -> Principal {
        self.caller
    }

    #[must_use]
    pub const fn authenticated_subject(&self) -> Principal {
        self.authenticated_caller
    }
}

///
/// DefaultAppGuard
///
/// Synchronous app-mode guards used for implicit gating.
///
pub enum DefaultAppGuard {
    AllowsUpdates,
    IsQueryable,
}

///
/// AccessExpr
///

#[derive(Clone)]
pub enum AccessExpr {
    All(Vec<Self>),
    Any(Vec<Self>),
    Not(Box<Self>),
    Pred(AccessPredicate),
}

///
/// AccessPredicate
///

#[derive(Clone)]
pub enum AccessPredicate {
    Builtin(BuiltinPredicate),
    Custom(Arc<dyn AsyncAccessPredicate>),
}

///
/// BuiltinPredicate
///

#[derive(Clone, Copy, Debug)]
pub enum BuiltinPredicate {
    App(AppPredicate),
    Caller(CallerPredicate),
    Environment(EnvironmentPredicate),
    Authenticated {
        required_scope: Option<&'static str>,
    },
}

impl BuiltinPredicate {
    /// name
    ///
    /// Return the stable metrics/logging name for this builtin predicate.
    fn name(self) -> &'static str {
        evaluators::name(self)
    }

    /// metric_kind
    ///
    /// Return the metric family used to classify this builtin predicate.
    fn metric_kind(self) -> AccessMetricKind {
        evaluators::metric_kind(self)
    }
}

///
/// AppPredicate
///

#[derive(Clone, Copy, Debug)]
pub enum AppPredicate {
    AllowsUpdates,
    IsQueryable,
}

///
/// CallerPredicate
///

#[derive(Clone, Copy, Debug)]
pub enum CallerPredicate {
    IsController,
    IsParent,
    IsChild,
    IsRoot,
    IsSameCanister,
    IsRegisteredToSubnet,
    IsWhitelisted,
}

///
/// EnvironmentPredicate
///

#[derive(Clone, Copy, Debug)]
pub enum EnvironmentPredicate {
    SelfIsPrimeSubnet,
    SelfIsPrimeRoot,
    BuildIcOnly,
    BuildLocalOnly,
}

#[async_trait]
pub trait AsyncAccessPredicate: Send + Sync {
    ///
    /// Custom predicate contract:
    /// - May be async and perform I/O.
    /// - May depend on application state.
    /// - Must be side-effect free, idempotent, and must not panic.
    ///
    async fn eval(&self, ctx: &AccessContext) -> Result<(), AccessError>;
    fn name(&self) -> &'static str;
}

pub fn all<I>(exprs: I) -> AccessExpr
where
    I: IntoIterator<Item = AccessExpr>,
{
    AccessExpr::All(exprs.into_iter().collect())
}

pub fn any<I>(exprs: I) -> AccessExpr
where
    I: IntoIterator<Item = AccessExpr>,
{
    AccessExpr::Any(exprs.into_iter().collect())
}

#[must_use]
pub fn not(expr: AccessExpr) -> AccessExpr {
    AccessExpr::Not(Box::new(expr))
}

pub fn requires<I>(exprs: I) -> AccessExpr
where
    I: IntoIterator<Item = AccessExpr>,
{
    all(exprs)
}

pub fn custom<P>(pred: P) -> AccessExpr
where
    P: AsyncAccessPredicate + 'static,
{
    AccessExpr::Pred(AccessPredicate::Custom(Arc::new(pred)))
}

pub mod app {
    use super::{AccessExpr, AppPredicate, BuiltinPredicate, builtin};

    #[must_use]
    pub const fn allows_updates() -> AccessExpr {
        builtin(BuiltinPredicate::App(AppPredicate::AllowsUpdates))
    }

    #[must_use]
    pub const fn is_queryable() -> AccessExpr {
        builtin(BuiltinPredicate::App(AppPredicate::IsQueryable))
    }
}

pub mod caller {
    use super::{AccessExpr, BuiltinPredicate, CallerPredicate, builtin};

    #[must_use]
    pub const fn is_controller() -> AccessExpr {
        builtin(BuiltinPredicate::Caller(CallerPredicate::IsController))
    }

    #[must_use]
    pub const fn is_parent() -> AccessExpr {
        builtin(BuiltinPredicate::Caller(CallerPredicate::IsParent))
    }

    #[must_use]
    pub const fn is_child() -> AccessExpr {
        builtin(BuiltinPredicate::Caller(CallerPredicate::IsChild))
    }

    #[must_use]
    pub const fn is_root() -> AccessExpr {
        builtin(BuiltinPredicate::Caller(CallerPredicate::IsRoot))
    }

    #[must_use]
    pub const fn is_same_canister() -> AccessExpr {
        builtin(BuiltinPredicate::Caller(CallerPredicate::IsSameCanister))
    }

    #[must_use]
    pub const fn is_registered_to_subnet() -> AccessExpr {
        builtin(BuiltinPredicate::Caller(
            CallerPredicate::IsRegisteredToSubnet,
        ))
    }

    #[must_use]
    pub const fn is_whitelisted() -> AccessExpr {
        builtin(BuiltinPredicate::Caller(CallerPredicate::IsWhitelisted))
    }
}

pub mod env {
    use super::{AccessExpr, BuiltinPredicate, EnvironmentPredicate, builtin};

    #[must_use]
    pub const fn is_prime_subnet() -> AccessExpr {
        builtin(BuiltinPredicate::Environment(
            EnvironmentPredicate::SelfIsPrimeSubnet,
        ))
    }

    #[must_use]
    pub const fn is_prime_root() -> AccessExpr {
        builtin(BuiltinPredicate::Environment(
            EnvironmentPredicate::SelfIsPrimeRoot,
        ))
    }

    #[must_use]
    pub const fn build_ic_only() -> AccessExpr {
        builtin(BuiltinPredicate::Environment(
            EnvironmentPredicate::BuildIcOnly,
        ))
    }

    #[must_use]
    pub const fn build_local_only() -> AccessExpr {
        builtin(BuiltinPredicate::Environment(
            EnvironmentPredicate::BuildLocalOnly,
        ))
    }
}

pub mod auth {
    use super::{AccessExpr, BuiltinPredicate, builtin};

    #[must_use]
    pub const fn authenticated(required_scope: Option<&'static str>) -> AccessExpr {
        builtin(BuiltinPredicate::Authenticated { required_scope })
    }

    #[must_use]
    pub const fn authenticated_with_scope(required_scope: &'static str) -> AccessExpr {
        authenticated(Some(required_scope))
    }
}

pub async fn eval_access(expr: &AccessExpr, ctx: &AccessContext) -> Result<(), AccessError> {
    match eval_access_inner(expr, ctx).await {
        Ok(()) => Ok(()),
        Err(failure) => Err(record_access_failure(ctx, failure)),
    }
}

type AccessEvalFuture<'a> = Pin<Box<dyn Future<Output = Result<(), AccessFailure>> + Send + 'a>>;

pub fn eval_default_app_guard(
    guard: DefaultAppGuard,
    ctx: &AccessContext,
) -> Result<(), AccessError> {
    let result = match guard {
        DefaultAppGuard::AllowsUpdates => access::app::guard_app_update(),
        DefaultAppGuard::IsQueryable => access::app::guard_app_query(),
    };

    match result {
        Ok(()) => Ok(()),
        Err(err) => {
            let predicate = match guard {
                DefaultAppGuard::AllowsUpdates => {
                    BuiltinPredicate::App(AppPredicate::AllowsUpdates)
                }
                DefaultAppGuard::IsQueryable => BuiltinPredicate::App(AppPredicate::IsQueryable),
            };
            Err(record_access_failure(
                ctx,
                AccessFailure::from_builtin(predicate, err),
            ))
        }
    }
}

fn eval_access_inner<'a>(expr: &'a AccessExpr, ctx: &'a AccessContext) -> AccessEvalFuture<'a> {
    Box::pin(async move {
        match expr {
            AccessExpr::All(exprs) => {
                if exprs.is_empty() {
                    return Err(AccessFailure::no_predicates("all"));
                }
                for expr in exprs {
                    if let Err(failure) = eval_access_inner(expr, ctx).await {
                        return Err(failure.with_context("all"));
                    }
                }
                Ok(())
            }
            AccessExpr::Any(exprs) => {
                if exprs.is_empty() {
                    return Err(AccessFailure::no_predicates("any"));
                }
                let mut last = None;
                for expr in exprs {
                    match eval_access_inner(expr, ctx).await {
                        Ok(()) => return Ok(()),
                        Err(failure) => last = Some(failure.with_context("any")),
                    }
                }
                Err(last.unwrap_or_else(|| AccessFailure::no_predicates("any")))
            }
            AccessExpr::Not(expr) => match eval_access_inner(expr, ctx).await {
                Ok(()) => Err(AccessFailure::negated()),
                Err(_) => Ok(()),
            },
            AccessExpr::Pred(pred) => match pred {
                AccessPredicate::Builtin(builtin) => evaluators::evaluate(*builtin, ctx)
                    .await
                    .map_err(|err| AccessFailure::from_builtin(*builtin, err)),
                AccessPredicate::Custom(custom) => custom
                    .eval(ctx)
                    .await
                    .map_err(|err| AccessFailure::from_custom(custom.name(), err)),
            },
        }
    })
}

const fn builtin(pred: BuiltinPredicate) -> AccessExpr {
    AccessExpr::Pred(AccessPredicate::Builtin(pred))
}

///
/// AccessFailure
///

#[derive(Debug)]
struct AccessFailure {
    error: AccessError,
    metric_kind: AccessMetricKind,
    predicate: &'static str,
    context: Option<&'static str>,
}

impl AccessFailure {
    fn from_builtin(pred: BuiltinPredicate, error: AccessError) -> Self {
        Self {
            error,
            metric_kind: pred.metric_kind(),
            predicate: pred.name(),
            context: None,
        }
    }

    const fn from_custom(name: &'static str, error: AccessError) -> Self {
        Self {
            error,
            metric_kind: AccessMetricKind::Custom,
            predicate: name,
            context: None,
        }
    }

    fn no_predicates(context: &'static str) -> Self {
        Self {
            error: AccessError::Denied("one or more rules must be defined".to_string()),
            metric_kind: AccessMetricKind::Auth,
            predicate: "no_rules",
            context: Some(context),
        }
    }

    fn negated() -> Self {
        Self {
            error: AccessError::Denied("negated predicate matched".to_string()),
            metric_kind: AccessMetricKind::Auth,
            predicate: "not",
            context: Some("not"),
        }
    }

    fn with_context(mut self, context: &'static str) -> Self {
        self.context.get_or_insert(context);
        self
    }
}

fn record_access_failure(ctx: &AccessContext, failure: AccessFailure) -> AccessError {
    AccessMetrics::increment(ctx.call, failure.metric_kind, failure.predicate);
    log!(
        Topic::Auth,
        Warn,
        "access denied kind={} predicate={} context={:?}: {}",
        failure.metric_kind.as_str(),
        failure.predicate,
        failure.context,
        failure.error,
    );
    failure.error
}

///
/// TESTS
///

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        access,
        ids::{EndpointCall, EndpointCallKind, EndpointId},
        storage::stable::env::{Env, EnvRecord},
        storage::stable::state::app::{AppMode, AppState, AppStateRecord},
        test::seams,
    };

    ///
    /// EnvRestore
    ///

    struct EnvRestore(EnvRecord);

    impl Drop for EnvRestore {
        fn drop(&mut self) {
            Env::import(self.0.clone());
        }
    }

    ///
    /// AppRestore
    ///

    struct AppRestore(AppStateRecord);

    impl Drop for AppRestore {
        fn drop(&mut self) {
            AppState::import(self.0);
        }
    }

    #[test]
    fn caller_is_parent_matches_access_auth() {
        let _guard = seams::lock();
        let original = Env::export();
        let _restore = EnvRestore(original);

        let parent = seams::p(1);
        let other = seams::p(2);
        Env::import(EnvRecord {
            parent_pid: Some(parent),
            ..EnvRecord::default()
        });

        let expr = caller::is_parent();

        let ctx_parent = AccessContext {
            caller: parent,
            authenticated_caller: parent,
            identity_source: access::auth::AuthenticatedIdentitySource::RawCaller,
            call: test_call(),
        };
        let ctx_other = AccessContext {
            caller: other,
            authenticated_caller: other,
            identity_source: access::auth::AuthenticatedIdentitySource::RawCaller,
            call: test_call(),
        };

        let expr_parent = futures::executor::block_on(eval_access(&expr, &ctx_parent));
        let auth_parent = futures::executor::block_on(access::auth::is_parent(parent));
        assert_eq!(expr_parent.is_ok(), auth_parent.is_ok());

        let expr_other = futures::executor::block_on(eval_access(&expr, &ctx_other));
        let auth_other = futures::executor::block_on(access::auth::is_parent(other));
        assert_eq!(expr_other.is_ok(), auth_other.is_ok());
    }

    #[test]
    fn app_allows_updates_matches_access_app_guard() {
        let _guard = seams::lock();
        let original = AppState::export();
        let _restore = AppRestore(original);
        let expr = app::allows_updates();
        let ctx = AccessContext {
            caller: seams::p(1),
            authenticated_caller: seams::p(1),
            identity_source: access::auth::AuthenticatedIdentitySource::RawCaller,
            call: test_call(),
        };

        for mode in [AppMode::Enabled, AppMode::Readonly, AppMode::Disabled] {
            AppState::import(AppStateRecord {
                mode,
                ..AppStateRecord::default()
            });
            let expr_result = futures::executor::block_on(eval_access(&expr, &ctx));
            let direct_result = access::app::guard_app_update();
            assert_eq!(
                expr_result.is_ok(),
                direct_result.is_ok(),
                "app::allows_updates parity mismatch for mode={mode:?}"
            );
        }
    }

    #[test]
    fn app_is_queryable_matches_access_app_guard() {
        let _guard = seams::lock();
        let original = AppState::export();
        let _restore = AppRestore(original);
        let expr = app::is_queryable();
        let ctx = AccessContext {
            caller: seams::p(1),
            authenticated_caller: seams::p(1),
            identity_source: access::auth::AuthenticatedIdentitySource::RawCaller,
            call: test_call(),
        };

        for mode in [AppMode::Enabled, AppMode::Readonly, AppMode::Disabled] {
            AppState::import(AppStateRecord {
                mode,
                ..AppStateRecord::default()
            });
            let expr_result = futures::executor::block_on(eval_access(&expr, &ctx));
            let direct_result = access::app::guard_app_query();
            assert_eq!(
                expr_result.is_ok(),
                direct_result.is_ok(),
                "app::is_queryable parity mismatch for mode={mode:?}"
            );
        }
    }

    #[test]
    fn build_network_predicates_match_env_access_checks() {
        let ctx = AccessContext {
            caller: seams::p(1),
            authenticated_caller: seams::p(1),
            identity_source: access::auth::AuthenticatedIdentitySource::RawCaller,
            call: test_call(),
        };

        let expr_local = futures::executor::block_on(eval_access(&env::build_local_only(), &ctx));
        let direct_local = access::env::build_network_local();
        assert_eq!(expr_local.is_ok(), direct_local.is_ok());

        let expr_ic = futures::executor::block_on(eval_access(&env::build_ic_only(), &ctx));
        let direct_ic = access::env::build_network_ic();
        assert_eq!(expr_ic.is_ok(), direct_ic.is_ok());
    }

    fn test_call() -> EndpointCall {
        EndpointCall {
            endpoint: EndpointId::new("test"),
            kind: EndpointCallKind::Update,
        }
    }

    #[test]
    fn caller_predicates_use_transport_caller_not_authenticated_subject() {
        let _guard = seams::lock();
        let original = Env::export();
        let _restore = EnvRestore(original);

        let parent = seams::p(10);
        let delegated_subject = seams::p(11);
        Env::import(EnvRecord {
            parent_pid: Some(parent),
            ..EnvRecord::default()
        });

        let expr = caller::is_parent();
        let ctx = AccessContext {
            caller: parent,
            authenticated_caller: delegated_subject,
            identity_source: access::auth::AuthenticatedIdentitySource::DelegatedSession,
            call: test_call(),
        };

        let result = futures::executor::block_on(eval_access(&expr, &ctx));
        assert!(result.is_ok());
    }
}