praxis-proxy 0.4.0

Cloud-native proxy server
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Praxis Contributors

//! `--dump` output: serializable effective configuration with resolved top-level listener chains.

use std::{collections::HashMap, io::Write as _};

use praxis_core::config::{ChainRef, Config, FailureMode, FilterEntry};
use serde::Serialize;

// -----------------------------------------------------------------------------
// Dump Model
// -----------------------------------------------------------------------------

/// Top-level dump output written to stdout as YAML.
#[derive(Serialize)]
pub(crate) struct EffectiveConfigDump {
    /// Where the configuration was loaded from.
    pub config_source: String,

    /// The fully parsed configuration (with defaults applied),
    /// sensitive values redacted.
    pub configuration: Config,

    /// Resolved top-level listener chains, preserving config order.
    pub resolved_listeners: Vec<ResolvedListenerDump>,
}

/// A single listener with its resolved chain and filter information.
#[derive(Serialize)]
pub(crate) struct ResolvedListenerDump {
    /// Listener name from configuration.
    pub name: String,

    /// Named chains referenced by this listener, in config order.
    pub chains: Vec<String>,

    /// Flattened filters across all chains, in execution order.
    pub filters: Vec<ResolvedFilterDump>,
}

/// A single resolved filter entry with its position metadata.
#[derive(Serialize)]
pub(crate) struct ResolvedFilterDump {
    /// Optional user-assigned name for this filter entry.
    pub name: Option<String>,

    /// Name of the chain this filter belongs to.
    pub chain: String,

    /// Zero-based index of this filter within its chain.
    pub chain_index: usize,

    /// Per-filter failure behaviour.
    pub failure_mode: FailureMode,

    /// Filter type name (e.g. `"router"`, `"load_balancer"`).
    pub filter: String,

    /// Zero-based index of this filter in the overall pipeline.
    pub pipeline_index: usize,
}

// -----------------------------------------------------------------------------
// Build + Write
// -----------------------------------------------------------------------------

/// Build the dump model from a validated configuration.
///
/// Sensitive values (e.g. credential injection literals) are
/// redacted before inclusion in the dump.
///
/// # Errors
///
/// Returns an error if a listener references a chain not present in the config
/// (should not happen after validation).
pub(crate) fn build_dump(
    config: &Config,
    config_source: &str,
) -> Result<EffectiveConfigDump, Box<dyn std::error::Error + Send + Sync>> {
    let chains: HashMap<&str, &[_]> = config
        .filter_chains
        .iter()
        .map(|c| (c.name.as_str(), c.filters.as_slice()))
        .collect();

    Ok(EffectiveConfigDump {
        config_source: config_source.to_owned(),
        configuration: redact_secrets(config),
        resolved_listeners: build_resolved_listeners(config, &chains)?,
    })
}

/// Clone a [`Config`] and redact sensitive values.
fn redact_secrets(config: &Config) -> Config {
    let mut redacted = config.clone();
    for chain in &mut redacted.filter_chains {
        for entry in &mut chain.filters {
            redact_filter_entry(entry);
        }
    }
    redacted
}

/// Redact sensitive values in a filter entry and nested inline branch chains.
fn redact_filter_entry(entry: &mut FilterEntry) {
    if entry.filter_type == "credential_injection" {
        redact_credential_values(&mut entry.config);
    }
    redact_sensitive_keys(&mut entry.config);

    let Some(branches) = &mut entry.branch_chains else {
        return;
    };
    for branch in branches {
        for chain in &mut branch.chains {
            if let ChainRef::Inline { filters, .. } = chain {
                for entry in filters {
                    redact_filter_entry(entry);
                }
            }
        }
    }
}

