-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhorizon_fallback.rs
More file actions
672 lines (569 loc) · 23.4 KB
/
horizon_fallback.rs
File metadata and controls
672 lines (569 loc) · 23.4 KB
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
use axum::{
body::{to_bytes, Body},
extract::State,
http::{Method, Request, Response, StatusCode},
response::IntoResponse,
};
use http::header::HeaderName;
use reqwest::header::HeaderValue;
use std::error::Error as StdError;
use crate::state::AppState;
/// Forward unmatched requests to the legacy Horizon PDP service (Python-based PDP)
pub(super) async fn fallback_to_horizon(
State(state): State<AppState>,
req: Request<Body>,
) -> impl IntoResponse {
// Get the path for forwarding
let path = req.uri().path_and_query();
let path = match path {
Some(path) => path.to_string(),
None => "".to_string(),
};
// Convert method to reqwest method
let method = match *req.method() {
Method::GET => reqwest::Method::GET,
Method::POST => reqwest::Method::POST,
Method::PUT => reqwest::Method::PUT,
Method::DELETE => reqwest::Method::DELETE,
Method::PATCH => reqwest::Method::PATCH,
Method::HEAD => reqwest::Method::HEAD,
Method::OPTIONS => reqwest::Method::OPTIONS,
_ => {
log::error!("Unsupported HTTP method: {}", req.method());
return (
StatusCode::METHOD_NOT_ALLOWED,
format!("Unsupported HTTP method: {}", req.method()),
)
.into_response();
}
};
// Prepare request builder
let url = state.config.horizon.get_url(path);
log::debug!("Forwarding request to Horizon: {} {}", req.method(), url);
let req_builder = state.horizon_client.request(method, &url);
// Forward headers
let mut req_builder = req_builder;
for (key, value) in req.headers() {
if let Ok(header_value) = HeaderValue::from_bytes(value.as_bytes()) {
req_builder = req_builder.header(key.as_str(), header_value);
}
}
// Forward body if present
let body_bytes = match to_bytes(req.into_body(), usize::MAX).await {
Ok(bytes) => bytes,
Err(_) => return (StatusCode::BAD_GATEWAY, "Failed to read request body").into_response(),
};
if !body_bytes.is_empty() {
req_builder = req_builder.body(body_bytes);
}
// Send request using horizon_client's send method
match req_builder.send().await {
Ok(response) => {
// Get response details
let status = response.status();
let headers = response.headers().clone();
let bytes = match response.bytes().await {
Ok(bytes) => bytes,
Err(e) => {
log::error!("Failed to read response body: {e}");
return (StatusCode::BAD_GATEWAY, "Failed to read response body")
.into_response();
}
};
// Build response
let mut resp = Response::new(Body::from(bytes));
*resp.status_mut() = status;
// Forward response headers
for (key, value) in headers {
if let Some(key) = key {
if let Ok(name) = HeaderName::from_bytes(key.as_ref()) {
resp.headers_mut().insert(name, value);
}
}
}
resp
}
Err(e) => {
// Log detailed error information for debugging
log::error!(
"Failed to send request: {} ({:?})\nURL: {}\nError details: {:?}\nSource error: {:?}",
e, e.status(), url, e, e.source()
);
// Check for specific error types
let error_message = if e.is_timeout() {
"Request timed out while connecting to horizon server".to_string()
} else if e.is_connect() {
"Connection error occurred while connecting to horizon server".to_string()
} else if let Some(status) = e.status() {
// For status errors, forward the status code with explanation
let status_code =
StatusCode::from_u16(status.as_u16()).unwrap_or(StatusCode::BAD_GATEWAY);
return (
status_code,
format!("Error response from horizon server: {e}"),
)
.into_response();
} else {
// Generic error message for other types of errors
format!("Failed to send request: {e}")
};
(StatusCode::BAD_GATEWAY, error_message).into_response()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::TestFixture;
use http::{header::AUTHORIZATION, Method, StatusCode};
use serde_json::json;
use std::time::Duration;
use wiremock::{matchers, Mock, ResponseTemplate};
#[tokio::test]
async fn test_forward_unmatched_fail_with_wrong_auth() {
let fixture = TestFixture::with_config_modifier(|config| {
config.api_key = "test-token".to_string();
})
.await;
Mock::given(matchers::method("GET"))
.and(matchers::path("/test"))
.respond_with(ResponseTemplate::new(200).set_body_string("reached horizon unsafely"))
.expect(0)
.mount(&fixture.horizon_mock)
.await;
let mut request = fixture
.request_builder(Method::GET, "/test")
.body(Body::empty())
.expect("Failed to build request");
// replace authorization header with wrong token
request.headers_mut().remove(AUTHORIZATION);
request.headers_mut().insert(
AUTHORIZATION,
HeaderValue::from_str("Bearer wrong-token").unwrap(),
);
let response = fixture.send(request).await;
response.assert_status(StatusCode::FORBIDDEN);
assert_eq!(
response.json(),
"You are not authorized to access this resource, please check your API key."
);
fixture.horizon_mock.verify().await;
}
#[tokio::test]
async fn test_forward_unmatched_fail_with_no_auth() {
let fixture = TestFixture::new().await;
Mock::given(matchers::method("GET"))
.and(matchers::path("/test"))
.respond_with(ResponseTemplate::new(200).set_body_string("reached horizon unsafely"))
.expect(0)
.mount(&fixture.horizon_mock)
.await;
let mut request = fixture
.request_builder(Method::GET, "/test")
.body(Body::empty())
.expect("Failed to build request");
request.headers_mut().remove(AUTHORIZATION);
let response = fixture.send(request).await;
response.assert_status(StatusCode::UNAUTHORIZED);
assert_eq!(response.json(), "Missing Authorization header");
fixture.horizon_mock.verify().await;
}
#[tokio::test]
async fn test_forward_unmatched_basic() {
// Setup test fixture
let fixture = TestFixture::new().await;
// Setup mock response on horizon_mock instead of a separate mock server
Mock::given(matchers::method("GET"))
.and(matchers::path("/test"))
.respond_with(
ResponseTemplate::new(200)
.set_body_string("test response")
.insert_header("X-Response", "test"),
)
.expect(1)
.mount(&fixture.horizon_mock)
.await;
// Forward request using the state from the fixture
let response = fixture.get("/test").await;
response.assert_status(StatusCode::OK);
response.assert_header("X-Response", "test");
assert_eq!(response.json(), "test response");
// Verify mock expectations
fixture.horizon_mock.verify().await;
}
#[tokio::test]
async fn test_forward_unmatched_with_body() {
// Setup test fixture
let fixture = TestFixture::new().await;
// Setup mock with more lenient body matching
Mock::given(matchers::method("POST"))
.and(matchers::path("/echo"))
// Use any body matcher instead of specific bytes
.respond_with(|req: &wiremock::Request| {
println!(
"Received request body: {:?}",
String::from_utf8_lossy(&req.body)
);
ResponseTemplate::new(200).set_body_bytes(req.body.clone())
})
.expect(1)
.mount(&fixture.horizon_mock)
.await;
// Forward request using the state from the fixture
let response = fixture.post("/echo", &"test body").await;
response.assert_status(StatusCode::OK);
assert_eq!(response.json(), "test body");
// Verify mock expectations
fixture.horizon_mock.verify().await;
}
#[tokio::test]
async fn test_forward_unmatched_not_found() {
// Setup test fixture
let fixture = TestFixture::new().await;
// Setup mock to return 404
Mock::given(matchers::any())
.and(matchers::path_regex(".*not-found$"))
.respond_with(ResponseTemplate::new(404).set_body_string("Not Found"))
.expect(1)
.mount(&fixture.horizon_mock)
.await;
// Forward request using the state from the fixture
let response = fixture.get("/not-found").await;
response.assert_status(StatusCode::NOT_FOUND);
assert_eq!(response.json(), "Not Found");
// Verify mock expectations
fixture.horizon_mock.verify().await;
}
#[tokio::test]
async fn test_forward_unmatched_error() {
// Setup test fixture
let fixture = TestFixture::new().await;
// Setup mock to return error
Mock::given(matchers::any())
.and(matchers::path_regex(".*error$"))
.respond_with(ResponseTemplate::new(503).set_body_string("Service Unavailable"))
.expect(1)
.mount(&fixture.horizon_mock)
.await;
// Forward request using the state from the fixture
let response = fixture.get("/error").await;
response.assert_status(StatusCode::SERVICE_UNAVAILABLE);
assert_eq!(response.json(), "Service Unavailable");
// Verify mock expectations
fixture.horizon_mock.verify().await;
}
#[tokio::test]
async fn test_forward_with_query_parameters() {
// Setup test fixture
let fixture = TestFixture::new().await;
// Setup mock to verify query parameters
Mock::given(matchers::method("GET"))
.and(matchers::path_regex(".*query$"))
.and(matchers::query_param("param1", "value1"))
.and(matchers::query_param("param2", "value2"))
.respond_with(ResponseTemplate::new(200).set_body_string("query params received"))
.expect(1)
.mount(&fixture.horizon_mock)
.await;
// Forward request
let response = fixture.get("/query?param1=value1¶m2=value2").await;
response.assert_status(StatusCode::OK);
assert_eq!(response.json(), "query params received");
// Verify mock expectations
fixture.horizon_mock.verify().await;
}
#[tokio::test]
async fn test_http_method_get() {
test_specific_http_method(Method::GET).await;
}
#[tokio::test]
async fn test_http_method_post() {
test_specific_http_method(Method::POST).await;
}
#[tokio::test]
async fn test_http_method_put() {
test_specific_http_method(Method::PUT).await;
}
#[tokio::test]
async fn test_http_method_delete() {
test_specific_http_method(Method::DELETE).await;
}
#[tokio::test]
async fn test_http_method_patch() {
test_specific_http_method(Method::PATCH).await;
}
#[tokio::test]
async fn test_http_method_head() {
test_specific_http_method(Method::HEAD).await;
}
#[tokio::test]
async fn test_http_method_options() {
test_specific_http_method(Method::OPTIONS).await;
}
async fn test_specific_http_method(method: Method) {
// Setup test fixture
let fixture = TestFixture::new().await;
// Setup mock for this method
Mock::given(matchers::method(method.as_str()))
.and(matchers::path_regex(".*method-test$"))
.respond_with(
ResponseTemplate::new(200)
.set_body_string(format!("{} method works", method.as_str())),
)
.expect(1)
.mount(&fixture.horizon_mock)
.await;
// Create test request
let req = fixture
.request_builder(method.clone(), "/method-test")
.body(Body::empty())
.expect("Failed to build request");
// Forward request
let response = fixture.send(req).await;
assert_eq!(response.status, StatusCode::OK);
// For HEAD, there shouldn't be a body
if method != Method::HEAD {
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
assert_eq!(
&body[..],
format!("{} method works", method.as_str()).as_bytes()
);
}
fixture.horizon_mock.verify().await;
}
#[tokio::test]
async fn test_unsupported_http_method() {
// Setup test fixture
let fixture = TestFixture::new().await;
// Create test request with CONNECT method (not supported in our implementation)
let req = fixture
.request_builder(Method::CONNECT, "/test")
.body(Body::empty())
.expect("Failed to build request");
// Forward request
let response = fixture.send(req).await;
response.assert_status(StatusCode::METHOD_NOT_ALLOWED);
// No need to verify mock as request should not be forwarded
}
#[tokio::test]
async fn test_complex_headers() {
// Setup test fixture
let fixture = TestFixture::new().await;
// Create response headers
let response_headers = [
("Content-Type", "text/plain"),
("Cache-Control", "no-cache, must-revalidate"),
("X-Rate-Limit", "100"),
("X-Server", "Test-Server"),
("Vary", "Accept-Encoding"),
];
let auth_header = format!("Bearer {}", fixture.config.api_key);
// Setup mock that demonstrates header forwarding
// We use multiple headers in the request and in the response
Mock::given(matchers::method("GET"))
.and(matchers::path_regex(".*headers.*"))
// Use header matchers to verify headers are forwarded correctly
.and(matchers::header("Content-Type", "application/json"))
.and(matchers::header("Authorization", &auth_header))
.and(matchers::header("X-Custom-Header", "custom value"))
.respond_with({
let mut template =
ResponseTemplate::new(200).set_body_string("{\"status\":\"success\"}");
for (name, value) in &response_headers {
template = template.insert_header(*name, *value);
}
template
})
.expect(1)
.mount(&fixture.horizon_mock)
.await;
// Create test request with multiple headers
let request_headers = [
("Content-Type", "application/json"),
("Authorization", &auth_header),
("X-Custom-Header", "custom value"),
("Accept-Language", "en-US,en;q=0.9"),
("Cache-Control", "no-cache"),
];
let mut request_builder = Request::builder().method(Method::GET).uri("/headers");
for (name, value) in &request_headers {
request_builder = request_builder.header(*name, *value);
}
let req = request_builder.body(Body::empty()).unwrap();
// Forward request
let response = fixture.send(req).await;
assert_eq!(response.json(), json!({"status": "success"}));
response.assert_status(StatusCode::OK);
for (name, value) in &response_headers {
response.assert_header(name, value);
}
// When this verification succeeds, it confirms that our mock received
// the expected headers that we specified in the matchers above
fixture.horizon_mock.verify().await;
let requests = fixture.horizon_mock.received_requests().await.unwrap();
assert_eq!(requests.len(), 1);
let request = &requests[0];
assert_eq!(request.method, Method::GET);
for (name, value) in request_headers {
assert_eq!(request.headers.get(name).unwrap(), value);
}
}
#[tokio::test]
async fn test_empty_body_post() {
// Setup test fixture
let fixture = TestFixture::new().await;
// Setup mock to verify empty body POST
Mock::given(matchers::method("POST"))
.and(matchers::path_regex(".*empty-body$"))
.and(matchers::body_bytes(""))
.respond_with(ResponseTemplate::new(200).set_body_string("Empty body received"))
.expect(1)
.mount(&fixture.horizon_mock)
.await;
// Create test request with empty body
let request = fixture
.request_builder(Method::POST, "/empty-body")
.body(Body::empty())
.expect("Failed to build request");
let response = fixture.send(request).await;
// Forward request
// let response = fixture.send(req).await;
response.assert_status(StatusCode::OK);
assert_eq!(response.json(), "Empty body received");
fixture.horizon_mock.verify().await;
}
#[tokio::test]
async fn test_connection_error() {
// Set up the fixture with default settings
let fixture = TestFixture::new().await;
// Create a modified config for testing
let config = crate::config::PDPConfig {
api_key: "test_api_key".to_string(),
host: "0.0.0.0".to_string(),
debug: None,
port: 0,
use_new_authorized_users: false,
healthcheck_timeout: 1.0,
// Point to a non-existent server with a reserved port
horizon: crate::config::horizon::HorizonConfig {
host: "127.0.0.1".to_string(),
port: 1, // Use port 1 which should be unavailable
python_path: "python3".to_string(),
client_timeout: 1, // 1 second timeout for speed
health_check_timeout: 1,
health_check_interval: 5,
health_check_failure_threshold: 12,
startup_delay: 5,
restart_interval: 1,
termination_timeout: 30,
},
opa: fixture.config.opa.clone(),
cache: fixture.config.cache.clone(),
};
// Create app state with the modified config
let state = crate::state::AppState::for_testing(&config);
// Create a test request
let req = http::Request::builder()
.method(Method::GET)
.uri("/test")
.body(Body::empty())
.unwrap();
// Call the fallback function directly
let response = fallback_to_horizon(State(state), req).await.into_response();
// Assert the response has BAD_GATEWAY status
assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
// Check the response body contains the expected error message
let body_bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
let body_text = String::from_utf8_lossy(&body_bytes);
assert!(
body_text.contains("Connection"),
"Expected connection error message, got: {body_text}"
);
}
#[tokio::test]
async fn test_horizon_request_timeout() {
// For this test, we need to modify the client timeout
// but keep using the mock server
// Set up a mock server
let horizon_mock = wiremock::MockServer::start().await;
// Set up a timeout mock - mock will delay longer than timeout
Mock::given(matchers::method("GET"))
.and(matchers::path("/timeout-test"))
.respond_with(ResponseTemplate::new(200).set_delay(Duration::from_secs(3)))
.expect(1)
.mount(&horizon_mock)
.await;
// Create custom config with very short timeout
let config = crate::config::PDPConfig {
api_key: "test_api_key".to_string(),
host: "0.0.0.0".to_string(),
debug: None,
port: 0,
use_new_authorized_users: false,
healthcheck_timeout: 1.0,
horizon: crate::config::horizon::HorizonConfig {
host: horizon_mock.address().ip().to_string(),
port: horizon_mock.address().port(),
python_path: "python3".to_string(),
client_timeout: 1, // 1 second timeout (mock delays 3s)
health_check_timeout: 1,
health_check_interval: 5,
health_check_failure_threshold: 12,
startup_delay: 5,
restart_interval: 1,
termination_timeout: 30,
},
opa: crate::config::opa::OpaConfig {
url: "http://localhost:8181".to_string(),
client_query_timeout: 1,
},
cache: crate::config::cache::CacheConfig::default(),
};
// Create app state with the modified config
let state = crate::state::AppState::for_testing(&config);
// Create a test request
let req = http::Request::builder()
.method(Method::GET)
.uri("/timeout-test")
.body(Body::empty())
.unwrap();
// Call the fallback function directly
let response = fallback_to_horizon(State(state), req).await.into_response();
// Assert the response has BAD_GATEWAY status
assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
// Check the response body contains a timeout message
let body_bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
let body_text = String::from_utf8_lossy(&body_bytes);
assert!(
body_text.contains("timeout") || body_text.contains("timed out"),
"Expected timeout error message, got: {body_text}"
);
// We don't verify the mock expectations as the request might time out
// before reaching the mock
}
#[tokio::test]
async fn test_horizon_slow_but_within_timeout() {
// Setup test fixture
let mut fixture = TestFixture::new().await;
// Set a 2 second timeout
fixture.config.horizon.client_timeout = 2;
// Setup mock that responds slower but within the timeout
Mock::given(matchers::method("GET"))
.and(matchers::path("/slow-response"))
.respond_with(
ResponseTemplate::new(200)
.set_delay(Duration::from_millis(500)) // 500ms delay - within timeout
.set_body_string("slow but successful response"),
)
.expect(1)
.mount(&fixture.horizon_mock)
.await;
// Forward request - should succeed despite being slow
let response = fixture.get("/slow-response").await;
// Assert success
response.assert_status(StatusCode::OK);
assert_eq!(response.json(), "slow but successful response");
// Verify mock expectations
fixture.horizon_mock.verify().await;
}
}