a3s-gateway 0.2.5

A3S Gateway - AI-native API gateway with reverse proxy, routing, and agent orchestration
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
//! Forward auth middleware — delegate authentication to an external service
//!
//! Sends a verification request to an external auth service (Keycloak, Auth0,
//! Authelia, etc.) before allowing the request through. On 2xx response,
//! copies configured headers from the auth response to the upstream request.
//! On non-2xx, short-circuits with the auth service's status code.

use crate::config::MiddlewareConfig;
use crate::error::{GatewayError, Result};
use crate::middleware::{Middleware, RequestContext};
use async_trait::async_trait;
use http::Response;

/// Forward auth middleware
pub struct ForwardAuthMiddleware {
    /// URL of the external auth service
    auth_url: String,
    /// Headers to copy from auth response to upstream request
    response_headers: Vec<String>,
    /// HTTP client for auth requests
    client: reqwest::Client,
}

impl ForwardAuthMiddleware {
    /// Create from middleware config
    pub fn new(config: &MiddlewareConfig) -> Result<Self> {
        let auth_url = config.forward_auth_url.as_deref().ok_or_else(|| {
            GatewayError::Config(
                "forward-auth middleware requires 'forward_auth_url' field".to_string(),
            )
        })?;

        if auth_url.is_empty() {
            return Err(GatewayError::Config(
                "forward_auth_url cannot be empty".to_string(),
            ));
        }

        Ok(Self {
            auth_url: auth_url.to_string(),
            response_headers: config.forward_auth_response_headers.clone(),
            client: reqwest::Client::new(),
        })
    }

    /// Create directly with URL and headers (for programmatic use)
    #[allow(dead_code)]
    pub fn with_url(auth_url: &str, response_headers: Vec<String>) -> Result<Self> {
        if auth_url.is_empty() {
            return Err(GatewayError::Config(
                "forward_auth_url cannot be empty".to_string(),
            ));
        }
        Ok(Self {
            auth_url: auth_url.to_string(),
            response_headers,
            client: reqwest::Client::new(),
        })
    }

    /// Create with a custom client (for testing)
    #[cfg(test)]
    fn with_client(auth_url: &str, response_headers: Vec<String>, client: reqwest::Client) -> Self {
        Self {
            auth_url: auth_url.to_string(),
            response_headers,
            client,
        }
    }

    /// Get the configured auth URL
    #[allow(dead_code)]
    pub fn auth_url(&self) -> &str {
        &self.auth_url
    }
}

