cedarling 0.0.8

The Cedarling: a high-performance local authorization service powered by the Rust Cedar Engine.
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
// This software is available under the Apache-2.0 license.
// See https://www.apache.org/licenses/LICENSE-2.0.txt for full text.
//
// Copyright (c) 2024, Gluu, Inc.

use std::{
    collections::HashMap,
    sync::{Arc, RwLock},
    time::Duration,
};

use jsonwebtoken::DecodingKey;
use url::Url;

use crate::{
    async_sleep::sleep,
    http::HttpClient,
    jwt::{
        decode::{decode_jwt, DecodeJwtError},
        key_service::KeyService,
        log_entry::JwtLogEntry,
        token_cache::IndexKey,
        validation::{JwtValidator, JwtValidatorCache, TokenKind, ValidatorInfo},
        IssuerConfig, TokenCache,
    },
    log::Logger,
    LogLevel, LogWriter,
};

use super::{StatusList, StatusListJwt, StatusListJwtStr, UpdateStatusListError};

/// The value of the `status_list_uri` claim from a JWT
type StatusListUri = String;

struct StatusListUpdateCtx {
    ttl: Duration,
    /// Upper bound on the refresh interval. The Status List JWT's `ttl`
    /// claim is honored when present, but capped to this value; when the JWT omits
    /// `ttl`, this value is used directly.
    refresh_interval_max: Duration,
    status_list_url: Url,
    decoding_key: Option<Arc<DecodingKey>>,
    validator: Arc<JwtValidator>,
    status_lists: Arc<RwLock<HashMap<String, StatusList>>>,
    logger: Option<Logger>,
    http_client: HttpClient,
}

pub(crate) struct InitForIssArgs<'a> {
    pub validators: &'a JwtValidatorCache,
    pub key_service: &'a KeyService,
    pub token_cache: TokenCache,
    pub logger: Option<Logger>,
    pub http_client: HttpClient,
    pub refresh_interval_max: Duration,
}

/// Resolve effective refresh interval as `min(jwt_ttl, refresh_interval_max)`,
/// or `refresh_interval_max` when the JWT omits `ttl`.
fn effective_refresh_interval(jwt_ttl_secs: Option<u64>, max: Duration) -> Duration {
    match jwt_ttl_secs {
        Some(t) => Duration::from_secs(t).min(max),
        None => max,
    }
}

/// Contains an `Arc<RwLock<_>>` internally so clone should be fine
#[derive(Debug, Default, Clone)]
pub(crate) struct StatusListCache {
    pub status_lists: Arc<RwLock<HashMap<StatusListUri, StatusList>>>,
}

