union_square 0.2.0

A proxy/wire-tap service for making LLM calls and recording everything that happens in a session for later analysis and test-case extraction
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
//! Integration tests for end-to-end proxy flow

use crate::proxy::service::ProxyService;
use crate::proxy::types::*;
use crate::proxy::AuthConfig;
use axum::body::Body;
use axum::extract::Request as ExtractRequest;
use axum::http::{Request, StatusCode};
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tokio::net::TcpListener;
use tower::ServiceExt;

/// Mock backend server for testing
async fn run_mock_backend(port: u16) -> Result<(), Box<dyn std::error::Error>> {
    let app = axum::Router::new()
        .route("/", axum::routing::get(|| async { "Hello from backend" }))
        .route(
            "/echo",
            axum::routing::post(|body: String| async move { body }),
        )
        .route(
            "/status/{code}",
            axum::routing::get(
                |axum::extract::Path(code): axum::extract::Path<u16>| async move {
                    let status = StatusCode::from_u16(code).unwrap_or(StatusCode::OK);
                    (status, "Status response")
                },
            ),
        )
        .route(
            "/slow",
            axum::routing::get(|| async {
                tokio::time::sleep(Duration::from_millis(TIMEOUT_SHORT_MS)).await;
                "Slow response"
            }),
        )
        .route(
            "/large",
            axum::routing::get(|| async {
                "x".repeat(BYTES_1MB) // 1MB
            }),
        )
        .fallback(|request: ExtractRequest| async move {
            (
                StatusCode::NOT_FOUND,
                format!("Not found: {} {}", request.method(), request.uri()),
            )
        });

    let addr = SocketAddr::from(([127, 0, 0, 1], port));
    let listener = TcpListener::bind(addr).await?;

    tokio::spawn(async move {
        axum::serve(listener, app).await.unwrap();
    });

    // Give the server time to start
    tokio::time::sleep(Duration::from_millis(TIMEOUT_SHORT_MS)).await;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_basic_proxy_flow() {
        // Start mock backend
        run_mock_backend(TEST_PORT_BASE + 1)
            .await
            .expect("Failed to start mock backend");

        // Create proxy configuration
        let config = ProxyConfig {
            max_request_size: RequestSizeLimit::try_new(BYTES_10MB).unwrap(),
            max_response_size: ResponseSizeLimit::try_new(BYTES_10MB).unwrap(),
            request_timeout: Duration::from_secs(5),
            ring_buffer: RingBufferConfig::default(),
            bedrock_region: None,
        };

        // Create auth configuration
        let mut auth_config = AuthConfig::default();
        auth_config
            .api_keys
            .insert(ApiKey::try_new("test-key".to_string()).unwrap());

        // Create proxy service
        let service = ProxyService::new(config);
        let ring_buffer = service.ring_buffer();
        let app = service.into_router(auth_config);

        // Test 1: Successful GET request
        let request = Request::builder()
            .method("GET")
            .uri("http://localhost:8080/")
            .header("Authorization", "Bearer test-key")
            .header(
                crate::proxy::headers::X_TARGET_URL,
                format!("http://localhost:{}/", TEST_PORT_BASE + 1),
            )
            .body(Body::empty())
            .unwrap();

        let response = app.clone().oneshot(request).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);

        let body_bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        assert_eq!(&body_bytes[..], b"Hello from backend");

        // Verify audit event was recorded
        tokio::time::sleep(Duration::from_millis(10)).await; // Give audit path time to process
        let stats = ring_buffer.stats();
        assert!(stats.total_writes > 0, "Should have recorded audit events");

        // Test 2: POST request with body
        let request_body = "Test request body";
        let request = Request::builder()
            .method("POST")
            .uri("http://localhost:8080/echo")
            .header("Authorization", "Bearer test-key")
            .header(
                crate::proxy::headers::X_TARGET_URL,
                "http://localhost:8081/echo",
            )
            .header("Content-Type", "text/plain")
            .body(Body::from(request_body))
            .unwrap();

        let response = app.clone().oneshot(request).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);

        let body_bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        assert_eq!(&body_bytes[..], request_body.as_bytes());

        // Test 3: Request without auth should fail
        let request = Request::builder()
            .method("GET")
            .uri("http://localhost:8080/")
            .header(
                crate::proxy::headers::X_TARGET_URL,
                "http://localhost:8081/",
            )
            .body(Body::empty())
            .unwrap();

        let response = app.clone().oneshot(request).await.unwrap();
        assert_eq!(response.status(), StatusCode::UNAUTHORIZED);

        // Test 4: Health check should bypass auth
        let request = Request::builder()
            .method("GET")
            .uri("http://localhost:8080/health")
            .body(Body::empty())
            .unwrap();

        let response = app.clone().oneshot(request).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn test_error_handling() {
        // Start mock backend
        run_mock_backend(8082)
            .await
            .expect("Failed to start mock backend");

        let config = ProxyConfig::default();
        let mut auth_config = AuthConfig::default();
        auth_config
            .api_keys
            .insert(ApiKey::try_new("test-key".to_string()).unwrap());

        let service = ProxyService::new(config);
        let app = service.into_router(auth_config);

        // Test 1: Invalid target URL
        let request = Request::builder()
            .method("GET")
            .uri("http://localhost:8080/")
            .header("Authorization", "Bearer test-key")
            .header(crate::proxy::headers::X_TARGET_URL, "not-a-url")
            .body(Body::empty())
            .unwrap();

        let response = app.clone().oneshot(request).await.unwrap();
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);

        // Test 2: Backend returns error status
        let request = Request::builder()
            .method("GET")
            .uri("http://localhost:8080/status/500")
            .header("Authorization", "Bearer test-key")
            .header(
                crate::proxy::headers::X_TARGET_URL,
                "http://localhost:8082/status/500",
            )
            .body(Body::empty())
            .unwrap();

        let response = app.clone().oneshot(request).await.unwrap();
        assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);

        // Test 3: Request to non-existent backend
        let request = Request::builder()
            .method("GET")
            .uri("http://localhost:8080/")
            .header("Authorization", "Bearer test-key")
            .header(
                crate::proxy::headers::X_TARGET_URL,
                "http://localhost:9999/",
            ) // Non-existent port
            .body(Body::empty())
            .unwrap();

        let response = app.clone().oneshot(request).await.unwrap();
        assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
    }

    #[tokio::test]
    async fn test_request_size_limits() {
        // Start mock backend
        run_mock_backend(8083)
            .await
            .expect("Failed to start mock backend");

        let config = ProxyConfig {
            max_request_size: RequestSizeLimit::try_new(1024).unwrap(), // 1KB limit
            max_response_size: ResponseSizeLimit::try_new(10 * 1024 * 1024).unwrap(),
            request_timeout: Duration::from_secs(5),
            ring_buffer: RingBufferConfig::default(),
            bedrock_region: None,
        };

        let mut auth_config = AuthConfig::default();
        auth_config
            .api_keys
            .insert(ApiKey::try_new("test-key".to_string()).unwrap());

        let service = ProxyService::new(config);
        let app = service.into_router(auth_config);

        // Test: Request exceeding size limit
        let large_body = "x".repeat(2048); // 2KB, exceeds limit
        let request = Request::builder()
            .method("POST")
            .uri("http://localhost:8080/echo")
            .header("Authorization", "Bearer test-key")
            .header(
                crate::proxy::headers::X_TARGET_URL,
                "http://localhost:8083/echo",
            )
            .header("Content-Type", "text/plain")
            .body(Body::from(large_body))
            .unwrap();

        let response = app.oneshot(request).await.unwrap();
        assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);
    }

    #[tokio::test]
    async fn test_response_size_limits() {
        // Start mock backend
        run_mock_backend(8084)
            .await
            .expect("Failed to start mock backend");

        let config = ProxyConfig {
            max_request_size: RequestSizeLimit::try_new(10 * 1024 * 1024).unwrap(),
            max_response_size: ResponseSizeLimit::try_new(1024).unwrap(), // 1KB limit
            request_timeout: Duration::from_secs(5),
            ring_buffer: RingBufferConfig::default(),
            bedrock_region: None,
        };

        let mut auth_config = AuthConfig::default();
        auth_config
            .api_keys
            .insert(ApiKey::try_new("test-key".to_string()).unwrap());

        let service = ProxyService::new(config);
        let app = service.into_router(auth_config);

        // Test: Response exceeding size limit
        let request = Request::builder()
            .method("GET")
            .uri("http://localhost:8080/large")
            .header("Authorization", "Bearer test-key")
            .header(
                crate::proxy::headers::X_TARGET_URL,
                "http://localhost:8084/large",
            )
            .body(Body::empty())
            .unwrap();

        let response = app.oneshot(request).await.unwrap();
        // Note: Response size limiting happens during streaming, so we should still get OK
        // but the body will be truncated
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn test_timeout_handling() {
        // Start mock backend
        run_mock_backend(8085)
            .await
            .expect("Failed to start mock backend");

        let config = ProxyConfig {
            max_request_size: RequestSizeLimit::try_new(10 * 1024 * 1024).unwrap(),
            max_response_size: ResponseSizeLimit::try_new(10 * 1024 * 1024).unwrap(),
            request_timeout: Duration::from_millis(50), // Very short timeout
            ring_buffer: RingBufferConfig::default(),
            bedrock_region: None,
        };

        let mut auth_config = AuthConfig::default();
        auth_config
            .api_keys
            .insert(ApiKey::try_new("test-key".to_string()).unwrap());

        let service = ProxyService::new(config);
        let app = service.into_router(auth_config);

        // Test: Request that takes too long
        let request = Request::builder()
            .method("GET")
            .uri("http://localhost:8080/slow")
            .header("Authorization", "Bearer test-key")
            .header(
                crate::proxy::headers::X_TARGET_URL,
                "http://localhost:8085/slow",
            )
            .body(Body::empty())
            .unwrap();

        let response = app.oneshot(request).await.unwrap();
        assert_eq!(response.status(), StatusCode::REQUEST_TIMEOUT);
    }

    #[tokio::test]
    async fn test_concurrent_requests() {
        // Start mock backend
        run_mock_backend(8086)
            .await
            .expect("Failed to start mock backend");

        let config = ProxyConfig::default();
        let mut auth_config = AuthConfig::default();
        auth_config
            .api_keys
            .insert(ApiKey::try_new("test-key".to_string()).unwrap());

        let service = ProxyService::new(config);
        let app = Arc::new(service.into_router(auth_config));

        // Launch multiple concurrent requests
        let mut handles = vec![];
        for i in 0..10 {
            let app = app.clone();
            let handle = tokio::spawn(async move {
                let request = Request::builder()
                    .method("POST")
                    .uri("http://localhost:8080/echo")
                    .header("Authorization", "Bearer test-key")
                    .header(
                        crate::proxy::headers::X_TARGET_URL,
                        "http://localhost:8086/echo",
                    )
                    .header("Content-Type", "text/plain")
                    .body(Body::from(format!("Request {i}")))
                    .unwrap();

                // Clone the router from the Arc for each request
                let response = (*app).clone().oneshot(request).await.unwrap();
                assert_eq!(response.status(), StatusCode::OK);

                let body_bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
                    .await
                    .unwrap();
                String::from_utf8(body_bytes.to_vec()).unwrap()
            });
            handles.push(handle);
        }

        // Wait for all requests to complete
        let results: Vec<String> = futures_util::future::join_all(handles)
            .await
            .into_iter()
            .map(|r| r.unwrap())
            .collect();

        // Verify all requests completed successfully
        assert_eq!(results.len(), 10);
        for result in &results {
            assert!(result.contains("Request"));
        }
    }

    #[tokio::test]
    async fn test_audit_event_recording() {
        // Start mock backend
        run_mock_backend(8087)
            .await
            .expect("Failed to start mock backend");

        let config = ProxyConfig::default();
        let mut auth_config = AuthConfig::default();
        auth_config
            .api_keys
            .insert(ApiKey::try_new("test-key".to_string()).unwrap());

        let service = ProxyService::new(config);
        let ring_buffer = service.ring_buffer();
        let app = service.into_router(auth_config);

        // Make a request
        let request = Request::builder()
            .method("POST")
            .uri("http://localhost:8080/echo")
            .header("Authorization", "Bearer test-key")
            .header(
                crate::proxy::headers::X_TARGET_URL,
                "http://localhost:8087/echo",
            )
            .header("Content-Type", "text/plain")
            .body(Body::from("Test audit"))
            .unwrap();

        let response = app.oneshot(request).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);

        // Wait a bit for the streaming service to write events to the ring buffer
        tokio::time::sleep(Duration::from_millis(10)).await;

        // Check ring buffer stats - should show events were written
        let stats = ring_buffer.stats();

        // The audit processor will be consuming events, so we just verify that events were written
        // This proves the audit recording system is working
        assert!(
            stats.total_writes >= 2,
            "Should have written at least 2 audit events (request + response), found: {}",
            stats.total_writes
        );
    }

    #[tokio::test]
    async fn test_invalid_http_methods() {
        // Start mock backend
        run_mock_backend(8088)
            .await
            .expect("Failed to start mock backend");

        let config = ProxyConfig::default();
        let mut auth_config = AuthConfig::default();
        auth_config
            .api_keys
            .insert(ApiKey::try_new("test-key".to_string()).unwrap());

        let service = ProxyService::new(config);
        let ring_buffer = service.ring_buffer();
        let app = service.into_router(auth_config);

        // Test with various HTTP methods
        let methods = [
            "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "CONNECT", "TRACE",
        ];

        for method in &methods {
            let request = Request::builder()
                .method(*method)
                .uri("http://localhost:8080/")
                .header("Authorization", "Bearer test-key")
                .header(
                    crate::proxy::headers::X_TARGET_URL,
                    "http://localhost:8088/",
                )
                .body(Body::empty())
                .unwrap();

            let response = app.clone().oneshot(request).await.unwrap();
            // All standard methods should work
            assert!(
                response.status().is_success()
                    || response.status().is_client_error()
                    || response.status().is_server_error()
            );
        }

        // Wait for audit events
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Verify no error events for invalid methods
        let mut error_events = 0;
        while let Some((_, data)) = ring_buffer.read() {
            if let Ok(event) = serde_json::from_slice::<AuditEvent>(&data) {
                if matches!(
                    event.event_type,
                    AuditEventType::Error {
                        phase: ErrorPhase::RequestParsing,
                        ..
                    }
                ) {
                    error_events += 1;
                }
            }
        }

        assert_eq!(
            error_events, 0,
            "Should not have any parsing errors for standard HTTP methods"
        );
    }
}