Skip to content

Commit

Permalink
Merge pull request #421 from amazonlinux/tuftool
Browse files Browse the repository at this point in the history
tuftool: Warn before downloading root.json
  • Loading branch information
sam-aws committed Oct 16, 2019
2 parents 285e64c + a5497b3 commit df4267c
Showing 1 changed file with 22 additions and 3 deletions.
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

0 comments on commit df4267c

Please sign in to comment.