Skip to content

Commit

Permalink
RootCertStore is optional, format and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
justinmchase committed Jul 26, 2021
1 parent 9312a6b commit bb30583
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 67 deletions.
18 changes: 7 additions & 11 deletions cli/file_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl FileFetcher {
http_cache: HttpCache,
cache_setting: CacheSetting,
allow_remote: bool,
root_store: RootCertStore,
root_store: Option<RootCertStore>,
blob_store: BlobStore,
) -> Result<Self, AnyError> {
Ok(Self {
Expand All @@ -230,7 +230,12 @@ impl FileFetcher {
cache: Default::default(),
cache_setting,
http_cache,
http_client: create_http_client(get_user_agent(), root_store, None, None)?,
http_client: create_http_client(
get_user_agent(),
root_store,
None,
None,
)?,
blob_store,
})
}
Expand Down Expand Up @@ -612,7 +617,6 @@ mod tests {
cache_setting,
true,
None,
None,
blob_store.clone(),
)
.expect("setup failed");
Expand Down Expand Up @@ -1058,7 +1062,6 @@ mod tests {
CacheSetting::ReloadAll,
true,
None,
None,
BlobStore::default(),
)
.expect("setup failed");
Expand Down Expand Up @@ -1086,7 +1089,6 @@ mod tests {
CacheSetting::Use,
true,
None,
None,
BlobStore::default(),
)
.expect("could not create file fetcher");
Expand Down Expand Up @@ -1115,7 +1117,6 @@ mod tests {
CacheSetting::Use,
true,
None,
None,
BlobStore::default(),
)
.expect("could not create file fetcher");
Expand Down Expand Up @@ -1277,7 +1278,6 @@ mod tests {
CacheSetting::Use,
true,
None,
None,
BlobStore::default(),
)
.expect("could not create file fetcher");
Expand Down Expand Up @@ -1309,7 +1309,6 @@ mod tests {
CacheSetting::Use,
true,
None,
None,
BlobStore::default(),
)
.expect("could not create file fetcher");
Expand Down Expand Up @@ -1420,7 +1419,6 @@ mod tests {
CacheSetting::Use,
false,
None,
None,
BlobStore::default(),
)
.expect("could not create file fetcher");
Expand Down Expand Up @@ -1448,7 +1446,6 @@ mod tests {
CacheSetting::Only,
true,
None,
None,
BlobStore::default(),
)
.expect("could not create file fetcher");
Expand All @@ -1457,7 +1454,6 @@ mod tests {
CacheSetting::Use,
true,
None,
None,
BlobStore::default(),
)
.expect("could not create file fetcher");
Expand Down
7 changes: 6 additions & 1 deletion cli/http_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,11 @@ pub async fn fetch_once(
mod tests {
use super::*;
use crate::version;
use deno_core::http::create_http_client;
use std::fs::read;

fn create_test_client(ca_data: Option<Vec<u8>>) -> Client {
create_http_client("test_client".to_string(), None, ca_data).unwrap()
create_http_client("test_client".to_string(), None, ca_data, None).unwrap()
}

#[tokio::test]
Expand Down Expand Up @@ -344,6 +345,7 @@ mod tests {
)
.unwrap(),
),
None,
)
.unwrap();
let result = fetch_once(FetchOnceArgs {
Expand Down Expand Up @@ -383,6 +385,7 @@ mod tests {
)
.unwrap(),
),
None,
)
.unwrap();
let result = fetch_once(FetchOnceArgs {
Expand Down Expand Up @@ -421,6 +424,7 @@ mod tests {
)
.unwrap(),
),
None,
)
.unwrap();
let result = fetch_once(FetchOnceArgs {
Expand Down Expand Up @@ -473,6 +477,7 @@ mod tests {
)
.unwrap(),
),
None,
)
.unwrap();
let result = fetch_once(FetchOnceArgs {
Expand Down
5 changes: 2 additions & 3 deletions cli/lsp/registries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use crate::http_cache::HttpCache;
use deno_core::error::anyhow;
use deno_core::error::AnyError;
use deno_core::error::Context;
use deno_core::http::create_default_root_store;
use deno_core::resolve_url;
use deno_core::serde::Deserialize;
use deno_core::serde_json;
Expand Down Expand Up @@ -264,7 +263,7 @@ impl Default for ModuleRegistry {
http_cache,
cache_setting,
true,
create_default_root_store(),
None,
BlobStore::default(),
)
.unwrap();
Expand All @@ -283,7 +282,7 @@ impl ModuleRegistry {
http_cache,
CacheSetting::Use,
true,
create_default_root_store(),
None,
BlobStore::default(),
)
.context("Error creating file fetcher in module registry.")
Expand Down
16 changes: 9 additions & 7 deletions cli/program_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ use std::collections::HashMap;
use std::collections::HashSet;
use std::env;
use std::fs::File;
use std::sync::Arc;
use std::io::BufReader;
use std::sync::Arc;

/// This structure represents state of single "deno" program.
///
Expand All @@ -56,7 +56,7 @@ pub struct ProgramState {
pub maybe_config_file: Option<ConfigFile>,
pub maybe_import_map: Option<ImportMap>,
pub maybe_inspector_server: Option<Arc<InspectorServer>>,
pub root_store: RootCertStore,
pub root_store: Option<RootCertStore>,
pub blob_store: BlobStore,
pub broadcast_channel: InMemoryBroadcastChannel,
pub shared_array_buffer_store: SharedArrayBufferStore,
Expand Down Expand Up @@ -98,10 +98,12 @@ impl ProgramState {
}
"system" => {
println!("Adding native certs");
let roots = load_native_certs().expect("could not load platform certs").roots;
let roots = load_native_certs()
.expect("could not load platform certs")
.roots;
root_store.roots.extend(roots);
},
_ => ()
}
_ => (),
}
}

Expand Down Expand Up @@ -131,7 +133,7 @@ impl ProgramState {
http_cache,
cache_usage,
!flags.no_remote,
root_store.clone(),
Some(root_store.clone()),
blob_store.clone(),
)?;

Expand Down Expand Up @@ -190,7 +192,7 @@ impl ProgramState {
maybe_config_file,
maybe_import_map,
maybe_inspector_server,
root_store: root_store.clone(),
root_store: Some(root_store.clone()),
blob_store,
broadcast_channel,
shared_array_buffer_store,
Expand Down
1 change: 0 additions & 1 deletion cli/specifier_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,6 @@ pub mod tests {
CacheSetting::Use,
true,
None,
None,
BlobStore::default(),
)
.expect("could not setup");
Expand Down
11 changes: 6 additions & 5 deletions cli/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use deno_core::error::uri_error;
use deno_core::error::AnyError;
use deno_core::error::Context;
use deno_core::futures::FutureExt;
use deno_core::http::create_default_root_store;
use deno_core::located_script_name;
use deno_core::resolve_url;
use deno_core::serde::Deserialize;
Expand Down Expand Up @@ -233,13 +234,13 @@ pub async fn run(

let mut root_store = program_state
.root_store
.clone();
.clone()
.or_else(|| Some(create_default_root_store()))
.unwrap();

if let Some(cert) = metadata.ca_data {
let reader = &mut BufReader::new(Cursor::new(cert));
root_store
.add_pem_file(reader)
.unwrap();
root_store.add_pem_file(reader).unwrap();
}

let options = WorkerOptions {
Expand All @@ -248,7 +249,7 @@ pub async fn run(
debug_flag: metadata.log_level.map_or(false, |l| l == log::Level::Debug),
user_agent: version::get_user_agent(),
unstable: metadata.unstable,
root_store,
root_store: Some(root_store),
seed: metadata.seed,
js_error_create_fn: None,
create_web_worker_cb,
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/integration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

use crate::itest;
use deno_core::rustls;
use deno_core::url;
use deno_runtime::deno_net::ops_tls::rustls;
use deno_runtime::deno_net::ops_tls::webpki;
use deno_core::webpki;
use deno_runtime::deno_net::ops_tls::TlsStream;
use std::fs;
use std::io::BufReader;
Expand Down
27 changes: 13 additions & 14 deletions core/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ pub use reqwest;
pub use rustls;
pub use webpki;

use crate::error::generic_error;
use crate::error::AnyError;
use crate::parking_lot::Mutex;
use crate::error::generic_error;

use reqwest::Client;
use rustls::ClientConfig;
use rustls::StoresClientSessions;
use rustls::RootCertStore;
use rustls::StoresClientSessions;
// use rustls_native_certs::load_native_certs;
use serde::Deserialize;
use std::sync::Arc;
use std::collections::HashMap;
use reqwest::header::HeaderMap;
use reqwest::header::USER_AGENT;
use reqwest::redirect::Policy;
use serde::Deserialize;
use std::collections::HashMap;
use std::io::BufReader;
use std::io::Cursor;
use std::sync::Arc;

#[derive(Deserialize, Default, Debug, Clone)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -64,34 +64,33 @@ pub fn create_default_root_store() -> RootCertStore {
let mut root_store = RootCertStore::empty();
// todo: Consider also loading the system keychain here
root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
return root_store;
root_store
}

pub fn create_client_config(
root_store: RootCertStore,
root_store: Option<RootCertStore>,
ca_data: Option<Vec<u8>>,
) -> Result<ClientConfig, AnyError> {
let mut tls_config = ClientConfig::new();
tls_config.set_persistence(CLIENT_SESSION_MEMORY_CACHE.clone());
tls_config.root_store = root_store;
tls_config.root_store = root_store
.or_else(|| Some(create_default_root_store()))
.unwrap();

// If a custom cert is specified, add it to the store
if let Some(cert) = ca_data {
let reader = &mut BufReader::new(Cursor::new(cert));
tls_config
.root_store
.add_pem_file(reader)
.unwrap();
tls_config.root_store.add_pem_file(reader).unwrap();
}

return Ok(tls_config);
Ok(tls_config)
}

/// Create new instance of async reqwest::Client. This client supports
/// proxies and doesn't follow redirects.
pub fn create_http_client(
user_agent: String,
root_store: RootCertStore,
root_store: Option<RootCertStore>,
ca_data: Option<Vec<u8>>,
proxy: Option<Proxy>,
) -> Result<Client, AnyError> {
Expand Down
4 changes: 2 additions & 2 deletions core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ mod runtime;
// Re-exports
pub use futures;
pub use parking_lot;
pub use rustls;
pub use rusty_v8 as v8;
pub use serde;
pub use serde_json;
pub use serde_v8;
pub use serde_v8::Buffer as ZeroCopyBuf;
pub use serde_v8::ByteString;
pub use url;
pub use rustls;
pub use webpki;

pub use crate::async_cancel::CancelFuture;
Expand All @@ -44,8 +44,8 @@ pub use crate::async_cell::AsyncRefFuture;
pub use crate::async_cell::RcLike;
pub use crate::async_cell::RcRef;
pub use crate::flags::v8_set_flags;
pub use crate::http::create_http_client;
pub use crate::http::create_client_config;
pub use crate::http::create_http_client;
pub use crate::inspector::InspectorSessionProxy;
pub use crate::inspector::JsRuntimeInspector;
pub use crate::inspector::LocalInspectorSession;
Expand Down
4 changes: 2 additions & 2 deletions extensions/fetch/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub use reqwest; // Re-export reqwest

pub fn init<P: FetchPermissions + 'static>(
user_agent: String,
root_store: RootCertStore,
root_store: Option<RootCertStore>,
proxy: Option<Proxy>,
) -> Extension {
Extension::builder()
Expand Down Expand Up @@ -101,7 +101,7 @@ pub fn init<P: FetchPermissions + 'static>(

pub struct HttpClientDefaults {
pub user_agent: String,
pub root_store: RootCertStore,
pub root_store: Option<RootCertStore>,
pub proxy: Option<Proxy>,
}

Expand Down
Loading

0 comments on commit bb30583

Please sign in to comment.