Skip to content

Commit 19e4dff

Browse files
committed
address review comments
Signed-off-by: Rajat Jindal <[email protected]>
1 parent 419848d commit 19e4dff

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed

crates/trigger-http/src/lib.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -738,8 +738,7 @@ impl OutboundWasiHttpHandler for HttpRuntimeData {
738738
// Once Wasmtime gives us the ability to do the spawn ourselves we can just call .instrument
739739
// and won't have to do this workaround.
740740
let response_handle = async move {
741-
let res =
742-
default_send_request_handler(request, config, client_tls_opts).await;
741+
let res = send_request_handler(request, config, client_tls_opts).await;
743742
if let Ok(res) = &res {
744743
tracing::Span::current()
745744
.record("http.response.status_code", res.resp.status().as_u16());
@@ -1008,30 +1007,10 @@ mod tests {
10081007
}
10091008
}
10101009

1011-
/// Translate a [`hyper::Error`] to a wasi-http `ErrorCode` in the context of a request.
1012-
pub fn hyper_request_error(err: hyper::Error) -> ErrorCode {
1013-
// If there's a source, we might be able to extract a wasi-http error from it.
1014-
if let Some(cause) = err.source() {
1015-
if let Some(err) = cause.downcast_ref::<ErrorCode>() {
1016-
return err.clone();
1017-
}
1018-
}
1019-
1020-
tracing::warn!("hyper request error: {err:?}");
1021-
1022-
ErrorCode::HttpProtocolError
1023-
}
1024-
1025-
pub fn dns_error(rcode: String, info_code: u16) -> ErrorCode {
1026-
ErrorCode::DnsError(wasmtime_wasi_http::bindings::http::types::DnsErrorPayload {
1027-
rcode: Some(rcode),
1028-
info_code: Some(info_code),
1029-
})
1030-
}
1031-
1032-
/// This is a fork of wasmtime_wasi_http::default_send_request_handler function.
1010+
/// This is a fork of wasmtime_wasi_http::default_send_request_handler function
1011+
/// forked from bytecodealliance/wasmtime commit-sha 29a76b68200fcfa69c8fb18ce6c850754279a05b
10331012
/// This fork provides the ability to configure client cert auth for mTLS
1034-
pub async fn default_send_request_handler(
1013+
pub async fn send_request_handler(
10351014
mut request: hyper::Request<HyperOutgoingBody>,
10361015
wasmtime_wasi_http::types::OutgoingRequestConfig {
10371016
use_tls,
@@ -1216,3 +1195,24 @@ fn get_client_tls_config_for_authority(
12161195
.with_no_client_auth(),
12171196
}
12181197
}
1198+
1199+
/// Translate a [`hyper::Error`] to a wasi-http `ErrorCode` in the context of a request.
1200+
pub fn hyper_request_error(err: hyper::Error) -> ErrorCode {
1201+
// If there's a source, we might be able to extract a wasi-http error from it.
1202+
if let Some(cause) = err.source() {
1203+
if let Some(err) = cause.downcast_ref::<ErrorCode>() {
1204+
return err.clone();
1205+
}
1206+
}
1207+
1208+
tracing::warn!("hyper request error: {err:?}");
1209+
1210+
ErrorCode::HttpProtocolError
1211+
}
1212+
1213+
pub fn dns_error(rcode: String, info_code: u16) -> ErrorCode {
1214+
ErrorCode::DnsError(wasmtime_wasi_http::bindings::http::types::DnsErrorPayload {
1215+
rcode: Some(rcode),
1216+
info_code: Some(info_code),
1217+
})
1218+
}

crates/trigger-http/src/tls.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl TlsConfig {
1919
// Creates a TLS acceptor from server config.
2020
pub(super) fn server_config(&self) -> anyhow::Result<TlsAcceptor> {
2121
let certs = load_certs(&self.cert_path)?;
22-
let private_key = load_keys(&self.key_path)?;
22+
let private_key = load_key(&self.key_path)?;
2323

2424
let cfg = rustls::ServerConfig::builder()
2525
.with_no_client_auth()
@@ -47,7 +47,7 @@ pub fn load_certs(
4747
}
4848

4949
// load_keys parse and return the first private key from the provided file
50-
pub fn load_keys(path: impl AsRef<Path>) -> io::Result<rustls_pki_types::PrivateKeyDer<'static>> {
50+
pub fn load_key(path: impl AsRef<Path>) -> io::Result<rustls_pki_types::PrivateKeyDer<'static>> {
5151
private_key(&mut io::BufReader::new(fs::File::open(path).map_err(
5252
|err| {
5353
io::Error::new(
@@ -110,7 +110,7 @@ mod tests {
110110
let mut path = testdatadir();
111111
path.push("non-existing-file.pem");
112112

113-
let keys = load_keys(path);
113+
let keys = load_key(path);
114114
assert!(keys.is_err());
115115
assert_eq!(keys.err().unwrap().to_string(), "failed to read private key file Os { code: 2, kind: NotFound, message: \"No such file or directory\" }");
116116
}
@@ -120,7 +120,7 @@ mod tests {
120120
let mut path = testdatadir();
121121
path.push("invalid-private-key.pem");
122122

123-
let keys = load_keys(path);
123+
let keys = load_key(path);
124124
assert!(keys.is_err());
125125
assert_eq!(keys.err().unwrap().to_string(), "invalid private key");
126126
}
@@ -130,7 +130,7 @@ mod tests {
130130
let mut path = testdatadir();
131131
path.push("valid-private-key.pem");
132132

133-
let keys = load_keys(path);
133+
let keys = load_key(path);
134134
assert!(keys.is_ok());
135135
}
136136
}

crates/trigger/src/runtime_config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use spin_sqlite::Connection;
1919
use crate::TriggerHooks;
2020

2121
use self::{
22-
client_tls::{load_certs, load_keys, ClientTlsOpts},
22+
client_tls::{load_certs, load_key, ClientTlsOpts},
2323
key_value::{KeyValueStore, KeyValueStoreOpts},
2424
llm::LlmComputeOpts,
2525
sqlite::SqliteDatabaseOpts,
@@ -555,18 +555,18 @@ pub struct ParsedClientTlsOpts {
555555

556556
fn parse_client_tls_opts(inp: &ClientTlsOpts) -> Result<ParsedClientTlsOpts, anyhow::Error> {
557557
let custom_root_ca = match &inp.custom_root_ca_file {
558-
Some(path) => Some(load_certs(&path).context("loading custom root ca")?),
558+
Some(path) => Some(load_certs(path).context("loading custom root ca")?),
559559
None => None,
560560
};
561561

562562
let cert_chain = match &inp.cert_chain_file {
563-
Some(file) => Some(load_certs(&file).context("loading client tls certs")?),
563+
Some(file) => Some(load_certs(file).context("loading client tls certs")?),
564564
None => None,
565565
};
566566

567567
let private_key = match &inp.private_key_file {
568568
Some(file) => {
569-
let privatekey = load_keys(&file).context("loading private key")?;
569+
let privatekey = load_key(file).context("loading private key")?;
570570
Some(Arc::from(privatekey))
571571
}
572572
None => None,

crates/trigger/src/runtime_config/client_tls.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ use anyhow::Context;
22
use rustls_pemfile::private_key;
33
use std::io;
44
use std::io::Cursor;
5-
use std::{fs, path::Path};
5+
use std::{fs, path::{Path, PathBuf}};
66

77
#[derive(Debug, serde::Deserialize)]
88
#[serde(rename_all = "snake_case", tag = "type")]
99
pub struct ClientTlsOpts {
1010
pub component_ids: Vec<String>,
1111
pub hosts: Vec<String>,
12-
pub custom_root_ca_file: Option<String>,
13-
pub cert_chain_file: Option<String>,
14-
pub private_key_file: Option<String>,
12+
pub custom_root_ca_file: Option<PathBuf>,
13+
pub cert_chain_file: Option<PathBuf>,
14+
pub private_key_file: Option<PathBuf>,
1515
}
1616

1717
// load_certs parse and return the certs from the provided file
@@ -28,7 +28,7 @@ pub fn load_certs(
2828
}
2929

3030
// load_keys parse and return the first private key from the provided file
31-
pub fn load_keys(
31+
pub fn load_key(
3232
path: impl AsRef<Path>,
3333
) -> anyhow::Result<rustls_pki_types::PrivateKeyDer<'static>> {
3434
private_key(&mut io::BufReader::new(

0 commit comments

Comments
 (0)