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 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all 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)
}
Comment on lines +57 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the event that the certs paths has been set we want to error if the certs cannot be read or parsed, rather than silently discarded as they are here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is usual behaviour.. It's quite common for runtimes like Python and Node to ignore missing certs as it is common behaviour to add the environment variable to your shell and not populate the certificate path until necessary.