apollo-router 2.14.0

A configurable, high-performance routing runtime for Apollo Federation 🚀
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
523
524
525
526
527
528
529
use std::sync::Arc;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;

use http::StatusCode;
use hyper_util::rt::TokioExecutor;
use rstest::rstest;
use rustls::RootCertStore;
use tower::BoxError;

use crate::integration::IntegrationTest;

/// See [`apollo_router::services::http::tests::tls_self_signed`] for detail about how this was generated
/// and when it expires.
const SERVER_CERT: &str = include_str!("../../src/services/http/testdata/server_self_signed.crt");
const TLS_CONFIG: &str = include_str!("./fixtures/tls.router.yml");
const TLS_CONFIG_WITH_SMALL_H2_HEADER_LIMIT: &str =
    include_str!("./fixtures/tls.header_limited.router.yml");
const TCP_CONFIG_WITH_H2_HEADER_LIMIT: &str =
    include_str!("./fixtures/tcp.header_limited.router.yml");
#[cfg(unix)]
const UNIX_CONFIG_WITH_H2_HEADER_LIMIT: &str =
    include_str!("./fixtures/unix.header_limited.router.yml");

fn load_cert_to_root_store(cert_pem: &str) -> RootCertStore {
    let mut root_store = RootCertStore::empty();
    let cert = rustls_pemfile::certs(&mut cert_pem.as_bytes())
        .collect::<Result<Vec<_>, _>>()
        .expect("valid cert");
    root_store.add(cert[0].clone()).expect("add cert");
    root_store
}

