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

Support adding custom CA #454

Merged
merged 4 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 7 additions & 5 deletions crates/taplo-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ use std::{
path::{Path, PathBuf},
sync::Arc,
};
use taplo_common::{config::Config, environment::Environment, schema::Schemas, util::Normalize};
use taplo_common::{
config::Config,
environment::Environment,
schema::Schemas,
util::{get_reqwest_client, Normalize},
};

pub mod args;
pub mod commands;
Expand All @@ -21,10 +26,7 @@ pub struct Taplo<E: Environment> {
impl<E: Environment> Taplo<E> {
pub fn new(env: E) -> Self {
#[cfg(not(target_arch = "wasm32"))]
let http = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(5))
.build()
.unwrap();
let http = get_reqwest_client(std::time::Duration::from_secs(5)).unwrap();

#[cfg(target_arch = "wasm32")]
let http = reqwest::Client::default();
Expand Down
24 changes: 24 additions & 0 deletions crates/taplo-common/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,27 @@ pub(crate) fn normalize_str(s: &str) -> Cow<str> {
percent_decoded
}
}

#[tracing::instrument]
pub fn get_reqwest_client(timeout: std::time::Duration) -> Result<reqwest::Client, reqwest::Error> {
fn get_cert() -> Result<reqwest::Certificate, anyhow::Error> {
let path = std::env::var("TAPLO_EXTRA_CA_CERTS")?;
let path = Path::new(&path);
ia0 marked this conversation as resolved.
Show resolved Hide resolved
let ext = path.extension().and_then(|ext| ext.to_str());
ia0 marked this conversation as resolved.
Show resolved Hide resolved
let buf = std::fs::read(path)?;
tracing::info!(
"Found a custom CA {}. Reading the CA...",
path.to_string_lossy()
);
match ext {
Some("der") => Ok(reqwest::Certificate::from_der(&buf)?),
_ => Ok(reqwest::Certificate::from_pem(&buf)?),
}
}
let mut builder = reqwest::Client::builder().timeout(timeout);
if let Ok(cert) = get_cert() {
builder = builder.add_root_certificate(cert);
tracing::info!("Added the custom CA");
}
ia0 marked this conversation as resolved.
Show resolved Hide resolved
builder.build()
}
6 changes: 2 additions & 4 deletions crates/taplo-lsp/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use taplo_common::{
associations::{priority, source, AssociationRule, SchemaAssociation},
Schemas,
},
util::get_reqwest_client,
AsyncRwLock, HashMap, IndexMap,
};

Expand Down Expand Up @@ -124,10 +125,7 @@ impl<E: Environment> WorkspaceState<E> {

#[cfg(not(target_arch = "wasm32"))]
{
client = reqwest::Client::builder()
.timeout(Duration::from_secs(10))
.build()
.unwrap();
client = get_reqwest_client(Duration::from_secs(10)).unwrap();
}

Self {
Expand Down
Loading