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

added --cert flag for http client (#2301) #3972

Merged
merged 7 commits into from
Feb 17, 2020
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: 4 additions & 2 deletions cli/file_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ impl SourceFileFetcher {
cache_blacklist: Vec<String>,
no_remote: bool,
cached_only: bool,
) -> std::io::Result<Self> {
ca_file: Option<String>,
) -> Result<Self, ErrBox> {
let file_fetcher = Self {
deps_cache,
progress,
Expand All @@ -121,7 +122,7 @@ impl SourceFileFetcher {
use_disk_cache,
no_remote,
cached_only,
http_client: create_http_client(),
http_client: create_http_client(ca_file)?,
};

Ok(file_fetcher)
Expand Down Expand Up @@ -862,6 +863,7 @@ mod tests {
vec![],
false,
false,
None,
)
.expect("setup fail")
}
Expand Down
188 changes: 188 additions & 0 deletions cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub struct DenoFlags {

pub lock: Option<String>,
pub lock_write: bool,
pub ca_file: Option<String>,
}

fn join_paths(whitelist: &[PathBuf], d: &str) -> String {
Expand Down Expand Up @@ -313,6 +314,7 @@ fn fmt_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {

fn install_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
permission_args_parse(flags, matches);
ca_file_arg_parse(flags, matches);

let dir = if matches.is_present("dir") {
let install_dir = matches.value_of("dir").unwrap();
Expand Down Expand Up @@ -343,6 +345,8 @@ fn install_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
}

fn bundle_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
ca_file_arg_parse(flags, matches);

let source_file = matches.value_of("source_file").unwrap().to_string();

let out_file = if let Some(out_file) = matches.value_of("out_file") {
Expand Down Expand Up @@ -375,6 +379,7 @@ fn completions_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {

fn repl_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
v8_flags_arg_parse(flags, matches);
ca_file_arg_parse(flags, matches);
flags.subcommand = DenoSubcommand::Repl;
flags.allow_net = true;
flags.allow_env = true;
Expand All @@ -387,6 +392,7 @@ fn repl_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {

fn eval_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
v8_flags_arg_parse(flags, matches);
ca_file_arg_parse(flags, matches);
flags.allow_net = true;
flags.allow_env = true;
flags.allow_run = true;
Expand All @@ -399,6 +405,8 @@ fn eval_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
}

fn info_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
ca_file_arg_parse(flags, matches);

flags.subcommand = DenoSubcommand::Info {
file: matches.value_of("file").map(|f| f.to_string()),
};
Expand All @@ -410,6 +418,7 @@ fn fetch_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
importmap_arg_parse(flags, matches);
config_arg_parse(flags, matches);
no_remote_arg_parse(flags, matches);
ca_file_arg_parse(flags, matches);
let files = matches
.values_of("file")
.unwrap()
Expand Down Expand Up @@ -444,6 +453,7 @@ fn run_test_args_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
v8_flags_arg_parse(flags, matches);
no_remote_arg_parse(flags, matches);
permission_args_parse(flags, matches);
ca_file_arg_parse(flags, matches);

if matches.is_present("cached-only") {
flags.cached_only = true;
Expand Down Expand Up @@ -558,6 +568,7 @@ fn repl_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("repl")
.about("Read Eval Print Loop")
.arg(v8_flags_arg())
.arg(ca_file_arg())
}

fn install_subcommand<'a, 'b>() -> App<'a, 'b> {
Expand Down Expand Up @@ -586,6 +597,7 @@ fn install_subcommand<'a, 'b>() -> App<'a, 'b> {
.multiple(true)
.allow_hyphen_values(true)
)
.arg(ca_file_arg())
.about("Install script as executable")
.long_about(
"Installs a script as executable. The default installation directory is
Expand All @@ -608,6 +620,7 @@ fn bundle_subcommand<'a, 'b>() -> App<'a, 'b> {
.required(true),
)
.arg(Arg::with_name("out_file").takes_value(true).required(false))
.arg(ca_file_arg())
.about("Bundle module and dependencies into single file")
.long_about(
"Output a single JavaScript file with all dependencies.
Expand Down Expand Up @@ -642,6 +655,7 @@ Example:

fn eval_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("eval")
.arg(ca_file_arg())
.about("Eval script")
.long_about(
"Evaluate JavaScript from command-line
Expand Down Expand Up @@ -677,6 +691,7 @@ Remote modules cache: directory containing remote modules
TypeScript compiler cache: directory containing TS compiler output",
)
.arg(Arg::with_name("file").takes_value(true).required(false))
.arg(ca_file_arg())
}

fn fetch_subcommand<'a, 'b>() -> App<'a, 'b> {
Expand All @@ -693,6 +708,7 @@ fn fetch_subcommand<'a, 'b>() -> App<'a, 'b> {
.required(true)
.min_values(1),
)
.arg(ca_file_arg())
.about("Fetch the dependencies")
.long_about(
"Fetch and compile remote dependencies recursively.
Expand Down Expand Up @@ -777,6 +793,7 @@ fn run_test_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
.arg(lock_write_arg())
.arg(no_remote_arg())
.arg(v8_flags_arg())
.arg(ca_file_arg())
.arg(
Arg::with_name("cached-only")
.long("cached-only")
Expand Down Expand Up @@ -896,6 +913,17 @@ fn config_arg_parse(flags: &mut DenoFlags, matches: &ArgMatches) {
flags.config_path = matches.value_of("config").map(ToOwned::to_owned);
}

fn ca_file_arg<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name("cert")
.long("cert")
.value_name("FILE")
.help("Load certificate authority from PEM encoded file")
.takes_value(true)
}
fn ca_file_arg_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
flags.ca_file = matches.value_of("cert").map(ToOwned::to_owned);
}

fn reload_arg<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name("reload")
.short("r")
Expand Down Expand Up @@ -2045,3 +2073,163 @@ mod tests {
);
}
}

#[test]
fn run_with_cafile() {
let r = flags_from_vec_safe(svec![
"deno",
"run",
"--cert",
"example.crt",
"script.ts"
]);
assert_eq!(
r.unwrap(),
DenoFlags {
subcommand: DenoSubcommand::Run {
script: "script.ts".to_string(),
},
ca_file: Some("example.crt".to_owned()),
..DenoFlags::default()
}
);
}

#[test]
fn bundle_with_cafile() {
let r = flags_from_vec_safe(svec![
"deno",
"bundle",
"--cert",
"example.crt",
"source.ts"
]);
assert_eq!(
r.unwrap(),
DenoFlags {
subcommand: DenoSubcommand::Bundle {
source_file: "source.ts".to_string(),
out_file: None,
},
ca_file: Some("example.crt".to_owned()),
..DenoFlags::default()
}
);
}

#[test]
fn eval_with_cafile() {
let r = flags_from_vec_safe(svec![
"deno",
"eval",
"--cert",
"example.crt",
"console.log('hello world')"
]);
assert_eq!(
r.unwrap(),
DenoFlags {
subcommand: DenoSubcommand::Eval {
code: "console.log('hello world')".to_string(),
},
ca_file: Some("example.crt".to_owned()),
allow_net: true,
allow_env: true,
allow_run: true,
allow_read: true,
allow_write: true,
allow_plugin: true,
allow_hrtime: true,
..DenoFlags::default()
}
);
}

#[test]
fn fetch_with_cafile() {
let r = flags_from_vec_safe(svec![
"deno",
"fetch",
"--cert",
"example.crt",
"script.ts",
"script_two.ts"
]);
assert_eq!(
r.unwrap(),
DenoFlags {
subcommand: DenoSubcommand::Fetch {
files: svec!["script.ts", "script_two.ts"],
},
ca_file: Some("example.crt".to_owned()),
..DenoFlags::default()
}
);
}

#[test]
fn info_with_cafile() {
let r = flags_from_vec_safe(svec![
"deno",
"info",
"--cert",
"example.crt",
"https://example.com"
]);
assert_eq!(
r.unwrap(),
DenoFlags {
subcommand: DenoSubcommand::Info {
file: Some("https://example.com".to_string()),
},
ca_file: Some("example.crt".to_owned()),
..DenoFlags::default()
}
);
}

#[test]
fn install_with_cafile() {
let r = flags_from_vec_safe(svec![
"deno",
"install",
"--cert",
"example.crt",
"deno_colors",
"https://deno.land/std/examples/colors.ts"
]);
assert_eq!(
r.unwrap(),
DenoFlags {
subcommand: DenoSubcommand::Install {
dir: None,
exe_name: "deno_colors".to_string(),
module_url: "https://deno.land/std/examples/colors.ts".to_string(),
args: vec![],
force: false,
},
ca_file: Some("example.crt".to_owned()),
..DenoFlags::default()
}
);
}

#[test]
fn repl_with_cafile() {
let r = flags_from_vec_safe(svec!["deno", "repl", "--cert", "example.crt"]);
assert_eq!(
r.unwrap(),
DenoFlags {
subcommand: DenoSubcommand::Repl {},
ca_file: Some("example.crt".to_owned()),
allow_read: true,
allow_write: true,
allow_net: true,
allow_env: true,
allow_run: true,
allow_plugin: true,
allow_hrtime: true,
..DenoFlags::default()
}
);
}
1 change: 1 addition & 0 deletions cli/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl GlobalState {
flags.cache_blacklist.clone(),
flags.no_remote,
flags.cached_only,
flags.ca_file.clone(),
)?;

let ts_compiler = TsCompiler::new(
Expand Down
Loading