acton-service 0.29.0

Production-ready Rust backend framework with type-enforced API versioning
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
//! gRPC middleware utilities
//!
//! Provides Tower middleware that can be used with gRPC services.

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Instant;
use tonic::server::NamedService;
use tonic::Status;
use tower::{Layer, Service};
use tracing::Instrument;

use crate::grpc::interceptors::RequestIdExtension;
use crate::middleware::token::{extract_token, TokenValidator};

/// Logging middleware for gRPC requests
///
/// An HTTP-level tower layer (gRPC requests are HTTP/2 requests), so it
/// composes both with
/// [`GrpcServicesBuilder::add_service`](crate::grpc::server::GrpcServicesBuilder::add_service)
/// (the `NamedService` impl forwards the inner service's name) and with
/// `axum::Router::layer`.
///
/// Logs the method path and duration of every request. The gRPC status is
/// logged from the `grpc-status` response header, which is only present on
/// trailers-only responses (errors produced before the handler, e.g. by
/// authentication layers); responses whose status arrives in HTTP/2 trailers
/// are logged as status `0`.
#[derive(Clone)]
pub struct LoggingLayer;

impl<S> Layer<S> for LoggingLayer {
    type Service = LoggingService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        LoggingService { inner }
    }
}

/// Logging service implementation
#[derive(Clone)]
pub struct LoggingService<S> {
    inner: S,
}

impl<S: NamedService> NamedService for LoggingService<S> {
    const NAME: &'static str = S::NAME;
}

impl<S, ReqBody, ResBody> Service<http::Request<ReqBody>> for LoggingService<S>
where
    S: Service<http::Request<ReqBody>, Response = http::Response<ResBody>> + Clone + Send + 'static,
    S::Future: Send + 'static,
    S::Error: std::fmt::Display,
    ReqBody: Send + 'static,
    ResBody: Send + 'static,
{
    type Response = http::Response<ResBody>;
    type Error = S::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: http::Request<ReqBody>) -> Self::Future {
        // Take the ready inner service and leave a fresh clone in its place,
        // so the readiness obtained via poll_ready is the one consumed here.
        let clone = self.inner.clone();
        let mut inner = std::mem::replace(&mut self.inner, clone);

        let method = req.uri().path().to_string();

        Box::pin(async move {
            let start = Instant::now();

            tracing::debug!(method = %method, "gRPC request started");

            let result = inner.call(req).await;

            let duration = start.elapsed();

            match &result {
                Ok(response) => {
                    let grpc_status = response
                        .headers()
                        .get("grpc-status")
                        .and_then(|v| v.to_str().ok())
                        .unwrap_or("0");
                    tracing::info!(
                        method = %method,
                        duration_ms = duration.as_millis(),
                        grpc.status_code = grpc_status,
                        "gRPC request completed"
                    );
                }
                Err(error) => {
                    tracing::warn!(
                        method = %method,
                        duration_ms = duration.as_millis(),
                        error = %error,
                        "gRPC request failed"
                    );
                }
            }

            result
        })
    }
}

/// Tracing middleware layer for gRPC
///
/// Creates an OpenTelemetry-compatible span for each gRPC request, with
/// `rpc.service`/`rpc.method` taken from the request URI path and the request
/// ID from a [`RequestIdExtension`] inserted by an earlier layer or from the
/// `x-request-id` header.
///
/// Like [`LoggingLayer`], this is an HTTP-level tower layer with a forwarding
/// `NamedService` impl, so a wrapped service can be registered with
/// [`GrpcServicesBuilder::add_service`](crate::grpc::server::GrpcServicesBuilder::add_service)
/// or layered onto an axum router.
#[derive(Clone)]
pub struct GrpcTracingLayer;

impl<S> Layer<S> for GrpcTracingLayer {
    type Service = GrpcTracingService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        GrpcTracingService { inner }
    }
}

/// Tracing service implementation
#[derive(Clone)]
pub struct GrpcTracingService<S> {
    inner: S,
}

impl<S: NamedService> NamedService for GrpcTracingService<S> {
    const NAME: &'static str = S::NAME;
}

