Skip to content

Commit

Permalink
Trait refactor (#240)
Browse files Browse the repository at this point in the history
Refactored the routes and client interface to use traits. There is still more work to be done e.g. change routes to only use the interface types.
  • Loading branch information
amouat committed Mar 18, 2021
1 parent 27f19d0 commit 7a08a83
Show file tree
Hide file tree
Showing 18 changed files with 1,715 additions and 700 deletions.
684 changes: 329 additions & 355 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ chrono = { version="^0.4", features = ["serde"] }
rusqlite = "0.23.1"
data-encoding = "2.3"
openssl = { version = "0.10", features = ["vendored"] }
lazy_static = "1.4.0"
regex = "1.3.9"
sha2 = "0.9"
hex = "0.4"
thiserror = "1.0"

[build-dependencies]

Expand Down
24 changes: 17 additions & 7 deletions lib/server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use tonic::{Request, Response, Status};
use uuid::Uuid;
use core::fmt::Display;
use reqwest::{self, header::{HeaderMap, HeaderValue}};

pub mod trow_server {
include!("../../protobuf/out/trow.rs");
}
Expand Down Expand Up @@ -75,6 +74,13 @@ pub struct ProxyError {
msg: String,
}

#[derive(Fail, Debug)]
#[fail(display = "Expected digest {} but got {}", user_digest, actual_digest)]
pub struct DigestValidationError {
user_digest: String,
actual_digest: String
}

#[derive(Clone, Debug, PartialEq)]
pub struct Image {
pub host: String, //Including port, docker.io by default
Expand Down Expand Up @@ -194,10 +200,7 @@ fn validate_digest(file: &PathBuf, digest: &str) -> Result<(), Error> {
"Upload did not match given digest. Was given {} but got {}",
digest, calculated_digest
);
return Err(failure::err_msg(format!(
"Upload did not match given digest. Was given {} but got {}",
digest, calculated_digest
)));
Err(DigestValidationError {user_digest: digest.to_string(), actual_digest: calculated_digest})?;
}

Ok(())
Expand Down Expand Up @@ -934,8 +937,15 @@ impl Registry for TrowServer {
digest: cr.user_digest.clone(),
})),
Err(e) => {
warn!("Failure when saving layer: {:?}", e);
Err(Status::internal("Internal error saving layer"))
match e.downcast::<DigestValidationError>() {
Ok(v_e) => {
Err(Status::invalid_argument(v_e.to_string()))
}
Err(e) => {
warn!("Failure when saving layer: {:?}", e);
Err(Status::internal("Internal error saving layer"))
}
}
}
};

Expand Down
Loading

0 comments on commit 7a08a83

Please sign in to comment.