impl StatusListCache {
    /// Initializes the statuslist for the given issuer
    ///
    /// If the issuer's `OpenID` configuration does not include a
    /// `status_list_endpoint`, the initialization is silently skipped with a
    /// WARN-level log message rather than failing.
    pub(crate) async fn init_for_iss(
        &self,
        iss_config: &IssuerConfig,
        args: InitForIssArgs<'_>,
    ) -> Result<(), UpdateStatusListError> {
        let InitForIssArgs {
            validators,
            key_service,
            token_cache,
            logger,
            http_client,
            refresh_interval_max,
        } = args;
        let Some(openid_config) = iss_config.openid_config.as_ref() else {
            if let Some(logger) = &logger {
                logger.log_any(JwtLogEntry::new(
                    "issuer has no OpenID configuration; status validation skipped".into(),
                    Some(LogLevel::WARN),
                ));
            }
            return Ok(());
        };
        let Some(status_list_url) = openid_config.status_list_endpoint.as_ref() else {
            if let Some(logger) = &logger {
                logger.log_any(JwtLogEntry::new(
                    format!(
                        "issuer '{}' does not publish a status_list_endpoint; \
                         status validation skipped",
                        openid_config.issuer,
                    ),
                    Some(LogLevel::WARN),
                ));
            }
            return Ok(());
        };
        let status_list_jwt = StatusListJwtStr::get_from_url(status_list_url, &http_client)
            .await
            .map_err(UpdateStatusListError::GetStatusListJwt)?;
        let iss = iss_config.policy.iss_claim();

        let decoded_jwt = decode_jwt(&status_list_jwt.0)?;

        // Get decoding key
        let decoding_key_info = decoded_jwt.decoding_key_info();
        let decoding_key = key_service.get_key(&decoding_key_info);

        let decoded_jwt_iss = decoded_jwt.iss();
        // get validator
        let validator_key = ValidatorInfo {
            iss: decoded_jwt_iss.as_ref(),
            token_kind: TokenKind::StatusList,
            algorithm: decoded_jwt.header.alg,
        };
        let validator =
            validators
                .get(&validator_key)
                .ok_or(UpdateStatusListError::MissingValidator(
                    status_list_url.to_string(),
                ))?;

        let status_list_jwt: StatusListJwt = validator
            .validate_jwt(&status_list_jwt.0, decoding_key.clone())?
            .try_into()
            .map_err(DecodeJwtError::DeserializeClaims)?;

        // Per the IETF `oauth-status-list` spec, the JWT's `ttl` claim drives the
        // refresh cadence, but the bootstrap
        // `CEDARLING_JWT_STATUS_LIST_REFRESH_INTERVAL_MAX` caps it: the issuer can
        // always request a *more frequent* refresh, but never a less frequent one.
        // When the JWT omits `ttl`, the max is used directly so the cache never goes
        // stale forever.
        let ttl = effective_refresh_interval(status_list_jwt.ttl, refresh_interval_max);
        let status_list: StatusList = status_list_jwt.try_into()?;

        let ctx = StatusListUpdateCtx {
            ttl,
            refresh_interval_max,
            status_list_url: status_list_url.clone(),
            decoding_key,
            validator,
            status_lists: self.status_lists.clone(),
            logger,
            http_client: http_client.clone(),
        };
        crate::http::spawn_task(keep_status_list_updated(
            // callback is called on updated status list
            move || {
                token_cache.invalidate_by_index(&IndexKey::Iss(iss.clone()));
            },
            ctx,
        ));

        {
            let mut status_lists = self
                .status_lists
                .write()
                .expect("acquire status_lists write lock");
            status_lists.insert(status_list_url.to_string(), status_list);
        }

        Ok(())
    }
}

/// Keeps the statuslist form the given URL updated based on the TTL
/// `cb` will be called when status list updated
async fn keep_status_list_updated<F>(cb: F, ctx: StatusListUpdateCtx)
where
    F: Fn(),
{
    let StatusListUpdateCtx {
        mut ttl,
        refresh_interval_max,
        status_list_url,
        decoding_key,
        validator,
        status_lists,
        logger,
        http_client,
    } = ctx;

    // If a refresh fails, drop the cached list instead of serving a stale one.
    // Tokens that need it then get rejected until a refresh works again. We also
    // clear the token cache so a token already cached as valid gets re-checked.
    let fail_closed = || {
        let removed = {
            let mut lists = status_lists.write().expect("obtain status list write lock");
            lists.remove(status_list_url.as_str()).is_some()
        };
        if removed {
            logger.log_any(JwtLogEntry::new(
                format!(
                    "dropped the cached status list for '{0}' after a failed refresh; \
                     tokens that reference it will be rejected until it refreshes successfully",
                    status_list_url.as_str(),
                ),
                Some(LogLevel::WARN),
            ));
            cb();
        }
    };

    loop {
        sleep(ttl).await;

        let status_list_jwt =
            match StatusListJwtStr::get_from_url(&status_list_url, &http_client).await {
                Ok(jwt) => jwt,
                Err(e) => {
                    logger.log_any(JwtLogEntry::new(
                        format!(
                            "failed to fetch an updated the status list from '{0}': {1}",
                            status_list_url.as_str(),
                            e
                        ),
                        Some(LogLevel::ERROR),
                    ));
                    fail_closed();
                    continue;
                },
            };

        let result = validator.validate_jwt(&status_list_jwt.0, decoding_key.clone());
        let validated_jwt = match result {
            Ok(validated_jwt) => validated_jwt,
            Err(e) => {
                logger.log_any(JwtLogEntry::new(
                    format!(
                        "failed to validate an updated the status list JWT from '{0}': {1}",
                        status_list_url.as_str(),
                        e
                    ),
                    Some(LogLevel::ERROR),
                ));
                fail_closed();
                continue;
            },
        };

        let status_list_jwt: StatusListJwt = match validated_jwt.try_into() {
            Ok(jwt) => jwt,
            Err(e) => {
                logger.log_any(JwtLogEntry::new(
                    format!(
                        "failed to deserialize an updated the status list JWT from '{0}': {1}",
                        status_list_url.as_str(),
                        e
                    ),
                    Some(LogLevel::ERROR),
                ));
                fail_closed();
                continue;
            },
        };
        // Honor the refreshed JWT's `ttl` but cap it at the configured max; if the
        // refreshed JWT no longer has `ttl`, use the max directly.
        ttl = effective_refresh_interval(status_list_jwt.ttl, refresh_interval_max);

        let updated_status_list: StatusList = match status_list_jwt.try_into() {
            Ok(status_list) => status_list,
            Err(e) => {
                logger.log_any(JwtLogEntry::new(
                    format!(
                        "failed to parse the updated the status list JWT from '{0}': {1}",
                        status_list_url.as_str(),
                        e
                    ),
                    Some(LogLevel::ERROR),
                ));
                fail_closed();
                continue;
            },
        };

        {
            let mut lists = status_lists.write().expect("obtain status list write lock");
            // insert, not update: re-creates the entry if a failed refresh dropped it
            lists.insert(status_list_url.to_string(), updated_status_list);
        }
        // call callback on updated status list
        cb();
    }
}

