Skip to content

Commit

Permalink
feat!: implement encryption scheme logic in storage
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalenic committed Jan 24, 2025
1 parent cdee739 commit f9b863a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 deletions.
3 changes: 0 additions & 3 deletions htsget-config/src/encryption_scheme.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Types related to the experimental encryption scheme.
//!
use crate::error::Error;
use crate::error::Result;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

/// The file encryption scheme
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
Expand Down
3 changes: 2 additions & 1 deletion htsget-config/src/storage/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ impl File {
scheme,
authority,
local_path,
..Default::default()
#[cfg(feature = "experimental")]
keys: None,
}
}

Expand Down
20 changes: 13 additions & 7 deletions htsget-config/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,6 @@ impl Query {
self
}

/// Set the encryption scheme.
#[cfg(feature = "experimental")]
pub fn with_encryption_scheme(mut self, encryption_scheme: EncryptionScheme) -> Self {
self.encryption_scheme = Some(encryption_scheme);
self
}

/// Id.
pub fn id(&self) -> &str {
&self.id
Expand Down Expand Up @@ -460,6 +453,19 @@ impl Query {
pub fn request(&self) -> &Request {
&self.request
}

/// Set the encryption scheme.
#[cfg(feature = "experimental")]
pub fn with_encryption_scheme(mut self, encryption_scheme: EncryptionScheme) -> Self {
self.encryption_scheme = Some(encryption_scheme);
self
}

/// Get the encryption scheme
#[cfg(feature = "experimental")]
pub fn encryption_scheme(&self) -> Option<EncryptionScheme> {
self.encryption_scheme
}
}

/// Htsget specific errors.
Expand Down
6 changes: 3 additions & 3 deletions htsget-search/src/from_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ impl HtsGet for HtsGetFromStorage {
#[async_trait]
impl ResolveResponse for HtsGetFromStorage {
async fn from_file(file_storage: &storage::file::File, query: &Query) -> Result<Response> {
let storage = Storage::from_file(file_storage).await?;
let storage = Storage::from_file(file_storage, query).await?;
let searcher = HtsGetFromStorage::new(storage);
searcher.search(query.clone()).await
}

#[cfg(feature = "aws")]
async fn from_s3(s3_storage: &storage::s3::S3, query: &Query) -> Result<Response> {
let storage = Storage::from_s3(s3_storage).await;
let storage = Storage::from_s3(s3_storage, query).await;
let searcher = HtsGetFromStorage::new(storage?);
searcher.search(query.clone()).await
}

#[cfg(feature = "url")]
async fn from_url(url_storage_config: &storage::url::Url, query: &Query) -> Result<Response> {
let storage = Storage::from_url(url_storage_config).await;
let storage = Storage::from_url(url_storage_config, query).await;
let searcher = HtsGetFromStorage::new(storage?);
searcher.search(query.clone()).await
}
Expand Down
2 changes: 1 addition & 1 deletion htsget-storage/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub enum StorageError {

#[error("invalid address: `{0}`")]
InvalidAddress(AddrParseError),

#[error("`{0}`")]
UnsupportedFormat(String),

Expand Down
32 changes: 20 additions & 12 deletions htsget-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use async_trait::async_trait;
use base64::engine::general_purpose;
use base64::Engine;
use cfg_if::cfg_if;
#[cfg(feature = "experimental")]
use htsget_config::encryption_scheme::EncryptionScheme;
use htsget_config::storage;
#[cfg(feature = "experimental")]
use htsget_config::storage::c4gh::C4GHKeys;
Expand Down Expand Up @@ -134,28 +136,34 @@ impl StorageTrait for Storage {
impl Storage {
#[cfg(feature = "experimental")]
/// Wrap an existing storage with C4GH storage
pub async fn from_c4gh_keys(keys: Option<&C4GHKeys>, storage: Storage) -> Result<Storage> {
if let Some(keys) = keys {
Ok(Storage::new(C4GHStorage::new_box(
pub async fn from_c4gh_keys(
keys: Option<&C4GHKeys>,
encryption_scheme: Option<EncryptionScheme>,
storage: Storage,
) -> Result<Storage> {
match (keys, encryption_scheme) {
(Some(keys), Some(EncryptionScheme::C4GH)) => Ok(Storage::new(C4GHStorage::new_box(
keys
.clone()
.keys()
.await
.map_err(|err| StorageError::InternalError(err.to_string()))?,
storage.into_inner(),
)))
} else {
Ok(storage)
))),
(None, Some(EncryptionScheme::C4GH)) => Err(StorageError::UnsupportedFormat(
"C4GH keys have not been configured for this id".to_string(),
)),
_ => Ok(storage),
}
}

/// Create from local storage config.
pub async fn from_file(file: &storage::file::File) -> Result<Storage> {
pub async fn from_file(file: &storage::file::File, query: &Query) -> Result<Storage> {
let storage = Storage::new(FileStorage::new(file.local_path(), file.clone())?);

cfg_if! {
if #[cfg(feature = "experimental")] {
Self::from_c4gh_keys(file.keys(), storage).await
Self::from_c4gh_keys(file.keys(), query.encryption_scheme(), storage).await
} else {
Ok(storage)
}
Expand All @@ -164,7 +172,7 @@ impl Storage {

/// Create from s3 config.
#[cfg(feature = "aws")]
pub async fn from_s3(s3: &storage::s3::S3) -> Result<Storage> {
pub async fn from_s3(s3: &storage::s3::S3, query: &Query) -> Result<Storage> {
let storage = Storage::new(
S3Storage::new_with_default_config(
s3.bucket().to_string(),
Expand All @@ -176,7 +184,7 @@ impl Storage {

cfg_if! {
if #[cfg(feature = "experimental")] {
Self::from_c4gh_keys(s3.keys(), storage).await
Self::from_c4gh_keys(s3.keys(), query.encryption_scheme(), storage).await
} else {
Ok(storage)
}
Expand All @@ -185,7 +193,7 @@ impl Storage {

/// Create from url config.
#[cfg(feature = "url")]
pub async fn from_url(url: &storage::url::Url) -> Result<Storage> {
pub async fn from_url(url: &storage::url::Url, query: &Query) -> Result<Storage> {
let storage = Storage::new(UrlStorage::new(
url.client_cloned(),
url.url().clone(),
Expand All @@ -196,7 +204,7 @@ impl Storage {

cfg_if! {
if #[cfg(feature = "experimental")] {
Self::from_c4gh_keys(url.keys(), storage).await
Self::from_c4gh_keys(url.keys(), query.encryption_scheme(), storage).await
} else {
Ok(storage)
}
Expand Down

0 comments on commit f9b863a

Please sign in to comment.