praxis-proxy-core 0.7.0

Configuration, error types, and server factory for Praxis
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024 Praxis Contributors

//! Request-phase condition predicates that gate filter execution.

use std::collections::HashMap;

use serde::Deserialize;

use super::{impl_condition_deserialize, impl_condition_serialize};

// -----------------------------------------------------------------------------
// Condition
// -----------------------------------------------------------------------------

/// Gates filter execution: `When` requires a match, `Unless` skips on match.
///
/// ```
/// use praxis_core::config::Condition;
///
/// let conditions: Vec<Condition> = serde_yaml::from_str(
///     r#"
/// - when:
///     path_prefix: "/api"
/// - unless:
///     methods: ["OPTIONS"]
/// "#,
/// )
/// .unwrap();
/// assert_eq!(conditions.len(), 2);
/// ```
#[derive(Clone, Debug)]
pub enum Condition {
    /// Execute the filter only if the predicate matches.
    When(ConditionMatch),

    /// Skip the filter if the predicate matches.
    Unless(ConditionMatch),
}

impl_condition_deserialize!(Condition, ConditionMatch, "condition");
impl_condition_serialize!(Condition, ConditionMatch);

// -----------------------------------------------------------------------------
// ConditionMatch
// -----------------------------------------------------------------------------

/// Match predicate for a condition (AND semantics).
///
/// ```
/// use praxis_core::config::ConditionMatch;
///
/// let m: ConditionMatch = serde_yaml::from_str(
///     r#"
/// path_prefix: "/api"
/// methods: ["GET", "POST"]
/// grpc: true
/// "#,
/// )
/// .unwrap();
/// assert_eq!(m.path_prefix.as_deref(), Some("/api"));
/// assert_eq!(m.methods.as_ref().unwrap().len(), 2);
/// assert_eq!(m.grpc, Some(true));
/// ```
#[derive(Clone, Debug, Deserialize, serde::Serialize)]
#[serde(deny_unknown_fields)]
pub struct ConditionMatch {
    /// Request must (`true`) or must not (`false`) be gRPC.
    ///
    /// Classified from the `content-type` header alone, so the
    /// predicate is independent of filter ordering and does not
    /// require the `grpc_detection` filter in the chain.
    #[serde(default)]
    pub grpc: Option<bool>,

    /// Request URI must match this exact path.
    #[serde(default)]
    pub path: Option<String>,

    /// Request URI must match this prefix at a segment boundary.
    /// `/api` matches `/api`, `/api/`, `/api/v1` but NOT `/apikeys`.
    #[serde(default)]
    pub path_prefix: Option<String>,

    /// Request method must be one of these (case-insensitive).
    #[serde(default)]
    pub methods: Option<Vec<String>>,

    /// Headers that must be present and match.
    #[serde(default)]
    pub headers: Option<HashMap<String, String>>,

    /// Request must be bound to a logical upstream matching this
    /// predicate.
    ///
    /// Only meaningful once the `router` filter has bound an upstream;
    /// validation rejects a `bound_upstream` condition on a filter that
    /// could run before routing. An unbound request never matches.
    #[serde(default)]
    pub bound_upstream: Option<ApplicationMatch>,

    /// Selected-upstream application metadata published by the load balancer.
    ///
    /// Matches the typed protocol/provider of the cluster the load balancer
    /// selected for this exchange, not request headers or writable metadata.
    /// Because the value is published only after upstream selection, a filter
    /// carrying this predicate requires a load balancer guaranteed to run
    /// before it (enforced at build time). Missing metadata never satisfies
    /// the predicate (fail-closed).
    #[serde(default)]
    pub selected_upstream: Option<SelectedUpstreamMatch>,
}

// -----------------------------------------------------------------------------
// ApplicationMatch
// -----------------------------------------------------------------------------

/// Predicate over the request's bound logical upstream (AND semantics
/// across the fields that are set).
///
/// The fields are matched against the `application_protocol` /
/// `application_provider` metadata that the bound cluster declares. Both
/// are opaque, canonical identifiers; a value that could never name a
/// valid cluster tag (e.g. containing uppercase) is rejected at
/// validation time. A field left unset imposes no constraint, but at
/// least one field must be set. If no upstream has been bound, or the
/// bound cluster declares no value for a set field, the predicate does
/// not match.
///
/// ```
/// use praxis_core::config::ApplicationMatch;
///
/// let m: ApplicationMatch = serde_yaml::from_str(
///     r#"
/// application_protocol: "openai_responses"
/// application_provider: "openai"
/// "#,
/// )
/// .unwrap();
/// assert_eq!(m.application_protocol.as_deref(), Some("openai_responses"));
/// assert_eq!(m.application_provider.as_deref(), Some("openai"));
/// ```
#[derive(Clone, Debug, Deserialize, serde::Serialize)]
#[serde(deny_unknown_fields)]
pub struct ApplicationMatch {
    /// Bound cluster's `application_protocol` must equal this value.
    #[serde(default)]
    pub application_protocol: Option<String>,