impl From<HashMap<String, StatusList>> for StatusListCache {
    fn from(status_lists: HashMap<String, StatusList>) -> Self {
        Self {
            status_lists: Arc::new(RwLock::new(status_lists)),
        }
    }
}

#[cfg(test)]
mod test {
    use jsonwebtoken::Algorithm;

    use super::*;
    use crate::common::policy_store::TrustedIssuer;
    use crate::http::HttpClientConfig;
    use crate::jwt::test_utils::MockServer;
    use crate::JwtConfig;
    use std::collections::HashSet;
    use std::time::Duration;

    #[tokio::test]
    async fn keep_status_list_updated() {
        let http_client = HttpClient::new(HttpClientConfig {
            max_retries: 0,
            retry_delay: Duration::from_millis(3),
            request_timeout: Duration::from_millis(500),
        max_response_size_bytes: None,
        })
        .expect("http client should be constructed");

        // Setup
        let validators = JwtValidatorCache::default();
        let key_service = KeyService::default();
        let mut mock_server = MockServer::new_with_defaults().await.unwrap();
        key_service
            .get_keys_using_oidc(&mock_server.openid_config(), None, http_client.clone())
            .await
            .unwrap();
        // we initialize the status list with a 1 sec ttl
        mock_server.generate_status_list_endpoint(1u8.try_into().unwrap(), &[0b1111_1110], Some(1));
        let status_list = StatusListCache::default();

        let ti = TrustedIssuer::new(
            "some_iss".into(),
            "is a trusted issuer".into(),
            mock_server.openid_config_endpoint().unwrap(),
            HashMap::default(),
        );

        let iss_config = IssuerConfig {
            issuer_id: "some_iss_id".into(),
            policy: Arc::new(ti),
            openid_config: Some(mock_server.openid_config()),
        };
        validators.init_for_iss(
            &iss_config,
            &JwtConfig {
                jwks: None,
                jwt_sig_validation: false,
                jwt_status_validation: true,
                signature_algorithms_supported: HashSet::from([Algorithm::HS256]),
                ..Default::default()
            },
            &status_list,
            None,
        );
        status_list
            .init_for_iss(
                &iss_config,
                InitForIssArgs {
                    validators: &validators,
                    key_service: &key_service,
                    token_cache: TokenCache::default(),
                    logger: None,
                    http_client,
                    // Small cap so the JWT ttl (1s) is capped to a sub-second
                    // value, keeping the test fast.
                    refresh_interval_max: Duration::from_millis(200),
                },
            )
            .await
            .unwrap();

        let status_list_uri = mock_server.status_list_endpoint().unwrap().to_string();

        // Check if the status list is the same as we first generated
        {
            let lists = status_list
                .status_lists
                .read()
                .expect("obtain status_lists read lock");
            let status_list = lists
                .get(&status_list_uri)
                .expect("should have a status list");

            assert_eq!(
                *status_list,
                StatusList {
                    bit_size: 1u8.try_into().unwrap(),
                    list: vec![0b1111_1110],
                },
                "the status list is wrong",
            );
        }

        // Update the status in the server
        mock_server.generate_status_list_endpoint(1u8.try_into().unwrap(), &[0b0000_0001], None);

        // Wait for the token to expire and get updated
        tokio::time::sleep(Duration::from_millis(600)).await;

        // Check if the status list got updated
        {
            let lists = status_list
                .status_lists
                .read()
                .expect("obtain status_lists read lock");
            let status_list = lists
                .get(&status_list_uri)
                .expect("should have a status list");

            assert_eq!(
                *status_list,
                StatusList {
                    bit_size: 1u8.try_into().unwrap(),
                    list: vec![0b0000_0001],
                },
                "the status list was not updated",
            );
        }
    }

