Skip to content

Commit

Permalink
refactor: Error replaces String
Browse files Browse the repository at this point in the history
  • Loading branch information
de-sh committed Nov 20, 2024
1 parent ea95a4b commit babb3c1
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions server/src/storage/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,31 +156,40 @@ pub enum SSECEncryptionKey {
},
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Expected SSE-C:AES256:<base64_encryption_key>")]
UnexpectedKey,
#[error("Only SSE-C is supported for object encryption for now")]
UnexpectedProtocol,
#[error("Invalid SSE algorithm. Following are supported: AES256")]
InvalidAlgorithm,
}

impl FromStr for SSECEncryptionKey {
type Err = String;
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts = s.split(':').collect::<Vec<_>>();
if parts.len() == 3 {
let sse_type = parts[0];
if sse_type != "SSE-C" {
return Err("Only SSE-C is supported for object encryption for now".into());
}
if parts.len() != 3 {
return Err(Error::UnexpectedKey);
}
let sse_type = parts[0];
if sse_type != "SSE-C" {
return Err(Error::UnexpectedProtocol);
}

let algorithm = parts[1];
let encryption_key = parts[2];
let algorithm = parts[1];
let encryption_key = parts[2];

let alg = ObjectEncryptionAlgorithm::from_str(algorithm)?;
let alg = ObjectEncryptionAlgorithm::from_str(algorithm)?;

Ok(match alg {
ObjectEncryptionAlgorithm::Aes256 => SSECEncryptionKey::SseC {
_algorithm: alg,
base64_encryption_key: encryption_key.to_owned(),
},
})
} else {
Err("Expected SSE-C:AES256:<base64_encryption_key>".into())
}
Ok(match alg {
ObjectEncryptionAlgorithm::Aes256 => SSECEncryptionKey::SseC {
_algorithm: alg,
base64_encryption_key: encryption_key.to_owned(),
},
})
}
}

Expand All @@ -190,12 +199,12 @@ pub enum ObjectEncryptionAlgorithm {
}

impl FromStr for ObjectEncryptionAlgorithm {
type Err = String;
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"AES256" => Ok(ObjectEncryptionAlgorithm::Aes256),
_ => Err("Invalid SSE algorithm. Following are supported: AES256".into()),
_ => Err(Error::InvalidAlgorithm),
}
}
}
Expand Down

0 comments on commit babb3c1

Please sign in to comment.