Fix clippy security errors

This commit is contained in:
Anthony Oteri
2023-09-21 14:28:15 -04:00
parent 0a6339e054
commit 3156a9f99e
5 changed files with 33 additions and 38 deletions
+15 -15
View File
@@ -24,7 +24,7 @@ use crate::error::ApiError;
///
/// The Docker Registry API specifies that when making a GET request, the
/// response will be paginated using a Link response header for the Next URI.
/// The URL will be encoded using RFC5988. [https://tools.ietf.org/html/rfc5988]
/// The URL will be encoded using [RFC5988](https://tools.ietf.org/html/rfc5988)
///
/// This function will continuously request the "Next" link as long as it is
/// returned, collecting and returning the deserialized response bodies as a
@@ -43,17 +43,17 @@ pub async fn fetch_all<T: for<'de> Deserialize<'de>>(
log::trace!("fetch_all({path:?})");
let mut responses: Vec<T> = Vec::default();
let mut uri = String::from(path);
let mut path = String::from(path);
loop {
log::debug!("GET {uri:?}");
let url = config.registry_url.join(&uri)?;
log::debug!("GET {path:?}");
let url = config.registry_url.join(&path)?;
let resp = reqwest::get(url).await?;
let headers = resp.headers().to_owned();
let headers = resp.headers().clone();
responses.push(resp.json().await?);
if let Some(path) = parse_rfc5988(headers.get(http::header::LINK))? {
uri = path;
if let Some(p) = parse_rfc5988(headers.get(http::header::LINK))? {
path = p;
} else {
break;
}
@@ -64,8 +64,8 @@ pub async fn fetch_all<T: for<'de> Deserialize<'de>>(
/// Given an optional header value possibly containing an RFC5988 formatted
/// URL, parse said URL into a `String`.
///
/// If the header_value does not contain a correctly formatted RFC5988 URL,
/// or if the header_value is not properly formatted containing a URL
/// If the `header_value` does not contain a correctly formatted RFC5988 URL,
/// or if the `header_value` is not properly formatted containing a URL
/// surrounded by angle brackets, separated from the link relation by a ';'
/// character, the `None` variant will be returned.
///
@@ -128,10 +128,10 @@ pub fn parse_response_status(response: &reqwest::Response) -> Result<(), ApiErro
http::StatusCode::OK => {
let headers = response.headers();
if let Some(header_value) = headers.get("Docker-Distribution-API-Version") {
if header_value.to_str()? != "registry/2.0" {
Err(ApiError::UnsupportedVersion(header_value.to_str()?.into()))
} else {
if header_value.to_str()? == "registry/2.0" {
Ok(())
} else {
Err(ApiError::UnsupportedVersion(header_value.to_str()?.into()))
}
} else {
Err(ApiError::UnexpectedResponse(
@@ -142,10 +142,10 @@ pub fn parse_response_status(response: &reqwest::Response) -> Result<(), ApiErro
http::StatusCode::UNAUTHORIZED => {
let headers = response.headers();
if let Some(header_value) = headers.get("Docker-Distribution-API-Version") {
if header_value.to_str()? != "registry/2.0" {
Err(ApiError::UnsupportedVersion(header_value.to_str()?.into()))
} else {
if header_value.to_str()? == "registry/2.0" {
Err(ApiError::AuthorizationFailed)
} else {
Err(ApiError::UnsupportedVersion(header_value.to_str()?.into()))
}
} else {
Err(ApiError::UnexpectedResponse(
+4 -2
View File
@@ -88,9 +88,10 @@ pub async fn tags_handler(config: &Config, name: &str) -> Result<(), ApiError> {
///
/// Returns an `ApiError` if there is a problem fetching the manifest or if there
/// is a problem parsing the response from the Docker Registry API.
#[allow(clippy::unused_async)]
pub async fn show_handler(config: &Config, image: &str, tag: &str) -> Result<(), ApiError> {
log::trace!("show_handler(image: {image}, tag: {tag})");
let base = config.registry_url.to_owned();
let base = config.registry_url.clone();
let path = format!("/v2/{image}/manifests/{tag}");
let _url = base.join(&path)?;
Ok(())
@@ -103,6 +104,7 @@ pub async fn show_handler(config: &Config, image: &str, tag: &str) -> Result<(),
/// Returns and `ApiError` if there is a problem converting the given tag to a
/// manifest digest, or if there is a problem deleting the manifest from the
/// Docker Registry API.
#[allow(clippy::unused_async)]
pub async fn delete_handler(_config: &Config, image: &str, tag: &str) -> Result<(), ApiError> {
log::trace!("delete_handler(image: {image}, tag: {tag})");
todo!()
@@ -119,7 +121,7 @@ pub async fn delete_handler(_config: &Config, image: &str, tag: &str) -> Result<
pub async fn check_handler(config: &Config) -> Result<(), ApiError> {
log::trace!("check_handler()");
let base = config.registry_url.to_owned();
let base = config.registry_url.clone();
let path = "/v2";
let url = base.join(path)?;
+1 -1
View File
@@ -15,7 +15,7 @@
*/
#![allow(clippy::enum_variant_names)]
#![allow(clippy::module_name_repetitions)]
use thiserror::Error;
/// The common error type for this Application.
+13 -19
View File
@@ -50,24 +50,18 @@ const CONFIG_PREFIX: &str = "dredge";
fn locate_config_file(path: Option<OsString>) -> Option<PathBuf> {
log::trace!("locate_config_file({path:?})");
match path {
Some(path) => {
let p = PathBuf::from(path);
log::debug!("Checking if path {p:?} exists");
p.try_exists().map(|_| Some(p)).unwrap_or(None)
}
None => {
let xdg_dirs = xdg::BaseDirectories::with_prefix(CONFIG_PREFIX).ok()?;
let search_paths: Vec<PathBuf> = vec![xdg_dirs.get_config_home()]
.into_iter()
.chain(xdg_dirs.get_config_dirs())
.collect();
log::debug!(
"Searching configuration directories for {CONFIG_FILE_NAME} {search_paths:?}"
);
xdg_dirs.find_config_file(CONFIG_FILE_NAME)
}
if let Some(path) = path {
let p = PathBuf::from(path);
log::debug!("Checking if path {p:?} exists");
p.try_exists().map(|_| Some(p)).unwrap_or(None)
} else {
let xdg_dirs = xdg::BaseDirectories::with_prefix(CONFIG_PREFIX).ok()?;
let search_paths: Vec<PathBuf> = vec![xdg_dirs.get_config_home()]
.into_iter()
.chain(xdg_dirs.get_config_dirs())
.collect();
log::debug!("Searching configuration directories for {CONFIG_FILE_NAME} {search_paths:?}");
xdg_dirs.find_config_file(CONFIG_FILE_NAME)
}
}
@@ -108,7 +102,7 @@ async fn main() -> Result<(), DredgeError> {
Commands::Catalog => commands::catalog_handler(&config).await?,
Commands::Tags { name } => commands::tags_handler(&config, &name).await?,
Commands::Show { image, tag } => {
commands::show_handler(&config, &image, &tag.unwrap_or("latest".to_string())).await?
commands::show_handler(&config, &image, &tag.unwrap_or("latest".to_string())).await?;
}
Commands::Delete { image, tag } => commands::delete_handler(&config, &image, &tag).await?,
Commands::Check => commands::check_handler(&config).await?,