Skip to content

Commit 0f59327

Browse files
author
Dylan Johnson
committed
add GLEAM_CACERTS_PATH env variable
1 parent 3128e3b commit 0f59327

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

compiler-cli/src/http.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use gleam_core::{Error, Result};
66
use http::{Request, Response};
77

88
static REQWEST_CLIENT: OnceLock<reqwest::Client> = OnceLock::new();
9+
static CERTS_ENV_VAR: &str = "GLEAM_CACERTS_PATH";
910

1011
#[derive(Debug)]
1112
pub struct HttpClient;
@@ -27,7 +28,7 @@ impl gleam_core::io::HttpClient for HttpClient {
2728
.try_into()
2829
.expect("Unable to convert HTTP request for use by reqwest library");
2930
let mut response = REQWEST_CLIENT
30-
.get_or_init(reqwest::Client::new)
31+
.get_or_init(init_client)
3132
.execute(request)
3233
.await
3334
.map_err(Error::http)?;
@@ -42,3 +43,32 @@ impl gleam_core::io::HttpClient for HttpClient {
4243
.map_err(Error::http)
4344
}
4445
}
46+
47+
fn init_client() -> reqwest::Client {
48+
if let Some(cert) = get_certificate() {
49+
return reqwest::Client::builder()
50+
.add_root_certificate(cert)
51+
.build()
52+
.expect("Unable to initialize a reqwest HTTP client");
53+
} else {
54+
return reqwest::Client::new();
55+
}
56+
}
57+
58+
fn get_certificate() -> Option<reqwest::Certificate> {
59+
match std::env::var(CERTS_ENV_VAR) {
60+
Ok(certs_path) => {
61+
let data = std::fs::read(certs_path).expect(&format!(
62+
"Unable to read certs file set as `{}`",
63+
CERTS_ENV_VAR
64+
));
65+
let cert = reqwest::Certificate::from_pem(&data).expect(&format!(
66+
"Unable to construct a certificate from certs file set as `{}`",
67+
CERTS_ENV_VAR
68+
));
69+
70+
Some(cert)
71+
}
72+
_ => None,
73+
}
74+
}

0 commit comments

Comments
 (0)