    /// When the status endpoint becomes unavailable, the background refresh
    /// fails closed: it drops the cached status list so tokens that depend on
    /// it are rejected instead of being checked against stale revocation data.
    #[tokio::test]
    async fn refresh_failure_evicts_status_list() {
        let http_client = HttpClient::new(HttpClientConfig {
            max_retries: 0,
            retry_delay: Duration::from_millis(3),
            request_timeout: Duration::from_millis(500),
            max_response_size_bytes: None,
        })
        .expect("http client should be constructed");

        let validators = JwtValidatorCache::default();
        let key_service = KeyService::default();
        let mut mock_server = MockServer::new_with_defaults().await.unwrap();
        key_service
            .get_keys_using_oidc(&mock_server.openid_config(), None, http_client.clone())
            .await
            .unwrap();
        mock_server.generate_status_list_endpoint(1u8.try_into().unwrap(), &[0b1111_1110], Some(1));
        let status_list = StatusListCache::default();

        let ti = TrustedIssuer::new(
            "some_iss".into(),
            "is a trusted issuer".into(),
            mock_server.openid_config_endpoint().unwrap(),
            HashMap::default(),
        );

        let iss_config = IssuerConfig {
            issuer_id: "some_iss_id".into(),
            policy: Arc::new(ti),
            openid_config: Some(mock_server.openid_config()),
        };
        validators.init_for_iss(
            &iss_config,
            &JwtConfig {
                jwks: None,
                jwt_sig_validation: false,
                jwt_status_validation: true,
                signature_algorithms_supported: HashSet::from([Algorithm::HS256]),
                ..Default::default()
            },
            &status_list,
            None,
        );
        status_list
            .init_for_iss(
                &iss_config,
                InitForIssArgs {
                    validators: &validators,
                    key_service: &key_service,
                    token_cache: TokenCache::default(),
                    logger: None,
                    http_client,
                    refresh_interval_max: Duration::from_millis(200),
                },
            )
            .await
            .unwrap();

        let status_list_uri = mock_server.status_list_endpoint().unwrap().to_string();

        // init cached the list; now make the endpoint fail and let one refresh run.
        mock_server.fail_status_list_endpoint();
        tokio::time::sleep(Duration::from_millis(600)).await;

        {
            let lists = status_list
                .status_lists
                .read()
                .expect("obtain status_lists read lock");
            assert!(
                !lists.contains_key(&status_list_uri),
                "a failed refresh should drop the cached status list (fail closed)",
            );
        }
    }

    /// When the Status List JWT has no `ttl` claim, the bootstrap-config
    /// `status_list_refresh_interval_max` is used directly so the background
    /// refresh task still runs.
    #[tokio::test]
    async fn refresh_max_used_when_jwt_has_no_ttl() {
        let http_client = HttpClient::new(HttpClientConfig {
            max_retries: 0,
            retry_delay: Duration::from_millis(3),
            request_timeout: Duration::from_millis(500),
        max_response_size_bytes: None,
        })
        .expect("http client should be constructed");

        let validators = JwtValidatorCache::default();
        let key_service = KeyService::default();
        let mut mock_server = MockServer::new_with_defaults().await.unwrap();
        key_service
            .get_keys_using_oidc(&mock_server.openid_config(), None, http_client.clone())
            .await
            .unwrap();
        // initial JWT has no `ttl` claim - relies on the max to schedule a refresh
        mock_server
            .generate_status_list_endpoint_without_ttl(1u8.try_into().unwrap(), &[0b1111_1110]);
        let status_list = StatusListCache::default();

        let ti = TrustedIssuer::new(
            "some_iss".into(),
            "is a trusted issuer".into(),
            mock_server.openid_config_endpoint().unwrap(),
            HashMap::default(),
        );

        let iss_config = IssuerConfig {
            issuer_id: "some_iss_id".into(),
            policy: Arc::new(ti),
            openid_config: Some(mock_server.openid_config()),
        };
        validators.init_for_iss(
            &iss_config,
            &JwtConfig {
                jwks: None,
                jwt_sig_validation: false,
                jwt_status_validation: true,
                signature_algorithms_supported: HashSet::from([Algorithm::HS256]),
                ..Default::default()
            },
            &status_list,
            None,
        );
        // 1-second max so the test runs fast
        status_list
            .init_for_iss(
                &iss_config,
                InitForIssArgs {
                    validators: &validators,
                    key_service: &key_service,
                    token_cache: TokenCache::default(),
                    logger: None,
                    http_client,
                    refresh_interval_max: Duration::from_millis(200),
                },
            )
            .await
            .unwrap();

        let status_list_uri = mock_server.status_list_endpoint().unwrap().to_string();

        // Update server-side list (still without ttl) before refresh fires
        mock_server
            .generate_status_list_endpoint_without_ttl(1u8.try_into().unwrap(), &[0b0000_0001]);

        tokio::time::sleep(Duration::from_millis(600)).await;

        let lists = status_list
            .status_lists
            .read()
            .expect("obtain status_lists read lock");
        let got = lists
            .get(&status_list_uri)
            .expect("should have a status list");
        assert_eq!(
            *got,
            StatusList {
                bit_size: 1u8.try_into().unwrap(),
                list: vec![0b0000_0001],
            },
            "max interval did not trigger a refresh",
        );
    }

