wasmcloud_provider_http_server/
lib.rs

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
//! The httpserver capability provider allows wasmcloud components to receive
//! and process http(s) messages from web browsers, command-line tools
//! such as curl, and other http clients. The server is fully asynchronous,
//! and built on Rust's high-performance axum library, which is in turn based
//! on hyper, and can process a large number of simultaneous connections.
//!
//! ## Features:
//!
//! - HTTP/1 and HTTP/2
//! - TLS
//! - CORS support (select `allowed_origins`, `allowed_methods`,
//!   `allowed_headers`.) Cors has sensible defaults so it should
//!   work as-is for development purposes, and may need refinement
//!   for production if a more secure configuration is required.
//! - All settings can be specified at runtime, using per-component link settings:
//!   - bind path/address
//!   - TLS
//!   - Cors
//! - Flexible confiuration loading: from host, or from local toml or json file.
//! - Fully asynchronous, using tokio lightweight "green" threads
//! - Thread pool (for managing a pool of OS threads). The default
//!   thread pool has one thread per cpu core.
//!

use core::future::Future;
use core::pin::Pin;
use core::str::FromStr as _;
use core::task::{ready, Context, Poll};
use core::time::Duration;

use std::net::{SocketAddr, TcpListener};

use anyhow::{anyhow, bail, Context as _};
use axum::extract;
use bytes::Bytes;
use futures::Stream;
use pin_project_lite::pin_project;
use tokio::task::JoinHandle;
use tokio::{spawn, time};
use tower_http::cors::{self, CorsLayer};
use tracing::{debug, info, trace};
use wasmcloud_provider_sdk::provider::WrpcClient;
use wasmcloud_provider_sdk::{initialize_observability, load_host_data, run_provider};
use wrpc_interface_http::InvokeIncomingHandler as _;

mod address;
mod path;
mod settings;
pub use settings::{default_listen_address, load_settings, ServiceSettings};

pub async fn run() -> anyhow::Result<()> {
    initialize_observability!(
        "http-server-provider",
        std::env::var_os("PROVIDER_HTTP_SERVER_FLAMEGRAPH_PATH")
    );

    let host_data = load_host_data().context("failed to load host data")?;
    match host_data.config.get("routing_mode").map(String::as_str) {
        // Run provider in address mode by default
        Some("address") | None => run_provider(
            address::HttpServerProvider::new(host_data).context(
                "failed to create address-mode HTTP server provider from hostdata configuration",
            )?,
            "http-server-provider",
        )
        .await?
        .await,
        // Run provider in path mode
        Some("path") => {
            run_provider(
                path::HttpServerProvider::new(host_data).await.context(
                    "failed to create path-mode HTTP server provider from hostdata configuration",
                )?,
                "http-server-provider",
            )
            .await?
            .await;
        }
        Some(other) => bail!("unknown routing_mode: {other}"),
    };

    Ok(())
}