#[async_trait]
impl Middleware for ForwardAuthMiddleware {
    async fn handle_request(
        &self,
        req: &mut http::request::Parts,
        _ctx: &RequestContext,
    ) -> Result<Option<Response<Vec<u8>>>> {
        // Build the auth request with forwarded headers
        let mut auth_req = self.client.get(&self.auth_url);

        // Copy original request headers to auth request
        for (key, value) in req.headers.iter() {
            if let Ok(v) = value.to_str() {
                auth_req = auth_req.header(key.as_str(), v);
            }
        }

        // Add X-Forwarded-Method and X-Forwarded-Uri
        auth_req = auth_req.header("X-Forwarded-Method", req.method.as_str());
        auth_req = auth_req.header("X-Forwarded-Uri", req.uri.to_string());

        // Send the auth request
        let auth_resp = match auth_req.send().await {
            Ok(resp) => resp,
            Err(e) => {
                tracing::warn!(
                    error = %e,
                    auth_url = self.auth_url,
                    "Forward auth service unreachable"
                );
                return Ok(Some(
                    Response::builder()
                        .status(502)
                        .header("Content-Type", "application/json")
                        .body(r#"{"error":"Auth service unavailable"}"#.as_bytes().to_vec())
                        .unwrap(),
                ));
            }
        };

        let status = auth_resp.status();

        if status.is_success() {
            // Copy configured headers from auth response to upstream request
            for header_name in &self.response_headers {
                if let Some(value) = auth_resp.headers().get(header_name.as_str()) {
                    if let Ok(v) = value.to_str() {
                        if let Ok(hv) = v.parse() {
                            if let Ok(hn) = http::header::HeaderName::from_bytes(
                                header_name.to_lowercase().as_bytes(),
                            ) {
                                req.headers.insert(hn, hv);
                            }
                        }
                    }
                }
            }
            Ok(None) // Continue pipeline
        } else {
            // Short-circuit with auth service's status code
            let body = auth_resp
                .bytes()
                .await
                .map(|b| b.to_vec())
                .unwrap_or_else(|_| {
                    format!(
                        r#"{{"error":"Authentication failed","status":{}}}"#,
                        status.as_u16()
                    )
                    .into_bytes()
                });

            tracing::debug!(
                status = status.as_u16(),
                auth_url = self.auth_url,
                "Forward auth rejected request"
            );

            Ok(Some(
                Response::builder()
                    .status(status.as_u16())
                    .header("Content-Type", "application/json")
                    .body(body)
                    .unwrap(),
            ))
        }
    }

    fn name(&self) -> &str {
        "forward-auth"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::net::TcpListener;

    fn make_ctx() -> RequestContext {
        RequestContext {
            client_ip: "127.0.0.1".to_string(),
            entrypoint: "web".to_string(),
            router: "test".to_string(),
        }
    }

    fn make_config(url: &str) -> MiddlewareConfig {
        MiddlewareConfig {
            middleware_type: "forward-auth".to_string(),
            forward_auth_url: Some(url.to_string()),
            forward_auth_response_headers: vec!["X-User-Id".to_string(), "X-User-Role".to_string()],
            ..Default::default()
        }
    }

    #[test]
    fn test_forward_auth_name() {
        let mw = ForwardAuthMiddleware::with_url("http://auth.local/verify", vec![]).unwrap();
        assert_eq!(mw.name(), "forward-auth");
    }

    #[test]
    fn test_forward_auth_url() {
        let mw = ForwardAuthMiddleware::with_url("http://auth.local/verify", vec![]).unwrap();
        assert_eq!(mw.auth_url(), "http://auth.local/verify");
    }

    #[test]
    fn test_from_config() {
        let config = make_config("http://auth.local/verify");
        let mw = ForwardAuthMiddleware::new(&config).unwrap();
        assert_eq!(mw.auth_url(), "http://auth.local/verify");
    }

    #[test]
    fn test_requires_auth_url() {
        let config = MiddlewareConfig {
            middleware_type: "forward-auth".to_string(),
            ..Default::default()
        };
        assert!(ForwardAuthMiddleware::new(&config).is_err());
    }

    #[test]
    fn test_empty_url_rejected() {
        assert!(ForwardAuthMiddleware::with_url("", vec![]).is_err());
    }

    #[test]
    fn test_empty_config_url_rejected() {
        let config = MiddlewareConfig {
            middleware_type: "forward-auth".to_string(),
            forward_auth_url: Some(String::new()),
            ..Default::default()
        };
        assert!(ForwardAuthMiddleware::new(&config).is_err());
    }

    /// Start a mock TCP server that responds with a fixed HTTP response
    async fn start_mock_auth_server(response: &str) -> String {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let response = response.to_string();

        tokio::spawn(async move {
            // Accept one connection
            if let Ok((mut stream, _)) = listener.accept().await {
                let mut buf = vec![0u8; 4096];
                let _ = stream.read(&mut buf).await;
                let _ = stream.write_all(response.as_bytes()).await;
                let _ = stream.shutdown().await;
            }
        });

        format!("http://127.0.0.1:{}/verify", addr.port())
    }

    #[tokio::test]
    async fn test_auth_success_200() {
        let url = start_mock_auth_server(
            "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nX-User-Id: user-42\r\n\r\nOK",
        )
        .await;

        let mw = ForwardAuthMiddleware::with_client(
            &url,
            vec!["X-User-Id".to_string()],
            reqwest::Client::new(),
        );

        let (mut parts, _) = http::Request::builder()
            .uri("/api/data")
            .header("Authorization", "Bearer token")
            .body(())
            .unwrap()
            .into_parts();
        let ctx = make_ctx();
        let result = mw.handle_request(&mut parts, &ctx).await.unwrap();
        assert!(result.is_none()); // Should pass through
                                   // Auth header should be copied
        assert_eq!(parts.headers.get("x-user-id").unwrap(), "user-42");
    }

    #[tokio::test]
    async fn test_auth_rejected_401() {
        let url = start_mock_auth_server(
            "HTTP/1.1 401 Unauthorized\r\nContent-Length: 12\r\n\r\nUnauthorized",
        )
        .await;

        let mw = ForwardAuthMiddleware::with_client(&url, vec![], reqwest::Client::new());

        let (mut parts, _) = http::Request::builder()
            .uri("/api/data")
            .body(())
            .unwrap()
            .into_parts();
        let ctx = make_ctx();
        let result = mw.handle_request(&mut parts, &ctx).await.unwrap();
        assert!(result.is_some());
        assert_eq!(result.unwrap().status(), 401);
    }

    #[tokio::test]
    async fn test_auth_rejected_403() {
        let url =
            start_mock_auth_server("HTTP/1.1 403 Forbidden\r\nContent-Length: 9\r\n\r\nForbidden")
                .await;

        let mw = ForwardAuthMiddleware::with_client(&url, vec![], reqwest::Client::new());

        let (mut parts, _) = http::Request::builder()
            .uri("/api/admin")
            .body(())
            .unwrap()
            .into_parts();
        let ctx = make_ctx();
        let result = mw.handle_request(&mut parts, &ctx).await.unwrap();
        assert!(result.is_some());
        assert_eq!(result.unwrap().status(), 403);
    }

    #[tokio::test]
    async fn test_auth_service_unreachable() {
        // Use a port that definitely won't have a server
        let mw = ForwardAuthMiddleware::with_client(
            "http://127.0.0.1:1/verify",
            vec![],
            reqwest::Client::builder()
                .timeout(std::time::Duration::from_millis(100))
                .build()
                .unwrap(),
        );

        let (mut parts, _) = http::Request::builder()
            .uri("/api/data")
            .body(())
            .unwrap()
            .into_parts();
        let ctx = make_ctx();
        let result = mw.handle_request(&mut parts, &ctx).await.unwrap();
        assert!(result.is_some());
        assert_eq!(result.unwrap().status(), 502);
    }

    #[tokio::test]
    async fn test_forwards_method_and_uri() {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();

        let (tx, mut rx) = tokio::sync::oneshot::channel::<String>();
        tokio::spawn(async move {
            if let Ok((mut stream, _)) = listener.accept().await {
                let mut buf = vec![0u8; 4096];
                let n = stream.read(&mut buf).await.unwrap();
                let request = String::from_utf8_lossy(&buf[..n]).to_string();
                let _ = tx.send(request);
                let resp = "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK";
                let _ = stream.write_all(resp.as_bytes()).await;
                let _ = stream.shutdown().await;
            }
        });

        let url = format!("http://127.0.0.1:{}/verify", addr.port());
        let mw = ForwardAuthMiddleware::with_client(&url, vec![], reqwest::Client::new());

        let (mut parts, _) = http::Request::builder()
            .method("POST")
            .uri("/api/users")
            .body(())
            .unwrap()
            .into_parts();
        let ctx = make_ctx();
        let _ = mw.handle_request(&mut parts, &ctx).await.unwrap();

        // Check that X-Forwarded-Method and X-Forwarded-Uri were sent
        let captured = rx.try_recv().unwrap();
        assert!(
            captured.contains("x-forwarded-method: POST")
                || captured.contains("X-Forwarded-Method: POST"),
            "Expected X-Forwarded-Method header, got: {}",
            captured
        );
        assert!(
            captured.contains("x-forwarded-uri: /api/users")
                || captured.contains("X-Forwarded-Uri: /api/users"),
            "Expected X-Forwarded-Uri header, got: {}",
            captured
        );
    }

    #[tokio::test]
    async fn test_no_headers_copied_when_empty() {
        let url = start_mock_auth_server(
            "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nX-Custom: value\r\n\r\nOK",
        )
        .await;

        // Don't configure any response headers to copy
        let mw = ForwardAuthMiddleware::with_client(&url, vec![], reqwest::Client::new());

        let (mut parts, _) = http::Request::builder()
            .uri("/api/data")
            .body(())
            .unwrap()
            .into_parts();
        let ctx = make_ctx();
        let result = mw.handle_request(&mut parts, &ctx).await.unwrap();
        assert!(result.is_none());
        // X-Custom should NOT be copied since it's not in response_headers
        assert!(parts.headers.get("x-custom").is_none());
    }
}