impl<S, ReqBody, ResBody> Service<http::Request<ReqBody>> for GrpcTracingService<S>
where
    S: Service<http::Request<ReqBody>, Response = http::Response<ResBody>> + Clone + Send + 'static,
    S::Future: Send + 'static,
    S::Error: std::fmt::Display,
    ReqBody: Send + 'static,
    ResBody: Send + 'static,
{
    type Response = http::Response<ResBody>;
    type Error = S::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: http::Request<ReqBody>) -> Self::Future {
        // Take the ready inner service and leave a fresh clone in its place,
        // so the readiness obtained via poll_ready is the one consumed here.
        let clone = self.inner.clone();
        let mut inner = std::mem::replace(&mut self.inner, clone);

        let request_id = req
            .extensions()
            .get::<RequestIdExtension>()
            .map(|ext| ext.0.clone())
            .or_else(|| {
                req.headers()
                    .get("x-request-id")
                    .and_then(|v| v.to_str().ok())
                    .map(|s| s.to_string())
            })
            .unwrap_or_else(|| "unknown".to_string());

        let method = req.uri().path().to_string();

        let span = tracing::info_span!(
            "grpc_request",
            otel.kind = "server",
            rpc.system = "grpc",
            rpc.service = %extract_service_name(&method),
            rpc.method = %extract_method_name(&method),
            request_id = %request_id,
        );

        Box::pin(
            async move {
                let start = Instant::now();

                tracing::debug!(method = %method, "gRPC request started");

                let result = inner.call(req).await;

                let duration = start.elapsed();

                match &result {
                    Ok(response) => {
                        // Only trailers-only (error) responses carry grpc-status
                        // in the headers; anything else is OK at this point.
                        let status = response
                            .headers()
                            .get("grpc-status")
                            .and_then(|v| v.to_str().ok())
                            .unwrap_or("0");

                        tracing::info!(
                            duration_ms = duration.as_millis(),
                            grpc.status_code = status,
                            "gRPC request completed"
                        );
                    }
                    Err(error) => {
                        tracing::warn!(
                            duration_ms = duration.as_millis(),
                            error.message = %error,
                            "gRPC request failed"
                        );
                    }
                }

                result
            }
            .instrument(span),
        )
    }
}

/// Extract service name from gRPC method path
///
/// gRPC method paths are in the format: /package.Service/Method
fn extract_service_name(path: &str) -> &str {
    path.trim_start_matches('/')
        .split('/')
        .next()
        .and_then(|s| s.rsplit('.').next())
        .unwrap_or("unknown")
}

/// Extract method name from gRPC method path
fn extract_method_name(path: &str) -> &str {
    path.trim_start_matches('/')
        .split('/')
        .nth(1)
        .unwrap_or("unknown")
}

/// Governor rate limiter shared by every service a [`GrpcRateLimitLayer`]
/// wraps, and by every clone of those services.
#[cfg(feature = "governor")]
type DirectRateLimiter = governor::RateLimiter<
    governor::state::NotKeyed,
    governor::state::InMemoryState,
    governor::clock::DefaultClock,
>;

/// Token bucket rate limiting layer for gRPC
///
/// Sustains `requests_per_period` requests per `period_secs`, allowing spikes
/// up to `burst_size`. Requests over the limit are answered with a gRPC
/// `RESOURCE_EXHAUSTED` status without reaching the inner service. Health and
/// reflection methods (see the module docs) are exempt so infrastructure
/// probes are never throttled.
///
/// The limiter state lives in the layer, so all services wrapped by one layer
/// (and all clones of them) share a single bucket. The bucket is in-memory
/// and per-instance; for distributed rate limiting, use Redis-based rate
/// limiting in your gRPC handlers.
///
/// Like the other layers in this module, this is an HTTP-level tower layer
/// with a forwarding `NamedService` impl, so a wrapped service can be
/// registered with
/// [`GrpcServicesBuilder::add_service`](crate::grpc::server::GrpcServicesBuilder::add_service)
/// or layered onto an axum router.
#[cfg(feature = "governor")]
#[derive(Clone)]
pub struct GrpcRateLimitLayer {
    enabled: bool,
    limiter: Arc<DirectRateLimiter>,
}

#[cfg(feature = "governor")]
impl GrpcRateLimitLayer {
    /// Create a new rate limiting layer
    pub fn new(config: crate::config::LocalRateLimitConfig) -> Self {
        use std::num::NonZeroU32;

        // Replenish one token every period/requests, with bucket capacity
        // burst_size — the same quota shape as the HTTP-side governor.
        let requests = u64::from(config.requests_per_period.max(1));
        let replenish_interval =
            std::time::Duration::from_millis((config.period().as_millis() as u64 / requests).max(1));
        let burst = NonZeroU32::new(config.burst_size.max(1)).expect("max(1) is non-zero");
        let quota = governor::Quota::with_period(replenish_interval)
            .expect("replenish interval is non-zero")
            .allow_burst(burst);

        Self {
            enabled: config.enabled,
            limiter: Arc::new(governor::RateLimiter::direct(quota)),
        }
    }
}

