saddle-framework 0.3.30

The single business-facing facade for Saddle applications
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
//! Generated standard unary protocol types. Application capabilities own calls.
use saddle_boundary::standard_wire::{Encode, MethodSpec};

/// Implemented by the frozen descriptor binding generator. A method has one
/// full service/method identity and descriptor-defined input and output.
#[doc(hidden)]
pub trait Method: 'static {
    type Fields<'a>: Encode;
    type View<'a>;
    const SPEC: MethodSpec;
    fn view<'a>(bytes: &'a [u8]) -> Self::View<'a>;
}

/// Immutable request storage, billed until the transport encoder finishes.
pub struct Request<M: Method> {
    pub(crate) wire: saddle_admission::ReadOnlyInput<Vec<u8>>,
    _method: std::marker::PhantomData<fn() -> M>,
}
/// Owned response wire remains billed for as long as the business retains it.
pub struct Response<M: Method> {
    pub(crate) wire: saddle_admission::ReadOnlyInput<Vec<u8>>,
    _method: std::marker::PhantomData<fn() -> M>,
}
impl<M: Method> Response<M> {
    pub fn message(&self) -> M::View<'_> {
        M::view(self.wire.get())
    }
    pub fn storage_bytes(&self) -> usize {
        self.wire.storage_bytes()
    }
    pub(crate) fn from_wire(wire: saddle_admission::ReadOnlyInput<Vec<u8>>) -> Self {
        Self {
            wire,
            _method: std::marker::PhantomData,
        }
    }
}
impl<M: Method> Request<M> {
    pub fn storage_bytes(&self) -> usize {
        self.wire.storage_bytes()
    }
}
#[derive(Debug)]
#[doc(hidden)]
pub enum BuildError {
    Unregistered,
    TooLarge,
    Wire(saddle_boundary::standard_wire::WireError),
    Resource(saddle_admission::AdmissionError),
}
impl std::fmt::Display for BuildError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Unregistered => f.write_str("gRPC method is not registered in the application"),
            Self::TooLarge => f.write_str("gRPC request exceeds the one-megabyte wire limit"),
            Self::Wire(error) => error.fmt(f),
            Self::Resource(error) => error.fmt(f),
        }
    }
}
impl std::error::Error for BuildError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Wire(error) => Some(error),
            Self::Resource(error) => Some(error),
            _ => None,
        }
    }
}

pub(crate) fn registered<M: Method>(registry: &[&MethodSpec]) -> bool {
    registry.iter().any(|entry| {
        (entry.marker)() == std::any::TypeId::of::<M>()
            && entry.dependency == M::SPEC.dependency
            && entry.contract == M::SPEC.contract
            && entry.service == M::SPEC.service
            && entry.method == M::SPEC.method
            && entry.path == M::SPEC.path
            && std::ptr::eq(entry.request, M::SPEC.request)
            && std::ptr::eq(entry.response, M::SPEC.response)
    })
}
/// Generated assembly only: type identity and complete descriptor registration
/// are checked before any codec invocation or storage allocation.
#[doc(hidden)]
pub fn prepare<M: Method>(
    memory: &saddle_admission::RequestMemory,
    registry: &[&MethodSpec],
    fields: &M::Fields<'_>,
) -> Result<Request<M>, BuildError> {
    if !registered::<M>(registry) {
        return Err(BuildError::Unregistered);
    }
    let len = fields.encoded_len().map_err(BuildError::Wire)?;
    if len > saddle_boundary::MAX_UNARY_PAYLOAD_BYTES {
        return Err(BuildError::TooLarge);
    }
    let mut original = None;
    let wire = memory
        .framework_output(|builder| {
            builder.write_bytes(len, |bytes| {
                let mut writer = saddle_boundary::standard_wire::Writer::new(bytes);
                fields
                    .encode(&mut writer)
                    .and_then(|_| writer.finish())
                    .map_err(|error| {
                        original = Some(error);
                        saddle_admission::AdmissionError::ResponseCapacityExceeded
                    })
            })
        })
        .map_err(|error| match original {
            Some(error) => BuildError::Wire(error),
            None => BuildError::Resource(error),
        })?;
    M::SPEC
        .request
        .validate(wire.get())
        .map_err(BuildError::Wire)?;
    Ok(Request {
        wire,
        _method: std::marker::PhantomData,
    })
}

/// Startup-only endpoint input. Tokens have no Debug or Serialize projection.
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
#[doc(hidden)]
pub struct DependencyConfig {
    pub authority: String,
    #[serde(default)]
    pub token: Option<String>,
    #[serde(default)]
    pub metadata: Vec<saddle_boundary::standard_rpc::MetadataConfig>,
}