/// Build a request to send to the component from the incoming request
pub(crate) fn build_request(
    request: extract::Request,
    scheme: http::uri::Scheme,
    authority: String,
    settings: &ServiceSettings,
) -> Result<http::Request<axum::body::Body>, axum::response::ErrorResponse> {
    let method = request.method();
    if let Some(readonly_mode) = settings.readonly_mode {
        if readonly_mode
            && method != http::method::Method::GET
            && method != http::method::Method::HEAD
        {
            debug!("only GET and HEAD allowed in read-only mode");
            Err((
                http::StatusCode::METHOD_NOT_ALLOWED,
                "only GET and HEAD allowed in read-only mode",
            ))?;
        }
    }
    let (
        http::request::Parts {
            method,
            uri,
            headers,
            ..
        },
        body,
    ) = request.into_parts();
    let http::uri::Parts { path_and_query, .. } = uri.into_parts();

    let mut uri = http::Uri::builder().scheme(scheme);
    if !authority.is_empty() {
        uri = uri.authority(authority);
    }
    if let Some(path_and_query) = path_and_query {
        uri = uri.path_and_query(path_and_query);
    }
    let uri = uri
        .build()
        .map_err(|err| (http::StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
    let mut req = http::Request::builder();
    *req.headers_mut().ok_or((
        http::StatusCode::INTERNAL_SERVER_ERROR,
        "invalid request generated",
    ))? = headers;
    let req = req
        .uri(uri)
        .method(method)
        .body(body)
        .map_err(|err| (http::StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;

    Ok(req)
}

/// Invoke a component with the given request
pub(crate) async fn invoke_component(
    wrpc: &WrpcClient,
    target: &str,
    req: http::Request<axum::body::Body>,
    timeout: Option<Duration>,
    cache_control: Option<&String>,
) -> impl axum::response::IntoResponse {
    // Create a new wRPC client with all headers from the current span injected
    let mut cx = async_nats::HeaderMap::new();
    for (k, v) in
        wasmcloud_provider_sdk::wasmcloud_tracing::context::TraceContextInjector::new_with_extractor(
            &wasmcloud_provider_sdk::wasmcloud_tracing::http::HeaderExtractor(req.headers()),
        )
        .iter()
    {
        cx.insert(k.as_str(), v.as_str());
    }

    trace!(?req, component_id = target, "httpserver calling component");
    let fut = wrpc.invoke_handle_http(Some(cx), req);
    let res = if let Some(timeout) = timeout {
        let Ok(res) = time::timeout(timeout, fut).await else {
            Err(http::StatusCode::REQUEST_TIMEOUT)?
        };
        res
    } else {
        fut.await
    };
    let (res, errors, io) =
        res.map_err(|err| (http::StatusCode::INTERNAL_SERVER_ERROR, format!("{err:#}")))?;
    let io = io.map(spawn);
    let errors: Box<dyn Stream<Item = _> + Send + Unpin> = Box::new(errors);
    // TODO: Convert this to http status code
    let mut res =
        res.map_err(|err| (http::StatusCode::INTERNAL_SERVER_ERROR, format!("{err:?}")))?;
    if let Some(cache_control) = cache_control {
        let cache_control = http::HeaderValue::from_str(cache_control)
            .map_err(|err| (http::StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
        res.headers_mut().append("Cache-Control", cache_control);
    };
    axum::response::Result::<_, axum::response::ErrorResponse>::Ok(res.map(|body| ResponseBody {
        body,
        errors,
        io,
    }))
}

/// Helper function to construct a [`CorsLayer`] according to the [`ServiceSettings`].
pub(crate) fn get_cors_layer(settings: &ServiceSettings) -> anyhow::Result<CorsLayer> {
    let allow_origin = settings.cors_allowed_origins.as_ref();
    let allow_origin: Vec<_> = allow_origin
        .map(|origins| {
            origins
                .iter()
                .map(AsRef::as_ref)
                .map(http::HeaderValue::from_str)
                .collect::<Result<_, _>>()
                .context("failed to parse allowed origins")
        })
        .transpose()?
        .unwrap_or_default();
    let allow_origin = if allow_origin.is_empty() {
        cors::AllowOrigin::any()
    } else {
        cors::AllowOrigin::list(allow_origin)
    };
    let allow_headers = settings.cors_allowed_headers.as_ref();
    let allow_headers: Vec<_> = allow_headers
        .map(|headers| {
            headers
                .iter()
                .map(AsRef::as_ref)
                .map(http::HeaderName::from_str)
                .collect::<Result<_, _>>()
                .context("failed to parse allowed header names")
        })
        .transpose()?
        .unwrap_or_default();
    let allow_headers = if allow_headers.is_empty() {
        cors::AllowHeaders::any()
    } else {
        cors::AllowHeaders::list(allow_headers)
    };
    let allow_methods = settings.cors_allowed_methods.as_ref();
    let allow_methods: Vec<_> = allow_methods
        .map(|methods| {
            methods
                .iter()
                .map(AsRef::as_ref)
                .map(http::Method::from_str)
                .collect::<Result<_, _>>()
                .context("failed to parse allowed methods")
        })
        .transpose()?
        .unwrap_or_default();
    let allow_methods = if allow_methods.is_empty() {
        cors::AllowMethods::any()
    } else {
        cors::AllowMethods::list(allow_methods)
    };
    let expose_headers = settings.cors_exposed_headers.as_ref();
    let expose_headers: Vec<_> = expose_headers
        .map(|headers| {
            headers
                .iter()
                .map(AsRef::as_ref)
                .map(http::HeaderName::from_str)
                .collect::<Result<_, _>>()
                .context("failed to parse exposeed header names")
        })
        .transpose()?
        .unwrap_or_default();
    let expose_headers = if expose_headers.is_empty() {
        cors::ExposeHeaders::any()
    } else {
        cors::ExposeHeaders::list(expose_headers)
    };
    let mut cors = CorsLayer::new()
        .allow_origin(allow_origin)
        .allow_headers(allow_headers)
        .allow_methods(allow_methods)
        .expose_headers(expose_headers);
    if let Some(max_age) = settings.cors_max_age_secs {
        cors = cors.max_age(Duration::from_secs(max_age));
    }

    Ok(cors)
}

/// Helper function to create and listen on a [`TcpListener`] from the given [`ServiceSettings`].
///
/// Note that this function actually calls the `bind` method on the [`TcpSocket`], it's up to the
/// caller to ensure that the address is not already in use (or to handle the error if it is).
pub(crate) fn get_tcp_listener(settings: &ServiceSettings) -> anyhow::Result<TcpListener> {
    let socket = match &settings.address {
        SocketAddr::V4(_) => tokio::net::TcpSocket::new_v4(),
        SocketAddr::V6(_) => tokio::net::TcpSocket::new_v6(),
    }
    .context("Unable to open socket")?;
    // Copied this option from
    // https://github.com/bytecodealliance/wasmtime/blob/05095c18680927ce0cf6c7b468f9569ec4d11bd7/src/commands/serve.rs#L319.
    // This does increase throughput by 10-15% which is why we're creating the socket. We're
    // using the tokio one because it exposes the `reuseaddr` option.
    socket
        .set_reuseaddr(!cfg!(windows))
        .context("Error when setting socket to reuseaddr")?;
    socket
        .set_nodelay(true)
        .context("failed to set `TCP_NODELAY`")?;

    match settings.disable_keepalive {
        Some(false) => {
            info!("disabling TCP keepalive");
            socket
                .set_keepalive(false)
                .context("failed to disable TCP keepalive")?
        }
        None | Some(true) => socket
            .set_keepalive(true)
            .context("failed to enable TCP keepalive")?,
    }

    socket
        .bind(settings.address)
        .context("Unable to bind to address")?;
    let listener = socket.listen(1024).context("unable to listen on socket")?;
    let listener = listener.into_std().context("Unable to get listener")?;

    Ok(listener)
}

pin_project! {
    struct ResponseBody {
        #[pin]
        body: wrpc_interface_http::HttpBody,
        #[pin]
        errors: Box<dyn Stream<Item = wrpc_interface_http::HttpBodyError<axum::Error>> + Send + Unpin>,
        #[pin]
        io: Option<JoinHandle<anyhow::Result<()>>>,
    }
}

impl http_body::Body for ResponseBody {
    type Data = Bytes;
    type Error = anyhow::Error;

    fn poll_frame(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
        let mut this = self.as_mut().project();
        if let Some(io) = this.io.as_mut().as_pin_mut() {
            match io.poll(cx) {
                Poll::Ready(Ok(Ok(()))) => {
                    this.io.take();
                }
                Poll::Ready(Ok(Err(err))) => {
                    return Poll::Ready(Some(Err(
                        anyhow!(err).context("failed to complete async I/O")
                    )))
                }
                Poll::Ready(Err(err)) => {
                    return Poll::Ready(Some(Err(anyhow!(err).context("I/O task failed"))))
                }
                Poll::Pending => {}
            }
        }
        match this.errors.poll_next(cx) {
            Poll::Ready(Some(err)) => {
                if let Some(io) = this.io.as_pin_mut() {
                    io.abort();
                }
                return Poll::Ready(Some(Err(anyhow!(err).context("failed to process body"))));
            }
            Poll::Ready(None) | Poll::Pending => {}
        }
        match ready!(this.body.poll_frame(cx)) {
            Some(Ok(frame)) => Poll::Ready(Some(Ok(frame))),
            Some(Err(err)) => {
                if let Some(io) = this.io.as_pin_mut() {
                    io.abort();
                }
                Poll::Ready(Some(Err(err)))
            }
            None => {
                if let Some(io) = this.io.as_pin_mut() {
                    io.abort();
                }
                Poll::Ready(None)
            }
        }
    }
}

#[cfg(test)]
mod test {
    use std::collections::HashMap;

    use anyhow::Result;
    use futures::StreamExt;
    use wasmcloud_provider_sdk::{
        provider::initialize_host_data, run_provider, HostData, InterfaceLinkDefinition,
    };
    use wasmcloud_test_util::testcontainers::{AsyncRunner, NatsServer};

    use crate::{address, path};

    // This test is ignored by default as it requires a container runtime to be installed
    // to run the testcontainer. In GitHub Actions CI, this is only works on `linux`
    #[ignore]
    #[tokio::test]
    async fn can_listen_and_invoke_with_timeout() -> Result<()> {
        let nats_container = NatsServer::default()
            .start()
            .await
            .expect("failed to start nats-server container");
        let nats_port = nats_container
            .get_host_port_ipv4(4222)
            .await
            .expect("should be able to find the NATS port");
        let nats_address = format!("nats://127.0.0.1:{nats_port}");

        let default_address = "0.0.0.0:8080";
        let host_data = HostData {
            lattice_rpc_url: nats_address.clone(),
            lattice_rpc_prefix: "lattice".to_string(),
            provider_key: "http-server-provider-test".to_string(),
            config: std::collections::HashMap::from([
                ("default_address".to_string(), default_address.to_string()),
                ("routing_mode".to_string(), "address".to_string()),
            ]),
            link_definitions: vec![InterfaceLinkDefinition {
                source_id: "http-server-provider-test".to_string(),
                target: "test-component".to_string(),
                name: "default".to_string(),
                wit_namespace: "wasi".to_string(),
                wit_package: "http".to_string(),
                interfaces: vec!["incoming-handler".to_string()],
                source_config: std::collections::HashMap::from([(
                    "timeout_ms".to_string(),
                    "100".to_string(),
                )]),
                target_config: HashMap::new(),
                source_secrets: None,
                target_secrets: None,
            }],
            ..Default::default()
        };
        initialize_host_data(host_data.clone()).expect("should be able to initialize host data");

        let provider = run_provider(
            address::HttpServerProvider::new(&host_data)
                .expect("should be able to create provider"),
            "http-server-provider-test",
        )
        .await
        .expect("should be able to run provider");

        // Use a separate task to listen for the component message
        let conn = async_nats::connect(nats_address)
            .await
            .expect("should be able to connect");
        let mut subscriber = conn
            .subscribe("lattice.test-component.wrpc.>")
            .await
            .expect("should be able to subscribe");

        let provider_handle = tokio::spawn(provider);

        // Let the provider have a second to setup the listener
        tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
        let resp = reqwest::get("http://127.0.0.1:8080")
            .await
            .expect("should be able to make request");

        // Should have timed out
        assert_eq!(resp.status(), 408);
        // Ensure component received the message
        let msg = subscriber
            .next()
            .await
            .expect("should be able to get a message");
        assert!(msg.subject.contains("test-component"));
        provider_handle.abort();
        let _ = nats_container.stop().await;

        Ok(())
    }

    // This test is ignored by default as it requires a container runtime to be installed
    // to run the testcontainer. In GitHub Actions CI, this is only works on `linux`
    #[ignore]
    #[tokio::test]
    async fn can_support_path_based_routing() -> Result<()> {
        let nats_container = NatsServer::default()
            .start()
            .await
            .expect("failed to start nats-server container");
        let nats_port = nats_container
            .get_host_port_ipv4(4222)
            .await
            .expect("should be able to find the NATS port");
        let nats_address = format!("nats://127.0.0.1:{nats_port}");

        let default_address = "0.0.0.0:8081";
        let host_data = HostData {
            lattice_rpc_url: nats_address.clone(),
            lattice_rpc_prefix: "lattice".to_string(),
            provider_key: "http-server-provider-test".to_string(),
            config: std::collections::HashMap::from([
                ("default_address".to_string(), default_address.to_string()),
                ("routing_mode".to_string(), "path".to_string()),
                ("timeout_ms".to_string(), "100".to_string()),
            ]),
            link_definitions: vec![
                InterfaceLinkDefinition {
                    source_id: "http-server-provider-test".to_string(),
                    target: "test-component-one".to_string(),
                    name: "default".to_string(),
                    wit_namespace: "wasi".to_string(),
                    wit_package: "http".to_string(),
                    interfaces: vec!["incoming-handler".to_string()],
                    source_config: std::collections::HashMap::from([(
                        "path".to_string(),
                        "/foo".to_string(),
                    )]),
                    target_config: HashMap::new(),
                    source_secrets: None,
                    target_secrets: None,
                },
                InterfaceLinkDefinition {
                    source_id: "http-server-provider-test".to_string(),
                    target: "test-component-two".to_string(),
                    name: "default".to_string(),
                    wit_namespace: "wasi".to_string(),
                    wit_package: "http".to_string(),
                    interfaces: vec!["incoming-handler".to_string()],
                    source_config: std::collections::HashMap::from([(
                        "path".to_string(),
                        "/bar".to_string(),
                    )]),
                    target_config: HashMap::new(),
                    source_secrets: None,
                    target_secrets: None,
                },
            ],
            ..Default::default()
        };
        initialize_host_data(host_data.clone()).expect("should be able to initialize host data");

        let provider = run_provider(
            path::HttpServerProvider::new(&host_data)
                .await
                .expect("should be able to create provider"),
            "http-server-provider-test",
        )
        .await
        .expect("should be able to run provider");

        // Use a separate task to listen for the component message
        let conn = async_nats::connect(nats_address)
            .await
            .expect("should be able to connect");
        let mut subscriber_one = conn
            .subscribe("lattice.test-component-one.wrpc.>")
            .await
            .expect("should be able to subscribe");
        let mut subscriber_two = conn
            .subscribe("lattice.test-component-two.wrpc.>")
            .await
            .expect("should be able to subscribe");

        let provider_handle = tokio::spawn(provider);
        // Let the provider have a second to setup the listeners
        tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;

        // Invoke component one
        let resp = reqwest::get("http://127.0.0.1:8081/foo")
            .await
            .expect("should be able to make request");
        // Should have timed out
        assert_eq!(resp.status(), 408);
        let msg = subscriber_one
            .next()
            .await
            .expect("should be able to get a message");
        assert!(msg.subject.contains("test-component-one"));

        // Invoke component two
        let resp = reqwest::get("http://127.0.0.1:8081/bar")
            .await
            .expect("should be able to make request");
        // Should have timed out
        assert_eq!(resp.status(), 408);
        let msg = subscriber_two
            .next()
            .await
            .expect("should be able to get a message");
        assert!(msg.subject.contains("test-component-two"));

        // Invoke component two with a query parameter
        let resp = reqwest::get("http://127.0.0.1:8081/bar?someparam=foo")
            .await
            .expect("should be able to make request");
        // Should have timed out
        assert_eq!(resp.status(), 408);
        let msg = subscriber_two
            .next()
            .await
            .expect("should be able to get a message");
        assert!(msg.subject.contains("test-component-two"));

        // Unknown path should return 404
        let resp = reqwest::get("http://127.0.0.1:8081/some/other/route/idk")
            .await
            .expect("should be able to make request");
        assert_eq!(resp.status(), 404);

        // No other messages should have been received
        // (the assertion is that the operation timed out)
        assert!(
            tokio::time::timeout(tokio::time::Duration::from_secs(1), subscriber_one.next())
                .await
                .is_err(),
        );
        assert!(
            tokio::time::timeout(tokio::time::Duration::from_secs(1), subscriber_two.next())
                .await
                .is_err(),
        );

        provider_handle.abort();
        let _ = nats_container.stop().await;

        Ok(())
    }
}