apollo-router 2.13.1

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
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
//! Snapshot server to capture and replay HTTP responses. This is useful for:
//!
//! * Capturing HTTP responses from a real API or server, and replaying them in tests
//! * Mocking responses from a non-existent HTTP API for testing
//! * Working offline by capturing output from a server, and replaying it
//!
//! For example, this can be used with the router `override_subgraph_url` to replay recorded
//! responses from GraphQL subgraphs. Or it can be used with `override_url` in Connectors, to
//! record the HTTP responses from an external REST API. This allows the replayed responses to
//! be used in tests, or even in Apollo Sandbox to work offline or avoid hitting the REST API
//! too frequently.
//!
//! The snapshot server can be started from tests by calling the [`SnapshotServer::spawn`] method,
//! or as a standalone application by invoking [`standalone::main`]. In the latter case, there
//! is a binary wrapper in `http_snapshot_main` that can be run like this:
//!
//! `cargo run --bin snapshot --features snapshot -- --snapshot-path <file> --url <base URL to snapshot> [--offline] [--update] [--port <port number>] [-v]`
//!
//! Any requests made to the snapshot server will be proxied on to the given base URL, and the
//! responses will be saved to the given file. The next time the snapshot server receives the
//! same request, it will respond with the response recorded in the file rather than sending the
//! request to the upstream server.
//!
//! The snapshot file can be manually edited to manipulate responses for testing purposes, or to
//! redact information that you don't want to include in source-controlled snapshot files.
//!
//! Requests are matched to snapshots based on the URL path, HTTP method, and base64 encoding of
//! the request body (if there is one). If the snapshot specifies the `path` field, the URL path
//! must match exactly. Alternatively, a snapshot containing a `regex` field will match any URL
//! path that matches the regular expression. A snapshot with an exact `path` match takes
//! precedence over a snapshot with `regex`. Snapshots recorded by the server will always specify
//! `path`. The only way to use `regex` is to manually edit a snapshot file. A typical pattern is
//! to record a snapshot from a REST API, then manually change `path` to `regex` and replace the
//! variable part of the path with `.*`. Note that any special characters in the path that have
//! meaning to the `regex` crate must be escaped with `\\`, such as the `?` in a URL query
//! parameter.
//!
//! The offline mode will never call the upstream server, and will always return a saved snapshot
//! response. If one is not available, a `500` error is returned. This is useful for tests, for
//! example to ensure that CI builds never attempt to access the network.
//!
//! The update mode can be used to force an update of recorded snapshots, even if there is already
//! a snapshot saved in the file. This overrides the offline mode, and is useful to update tests
//! when a change is made to the upstream HTTP responses.
//!
//! The set of response headers returned can be filtered by supplying a list of headers to include.
//! This is typically desirable, as headers may contain ephemeral information like dates or tokens.
//!
//! **IMPORTANT:** this module stores HTTP responses to the local file system in plain text. It
//! should not be used with APIs that return sensitive data.

use std::collections::BTreeMap;
use std::net::SocketAddr;
use std::path::Path;
use std::str::FromStr;
use std::sync::Arc;

use axum::Router;
use axum::extract::Path as AxumPath;
use axum::extract::RawQuery;
use axum::extract::State;
use axum::routing::any;
use base64::Engine;
use http::HeaderMap;
use http::HeaderName;
use http::HeaderValue;
use http::Method;
use http::Uri;
use http::header::CONNECTION;
use http::header::CONTENT_LENGTH;
use http::header::HOST;
use http::header::TRAILER;
use http::header::TRANSFER_ENCODING;
use http::header::UPGRADE;
use hyper::StatusCode;
use hyper_rustls::ConfigBuilderExt;
use indexmap::IndexMap;
use parking_lot::Mutex;
use regex::Regex;
use serde::Deserialize;
use serde::Serialize;
use serde_json_bytes::Value;
use serde_json_bytes::json;
use tokio::net::TcpListener;
use tower::ServiceExt;
use tracing::debug;
use tracing::error;
use tracing::info;
use tracing::warn;

use crate::configuration::shared::Client;
use crate::services::http::HttpClientService;
use crate::services::http::HttpRequest;
use crate::services::router;
use crate::services::router::body::RouterBody;