#[derive(Clone)]
#[doc(hidden)]
pub struct FrozenRegistry {
    methods: &'static [&'static MethodSpec],
    endpoints: std::sync::Arc<[FrozenEndpoint]>,
    identities: std::sync::Arc<[FrozenMethodIdentity]>,
}
struct FrozenMethodIdentity {
    method: &'static MethodSpec,
    module: saddle_core::ModuleId,
    service: saddle_core::ServiceId,
    operation: saddle_core::OperationId,
}
struct FrozenEndpoint {
    alias: &'static str,
    authority: saddle_boundary::ProfuseContractAuthorityTemplate,
    metadata: Box<[saddle_boundary::standard_rpc::FrozenMetadata]>,
}
#[derive(Debug)]
pub(crate) enum RegistrationError {
    Empty,
    DuplicateMethod,
    MissingDependency(&'static str),
    UnexpectedDependency(String),
    InvalidAuthority(saddle_boundary::BoundaryError),
    InvalidMetadata(&'static str),
}
impl std::fmt::Display for RegistrationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Empty => f.write_str("standard gRPC registry must select a method"),
            Self::DuplicateMethod => f.write_str("duplicate selected gRPC method identity"),
            Self::MissingDependency(alias) => write!(f, "missing gRPC dependency `{alias}`"),
            Self::UnexpectedDependency(alias) => {
                write!(f, "unregistered gRPC dependency `{alias}`")
            }
            Self::InvalidAuthority(error) => error.fmt(f),
            Self::InvalidMetadata(reason) => f.write_str(reason),
        }
    }
}
impl std::error::Error for RegistrationError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::InvalidAuthority(error) => Some(error),
            _ => None,
        }
    }
}
impl FrozenRegistry {
    // Called only during generated startup, before listener publication.
    pub(crate) fn freeze(
        methods: &'static [&'static MethodSpec],
        mut config: std::collections::BTreeMap<String, DependencyConfig>,
    ) -> Result<Self, RegistrationError> {
        if methods.is_empty() {
            return Err(RegistrationError::Empty);
        }
        for (index, method) in methods.iter().enumerate() {
            if methods[..index].iter().any(|prior| {
                (prior.marker)() == (method.marker)()
                    || (prior.dependency == method.dependency
                        && prior.service == method.service
                        && prior.method == method.method)
            }) {
                return Err(RegistrationError::DuplicateMethod);
            }
        }
        let mut endpoints = Vec::new();
        for method in methods {
            if endpoints
                .iter()
                .any(|entry: &FrozenEndpoint| entry.alias == method.dependency)
            {
                continue;
            }
            let input = config
                .remove(method.dependency)
                .ok_or(RegistrationError::MissingDependency(method.dependency))?;
            let mut headers = std::collections::BTreeSet::new();
            let metadata = input
                .metadata
                .into_iter()
                .map(|mapping| {
                    if !headers.insert(mapping.header.clone()) {
                        return Err(RegistrationError::InvalidMetadata(
                            "duplicate trace metadata header",
                        ));
                    }
                    saddle_boundary::standard_rpc::FrozenMetadata::freeze(mapping)
                        .map_err(RegistrationError::InvalidMetadata)
                })
                .collect::<Result<Vec<_>, _>>()?
                .into_boxed_slice();
            let authority = saddle_boundary::ProfuseContractAuthorityTemplate::new(input.authority)
                .and_then(|value| value.with_token(input.token))
                .map_err(RegistrationError::InvalidAuthority)?;
            endpoints.push(FrozenEndpoint {
                alias: method.dependency,
                authority,
                metadata,
            });
        }
        if let Some((alias, _)) = config.into_iter().next() {
            return Err(RegistrationError::UnexpectedDependency(alias));
        }
        Ok(Self {
            methods,
            endpoints: endpoints.into(),
            identities: methods
                .iter()
                .map(|method| FrozenMethodIdentity {
                    method,
                    module: saddle_core::ModuleId::new(method.dependency),
                    service: saddle_core::ServiceId::new(method.service),
                    operation: saddle_core::OperationId::new(method.method),
                })
                .collect::<Vec<_>>()
                .into(),
        })
    }
    pub(crate) fn endpoint<M: Method>(
        &self,
    ) -> Option<&saddle_boundary::ProfuseContractAuthorityTemplate> {
        if !registered::<M>(self.methods) {
            return None;
        }
        self.endpoints
            .iter()
            .find(|entry| entry.alias == M::SPEC.dependency)
            .map(|entry| &entry.authority)
    }
    pub(crate) fn metadata<M: Method>(&self) -> &[saddle_boundary::standard_rpc::FrozenMetadata] {
        if !registered::<M>(self.methods) {
            return &[];
        }
        self.endpoints
            .iter()
            .find(|entry| entry.alias == M::SPEC.dependency)
            .map(|entry| entry.metadata.as_ref())
            .unwrap_or(&[])
    }
    pub(crate) fn child_context<M: Method>(
        &self,
        observer: &saddle_observability::Observer,
        parent: &saddle_core::CallContext,
    ) -> Option<saddle_core::CallContext> {
        if !registered::<M>(self.methods) {
            return None;
        }
        let identity = self
            .identities
            .iter()
            .find(|identity| (identity.method.marker)() == std::any::TypeId::of::<M>())?;
        Some(observer.managed_child_context(
            parent,
            identity.module.clone(),
            identity.service.clone(),
            identity.operation.clone(),
        ))
    }
    pub(crate) fn methods(&self) -> &'static [&'static MethodSpec] {
        self.methods
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    crate::grpc_bindings! {
        mod protocol {
            proto_root "../../tests/fixtures/standard-grpc";
            dependency mirror { service "alpha.Mirror"; methods { Echo => "Echo"; } }
            dependency alternate { service "beta.Alternate"; methods { Echo => "Echo"; } }
        }
    }
    fn config() -> std::collections::BTreeMap<String, DependencyConfig> {
        [
            ("mirror", "http://mirror-{zone}:50051"),
            ("alternate", "http://127.0.0.1:50052"),
        ]
        .into_iter()
        .map(|(alias, authority)| {
            (
                alias.to_owned(),
                DependencyConfig {
                    authority: authority.to_owned(),
                    token: None,
                    metadata: Vec::new(),
                },
            )
        })
        .collect()
    }
    // A manually implemented method can copy all public metadata, including
    // the generated marker callback, but cannot acquire that marker's TypeId.
    struct Forged;
    impl Method for Forged {
        type Fields<'a> = <protocol::methods::mirror::Echo as Method>::Fields<'a>;
        type View<'a> = <protocol::methods::mirror::Echo as Method>::View<'a>;
        const SPEC: MethodSpec = <protocol::methods::mirror::Echo as Method>::SPEC;
        fn view<'a>(bytes: &'a [u8]) -> Self::View<'a> {
            <protocol::methods::mirror::Echo as Method>::view(bytes)
        }
    }
    #[test]
    fn frozen_aliases_and_actual_marker_identity_guard_endpoints() {
        let registry = FrozenRegistry::freeze(protocol::METHODS, config()).unwrap();
        assert_eq!(
            registry
                .endpoint::<protocol::methods::mirror::Echo>()
                .unwrap()
                .resolve("zone-a")
                .unwrap()
                .as_str(),
            "http://mirror-zone-a:50051"
        );
        assert_eq!(
            registry
                .endpoint::<protocol::methods::alternate::Echo>()
                .unwrap()
                .as_str(),
            "http://127.0.0.1:50052"
        );
        assert!(registry.endpoint::<Forged>().is_none());
        assert_eq!(registry.methods().len(), 2);
    }
    #[test]
    fn startup_rejects_missing_extra_and_invalid_endpoint_credentials() {
        let mut input = config();
        input.remove("alternate");
        assert!(matches!(
            FrozenRegistry::freeze(protocol::METHODS, input),
            Err(RegistrationError::MissingDependency("alternate"))
        ));
        let mut input = config();
        input.insert(
            "unselected".into(),
            DependencyConfig {
                authority: "http://unused:1".into(),
                token: None,
                metadata: Vec::new(),
            },
        );
        assert!(matches!(
            FrozenRegistry::freeze(protocol::METHODS, input),
            Err(RegistrationError::UnexpectedDependency(_))
        ));
        let mut input = config();
        input.get_mut("mirror").unwrap().token = Some("invalid\ncredential".into());
        assert!(matches!(
            FrozenRegistry::freeze(protocol::METHODS, input),
            Err(RegistrationError::InvalidAuthority(_))
        ));
        assert!(matches!(
            FrozenRegistry::freeze(&[], config()),
            Err(RegistrationError::Empty)
        ));
        static DUPLICATE: &[&MethodSpec] = &[
            &<protocol::methods::mirror::Echo as Method>::SPEC,
            &<protocol::methods::mirror::Echo as Method>::SPEC,
        ];
        assert!(matches!(
            FrozenRegistry::freeze(DUPLICATE, config()),
            Err(RegistrationError::DuplicateMethod)
        ));
    }
    #[test]
    fn trace_mapping_rejects_reserved_headers_and_duplicate_destinations() {
        use saddle_boundary::standard_rpc::{MetadataConfig, MetadataSource};
        for header in [
            "authorization",
            "grpc-timeout",
            "content-type",
            "host",
            "X-UPPER",
        ] {
            let mut input = config();
            input
                .get_mut("mirror")
                .unwrap()
                .metadata
                .push(MetadataConfig {
                    header: header.into(),
                    source: MetadataSource::TraceId,
                });
            assert!(matches!(
                FrozenRegistry::freeze(protocol::METHODS, input),
                Err(RegistrationError::InvalidMetadata(_))
            ));
        }
        let mut input = config();
        input.get_mut("mirror").unwrap().metadata = vec![
            MetadataConfig {
                header: "x-trace".into(),
                source: MetadataSource::TraceId,
            },
            MetadataConfig {
                header: "x-trace".into(),
                source: MetadataSource::RpcId,
            },
        ];
        assert!(matches!(
            FrozenRegistry::freeze(protocol::METHODS, input),
            Err(RegistrationError::InvalidMetadata(_))
        ));
    }
}