/// Walk the flattened config YAML for a `credential_injection` filter
/// and replace `value` and `env_var` keys inside `clusters` entries.
fn redact_credential_values(config: &mut serde_yaml::Value) {
    let Some(mapping) = config.as_mapping_mut() else {
        return;
    };
    let clusters_key = serde_yaml::Value::String("clusters".to_owned());
    let Some(clusters) = mapping.get_mut(&clusters_key) else {
        return;
    };
    let Some(seq) = clusters.as_sequence_mut() else {
        return;
    };
    let value_key = serde_yaml::Value::String("value".to_owned());
    let env_var_key = serde_yaml::Value::String("env_var".to_owned());
    let redacted = serde_yaml::Value::String("[REDACTED]".to_owned());
    for entry in seq {
        if let Some(m) = entry.as_mapping_mut() {
            if m.contains_key(&value_key) {
                m.insert(value_key.clone(), redacted.clone());
            }
            if m.contains_key(&env_var_key) {
                m.insert(env_var_key.clone(), redacted.clone());
            }
        }
    }
}

/// Recursively redact known sensitive field names in any filter config.
fn redact_sensitive_keys(value: &mut serde_yaml::Value) {
    let Some(mapping) = value.as_mapping_mut() else {
        return;
    };
    let redacted = serde_yaml::Value::String("[REDACTED]".to_owned());
    for key_name in SENSITIVE_FIELD_NAMES {
        let key = serde_yaml::Value::String((*key_name).to_owned());
        if mapping.contains_key(&key) {
            mapping.insert(key, redacted.clone());
        }
    }
    for (_, v) in mapping.iter_mut() {
        match v {
            serde_yaml::Value::Mapping(_) => redact_sensitive_keys(v),
            serde_yaml::Value::Sequence(seq) => {
                for item in seq {
                    redact_sensitive_keys(item);
                }
            },
            _ => {},
        }
    }
}

/// Field names that should be redacted in config dumps.
const SENSITIVE_FIELD_NAMES: &[&str] = &["database_url", "key_path", "password", "secret", "token"];

/// Resolve all listeners into their dump representations.
fn build_resolved_listeners(
    config: &Config,
    chains: &HashMap<&str, &[FilterEntry]>,
) -> Result<Vec<ResolvedListenerDump>, Box<dyn std::error::Error + Send + Sync>> {
    config
        .listeners
        .iter()
        .map(|listener| build_resolved_listener(listener, chains))
        .collect()
}

/// Resolve a single listener's chains into a flat filter list.
fn build_resolved_listener(
    listener: &praxis_core::config::Listener,
    chains: &HashMap<&str, &[FilterEntry]>,
) -> Result<ResolvedListenerDump, Box<dyn std::error::Error + Send + Sync>> {
    Ok(ResolvedListenerDump {
        name: listener.name.clone(),
        chains: listener.filter_chains.clone(),
        filters: build_resolved_filters(&listener.filter_chains, chains)?,
    })
}

/// Flatten chain references into an ordered list of resolved filters.
fn build_resolved_filters(
    chain_names: &[String],
    chains: &HashMap<&str, &[FilterEntry]>,
) -> Result<Vec<ResolvedFilterDump>, Box<dyn std::error::Error + Send + Sync>> {
    let mut filters = Vec::new();
    let mut pipeline_index = 0;

    for chain_name in chain_names {
        let chain_filters = chains
            .get(chain_name.as_str())
            .ok_or_else(|| format!("unknown chain '{chain_name}' in validated config"))?;
        for (chain_index, entry) in chain_filters.iter().enumerate() {
            filters.push(ResolvedFilterDump {
                name: entry.name.clone(),
                chain: chain_name.clone(),
                chain_index,
                failure_mode: entry.failure_mode,
                filter: entry.filter_type.clone(),
                pipeline_index,
            });
            pipeline_index += 1;
        }
    }

    Ok(filters)
}

/// Serialize the dump to YAML and write it to stdout.
///
/// # Errors
///
/// Returns an error if YAML serialization or stdout write fails.
pub(crate) fn write_dump(dump: &EffectiveConfigDump) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let yaml = serde_yaml::to_string(dump)?;
    std::io::stdout().lock().write_all(yaml.as_bytes())?;
    Ok(())
}

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

#[cfg(test)]
#[expect(clippy::allow_attributes, reason = "blanket test suppressions")]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing, reason = "tests")]
mod tests {
    use praxis_core::config::{Config, FailureMode};

    use super::*;

