Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add GLEAM_CACERTS_PATH env variable #3939

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions compiler-cli/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use std::sync::OnceLock;
use async_trait::async_trait;
use gleam_core::{Error, Result};
use http::{Request, Response};
use reqwest::{Certificate, Client};

static REQWEST_CLIENT: OnceLock<reqwest::Client> = OnceLock::new();
static REQWEST_CLIENT: OnceLock<Client> = OnceLock::new();

#[derive(Debug)]
pub struct HttpClient;
Expand All @@ -27,7 +28,7 @@ impl gleam_core::io::HttpClient for HttpClient {
.try_into()
.expect("Unable to convert HTTP request for use by reqwest library");
let mut response = REQWEST_CLIENT
.get_or_init(reqwest::Client::new)
.get_or_init(init_client)
.execute(request)
.await
.map_err(Error::http)?;
Expand All @@ -42,3 +43,21 @@ impl gleam_core::io::HttpClient for HttpClient {
.map_err(Error::http)
}
}

fn init_client() -> Client {
match get_certificate() {
Ok(cert) => Client::builder()
.add_root_certificate(cert)
.build()
.expect("Unable to build reqwest client with certificate"),
_ => Client::new(),
}
}

fn get_certificate() -> Result<Certificate, Box<dyn std::error::Error>> {
let certificate_path = std::env::var("GLEAM_CACERTS_PATH")?;
let certificate_bytes = std::fs::read(&certificate_path)?;
let certificate = Certificate::from_pem(&certificate_bytes)?;

Ok(certificate)
}
winstxnhdw marked this conversation as resolved.
Show resolved Hide resolved