    /// Bound cluster's `application_provider` must equal this value.
    #[serde(default)]
    pub application_provider: Option<String>,
}

impl ApplicationMatch {
    /// Whether this predicate constrains neither application field.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.application_protocol.is_none() && self.application_provider.is_none()
    }
}

// -----------------------------------------------------------------------------
// SelectedUpstreamMatch
// -----------------------------------------------------------------------------

/// Backward-compatible name for the shared application-metadata predicate used
/// by `selected_upstream` and `bound_upstream` conditions.
///
/// Both fields are optional; an unset field imposes no constraint. A
/// `selected_upstream` condition reads the selection the load balancer
/// published for the exchange; a `bound_upstream` condition reads the
/// request's logical binding. Either way the values are opaque to Praxis
/// core, and a cluster that declared neither field never satisfies a
/// configured one.
///
/// ```
/// use praxis_core::config::SelectedUpstreamMatch;
///
/// let m: SelectedUpstreamMatch = serde_yaml::from_str(
///     r#"
/// application_protocol: openai_chat_completions
/// application_provider: vllm
/// "#,
/// )
/// .unwrap();
/// assert_eq!(
///     m.application_protocol.as_deref(),
///     Some("openai_chat_completions")
/// );
/// assert_eq!(m.application_provider.as_deref(), Some("vllm"));
/// ```
pub type SelectedUpstreamMatch = ApplicationMatch;

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

#[cfg(test)]
#[expect(clippy::allow_attributes, reason = "blanket test suppressions")]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::indexing_slicing,
    clippy::needless_raw_strings,
    clippy::needless_raw_string_hashes,
    clippy::min_ident_chars,
    reason = "tests use unwrap/expect/indexing/raw strings for brevity"
)]
mod tests {
    use super::*;

    #[test]
    fn parse_condition_match_all_fields() {
        let yaml = r#"
path_prefix: "/api"
methods: ["GET", "POST"]
headers:
  x-tenant: "acme"
  x-debug: "true"
"#;
        let m: ConditionMatch = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(m.path_prefix.as_deref(), Some("/api"), "path_prefix mismatch");
        let methods = m.methods.unwrap();
        assert_eq!(methods, vec!["GET", "POST"], "methods mismatch");
        let headers = m.headers.unwrap();
        assert_eq!(headers.get("x-tenant").unwrap(), "acme", "x-tenant header mismatch");
        assert_eq!(headers.get("x-debug").unwrap(), "true", "x-debug header mismatch");
    }

    #[test]
    fn parse_condition_match_partial() {
        let yaml = r#"
path_prefix: "/health"
"#;
        let m: ConditionMatch = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(m.path_prefix.as_deref(), Some("/health"), "path_prefix mismatch");
        assert!(m.methods.is_none(), "methods should be None when omitted");
        assert!(m.headers.is_none(), "headers should be None when omitted");
    }

    #[test]
    fn parse_when_condition() {
        let yaml = r#"
- when:
    path_prefix: "/api"
"#;
        let conditions: Vec<Condition> = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(conditions.len(), 1, "should parse 1 condition");
        assert!(
            matches!(&conditions[0], Condition::When(m) if m.path_prefix.as_deref() == Some("/api")),
            "should be When with /api prefix"
        );
    }

    #[test]
    fn parse_unless_condition() {
        let yaml = r#"
- unless:
    methods: ["OPTIONS"]
"#;
        let conditions: Vec<Condition> = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(conditions.len(), 1, "should parse 1 condition");
        assert!(
            matches!(&conditions[0], Condition::Unless(m) if m.methods.as_ref().unwrap() == &["OPTIONS"]),
            "should be Unless with OPTIONS method"
        );
    }

