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

pubsys-setup: allow file URLs for role #1194

Merged
merged 1 commit into from
Nov 2, 2020
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
30 changes: 21 additions & 9 deletions tools/pubsys-setup/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn run() -> Result<()> {

Url::from_file_path(&args.default_key_path)
.ok()
.context(error::FileUrl {
.context(error::FileToUrl {
path: args.default_key_path,
})?
};
Expand Down Expand Up @@ -222,10 +222,19 @@ fn find_root_role_and_key(args: &Args) -> Result<(Option<&PathBuf>, Option<Url>)
);
} else {
// Download the root role by URL and verify its checksum before writing it.
let root_role_data = reqwest::blocking::get(url.clone())
.with_context(|| error::GetUrl { url: url.clone() })?
.text()
.with_context(|| error::GetUrl { url: url.clone() })?;
let root_role_data = if url.scheme() == "file" {
// reqwest won't fetch a file URL, so just read the file.
let path = url
.to_file_path()
.ok()
.with_context(|| error::UrlToFile { url: url.clone() })?;
fs::read_to_string(&path).context(error::ReadFile { path: &path })?
} else {
reqwest::blocking::get(url.clone())
.with_context(|| error::GetUrl { url: url.clone() })?
.text()
.with_context(|| error::GetUrl { url: url.clone() })?
};

let mut d = Sha512::new();
d.update(&root_role_data);
Expand Down Expand Up @@ -281,7 +290,7 @@ fn find_root_role_and_key(args: &Args) -> Result<(Option<&PathBuf>, Option<Url>)
}
if key_url.is_none() && args.default_key_path.exists() {
key_url = Some(Url::from_file_path(&args.default_key_path).ok().context(
error::FileUrl {
error::FileToUrl {
path: &args.default_key_path,
},
)?);
Expand Down Expand Up @@ -319,7 +328,7 @@ mod error {
Config { source: pubsys_config::Error },

#[snafu(display("Path not valid as a URL: {}", path.display()))]
FileUrl { path: PathBuf },
FileToUrl { path: PathBuf },

#[snafu(display("Failed to fetch URL '{}': {}", url, source))]
GetUrl { url: Url, source: reqwest::Error },
Expand Down Expand Up @@ -354,6 +363,9 @@ mod error {
#[snafu(display("Failed to set permissions on {}: {}", path.display(), source))]
SetMode { path: PathBuf, source: io::Error },

#[snafu(display("Unable to build URL from signing key for repo '{}'", repo))]
SigningKeyUrl { repo: String },

#[snafu(display("Failed to create temp file for {}: {}", purpose, source))]
TempFileCreate { purpose: String, source: io::Error },

Expand All @@ -369,8 +381,8 @@ mod error {
#[snafu(display("Failed to start tuftool: {}", source))]
TuftoolSpawn { source: io::Error },

#[snafu(display("Unable to build URL from signing key for repo '{}'", repo))]
SigningKeyUrl { repo: String },
#[snafu(display("URL not valid as a path: {}", url))]
UrlToFile { url: Url },

#[snafu(display("Failed to write '{}': {}", path.display(), source))]
WriteFile { path: PathBuf, source: io::Error },
Expand Down