    const ORDERED_CHAINS_YAML: &str = r#"
listeners:
  - name: web
    address: "127.0.0.1:8080"
    filter_chains: [first, second]
filter_chains:
  - name: first
    filters:
      - filter: request_id
  - name: second
    filters:
      - filter: access_log
      - filter: router
        routes:
          - path_prefix: "/"
            cluster: backend
"#;

    const REPRESENTATIVE_CONFIG_YAML: &str = r#"
listeners:
  - name: web
    address: "127.0.0.1:8080"
    filter_chains: [main]
filter_chains:
  - name: branch_target
    filters:
      - filter: request_id
  - name: main
    filters:
      - filter: request_id
        name: mark
        conditions:
          - when:
              path_prefix: /api
              methods: [GET, POST]
        branch_chains:
          - name: audit_branch
            on_result:
              filter: mark
              result: hit
            chains:
              - branch_target
              - name: inline_audit
                filters:
                  - filter: access_log
            rejoin: next
      - filter: access_log
        response_conditions:
          - unless:
              status: [500]
      - filter: static_response
        status: 204
"#;

    #[test]
    fn dump_defaults_appear_under_configuration() {
        let config = Config::from_yaml(
            r#"
listeners:
  - name: web
    address: "127.0.0.1:8080"
    filter_chains: [main]
filter_chains:
  - name: main
    filters: []
"#,
        )
        .unwrap();

        let dump = build_dump(&config, "test.yaml").unwrap();
        let yaml = serde_yaml::to_string(&dump).unwrap();
        assert!(
            yaml.contains("shutdown_timeout_secs: 30"),
            "defaults should appear: {yaml}"
        );
        assert!(
            yaml.contains("config_source: test.yaml"),
            "config_source should appear: {yaml}"
        );
    }

    #[test]
    fn resolved_filters_preserve_chain_order() {
        let config = Config::from_yaml(ORDERED_CHAINS_YAML).unwrap();

        let dump = build_dump(&config, "test.yaml").unwrap();
        let filters = &dump.resolved_listeners[0].filters;
        assert_eq!(filters.len(), 3);
        assert_filter(&filters[0], "first", 0, 0, "request_id");
        assert_filter(&filters[1], "second", 0, 1, "access_log");
        assert_filter(&filters[2], "second", 1, 2, "router");
    }

    const CREDENTIAL_INJECTION_ENV_VAR_YAML: &str = r#"
listeners:
  - name: web
    address: "127.0.0.1:8080"
    filter_chains: [main]
filter_chains:
  - name: main
    filters:
      - filter: credential_injection
        clusters:
          - name: backend
            header: Authorization
            env_var: "SECRET_TOKEN"
            header_prefix: "Bearer "
      - filter: router
        routes:
          - path_prefix: "/"
            cluster: backend
      - filter: load_balancer
        clusters:
          - name: backend
            endpoints:
              - "127.0.0.1:9090"
"#;

    /// Assert a resolved filter's chain, indices, and type name.
    fn assert_filter(f: &ResolvedFilterDump, chain: &str, chain_idx: usize, pipeline_idx: usize, filter: &str) {
        assert_eq!(f.chain, chain, "chain mismatch for filter {filter}");
        assert_eq!(f.chain_index, chain_idx, "chain_index mismatch for filter {filter}");
        assert_eq!(
            f.pipeline_index, pipeline_idx,
            "pipeline_index mismatch for filter {filter}"
        );
        assert_eq!(f.filter, filter, "filter type mismatch");
    }

    #[test]
    fn representative_config_roundtrips_through_yaml_serialization() {
        let config = Config::from_yaml(REPRESENTATIVE_CONFIG_YAML).unwrap();
        let serialized = serde_yaml::to_string(&config).unwrap();

        assert!(
            serialized.contains("when:"),
            "request conditions should serialize as maps"
        );
        assert!(
            serialized.contains("unless:"),
            "response conditions should serialize as maps"
        );
        assert!(
            !serialized.contains("!when"),
            "request conditions should not serialize as tags"
        );
        assert!(
            !serialized.contains("!unless"),
            "response conditions should not serialize as tags"
        );

        let reparsed = Config::from_yaml(&serialized).unwrap();
        assert_eq!(reparsed.listeners.len(), config.listeners.len());
        assert_eq!(reparsed.filter_chains.len(), config.filter_chains.len());
    }