#[tokio::test(flavor = "multi_thread")]
async fn test_router_server_negotiates_http2_with_client() -> Result<(), BoxError> {
    let mut router = IntegrationTest::builder().config(TLS_CONFIG).build().await;

    router.start().await;
    router.assert_started().await;

    let https_addr = router.bind_address();

    let root_store = load_cert_to_root_store(SERVER_CERT);
    let tls_config = rustls::ClientConfig::builder()
        .with_root_certificates(root_store)
        .with_no_client_auth();

    // NOTE: both http1 and http2 are enabled
    let https_connector = hyper_rustls::HttpsConnectorBuilder::new()
        .with_tls_config(tls_config)
        .https_only()
        .enable_http1()
        .enable_http2()
        .build();

    let client =
        hyper_util::client::legacy::Client::builder(TokioExecutor::new()).build(https_connector);

    let uri: http::Uri = format!("https://localhost:{}/", https_addr.port()).parse()?;
    let request = http::Request::builder()
        .uri(uri)
        .method("POST")
        .header("content-type", "application/json")
        .body(http_body_util::Full::new(bytes::Bytes::from(
            r#"{"query":"{ __typename }"}"#,
        )))?;

    let response = client.request(request).await?;

    assert_eq!(response.status(), StatusCode::OK);

    // http2 used!
    assert_eq!(
        response.version(),
        http::Version::HTTP_2,
        "Expected HTTP/2 to be negotiated"
    );

    router.graceful_shutdown().await;
    Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn test_router_server_falls_back_to_http1_with_client() -> Result<(), BoxError> {
    let mut router = IntegrationTest::builder().config(TLS_CONFIG).build().await;

    router.start().await;
    router.assert_started().await;

    let https_addr = router.bind_address();

    let root_store = load_cert_to_root_store(SERVER_CERT);
    let tls_config = rustls::ClientConfig::builder()
        .with_root_certificates(root_store)
        .with_no_client_auth();

    let https_connector = hyper_rustls::HttpsConnectorBuilder::new()
        .with_tls_config(tls_config)
        .https_only()
        // NOTE: only http1 enabled
        .enable_http1()
        .build();

    let client = hyper_util::client::legacy::Client::builder(TokioExecutor::new())
        // NOTE: disabling http2!
        .http2_only(false)
        .build(https_connector);

    let uri: http::Uri = format!("https://localhost:{}/", https_addr.port()).parse()?;
    let request = http::Request::builder()
        .uri(uri)
        .method("POST")
        .header("content-type", "application/json")
        .body(http_body_util::Full::new(bytes::Bytes::from(
            r#"{"query":"{ __typename }"}"#,
        )))?;

    let response = client.request(request).await?;

    assert_eq!(response.status(), StatusCode::OK);

    assert_eq!(
        response.version(),
        // http1 used!
        http::Version::HTTP_11,
        "Expected HTTP/1.1 to be negotiated as fallback"
    );

    router.graceful_shutdown().await;
    Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn test_http2_max_header_list_size_exceeded() -> Result<(), BoxError> {
    let mut router = IntegrationTest::builder()
        .config(TLS_CONFIG_WITH_SMALL_H2_HEADER_LIMIT)
        .build()
        .await;

    router.start().await;
    router.assert_started().await;

    let https_addr = router.bind_address();

    let root_store = load_cert_to_root_store(SERVER_CERT);
    let tls_config = rustls::ClientConfig::builder()
        .with_root_certificates(root_store)
        .with_no_client_auth();

    let https_connector = hyper_rustls::HttpsConnectorBuilder::new()
        .with_tls_config(tls_config)
        .https_only()
        .enable_http1()
        .enable_http2()
        .build();

    let client =
        hyper_util::client::legacy::Client::builder(TokioExecutor::new()).build(https_connector);

    let uri: http::Uri = format!("https://localhost:{}/", https_addr.port()).parse()?;

    // much bigger than the config's limit (20MiB)! this also tests that the hyper default (16kb)
    // is overridden
    let large_header_value = "x".repeat(21 * 1024 * 1024);

    let request = http::Request::builder()
        .uri(uri)
        .method("POST")
        .header("content-type", "application/json")
        .header("x-large-header", large_header_value)
        .body(http_body_util::Full::new(bytes::Bytes::from(
            r#"{"query":"{ __typename }"}"#,
        )))?;

    let response = client.request(request).await?;

    assert_eq!(
        response.status(),
        StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE,
        "Expected 431 Request Header Fields Too Large when header list exceeds 20MiB limit"
    );

    // Drop response and client before shutdown so the h2 connection is closed and the router can
    // drain its connections cleanly — otherwise graceful_shutdown hangs waiting for the open conn.
    drop(response);
    drop(client);
    router.graceful_shutdown().await;
    Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn test_http2_max_header_list_size_within_limit() -> Result<(), BoxError> {
    let mut router = IntegrationTest::builder()
        .config(TLS_CONFIG_WITH_SMALL_H2_HEADER_LIMIT)
        .build()
        .await;

    router.start().await;
    router.assert_started().await;

    let https_addr = router.bind_address();

    let root_store = load_cert_to_root_store(SERVER_CERT);
    let tls_config = rustls::ClientConfig::builder()
        .with_root_certificates(root_store)
        .with_no_client_auth();

    let https_connector = hyper_rustls::HttpsConnectorBuilder::new()
        .with_tls_config(tls_config)
        .https_only()
        .enable_http1()
        .enable_http2()
        .build();

    let client =
        hyper_util::client::legacy::Client::builder(TokioExecutor::new()).build(https_connector);

    let uri: http::Uri = format!("https://localhost:{}/", https_addr.port()).parse()?;

    // create a header value that stays within the 20MiB limit of the config
    let acceptable_header_value = "y".repeat(10 * 1024 * 1024);

    let request = http::Request::builder()
        .uri(uri)
        .method("POST")
        .header("content-type", "application/json")
        .header("x-medium-header", acceptable_header_value)
        .body(http_body_util::Full::new(bytes::Bytes::from(
            r#"{"query":"{ __typename }"}"#,
        )))?;

    let response = client.request(request).await?;

    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(
        response.version(),
        http::Version::HTTP_2,
        "Expected HTTP/2 to be negotiated"
    );

    drop(response);
    drop(client);
    router.graceful_shutdown().await;
    Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn test_tcp_max_header_list_size_exceeded() -> Result<(), BoxError> {
    let mut router = IntegrationTest::builder()
        .config(TCP_CONFIG_WITH_H2_HEADER_LIMIT)
        .build()
        .await;

    router.start().await;
    router.assert_started().await;

    let tcp_addr = router.bind_address();

    // Create a custom connector for TCP
    let connector = tower::service_fn(move |_uri: http::Uri| {
        Box::pin(async move {
            let stream = tokio::net::TcpStream::connect(tcp_addr).await?;
            Ok::<_, std::io::Error>(hyper_util::rt::TokioIo::new(stream))
        })
    });

    let client = hyper_util::client::legacy::Client::builder(TokioExecutor::new())
        .http2_only(true)
        .build(connector);

    let uri: http::Uri = format!("http://{}/", tcp_addr).parse()?;

    // much bigger than the config's limit (20MiB)! this also tests that the hyper default (16kb)
    // is overridden
    let large_header_value = "x".repeat(21 * 1024 * 1024);

    let request = http::Request::builder()
        .uri(uri)
        .method("POST")
        .header("content-type", "application/json")
        .header("x-large-header", large_header_value)
        .body(http_body_util::Full::new(bytes::Bytes::from(
            r#"{"query":"{ __typename }"}"#,
        )))?;

    let response = client.request(request).await?;

    assert_eq!(
        response.status(),
        StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE,
        "Expected 431 Request Header Fields Too Large when header list exceeds 20MiB limit for TCP"
    );

    drop(response);
    drop(client);
    router.graceful_shutdown().await;
    Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn test_tcp_max_header_list_size_within_limit() -> Result<(), BoxError> {
    let mut router = IntegrationTest::builder()
        .config(TCP_CONFIG_WITH_H2_HEADER_LIMIT)
        .build()
        .await;

    router.start().await;
    router.assert_started().await;

    let tcp_addr = router.bind_address();

    // Create a custom connector for TCP
    let connector = tower::service_fn(move |_uri: http::Uri| {
        Box::pin(async move {
            let stream = tokio::net::TcpStream::connect(tcp_addr).await?;
            Ok::<_, std::io::Error>(hyper_util::rt::TokioIo::new(stream))
        })
    });

    let client = hyper_util::client::legacy::Client::builder(TokioExecutor::new())
        .http2_only(true)
        .build(connector);

    let uri: http::Uri = format!("http://{}/", tcp_addr).parse()?;

    // create a header value that stays within the 20MiB limit of the config
    let acceptable_header_value = "y".repeat(10 * 1024 * 1024);

    let request = http::Request::builder()
        .uri(uri)
        .method("POST")
        .header("content-type", "application/json")
        .header("x-medium-header", acceptable_header_value)
        .body(http_body_util::Full::new(bytes::Bytes::from(
            r#"{"query":"{ __typename }"}"#,
        )))?;

    let response = client.request(request).await?;

    assert_eq!(
        response.status(),
        StatusCode::OK,
        "Expected successful response when header list is within 20MiB limit for TCP"
    );
    assert_eq!(
        response.version(),
        http::Version::HTTP_2,
        "Expected HTTP/2 to be negotiated for TCP"
    );

    drop(response);
    drop(client);
    router.graceful_shutdown().await;
    Ok(())
}

enum HttpProtocol {
    Http1,
    Http2,
}

// both http1 and http2 have connection persistence by default; http1 uses keep-alive, but since
// http2 uses a single connection with multiple requests, the headers sent to intermediate servers
// can't be used as connection-specific headers because connections are no longer identifiable with
// a single request; so, for http2 connections default to persistently open and only close when
// explicitly closed by the client or server (via GOAWAY frames, eg)
//
// this happens as the default, so the tests below only test the persistence of connections rather
// than the explicit headers (for http1, eg) to make sure that we haven't broken anything or that
// there wasn't a regression in any of the libraries we use breaking something
#[tokio::test(flavor = "multi_thread")]
#[rstest]
#[case::http1_conn_persistence(HttpProtocol::Http1)]
#[case::http2_conn_persistence(HttpProtocol::Http2)]
async fn test_http1_connection_persistence(
    #[case] http_protocol: HttpProtocol,
) -> Result<(), BoxError> {
    let mut router = IntegrationTest::builder()
        .config(
            r#"
            supergraph:
              listen: localhost:80
            "#,
        )
        .build()
        .await;

    router.start().await;
    router.assert_started().await;

    let addr = router.bind_address();

    // using an Arc to count connections across async boundaries
    let connection_count = Arc::new(AtomicUsize::new(0));
    let connection_count_clone = connection_count.clone();

    let connector = tower::service_fn(move |uri: http::Uri| {
        let connection_count = connection_count_clone.clone();
        Box::pin(async move {
            // Increment connection counter each time a new connection is established
            connection_count.fetch_add(1, Ordering::SeqCst);
            let stream = tokio::net::TcpStream::connect(format!(
                "{}:{}",
                uri.host().unwrap_or("localhost"),
                uri.port_u16().unwrap_or(80)
            ))
            .await?;
            Ok::<_, std::io::Error>(hyper_util::rt::TokioIo::new(stream))
        })
    });

    let is_http2 = matches!(http_protocol, HttpProtocol::Http2);
    let client = hyper_util::client::legacy::Client::builder(TokioExecutor::new())
        .http2_only(is_http2)
        .build(connector);

    let uri: http::Uri = format!("http://{}/", addr).parse()?;

    // same client, multiple requests
    let num_requests = 5;
    for i in 0..num_requests {
        let request = http::Request::builder()
            .uri(uri.clone())
            .method("POST")
            .header("content-type", "application/json")
            .body(http_body_util::Full::new(bytes::Bytes::from(
                r#"{"query":"{ __typename }"}"#,
            )))?;

        let response = client.request(request).await?;

        // keep-alive is the default; so, the header might not be there, but we only care if the
        // connection remains open (ie, doesn't contain 'close')
        let connection_header = response.headers().get(http::header::CONNECTION);
        if let Some(value) = connection_header {
            let value_str = value.to_str().unwrap_or("");
            assert!(
                !value_str.contains("close"),
                "Connection should not be closed, got: {} on request {}",
                value_str,
                i + 1
            );
        }
    }

    // this is the core thing to check for keep-alive: that the number of connections is fewer than
    // the number of requests, showing re-use
    let total_connections = connection_count.load(Ordering::SeqCst);
    assert!(
        total_connections < num_requests,
        "Expected connection reuse: {} connections should be less than {} requests",
        total_connections,
        num_requests
    );

    router.graceful_shutdown().await;
    Ok(())
}

#[cfg(unix)]
mod unix_tests {
    use hyper_util::rt::TokioIo;

    use super::*;

    #[tokio::test(flavor = "multi_thread")]
    #[rstest]
    #[case::header_within_limits_of_config(UNIX_CONFIG_WITH_H2_HEADER_LIMIT, "y".repeat(10*1024*1024), StatusCode::OK)]
    #[case::header_bigger_than_config(UNIX_CONFIG_WITH_H2_HEADER_LIMIT, "n".repeat(21*1024*1024), StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE)]
    async fn test_unix_socket_max_header_list_size(
        #[case] config: &str,
        #[case] header: String,
        #[case] status_code: StatusCode,
    ) -> Result<(), BoxError> {
        use uuid::Uuid;

        // generate a unique socket path to avoid conflicts
        let uuid = Uuid::new_v4().simple().to_string();
        let socket_path = format!("/tmp/apollo_router_test_{}.sock", uuid);
        let config = config.replace("{{RANDOM}}", &uuid);

        let mut router = IntegrationTest::builder().config(&config).build().await;

        router.start().await;
        router.assert_started().await;

        // connect directly to the Unix socket using HTTP/2
        let stream = tokio::net::UnixStream::connect(&socket_path).await?;
        let (mut sender, conn) =
            hyper::client::conn::http2::handshake(TokioExecutor::new(), TokioIo::new(stream))
                .await?;

        tokio::task::spawn(async move {
            if let Err(err) = conn.await {
                eprintln!("Connection failed: {err:?}");
            }
        });

        let request = http::Request::builder()
            .uri("http://localhost/")
            .method("POST")
            .header("content-type", "application/json")
            .header("x-target-header", header)
            .body(http_body_util::Full::new(bytes::Bytes::from(
                r#"{"query":"{ __typename }"}"#,
            )))?;

        let response = sender.send_request(request).await?;

        assert_eq!(
            response.status(),
            status_code,
            "Expected status code {:?} for Unix socket with header size test",
            status_code
        );
        assert_eq!(
            response.version(),
            http::Version::HTTP_2,
            "Expected HTTP/2 to be negotiated for Unix socket"
        );

        router.graceful_shutdown().await;

        // clean up the socket file
        let _ = std::fs::remove_file(&socket_path);

        Ok(())
    }
}