#[cfg(feature = "governor")]
impl<S> Layer<S> for GrpcRateLimitLayer {
    type Service = GrpcRateLimitService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        GrpcRateLimitService {
            inner,
            enabled: self.enabled,
            limiter: self.limiter.clone(),
        }
    }
}

/// Rate limiting service implementation
///
/// See [`GrpcRateLimitLayer`] for usage.
#[cfg(feature = "governor")]
#[derive(Clone)]
pub struct GrpcRateLimitService<S> {
    inner: S,
    enabled: bool,
    limiter: Arc<DirectRateLimiter>,
}

#[cfg(feature = "governor")]
impl<S: NamedService> NamedService for GrpcRateLimitService<S> {
    const NAME: &'static str = S::NAME;
}

#[cfg(feature = "governor")]
impl<S, ReqBody, ResBody> Service<http::Request<ReqBody>> for GrpcRateLimitService<S>
where
    S: Service<http::Request<ReqBody>, Response = http::Response<ResBody>> + Clone + Send + 'static,
    S::Future: Send + 'static,
    ReqBody: Send + 'static,
    ResBody: Default + Send + 'static,
{
    type Response = http::Response<ResBody>;
    type Error = S::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, req: http::Request<ReqBody>) -> Self::Future {
        // Take the ready inner service and leave a fresh clone in its place,
        // so the readiness obtained via poll_ready is the one consumed here.
        let clone = self.inner.clone();
        let mut inner = std::mem::replace(&mut self.inner, clone);

        if self.enabled && !is_grpc_infra_path(req.uri().path()) {
            if let Err(not_until) = self.limiter.check() {
                let retry_after = not_until.wait_time_from(governor::clock::Clock::now(
                    &governor::clock::DefaultClock::default(),
                ));
                tracing::warn!(
                    method = %req.uri().path(),
                    retry_after_ms = retry_after.as_millis(),
                    "gRPC rate limit exceeded"
                );
                let status = Status::resource_exhausted(format!(
                    "Rate limit exceeded; retry in {}ms",
                    retry_after.as_millis()
                ));
                return Box::pin(async move { Ok(status.into_http()) });
            }
        }

        Box::pin(async move { inner.call(req).await })
    }
}

/// Whether a gRPC method path belongs to infrastructure services that are
/// exempt from authentication and authorization.
///
/// Covers the standard health checking protocol (`grpc.health.v1.Health`),
/// which infrastructure probes call without credentials, and server
/// reflection (`grpc.reflection.*`), mirroring the `/health` and `/ready`
/// exemptions on the HTTP side.
pub(crate) fn is_grpc_infra_path(path: &str) -> bool {
    path.starts_with("/grpc.health.v1.Health/") || path.starts_with("/grpc.reflection.")
}

/// HTTP-level token authentication layer for gRPC services
///
/// Validates the `authorization` bearer token (gRPC metadata is carried in
/// HTTP headers) using any [`TokenValidator`] and inserts the resulting
/// [`Claims`](crate::middleware::token::Claims) into the request extensions,
/// where downstream layers such as
/// [`CedarAuthzLayer`](crate::middleware::cedar::CedarAuthzLayer) and service
/// handlers can read them. Requests that fail validation are answered with a
/// gRPC `UNAUTHENTICATED` status without reaching the inner service.
///
/// Unlike the tonic interceptor helpers in
/// [`interceptors`](crate::grpc::interceptors), which run *inside* a generated
/// server via `with_interceptor`, this layer wraps the service from the
/// outside, so claims are available to other wrapping layers. The
/// `NamedService` impl forwards the inner service's name, so a wrapped service
/// can be registered with
/// [`GrpcServicesBuilder::add_service`](crate::grpc::server::GrpcServicesBuilder::add_service).
///
/// Health and reflection service methods are exempt, as are any configured
/// public path prefixes.
///
/// # Example
/// ```ignore
/// let auth_layer = GrpcTokenAuthLayer::new(paseto_auth);
///
/// let services = GrpcServicesBuilder::new()
///     .add_service(auth_layer.layer(MyServiceServer::new(svc)))
///     .build(None);
/// ```
///
/// When token auth is configured through [`Config`](crate::config::Config),
/// the framework applies this layer to all gRPC routes automatically; this
/// type is for manual composition.
///
/// Note: like the tonic interceptor helpers (and unlike the HTTP
/// middleware), this layer validates the token itself but does not consult
/// a token revocation list.
#[derive(Clone)]
pub struct GrpcTokenAuthLayer<V> {
    validator: V,
    public_paths: Arc<[String]>,
}

