coreason-runtime 0.1.0

Kinetic Plane execution engine for the CoReason Tripartite Cybernetic Manifold
Documentation
// Copyright (c) 2026 CoReason, Inc.
// All rights reserved.

//! DLP (Data Loss Prevention) API routes.
//!
//! Replaces `coreason_runtime/api/dlp_router.py`.
//! Proxies DLP scan and redact requests to the Python sidecar.

use axum::{
    extract::State,
    http::StatusCode,
    response::{IntoResponse, Response},
    routing::post,
    Json, Router,
};
use std::sync::Arc;

use crate::GatewayState;

// ── Proxy helpers ───────────────────────────────────────────────────────

async fn proxy_post(state: &GatewayState, path: &str, payload: &serde_json::Value) -> Response {
    let url = format!("{}{}", state.sidecar_url, path);
    match ureq::post(&url).send_json(payload) {
        Ok(res) => {
            let status = StatusCode::from_u16(res.status()).unwrap_or(StatusCode::OK);
            let body: serde_json::Value = res.into_json().unwrap_or(serde_json::Value::Null);
            (status, Json(body)).into_response()
        }
        Err(ureq::Error::Status(code, res)) => {
            let status = StatusCode::from_u16(code).unwrap_or(StatusCode::BAD_REQUEST);
            let body: serde_json::Value = res.into_json().unwrap_or(serde_json::Value::Null);
            (status, Json(body)).into_response()
        }
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({
                "status": "error",
                "message": format!("Failed to proxy POST {} to sidecar: {}", path, e),
            })),
        )
            .into_response(),
    }
}

// ── Handlers ────────────────────────────────────────────────────────────

/// POST /api/v1/dlp/scan
///
/// Scan content for sensitive data via the sidecar DLP engine.
async fn dlp_scan(
    State(state): State<Arc<GatewayState>>,
    Json(payload): Json<serde_json::Value>,
) -> Response {
    proxy_post(&state, "/api/v1/dlp/scan", &payload).await
}

/// POST /api/v1/dlp/redact
///
/// Redact sensitive data from content via the sidecar DLP engine.
async fn dlp_redact(
    State(state): State<Arc<GatewayState>>,
    Json(payload): Json<serde_json::Value>,
) -> Response {
    proxy_post(&state, "/api/v1/dlp/redact", &payload).await
}

// ── Router ──────────────────────────────────────────────────────────────

/// Build the DLP router.
pub fn router() -> Router<Arc<GatewayState>> {
    Router::new()
        .route("/api/v1/dlp/scan", post(dlp_scan))
        .route("/api/v1/dlp/redact", post(dlp_redact))
}