/// Headers that will not be passed on to the upstream API
static FILTERED_HEADERS: [HeaderName; 6] = [
    CONNECTION,
    TRAILER,
    TRANSFER_ENCODING,
    UPGRADE,
    HOST,
    HeaderName::from_static("keep-alive"),
];

/// An error from the snapshot server
#[derive(Debug, thiserror::Error)]
enum SnapshotError {
    /// Unable to load snapshots
    #[error("unable to load snapshot file - {0}")]
    IoError(#[from] std::io::Error),
    /// Unable to parse snapshots
    #[error("unable to parse snapshots - {0}")]
    ParseError(#[from] serde_json::Error),
    /// Invalid snapshot
    #[error("invalid snapshot - {0}")]
    InvalidSnapshot(String),
}

/// A server that mocks an API using snapshots recorded from actual HTTP responses.
#[cfg_attr(test, allow(unreachable_pub))]
pub struct SnapshotServer {
    // The socket address the server is listening on
    #[cfg_attr(not(test), allow(dead_code))]
    socket_address: SocketAddr,
}

#[derive(Clone)]
struct SnapshotServerState {
    client: HttpClientService,
    base_url: Uri,
    snapshots_by_key: Arc<Mutex<BTreeMap<String, Snapshot>>>,
    snapshots_by_regex: Arc<Mutex<Vec<Snapshot>>>,
    snapshot_file: Box<Path>,
    offline: bool,
    update: bool,
    include_headers: Option<Vec<String>>,
}

async fn root_handler(
    State(state): State<SnapshotServerState>,
    req: http::Request<axum::body::Body>,
) -> Result<http::Response<RouterBody>, StatusCode> {
    handle(State(state), req, "/".to_string(), None).await
}

async fn handler(
    State(state): State<SnapshotServerState>,
    AxumPath(path): AxumPath<String>,
    RawQuery(query): RawQuery,
    req: http::Request<axum::body::Body>,
) -> Result<http::Response<RouterBody>, StatusCode> {
    handle(State(state), req, path, query).await
}

async fn handle(
    State(state): State<SnapshotServerState>,
    req: http::Request<axum::body::Body>,
    path: String,
    query: Option<String>,
) -> Result<http::Response<RouterBody>, StatusCode> {
    let path = if let Some(query) = query {
        format!("{path}?{query}")
    } else {
        path
    };
    let uri = [state.base_url.to_string(), path.clone()].concat();
    let method = req.method().clone();
    let version = req.version();
    let request_headers: HeaderMap = req
        .headers()
        .clone()
        .drain()
        .filter_map(|(name, value)| {
            name.and_then(|name| {
                if !FILTERED_HEADERS.contains(&name) {
                    Some((name, value))
                } else {
                    None
                }
            })
        })
        .collect();
    let body_bytes = axum::body::to_bytes(req.into_body(), usize::MAX)
        .await
        .unwrap();
    let request_json_body = serde_json::from_slice(&body_bytes).unwrap_or(Value::Null);

    let key = snapshot_key(
        Some(method.as_str()),
        Some(path.as_str()),
        &request_json_body,
    );

    if let Some(response) = response_from_snapshot(&state, &uri, &method, &key) {
        Ok(response)
    } else if state.offline && !state.update {
        fail(
            uri,
            method,
            "Offline mode enabled and no snapshot available",
        )
    } else {
        debug!(
            url = %uri,
            method = %method,
            "Taking snapshot"
        );
        let mut request = http::Request::builder()
            .method(method.clone())
            .version(version)
            .uri(uri.clone())
            .body(router::body::from_bytes(body_bytes))
            .unwrap();
        *request.headers_mut() = request_headers;
        let response = state
            .client
            .oneshot(HttpRequest {
                http_request: request,
                context: crate::context::Context::new(),
            })
            .await
            .unwrap();
        let (parts, body) = response.http_response.into_parts();

        if let Ok(body_bytes) = router::body::into_bytes(body).await {
            if let Ok(response_json_body) = serde_json::from_slice(&body_bytes) {
                let snapshot = Snapshot {
                    request: Request {
                        method: Some(method.to_string()),
                        path: Some(path),
                        regex: None,
                        body: request_json_body,
                    },
                    response: Response {
                        status: parts.status.as_u16(),
                        headers: map_headers(parts.headers, |name| {
                            state
                                .include_headers
                                .as_ref()
                                .map(|headers| headers.contains(&name.to_string()))
                                .unwrap_or(true)
                        }),
                        body: response_json_body,
                    },
                };
                {
                    let mut snapshots_by_key = state.snapshots_by_key.lock();
                    let mut snapshots_by_regex = state.snapshots_by_regex.lock();
                    snapshots_by_key.insert(key, snapshot.clone());
                    if let Err(e) = save(
                        state.snapshot_file,
                        &mut snapshots_by_key,
                        &mut snapshots_by_regex,
                    ) {
                        error!(
                            url = %uri,
                            method = %method,
                            error = ?e,
                            "Unable to save snapshot"
                        );
                    }
                }
                if let Ok(response) = snapshot.into_response() {
                    Ok(response)
                } else {
                    fail(uri, method, "Unable to convert snapshot into response body")
                }
            } else {
                fail(uri, method, "Unable to parse response body as JSON")
            }
        } else {
            fail(uri, method, "Unable to read response body")
        }
    }
}

fn response_from_snapshot(
    state: &SnapshotServerState,
    uri: &String,
    method: &Method,
    key: &String,
) -> Option<http::Response<RouterBody>> {
    let mut snapshots_by_key = state.snapshots_by_key.lock();
    let snapshots_by_regex = state.snapshots_by_regex.lock();
    if state.update {
        snapshots_by_key.remove(key);
        None
    } else {
        snapshots_by_key
            .get(key)
            .inspect(|snapshot| {
                debug!(
                    url = %uri,
                    method = %method,
                    path = %snapshot.request.path.as_ref().unwrap_or(&String::from("")),
                    "Found existing snapshot"
                );
            })
            .or_else(|| {
                // Look up snapshot using regex
                for snapshot in snapshots_by_regex.iter() {
                    if let Some(regex) = &snapshot.request.regex
                        && regex.is_match(uri)
                    {
                        debug!(
                            url = %uri,
                            method = %method,
                            regex = %regex.to_string(),
                            "Found existing snapshot"
                        );
                        return Some(snapshot);
                    }
                }
                None
            })
            .and_then(|snapshot| {
                snapshot
                    .clone()
                    .into_response()
                    .map_err(|e| error!("Unable to convert snapshot into HTTP response: {:?}", e))
                    .ok()
            })
    }
}

fn fail(
    uri: String,
    method: Method,
    message: &str,
) -> Result<http::Response<RouterBody>, StatusCode> {
    error!(
        url = %uri,
        method = %method,
        message
    );
    http::Response::builder()
        .status(StatusCode::INTERNAL_SERVER_ERROR)
        .body(router::body::from_bytes(
            json!({ "error": message}).to_string(),
        ))
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
}

fn map_headers<F: Fn(&str) -> bool>(
    headers: HeaderMap<HeaderValue>,
    include: F,
) -> IndexMap<String, Vec<String>> {
    headers.iter().fold(
        IndexMap::new(),
        |mut map: IndexMap<String, Vec<String>>, (name, value)| {
            let name = name.to_string();
            if include(&name) {
                let value = value.to_str().unwrap_or_default().to_string();
                map.entry(name).or_default().push(value);
            }
            map
        },
    )
}

fn save<P: AsRef<Path>>(
    path: P,
    snapshots_by_key: &mut BTreeMap<String, Snapshot>,
    snapshots_by_regex: &mut [Snapshot],
) -> Result<(), SnapshotError> {
    let path = path.as_ref();
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let snapshots = snapshots_by_key
        .values()
        .cloned()
        .chain(snapshots_by_regex.iter().cloned())
        .collect::<Vec<_>>();
    std::fs::write(path, serde_json::to_string_pretty(&snapshots)?).map_err(Into::into)
}

fn load<P: AsRef<Path>>(
    path: P,
) -> Result<(BTreeMap<String, Snapshot>, Vec<Snapshot>), SnapshotError> {
    let str = std::fs::read_to_string(path)?;
    let snapshots: Vec<Snapshot> = serde_json::from_str(&str)?;
    let mut snapshots_by_key: BTreeMap<String, Snapshot> = Default::default();
    let mut snapshots_by_regex: Vec<Snapshot> = Default::default();
    for snapshot in snapshots.into_iter() {
        if snapshot.request.regex.is_some() {
            if snapshot.request.path.is_some() {
                return Err(SnapshotError::InvalidSnapshot(String::from(
                    "snapshot cannot specify both regex and path",
                )));
            }
            snapshots_by_regex.push(snapshot);
        } else {
            snapshots_by_key.insert(snapshot.key(), snapshot);
        }
    }
    Ok((snapshots_by_key, snapshots_by_regex))
}

impl SnapshotServer {
    /// Spawn the server in a new task and return. Used for tests.
    #[cfg_attr(test, allow(unreachable_pub))]
    pub async fn spawn<P: AsRef<Path>>(
        snapshot_path: P,
        base_url: Uri,
        offline: bool,
        update: bool,
        include_headers: Option<Vec<String>>,
        port: Option<u16>,
    ) -> Self {
        let listener = match port {
            Some(port) => Some(
                TcpListener::bind(format!("127.0.0.1:{port}"))
                    .await
                    .expect("Failed to bind an OS port for snapshot server"),
            ),
            None => None,
        };
        Self::inner_start(
            snapshot_path,
            base_url,
            true,
            offline,
            update,
            include_headers,
            listener,
        )
        .await
    }

    /// Start the server and block. Can be used to run the server as a standalone application.
    pub(crate) async fn start<P: AsRef<Path>>(
        snapshot_path: P,
        base_url: Uri,
        offline: bool,
        update: bool,
        include_headers: Option<Vec<String>>,
        listener: Option<TcpListener>,
    ) -> Self {
        Self::inner_start(
            snapshot_path,
            base_url,
            false,
            offline,
            update,
            include_headers,
            listener,
        )
        .await
    }

    /// Get the URI the server is listening at
    #[cfg_attr(not(test), allow(dead_code))]
    #[cfg_attr(test, allow(unreachable_pub))]
    pub fn uri(&self) -> String {
        format!("http://{}", self.socket_address)
    }

    async fn inner_start<P: AsRef<Path>>(
        snapshot_path: P,
        base_url: Uri,
        spawn: bool,
        offline: bool,
        update: bool,
        include_headers: Option<Vec<String>>,
        listener: Option<TcpListener>,
    ) -> Self {
        if update {
            info!("Running in update mode ⬆️");
        } else if offline {
            info!("Running in offline mode ⛔️");
        }

        let snapshot_file = snapshot_path.as_ref();

        let (snapshots_by_key, snapshots_by_regex) = match load(snapshot_file) {
            Err(SnapshotError::IoError(ioe)) if ioe.kind() == std::io::ErrorKind::NotFound => {
                if offline {
                    warn!("Snapshot file not found in offline mode - all requests will fail");
                } else {
                    info!("Snapshot file not found - new snapshot file will be recorded");
                }
                (BTreeMap::default(), vec![])
            }
            Err(e) => {
                if offline {
                    warn!(
                        "Unable to load snapshot file in offline mode - all requests will fail: {e}"
                    );
                } else {
                    warn!("Unable to load snapshot file - new snapshot file will be recorded: {e}");
                }
                (BTreeMap::default(), vec![])
            }
            Ok((snapshots_by_key, snapshots_by_regex)) => {
                info!(
                    "Loaded {} snapshots",
                    snapshots_by_key.len() + snapshots_by_regex.len()
                );
                (snapshots_by_key, snapshots_by_regex)
            }
        };

        let http_service = HttpClientService::test_new(
            "test",
            rustls::ClientConfig::builder()
                .with_native_roots()
                .expect("Able to load native roots")
                .with_no_client_auth(),
            Client::builder().build(),
        )
        .expect("can create a HttpService");
        let app = Router::new()
            .route("/", any(root_handler))
            .route("/{*path}", any(handler)) // won't match root, so we need the root handler above
            .with_state(SnapshotServerState {
                client: http_service,
                base_url: base_url.clone(),
                snapshots_by_key: Arc::new(Mutex::new(snapshots_by_key)),
                snapshots_by_regex: Arc::new(Mutex::new(snapshots_by_regex)),
                snapshot_file: Box::from(snapshot_file),
                offline,
                update,
                include_headers,
            });
        let listener = match listener {
            Some(listener) => listener,
            None => TcpListener::bind("127.0.0.1:0")
                .await
                .expect("Failed to bind an OS port for snapshot server"),
        };
        let local_address = listener
            .local_addr()
            .expect("Failed to get snapshot server address.");
        info!(
            "Snapshot server listening on port {:?}",
            local_address.port()
        );
        if spawn {
            tokio::spawn(async move {
                axum::serve(listener, app).await.unwrap();
            });
        } else {
            axum::serve(listener, app).await.unwrap();
        }
        Self {
            socket_address: local_address,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct Snapshot {
    request: Request,
    response: Response,
}

impl Snapshot {
    fn into_response(self) -> Result<http::Response<RouterBody>, ()> {
        let mut response = http::Response::builder().status(self.response.status);
        let body_string = self.response.body.to_string();
        if let Some(headers) = response.headers_mut() {
            for (name, values) in self.response.headers.into_iter() {
                if let Ok(name) = HeaderName::from_str(&name.clone()) {
                    for value in values {
                        if let Ok(value) = HeaderValue::from_str(&value.clone()) {
                            headers.insert(name.clone(), value);
                        }
                    }
                } else {
                    warn!("Invalid header name `{}` in snapshot", name);
                }
            }

            // Rewrite the content length header to the actual body length. Serializing and
            // deserializing the snapshot may result in a different length due to formatting
            // differences.
            headers.insert(CONTENT_LENGTH, HeaderValue::from(body_string.len()));
        }
        if let Ok(response) = response.body(router::body::from_bytes(body_string)) {
            return Ok(response);
        }
        Err(())
    }

    fn key(&self) -> String {
        snapshot_key(
            self.request.method.as_deref(),
            self.request.path.as_deref(),
            &self.request.body,
        )
    }
}

fn snapshot_key(method: Option<&str>, path: Option<&str>, body: &Value) -> String {
    if body.is_null() {
        format!("{}-{}", method.unwrap_or("GET"), path.unwrap_or("/"))
    } else {
        let body = base64::engine::general_purpose::STANDARD.encode(body.to_string());
        format!(
            "{}-{}-{}",
            method.unwrap_or("GET"),
            path.unwrap_or("/"),
            body,
        )
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct Request {
    method: Option<String>,
    path: Option<String>,
    #[serde(with = "serde_regex", skip_serializing_if = "Option::is_none", default)]
    regex: Option<Regex>,
    body: Value,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct Response {
    status: u16,
    #[serde(default)]
    headers: IndexMap<String, Vec<String>>,
    body: Value,
}

/// Standalone snapshot server
pub(crate) mod standalone {
    use std::path::PathBuf;

    use clap::Parser;
    use http::Uri;
    use tokio::net::TcpListener;
    use tracing_core::Level;

    use super::SnapshotServer;

    #[derive(Parser, Debug)]
    #[clap(name = "snapshot", about = "Apollo snapshot server")]
    #[command(disable_version_flag(true))]
    struct Args {
        /// Snapshot location relative to the project directory.
        #[arg(short, long, value_parser)]
        snapshot_path: PathBuf,

        /// Base URL for the server.
        #[arg(short = 'l', long, value_parser)]
        url: Uri,

        /// Run in offline mode, without making any HTTP requests to the base URL.
        #[arg(short, long)]
        offline: bool,

        /// Force snapshot updates (overrides `offline`).
        #[arg(short, long)]
        update: bool,

        /// Optional port to listen on (defaults to an ephemeral port).
        #[arg(short, long)]
        port: Option<u16>,

        /// Turn on verbose output
        #[arg(short = 'v', long)]
        verbose: bool,
    }

    /// Run the snapshot server as a standalone application
    pub async fn main() {
        let args = Args::parse();

        let subscriber = tracing_subscriber::FmtSubscriber::builder()
            .with_max_level(if args.verbose {
                Level::DEBUG
            } else {
                Level::INFO
            })
            .finish();
        tracing::subscriber::set_global_default(subscriber)
            .expect("setting default subscriber failed");

        let listener = match args.port {
            Some(port) => Some(
                TcpListener::bind(format!("127.0.0.1:{port}"))
                    .await
                    .expect("Failed to bind an OS port for snapshot server"),
            ),
            None => None,
        };

        SnapshotServer::start(
            args.snapshot_path,
            args.url,
            args.offline,
            args.update,
            None,
            listener,
        )
        .await;
    }
}