impl<V> GrpcTokenAuthLayer<V> {
    /// Create a new token authentication layer from a validator
    pub fn new(validator: V) -> Self {
        Self {
            validator,
            public_paths: Arc::from(Vec::new()),
        }
    }

    /// Exempt gRPC method paths starting with any of these prefixes
    ///
    /// Prefixes match against the full method path, e.g.
    /// `"/hello.v1.HelloService/"` exempts every method of that service.
    pub fn with_public_paths(mut self, paths: Vec<String>) -> Self {
        self.public_paths = paths.into();
        self
    }
}

impl<S, V: Clone> Layer<S> for GrpcTokenAuthLayer<V> {
    type Service = GrpcTokenAuthService<S, V>;

    fn layer(&self, inner: S) -> Self::Service {
        GrpcTokenAuthService {
            inner,
            validator: self.validator.clone(),
            public_paths: self.public_paths.clone(),
        }
    }
}

/// HTTP-level token authentication service for gRPC
///
/// See [`GrpcTokenAuthLayer`] for usage.
#[derive(Clone)]
pub struct GrpcTokenAuthService<S, V> {
    inner: S,
    validator: V,
    public_paths: Arc<[String]>,
}

impl<S: NamedService, V> NamedService for GrpcTokenAuthService<S, V> {
    const NAME: &'static str = S::NAME;
}

