Skip to main content

guardrail_proxy/
lib.rs

1//! # guardrail-proxy
2//!
3//! HTTP reverse-proxy server for `guardrail-rs`.
4//!
5//! This crate ties together [`guardrail_core`], [`guardrail_classifiers`], and
6//! [`guardrail_config`] into a runnable server: it accepts HTTP requests
7//! shaped like OpenAI/Anthropic chat-completion calls, runs them through the
8//! configured [`guardrail_core::Pipeline`], and either forwards the (possibly
9//! redacted) request upstream or returns a block response.
10//!
11//! ## Architecture
12//!
13//! ```text
14//! ┌──────────┐     ┌────────────────┐     ┌───────────┐     ┌──────────────┐
15//! │  Client  │ ──▶ │  guardrail-rs  │ ──▶ │  Pipeline  │ ──▶ │   Upstream    │
16//! │          │     │  HTTP server   │     │  (Stages)  │     │  (OpenAI/...) │
17//! └──────────┘     └────────────────┘     └───────────┘     └──────────────┘
18//!                          │                                          │
19//!                          ▼                                          │
20//!                   ┌─────────────┐                                  │
21//!                   │  Block JSON │ ◀─── short-circuit on Block ─────┘
22//!                   │  response   │
23//!                   └─────────────┘
24//! ```
25//!
26//! ## Modules
27//!
28//! - [`server`]: the listener lifecycle — bind, accept, serve, shutdown
29//! - [`handler`]: per-request routing and the core proxy flow (auth check →
30//!   parse → run pipeline → forward or block)
31//! - [`auth`]: caller authentication (`[auth]`), as a standalone, pure,
32//!   independently-testable predicate
33//! - [`error`]: HTTP error-response construction and upstream error
34//!   classification, shared by [`handler`]
35//! - [`state`]: internal connection-handler state and [`ServerHandle`] — the
36//!   data every connection handler needs, with no behavior of its own
37//! - [`forward`]: upstream request forwarding (streaming + non-streaming)
38//! - [`translate`]: conversion between raw JSON bodies and [`guardrail_core::GuardrailRequest`]
39//! - [`response`]: output-side PII redaction for non-streaming responses
40//! - [`telemetry`]: OpenTelemetry OTLP layer and per-request/stage span helpers
41//! - [`metrics`]: Prometheus metrics registry and recording helpers
42//! - [`audit`]: structured audit logging (tracing events + rotating NDJSON file)
43//! - [`audit_log`]: rotating NDJSON file layer for `tracing_subscriber`
44//!
45//! Each module above has one job. This separation exists so that, for
46//! example, the caller-authentication decision can be unit-tested as a pure
47//! function (`auth::is_authorized`) without spinning up a real TCP listener,
48//! while [`server`]'s own tests focus purely on proving the listener
49//! lifecycle works end to end.
50//!
51//! ## Feature flags
52//!
53//! | Flag | Description | Default |
54//! |------|-------------|---------|
55//! | *(none)* | Base proxy: regex injection scanning, PII redaction, policy engine, Prometheus metrics, OTel tracing, NDJSON audit log | — |
56//! | `onnx` | Enables the semantic injection/toxicity classifiers via `guardrail-classifiers/onnx` and `guardrail-config/onnx` | off |
57//!
58//! ## Quick example
59//!
60//! Most applications run guardrail-proxy via the `guardrail` CLI binary
61//! rather than embedding [`run_server`] directly, but it is fully usable as
62//! a library:
63//!
64//! ```rust,no_run
65//! use guardrail_config::ConfigHandle;
66//! use std::sync::Arc;
67//!
68//! # tokio_test::block_on(async {
69//! let config = Arc::new(ConfigHandle::load("guardrail.toml").unwrap());
70//! let handle = guardrail_proxy::run_server(config).await.unwrap();
71//! println!("listening on {}", handle.local_addr());
72//! handle.shutdown();
73//! # });
74//! ```
75//!
76//! ## Further reading
77//!
78//! - [Configuration reference](https://github.com/Mattral/guardrail-rs/blob/main/docs/configuration.md) —
79//!   every TOML key this server consumes, including `[auth]` and `[observability]`.
80//! - [Threat model](https://github.com/Mattral/guardrail-rs/blob/main/docs/threat-model.md) —
81//!   what this proxy protects against, caller authentication, and residual risks.
82//! - [Architecture](https://github.com/Mattral/guardrail-rs/blob/main/docs/architecture.md) —
83//!   request/response pipeline diagram and hot-reload behavior.
84//! - [Changelog](https://github.com/Mattral/guardrail-rs/blob/main/CHANGELOG.md) —
85//!   release history and notable changes.
86
87#![deny(missing_docs)]
88#![warn(clippy::all)]
89
90pub mod audit;
91pub mod audit_log;
92pub mod auth;
93pub mod error;
94pub mod forward;
95pub(crate) mod handler;
96pub mod metrics;
97pub mod response;
98pub mod server;
99pub mod state;
100pub mod telemetry;
101pub mod translate;
102
103pub use server::{run_server, ServerHandle};