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
//! # guardrail-proxy
//!
//! HTTP reverse-proxy server for `guardrail-rs`.
//!
//! This crate ties together [`guardrail_core`], [`guardrail_classifiers`], and
//! [`guardrail_config`] into a runnable server: it accepts HTTP requests
//! shaped like OpenAI/Anthropic chat-completion calls, runs them through the
//! configured [`guardrail_core::Pipeline`], and either forwards the (possibly
//! redacted) request upstream or returns a block response.
//!
//! ## Architecture
//!
//! ```text
//! ┌──────────┐ ┌────────────────┐ ┌───────────┐ ┌──────────────┐
//! │ Client │ ──▶ │ guardrail-rs │ ──▶ │ Pipeline │ ──▶ │ Upstream │
//! │ │ │ HTTP server │ │ (Stages) │ │ (OpenAI/...) │
//! └──────────┘ └────────────────┘ └───────────┘ └──────────────┘
//! │ │
//! ▼ │
//! ┌─────────────┐ │
//! │ Block JSON │ ◀─── short-circuit on Block ─────┘
//! │ response │
//! └─────────────┘
//! ```
//!
//! ## Modules
//!
//! - [`server`]: the listener lifecycle — bind, accept, serve, shutdown
//! - `handler`: per-request routing and the core proxy flow (auth check →
//! parse → run pipeline → forward or block)
//! - [`auth`]: caller authentication (`[auth]`), as a standalone, pure,
//! independently-testable predicate
//! - [`error`]: HTTP error-response construction and upstream error
//! classification, shared by `handler`
//! - [`state`]: internal connection-handler state and [`ServerHandle`] — the
//! data every connection handler needs, with no behavior of its own
//! - [`forward`]: upstream request forwarding (streaming + non-streaming)
//! - [`translate`]: conversion between raw JSON bodies and [`guardrail_core::GuardrailRequest`]
//! - [`response`]: output-side PII redaction for non-streaming responses
//! - [`telemetry`]: OpenTelemetry OTLP layer and per-request/stage span helpers
//! - [`metrics`]: Prometheus metrics registry and recording helpers
//! - [`audit`]: structured audit logging (tracing events + rotating NDJSON file)
//! - [`audit_log`]: rotating NDJSON file layer for `tracing_subscriber`
//!
//! Each module above has one job. This separation exists so that, for
//! example, the caller-authentication decision can be unit-tested as a pure
//! function (`auth::is_authorized`) without spinning up a real TCP listener,
//! while [`server`]'s own tests focus purely on proving the listener
//! lifecycle works end to end.
//!
//! ## Feature flags
//!
//! | Flag | Description | Default |
//! |------|-------------|---------|
//! | *(none)* | Base proxy: regex injection scanning, PII redaction, policy engine, Prometheus metrics, OTel tracing, NDJSON audit log | — |
//! | `onnx` | Enables the semantic injection/toxicity classifiers via `guardrail-classifiers/onnx` and `guardrail-config/onnx` | off |
//!
//! ## Quick example
//!
//! Most applications run guardrail-proxy via the `guardrail` CLI binary
//! rather than embedding [`run_server`] directly, but it is fully usable as
//! a library:
//!
//! ```rust,no_run
//! use guardrail_config::ConfigHandle;
//! use std::sync::Arc;
//!
//! # tokio_test::block_on(async {
//! let config = Arc::new(ConfigHandle::load("guardrail.toml").unwrap());
//! let handle = guardrail_proxy::run_server(config).await.unwrap();
//! println!("listening on {}", handle.local_addr());
//! handle.shutdown();
//! # });
//! ```
//!
//! ## Further reading
//!
//! - [Configuration reference](https://github.com/Mattral/guardrail-rs/blob/main/docs/configuration.md) —
//! every TOML key this server consumes, including `[auth]` and `[observability]`.
//! - [Threat model](https://github.com/Mattral/guardrail-rs/blob/main/docs/threat-model.md) —
//! what this proxy protects against, caller authentication, and residual risks.
//! - [Architecture](https://github.com/Mattral/guardrail-rs/blob/main/docs/architecture.md) —
//! request/response pipeline diagram and hot-reload behavior.
//! - [Changelog](https://github.com/Mattral/guardrail-rs/blob/main/CHANGELOG.md) —
//! release history and notable changes.
pub
pub use ;