impl<S, V, ReqBody, ResBody> Service<http::Request<ReqBody>> for GrpcTokenAuthService<S, V>
where
    S: Service<http::Request<ReqBody>, Response = http::Response<ResBody>> + Clone + Send + 'static,
    S::Future: Send + 'static,
    V: TokenValidator,
    ReqBody: Send + 'static,
    ResBody: Default + Send + 'static,
{
    type Response = http::Response<ResBody>;
    type Error = S::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, mut req: http::Request<ReqBody>) -> Self::Future {
        // Take the ready inner service and leave a fresh clone in its place,
        // so the readiness obtained via poll_ready is the one consumed here.
        let clone = self.inner.clone();
        let mut inner = std::mem::replace(&mut self.inner, clone);

        let path = req.uri().path();
        let public = is_grpc_infra_path(path)
            || self
                .public_paths
                .iter()
                .any(|p| path.starts_with(p.as_str()));

        if !public {
            let validated = extract_token(req.headers())
                .and_then(|token| self.validator.validate_token(&token));
            match validated {
                Ok(claims) => {
                    tracing::debug!(
                        sub = %claims.sub,
                        roles = ?claims.roles,
                        "gRPC request authenticated"
                    );
                    req.extensions_mut().insert(claims);
                }
                Err(e) => {
                    let status = Status::unauthenticated(e.to_string());
                    return Box::pin(async move { Ok(status.into_http()) });
                }
            }
        }

        Box::pin(async move { inner.call(req).await })
    }
}

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

    #[test]
    fn test_extract_service_name() {
        assert_eq!(
            extract_service_name("/example.v1.Greeter/SayHello"),
            "Greeter"
        );
        assert_eq!(
            extract_service_name("/mypackage.UserService/GetUser"),
            "UserService"
        );
        assert_eq!(extract_service_name("/Service/Method"), "Service");
        // For malformed paths, the function extracts what it can
        assert_eq!(extract_service_name("invalid"), "invalid");
        // Empty path returns empty string (first split element)
        assert_eq!(extract_service_name(""), "");
    }

    #[test]
    fn test_extract_method_name() {
        assert_eq!(
            extract_method_name("/example.v1.Greeter/SayHello"),
            "SayHello"
        );
        assert_eq!(
            extract_method_name("/mypackage.UserService/GetUser"),
            "GetUser"
        );
        assert_eq!(extract_method_name("/Service/Method"), "Method");
        assert_eq!(extract_method_name("invalid"), "unknown");
    }

    #[test]
    fn test_is_grpc_infra_path() {
        assert!(is_grpc_infra_path("/grpc.health.v1.Health/Check"));
        assert!(is_grpc_infra_path("/grpc.health.v1.Health/Watch"));
        assert!(is_grpc_infra_path(
            "/grpc.reflection.v1.ServerReflection/ServerReflectionInfo"
        ));
        assert!(is_grpc_infra_path(
            "/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo"
        ));
        assert!(!is_grpc_infra_path("/hello.v1.HelloService/SayHello"));
    }

    mod token_auth {
        use super::super::*;
        use crate::error::Error;
        use crate::middleware::token::Claims;
        use std::convert::Infallible;

        #[derive(Clone)]
        struct TestValidator;

        impl TokenValidator for TestValidator {
            fn validate_token(&self, token: &str) -> Result<Claims, Error> {
                if token == "good" {
                    Ok(Claims {
                        sub: "user:123".to_string(),
                        email: None,
                        username: None,
                        roles: vec![],
                        perms: vec![],
                        exp: 0,
                        iat: None,
                        jti: None,
                        iss: None,
                        aud: None,
                        custom: Default::default(),
                    })
                } else {
                    Err(Error::Unauthorized("Invalid token".to_string()))
                }
            }
        }

        /// Reports whether Claims were present when the request arrived
        #[derive(Clone)]
        struct ClaimsProbe;

        impl NamedService for ClaimsProbe {
            const NAME: &'static str = "test.v1.ClaimsProbe";
        }

        impl Service<http::Request<String>> for ClaimsProbe {
            type Response = http::Response<String>;
            type Error = Infallible;
            type Future = std::future::Ready<Result<Self::Response, Self::Error>>;

            fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
                Poll::Ready(Ok(()))
            }

            fn call(&mut self, req: http::Request<String>) -> Self::Future {
                let body = if req.extensions().get::<Claims>().is_some() {
                    "claims"
                } else {
                    "no-claims"
                };
                std::future::ready(Ok(http::Response::new(body.to_string())))
            }
        }

        fn request(path: &str, token: Option<&str>) -> http::Request<String> {
            let mut builder = http::Request::builder().uri(path);
            if let Some(token) = token {
                builder = builder.header("authorization", format!("Bearer {token}"));
            }
            builder.body(String::new()).unwrap()
        }

        fn grpc_status(resp: &http::Response<String>) -> Option<&str> {
            resp.headers()
                .get("grpc-status")
                .and_then(|v| v.to_str().ok())
        }

        fn service() -> GrpcTokenAuthService<ClaimsProbe, TestValidator> {
            GrpcTokenAuthLayer::new(TestValidator).layer(ClaimsProbe)
        }

        #[test]
        fn named_service_impl_forwards_the_inner_name() {
            assert_eq!(
                <GrpcTokenAuthService<ClaimsProbe, TestValidator> as NamedService>::NAME,
                "test.v1.ClaimsProbe"
            );
        }

        #[tokio::test]
        async fn valid_token_injects_claims() {
            let resp = service()
                .call(request("/hello.v1.HelloService/SayHello", Some("good")))
                .await
                .unwrap();
            assert_eq!(resp.body(), "claims");
        }

        #[tokio::test]
        async fn missing_token_is_unauthenticated() {
            let resp = service()
                .call(request("/hello.v1.HelloService/SayHello", None))
                .await
                .unwrap();
            // tonic Code::Unauthenticated == 16
            assert_eq!(grpc_status(&resp), Some("16"));
        }

        #[tokio::test]
        async fn invalid_token_is_unauthenticated() {
            let resp = service()
                .call(request("/hello.v1.HelloService/SayHello", Some("bad")))
                .await
                .unwrap();
            assert_eq!(grpc_status(&resp), Some("16"));
        }

        #[tokio::test]
        async fn health_service_is_exempt() {
            let resp = service()
                .call(request("/grpc.health.v1.Health/Check", None))
                .await
                .unwrap();
            assert_eq!(resp.body(), "no-claims");
        }

        #[tokio::test]
        async fn public_path_prefixes_are_exempt() {
            let mut svc = GrpcTokenAuthLayer::new(TestValidator)
                .with_public_paths(vec!["/hello.v1.HelloService/".to_string()])
                .layer(ClaimsProbe);
            let resp = svc
                .call(request("/hello.v1.HelloService/SayHello", None))
                .await
                .unwrap();
            assert_eq!(resp.body(), "no-claims");

            let resp = svc
                .call(request("/other.v1.OtherService/Do", None))
                .await
                .unwrap();
            assert_eq!(grpc_status(&resp), Some("16"));
        }
    }

    mod observability_and_rate_limit {
        use super::super::*;
        use std::convert::Infallible;

        /// Echoes the request path back as the response body
        #[derive(Clone)]
        struct Echo;

        impl NamedService for Echo {
            const NAME: &'static str = "test.v1.Echo";
        }

        impl Service<http::Request<String>> for Echo {
            type Response = http::Response<String>;
            type Error = Infallible;
            type Future = std::future::Ready<Result<Self::Response, Self::Error>>;

            fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
                Poll::Ready(Ok(()))
            }

            fn call(&mut self, req: http::Request<String>) -> Self::Future {
                std::future::ready(Ok(http::Response::new(req.uri().path().to_string())))
            }
        }

        fn request(path: &str) -> http::Request<String> {
            http::Request::builder()
                .uri(path)
                .body(String::new())
                .unwrap()
        }

        #[test]
        fn named_service_impls_forward_the_inner_name() {
            assert_eq!(<LoggingService<Echo> as NamedService>::NAME, "test.v1.Echo");
            assert_eq!(
                <GrpcTracingService<Echo> as NamedService>::NAME,
                "test.v1.Echo"
            );
            #[cfg(feature = "governor")]
            assert_eq!(
                <GrpcRateLimitService<Echo> as NamedService>::NAME,
                "test.v1.Echo"
            );
        }

        #[tokio::test]
        async fn logging_layer_passes_the_request_through() {
            let mut svc = LoggingLayer.layer(Echo);
            let resp = svc
                .call(request("/hello.v1.HelloService/SayHello"))
                .await
                .unwrap();
            assert_eq!(resp.body(), "/hello.v1.HelloService/SayHello");
        }

        #[tokio::test]
        async fn tracing_layer_passes_the_request_through() {
            let mut svc = GrpcTracingLayer.layer(Echo);
            let resp = svc
                .call(request("/hello.v1.HelloService/SayHello"))
                .await
                .unwrap();
            assert_eq!(resp.body(), "/hello.v1.HelloService/SayHello");
        }

        #[cfg(feature = "governor")]
        mod rate_limit {
            use super::*;
            use crate::config::LocalRateLimitConfig;

            fn config(enabled: bool) -> LocalRateLimitConfig {
                // One request per hour with burst 1, so the second request in
                // a test deterministically exceeds the limit.
                LocalRateLimitConfig {
                    enabled,
                    requests_per_period: 1,
                    period_secs: 3600,
                    burst_size: 1,
                }
            }

            fn grpc_status(resp: &http::Response<String>) -> Option<&str> {
                resp.headers()
                    .get("grpc-status")
                    .and_then(|v| v.to_str().ok())
            }

            #[tokio::test]
            async fn requests_over_the_burst_get_resource_exhausted() {
                let mut svc = GrpcRateLimitLayer::new(config(true)).layer(Echo);

                let first = svc
                    .call(request("/hello.v1.HelloService/SayHello"))
                    .await
                    .unwrap();
                assert_eq!(grpc_status(&first), None);

                let second = svc
                    .call(request("/hello.v1.HelloService/SayHello"))
                    .await
                    .unwrap();
                // tonic Code::ResourceExhausted == 8
                assert_eq!(grpc_status(&second), Some("8"));
            }

            #[tokio::test]
            async fn disabled_limiter_passes_everything_through() {
                let mut svc = GrpcRateLimitLayer::new(config(false)).layer(Echo);

                for _ in 0..3 {
                    let resp = svc
                        .call(request("/hello.v1.HelloService/SayHello"))
                        .await
                        .unwrap();
                    assert_eq!(grpc_status(&resp), None);
                }
            }

            #[tokio::test]
            async fn health_service_is_exempt() {
                let mut svc = GrpcRateLimitLayer::new(config(true)).layer(Echo);

                for _ in 0..3 {
                    let resp = svc
                        .call(request("/grpc.health.v1.Health/Check"))
                        .await
                        .unwrap();
                    assert_eq!(grpc_status(&resp), None);
                }
            }

            #[tokio::test]
            async fn limiter_state_is_shared_across_wrapped_services() {
                let layer = GrpcRateLimitLayer::new(config(true));
                let mut first_svc = layer.layer(Echo);
                let mut second_svc = layer.layer(Echo);

                let first = first_svc
                    .call(request("/hello.v1.HelloService/SayHello"))
                    .await
                    .unwrap();
                assert_eq!(grpc_status(&first), None);

                // The second service shares the layer's bucket, so the burst
                // consumed above applies to it too.
                let second = second_svc
                    .call(request("/hello.v1.HelloService/SayHello"))
                    .await
                    .unwrap();
                assert_eq!(grpc_status(&second), Some("8"));
            }
        }
    }
}