    #[test]
    fn empty_listener_chains_produce_empty_filter_list() {
        let config = Config::from_yaml(
            r#"
listeners:
  - name: web
    address: "127.0.0.1:8080"
    filter_chains: [main]
filter_chains:
  - name: main
    filters: []
"#,
        )
        .unwrap();

        let dump = build_dump(&config, "test.yaml").unwrap();
        let listener = &dump.resolved_listeners[0];
        assert!(listener.filters.is_empty(), "empty chain should produce no filters");
        assert_eq!(listener.chains, vec!["main"]);
    }

    #[test]
    fn failure_mode_serializes_lowercase() {
        let config = Config::from_yaml(
            r#"
listeners:
  - name: web
    address: "127.0.0.1:8080"
    filter_chains: [main]
filter_chains:
  - name: main
    filters:
      - filter: access_log
        failure_mode: open
      - filter: router
        routes: []
"#,
        )
        .unwrap();

        let dump = build_dump(&config, "test.yaml").unwrap();
        assert_eq!(dump.resolved_listeners[0].filters[0].failure_mode, FailureMode::Open);
        assert_eq!(dump.resolved_listeners[0].filters[1].failure_mode, FailureMode::Closed);

        let yaml = serde_yaml::to_string(&dump).unwrap();
        assert!(
            yaml.contains("failure_mode: open"),
            "open should serialize lowercase: {yaml}"
        );
        assert!(
            yaml.contains("failure_mode: closed"),
            "closed should serialize lowercase: {yaml}"
        );
    }

    #[test]
    fn filter_name_included_when_set() {
        let config = Config::from_yaml(
            r#"
listeners:
  - name: web
    address: "127.0.0.1:8080"
    filter_chains: [main]
filter_chains:
  - name: main
    filters:
      - filter: router
        name: routing
        routes: []
"#,
        )
        .unwrap();

        let dump = build_dump(&config, "test.yaml").unwrap();
        assert_eq!(dump.resolved_listeners[0].filters[0].name.as_deref(), Some("routing"));
    }

    const CREDENTIAL_INJECTION_YAML: &str = r#"
listeners:
  - name: web
    address: "127.0.0.1:8080"
    filter_chains: [main]
filter_chains:
  - name: main
    filters:
      - filter: credential_injection
        clusters:
          - name: backend
            header: Authorization
            value: "super-secret-key"
            header_prefix: "Bearer "
      - filter: router
        routes:
          - path_prefix: "/"
            cluster: backend
      - filter: load_balancer
        clusters:
          - name: backend
            endpoints:
              - "127.0.0.1:9090"
"#;

    #[test]
    fn credential_injection_values_redacted_in_dump() {
        let config = Config::from_yaml(CREDENTIAL_INJECTION_YAML).unwrap();
        let dump = build_dump(&config, "test.yaml").unwrap();
        let yaml = serde_yaml::to_string(&dump).unwrap();
        assert!(
            !yaml.contains("super-secret-key"),
            "credential value must be redacted in dump: {yaml}"
        );
        assert!(yaml.contains("[REDACTED]"), "redaction marker must appear: {yaml}");
        assert!(
            yaml.contains("header_prefix"),
            "non-sensitive fields must remain: {yaml}"
        );
    }

    #[test]
    fn database_url_redacted_in_dump() {
        let config = Config::from_yaml(
            r#"
listeners:
  - name: web
    address: "127.0.0.1:8080"
    filter_chains: [main]
filter_chains:
  - name: main
    filters:
      - filter: some_db_filter
        backend: postgres
        database_url: "postgres://user:super-secret-db-pass@localhost:5432/praxis"
"#,
        )
        .unwrap();
        let dump = build_dump(&config, "test.yaml").unwrap();
        let yaml = serde_yaml::to_string(&dump).unwrap();
        assert!(
            !yaml.contains("super-secret-db-pass"),
            "database_url credential must be redacted in dump: {yaml}"
        );
        assert!(
            yaml.contains("database_url: '[REDACTED]'"),
            "database_url should be replaced with the redaction marker: {yaml}"
        );
    }