    /// When the Status List JWT's `ttl` exceeds the bootstrap-config
    /// `status_list_refresh_interval_max`, the refresh loop must cap the sleep
    /// at the max so the cache still refreshes before the JWT's own ttl.
    #[tokio::test]
    async fn refresh_capped_when_jwt_ttl_exceeds_max() {
        let http_client = HttpClient::new(HttpClientConfig {
            max_retries: 0,
            retry_delay: Duration::from_millis(3),
            request_timeout: Duration::from_millis(500),
        max_response_size_bytes: None,
        })
        .expect("http client should be constructed");

        let validators = JwtValidatorCache::default();
        let key_service = KeyService::default();
        let mut mock_server = MockServer::new_with_defaults().await.unwrap();
        key_service
            .get_keys_using_oidc(&mock_server.openid_config(), None, http_client.clone())
            .await
            .unwrap();
        // JWT ttl = 10s, much larger than refresh_interval_max = 1s
        mock_server.generate_status_list_endpoint(
            1u8.try_into().unwrap(),
            &[0b1111_1110],
            Some(10),
        );
        let status_list = StatusListCache::default();

        let ti = TrustedIssuer::new(
            "some_iss".into(),
            "is a trusted issuer".into(),
            mock_server.openid_config_endpoint().unwrap(),
            HashMap::default(),
        );

        let iss_config = IssuerConfig {
            issuer_id: "some_iss_id".into(),
            policy: Arc::new(ti),
            openid_config: Some(mock_server.openid_config()),
        };
        validators.init_for_iss(
            &iss_config,
            &JwtConfig {
                jwks: None,
                jwt_sig_validation: false,
                jwt_status_validation: true,
                signature_algorithms_supported: HashSet::from([Algorithm::HS256]),
                ..Default::default()
            },
            &status_list,
            None,
        );
        status_list
            .init_for_iss(
                &iss_config,
                InitForIssArgs {
                    validators: &validators,
                    key_service: &key_service,
                    token_cache: TokenCache::default(),
                    logger: None,
                    http_client,
                    refresh_interval_max: Duration::from_millis(200),
                },
            )
            .await
            .unwrap();

        let status_list_uri = mock_server.status_list_endpoint().unwrap().to_string();

        // Update the server-side list before the cap-driven refresh fires.
        mock_server.generate_status_list_endpoint(
            1u8.try_into().unwrap(),
            &[0b0000_0001],
            Some(10),
        );

        // Sleep > refresh_interval_max but < JWT ttl: refresh only fires if
        // the loop honored min(ttl, refresh_interval_max).
        tokio::time::sleep(Duration::from_millis(600)).await;

        let lists = status_list
            .status_lists
            .read()
            .expect("obtain status_lists read lock");
        let got = lists
            .get(&status_list_uri)
            .expect("should have a status list");
        assert_eq!(
            *got,
            StatusList {
                bit_size: 1u8.try_into().unwrap(),
                list: vec![0b0000_0001],
            },
            "refresh did not respect the configured cap over the JWT ttl",
        );
    }
}