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
Show file tree
Hide file tree
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
24 changes: 22 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().expect("Unable to create reqwest client"))
.execute(request)
.await
.map_err(Error::http)?;
Expand All @@ -42,3 +43,22 @@ impl gleam_core::io::HttpClient for HttpClient {
.map_err(Error::http)
}
}

fn init_client() -> Result<Client, Error> {
let certificate_path = std::env::var("GLEAM_CACERTS_PATH")
.map_err(|_| Error::CannotReadCertificate { path: "".into() })?;
let certificate_bytes =
std::fs::read(&certificate_path).map_err(|_| Error::CannotReadCertificate {
path: certificate_path.clone(),
})?;
let certificate =
Certificate::from_pem(&certificate_bytes).map_err(|_| Error::CannotReadCertificate {
path: certificate_path.clone(),
})?;
Client::builder()
.add_root_certificate(certificate)
.build()
.map_err(|_| Error::CannotReadCertificate {
path: certificate_path,
})
}
14 changes: 14 additions & 0 deletions compiler-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ file_names.iter().map(|x| x.as_str()).join(", "))]
wrongfully_allowed_version: SmallVersion,
},

#[error("The certificate at {path} could not be read")]
CannotReadCertificate { path: String },

#[error("Failed to encrypt data")]
FailedToEncrypt { detail: String },

Expand Down Expand Up @@ -1398,6 +1401,17 @@ https://learn.microsoft.com/en-us/windows/apps/get-started/enable-your-device-fo
}]
}

Error::CannotReadCertificate { path } => {
let text = wrap_format!("An error occurred while trying to read the certificate file at: {path}");

vec![Diagnostic {
title: "Failed to read certificate".into(),
text,
hint: None,
level: Level::Error,
location: None,
}]
}

Error::FailedToEncrypt { detail } => {
let text = wrap_format!("A problem was encountered encrypting data.
Expand Down