    #[test]
    #[expect(clippy::too_many_lines, reason = "test YAML is intentionally explicit")]
    fn branch_chain_database_url_redacted_in_dump() {
        let config = Config::from_yaml(
            r#"
listeners:
  - name: web
    address: "127.0.0.1:8080"
    filter_chains: [main]
filter_chains:
  - name: main
    filters:
      - filter: request_id
        name: mark
        branch_chains:
          - name: persist_branch
            chains:
              - name: inline_store
                filters:
                  - filter: some_db_filter
                    backend: postgres
                    database_url: "postgres://user:super-secret-db-pass@localhost:5432/praxis"
            rejoin: next
"#,
        )
        .unwrap();
        let dump = build_dump(&config, "test.yaml").unwrap();
        let yaml = serde_yaml::to_string(&dump).unwrap();
        assert!(
            !yaml.contains("super-secret-db-pass"),
            "branch chain database_url credential must be redacted in dump: {yaml}"
        );
        assert!(
            yaml.contains("database_url: '[REDACTED]'"),
            "branch chain database_url should be replaced with the redaction marker: {yaml}"
        );
    }

    #[test]
    fn redact_credential_values_no_clusters_key() {
        let mut config = serde_yaml::Value::Mapping(serde_yaml::Mapping::new());
        config.as_mapping_mut().unwrap().insert(
            serde_yaml::Value::String("header".to_owned()),
            serde_yaml::Value::String("Authorization".to_owned()),
        );
        redact_credential_values(&mut config);
        assert!(
            !config
                .as_mapping()
                .unwrap()
                .contains_key(serde_yaml::Value::String("clusters".to_owned())),
            "config without clusters key should remain unchanged"
        );
    }

    #[test]
    fn credential_injection_env_var_redacted_in_dump() {
        let config = Config::from_yaml(CREDENTIAL_INJECTION_ENV_VAR_YAML).unwrap();
        let dump = build_dump(&config, "test.yaml").unwrap();
        let output = serde_yaml::to_string(&dump).unwrap();
        assert!(
            !output.contains("SECRET_TOKEN"),
            "env_var credential must be redacted: {output}"
        );
        assert!(output.contains("[REDACTED]"), "redaction marker must appear: {output}");
        assert!(
            output.contains("header_prefix"),
            "non-sensitive fields must remain: {output}"
        );
    }

    #[test]
    fn redact_sensitive_keys_nested_password() {
        let mut value: serde_yaml::Value =
            serde_yaml::from_str("some_filter:\n  config:\n    password: secret123").expect("test YAML must parse");
        redact_sensitive_keys(&mut value);
        let nested_password = value
            .as_mapping()
            .unwrap()
            .get(serde_yaml::Value::String("some_filter".to_owned()))
            .unwrap()
            .as_mapping()
            .unwrap()
            .get(serde_yaml::Value::String("config".to_owned()))
            .unwrap()
            .as_mapping()
            .unwrap()
            .get(serde_yaml::Value::String("password".to_owned()))
            .unwrap();
        assert_eq!(
            nested_password.as_str(),
            Some("[REDACTED]"),
            "nested password field must be redacted"
        );
    }

    #[test]
    fn redact_credential_values_non_sequence_clusters() {
        let mut config = serde_yaml::Value::Mapping(serde_yaml::Mapping::new());
        config.as_mapping_mut().unwrap().insert(
            serde_yaml::Value::String("clusters".to_owned()),
            serde_yaml::Value::String("not-a-sequence".to_owned()),
        );
        redact_credential_values(&mut config);
        let clusters = config
            .as_mapping()
            .unwrap()
            .get(serde_yaml::Value::String("clusters".to_owned()))
            .unwrap();
        assert_eq!(
            clusters.as_str(),
            Some("not-a-sequence"),
            "non-sequence clusters value should remain unchanged"
        );
    }
}