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 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
6 changes: 2 additions & 4 deletions crates/taplo-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ 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 =
taplo_common::util::get_reqwest_client(std::time::Duration::from_secs(5)).unwrap();

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

#[cfg(not(target_arch = "wasm32"))]
#[tracing::instrument]
pub fn get_reqwest_client(timeout: std::time::Duration) -> Result<reqwest::Client, reqwest::Error> {
fn get_cert(path: impl AsRef<Path>) -> Result<reqwest::Certificate, anyhow::Error> {
let path = path.as_ref();
let is_der = path.extension().map_or(false, |ext| ext == "der");
let buf = std::fs::read(path)?;
tracing::info!(
"Found a custom CA {}. Reading the CA...",
path.to_string_lossy()
);
if is_der {
Ok(reqwest::Certificate::from_der(&buf)?)
} else {
Ok(reqwest::Certificate::from_pem(&buf)?)
}
}
let mut builder = reqwest::Client::builder().timeout(timeout);
if let Some(path) = std::env::var_os("TAPLO_EXTRA_CA_CERTS") {
match get_cert(&path) {
Ok(cert) => {
builder = builder.add_root_certificate(cert);
tracing::info!(?path, "Added the custom CA");
}
Err(err) => {
tracing::error!(error = %err, "Could not parse the custom CA");
}
}
}
ia0 marked this conversation as resolved.
Show resolved Hide resolved
builder.build()
}
5 changes: 1 addition & 4 deletions crates/taplo-lsp/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ impl<E: Environment> WorkspaceState<E> {

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

Self {
Expand Down
2 changes: 2 additions & 0 deletions site/site/configuration/using-schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ JSON schemas can be assigned to TOML documents according to the following in pri
1. default schema set in the [configuration file](./file#schema)
1. contributed by an [extension](./developing-schemas.md#visual-studio-code-extensions) *(Visual Studio Code only)*
1. an association based on a [schema catalog](./developing-schemas.md#publishing)

Extra root CA certificate could be added by specifying with the TAPLO_EXTRA_CA_CERTS environment. The provided paths must be absolute paths. For example, `TAPLO_EXTRA_CA_CERTS=/home/taplo-user/custom-ca.pem`
Loading