    #[test]
    fn parse_mixed_conditions() {
        let yaml = r#"
- when:
    path_prefix: "/api"
- unless:
    headers:
      x-internal: "true"
- when:
    methods: ["POST", "PUT", "DELETE"]
"#;
        let conditions: Vec<Condition> = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(conditions.len(), 3, "should parse 3 conditions");
        assert!(matches!(&conditions[0], Condition::When(_)), "first should be When");
        assert!(
            matches!(&conditions[1], Condition::Unless(_)),
            "second should be Unless"
        );
        assert!(matches!(&conditions[2], Condition::When(_)), "third should be When");
    }

    #[test]
    fn parse_empty_conditions() {
        let conditions: Vec<Condition> = serde_yaml::from_str("[]").unwrap();
        assert!(conditions.is_empty(), "empty array should parse to empty vec");
    }

    #[test]
    fn reject_both_when_and_unless() {
        let yaml = r#"
- when:
    path_prefix: "/api"
  unless:
    methods: ["GET"]
"#;
        let err = serde_yaml::from_str::<Vec<Condition>>(yaml).unwrap_err();
        assert!(err.to_string().contains("exactly one"));
    }

    #[test]
    fn reject_neither_when_nor_unless() {
        let yaml = "- {}";
        let err = serde_yaml::from_str::<Vec<Condition>>(yaml).unwrap_err();
        assert!(err.to_string().contains("either"));
    }

    #[test]
    fn parse_grpc_predicate_true() {
        let m: ConditionMatch = serde_yaml::from_str("grpc: true\n").unwrap();
        assert_eq!(m.grpc, Some(true), "grpc: true should parse");
        assert!(m.path.is_none(), "path should stay unset");
    }

    #[test]
    fn parse_grpc_predicate_false() {
        let m: ConditionMatch = serde_yaml::from_str("grpc: false\n").unwrap();
        assert_eq!(m.grpc, Some(false), "grpc: false should parse");
    }

    #[test]
    fn grpc_predicate_defaults_to_unset() {
        let m: ConditionMatch = serde_yaml::from_str("path: \"/\"\n").unwrap();
        assert!(m.grpc.is_none(), "grpc should be None when omitted");
    }

    #[test]
    fn reject_non_boolean_grpc_predicate() {
        let err = serde_yaml::from_str::<ConditionMatch>("grpc: \"yes\"\n").unwrap_err();
        assert!(
            err.to_string().contains("bool"),
            "a non-boolean grpc value should be rejected: {err}"
        );
    }

    #[test]
    fn grpc_predicate_round_trips_through_serialization() {
        let m: ConditionMatch = serde_yaml::from_str("grpc: true\npath_prefix: \"/pkg.Svc\"\n").unwrap();
        let yaml = serde_yaml::to_string(&m).unwrap();
        let back: ConditionMatch = serde_yaml::from_str(&yaml).unwrap();
        assert_eq!(back.grpc, Some(true), "grpc should survive a serialize round trip");
        assert_eq!(
            back.path_prefix.as_deref(),
            Some("/pkg.Svc"),
            "path_prefix should survive a serialize round trip"
        );
    }

    #[test]
    fn parse_exact_path_condition() {
        let m: ConditionMatch = serde_yaml::from_str(
            r#"
path: "/"
"#,
        )
        .unwrap();
        assert_eq!(m.path.as_deref(), Some("/"), "exact path should be /");
        assert!(
            m.path_prefix.is_none(),
            "path_prefix should be None for exact path match"
        );
    }

    #[test]
    fn parse_bound_upstream_condition() {
        let m: ConditionMatch = serde_yaml::from_str(
            r#"
bound_upstream:
  application_protocol: "openai_responses"
  application_provider: "openai"
"#,
        )
        .unwrap();
        let bound = m.bound_upstream.expect("bound_upstream should parse");
        assert_eq!(
            bound.application_protocol.as_deref(),
            Some("openai_responses"),
            "application_protocol mismatch"
        );
        assert_eq!(
            bound.application_provider.as_deref(),
            Some("openai"),
            "application_provider mismatch"
        );
    }

    #[test]
    fn parse_bound_upstream_partial() {
        let m: ConditionMatch = serde_yaml::from_str(
            r#"
bound_upstream:
  application_protocol: "openai_responses"
"#,
        )
        .unwrap();
        let bound = m.bound_upstream.expect("bound_upstream should parse");
        assert_eq!(
            bound.application_protocol.as_deref(),
            Some("openai_responses"),
            "application_protocol mismatch"
        );
        assert!(
            bound.application_provider.is_none(),
            "application_provider should be None when omitted"
        );
    }

