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

tuftool: Warn before downloading root.json #421

Merged
merged 2 commits into from
Oct 16, 2019
Merged
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
25 changes: 22 additions & 3 deletions workspaces/tuftool/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use snafu::{OptionExt, ResultExt};
use std::fs::{File, OpenOptions};
use std::io::{self};
use std::num::NonZeroU64;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use structopt::StructOpt;
use tempdir::TempDir;
use tough::{Limits, Repository, Settings};
Expand All @@ -27,16 +27,30 @@ pub(crate) struct DownloadArgs {
#[structopt(short = "t", long = "target-url")]
target_base_url: String,

/// Allow downloading the root.json file (unsafe)
#[structopt(long)]
allow_root_download: bool,

/// Output directory of targets
indir: PathBuf,
}

fn root_warning<P: AsRef<Path>>(path: P) {
#[rustfmt::skip]
eprintln!("\
=================================================================
WARNING: Downloading root.json to {}
This is unsafe and will not establish trust, use only for testing
=================================================================",
path.as_ref().display());
}

impl DownloadArgs {
pub(crate) fn run(&self) -> Result<()> {
// use local root.json or download from repository
let root_path = if let Some(path) = &self.root {
PathBuf::from(path)
} else {
} else if self.allow_root_download {
let name = if let Some(version) = self.root_version {
format!("{}.root.json", version)
} else {
Expand All @@ -53,7 +67,9 @@ impl DownloadArgs {
.context(error::UrlParse {
url: &self.metadata_base_url,
})?;
println!("Downloading {} to {:?}", &name, &path);

root_warning(&path);

let mut f = OpenOptions::new()
.write(true)
.create(true)
Expand All @@ -64,6 +80,9 @@ impl DownloadArgs {
.copy_to(&mut f)
.context(error::ReqwestCopy)?;
path
} else {
eprintln!("No root.json available");
std::process::exit(1);
};

// load repository
Expand Down