    #[test]
    fn bound_upstream_defaults_to_unset() {
        let m: ConditionMatch = serde_yaml::from_str("path: \"/\"\n").unwrap();
        assert!(m.bound_upstream.is_none(), "bound_upstream should be None when omitted");
    }

    #[test]
    fn reject_unknown_bound_upstream_field() {
        let err = serde_yaml::from_str::<ConditionMatch>(
            r#"
bound_upstream:
  application_flavour: "openai"
"#,
        )
        .unwrap_err();
        assert!(
            err.to_string().contains("unknown field"),
            "an unknown bound_upstream field should be rejected: {err}"
        );
    }

    #[test]
    fn bound_upstream_round_trips_through_serialization() {
        let m: ConditionMatch = serde_yaml::from_str(
            r#"
bound_upstream:
  application_protocol: "openai_responses"
  application_provider: "openai"
"#,
        )
        .unwrap();
        let yaml = serde_yaml::to_string(&m).unwrap();
        let back: ConditionMatch = serde_yaml::from_str(&yaml).unwrap();
        let bound = back.bound_upstream.expect("bound_upstream should survive a round trip");
        assert_eq!(
            bound.application_protocol.as_deref(),
            Some("openai_responses"),
            "application_protocol should survive a round trip"
        );
        assert_eq!(
            bound.application_provider.as_deref(),
            Some("openai"),
            "application_provider should survive a round trip"
        );
    }

    #[test]
    fn parse_selected_upstream_both_fields() {
        let m: ConditionMatch = serde_yaml::from_str(
            r#"
selected_upstream:
  application_protocol: openai_chat_completions
  application_provider: vllm
"#,
        )
        .unwrap();
        let su = m.selected_upstream.expect("selected_upstream should parse");
        assert_eq!(
            su.application_protocol.as_deref(),
            Some("openai_chat_completions"),
            "application_protocol mismatch"
        );
        assert_eq!(
            su.application_provider.as_deref(),
            Some("vllm"),
            "application_provider mismatch"
        );
        assert!(!su.is_empty(), "a populated selected_upstream is not empty");
    }

    #[test]
    fn parse_selected_upstream_protocol_only() {
        let m: ConditionMatch = serde_yaml::from_str(
            r#"
selected_upstream:
  application_protocol: openai_responses
"#,
        )
        .unwrap();
        let su = m.selected_upstream.expect("selected_upstream should parse");
        assert_eq!(su.application_protocol.as_deref(), Some("openai_responses"));
        assert!(
            su.application_provider.is_none(),
            "provider should be None when omitted"
        );
    }

    #[test]
    fn selected_upstream_provider_only_round_trips_through_serialization() {
        let m: ConditionMatch = serde_yaml::from_str(
            r#"
selected_upstream:
  application_provider: vllm
"#,
        )
        .unwrap();
        let yaml = serde_yaml::to_string(&m).unwrap();
        let back: ConditionMatch = serde_yaml::from_str(&yaml).unwrap();
        let su = back.selected_upstream.expect("selected_upstream should round-trip");
        assert_eq!(su.application_provider.as_deref(), Some("vllm"));
        assert!(
            su.application_protocol.is_none(),
            "protocol should remain None after round-trip"
        );
    }

    #[test]
    fn parse_selected_upstream_empty_is_empty() {
        let m: ConditionMatch = serde_yaml::from_str("selected_upstream: {}").unwrap();
        let su = m.selected_upstream.expect("empty map still parses");
        assert!(su.is_empty(), "an all-absent selected_upstream reports empty");
    }

    #[test]
    fn reject_unknown_selected_upstream_field() {
        let err = serde_yaml::from_str::<ConditionMatch>(
            r#"
selected_upstream:
  application_flavor: spicy
"#,
        )
        .unwrap_err();
        assert!(
            err.to_string().contains("application_flavor"),
            "unknown selected_upstream field should be rejected: {err}"
        );
    }

    #[test]
    fn parse_when_selected_upstream_condition() {
        let conditions: Vec<Condition> = serde_yaml::from_str(
            r#"
- when:
    selected_upstream:
      application_provider: vllm
"#,
        )
        .unwrap();
        assert_eq!(conditions.len(), 1, "should parse 1 condition");
        assert!(
            matches!(
                &conditions[0],
                Condition::When(m)
                    if m.selected_upstream.as_ref().and_then(|su| su.application_provider.as_deref()) == Some("vllm")
            ),
            "should be When gating on selected_upstream provider"
        );
    }
}