Skip to content

Commit

Permalink
cdh/kms: add enum to replace provider type
Browse files Browse the repository at this point in the history
Signed-off-by: Xynnn007 <[email protected]>
  • Loading branch information
Xynnn007 committed Aug 20, 2023
1 parent 910bd3d commit 7601425
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions confidential-data-hub/kms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sha2 = { workspace = true, optional = true }
serde.workspace = true
serde_json.workspace = true
sev = { path = "../../attestation-agent/deps/sev", optional = true }
strum.workspace = true
reqwest = { version = "0.11", optional = true }
thiserror.workspace = true
tokio = { workspace = true, features = ["fs"] }
Expand Down
28 changes: 22 additions & 6 deletions confidential-data-hub/kms/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// SPDX-License-Identifier: Apache-2.0
//

use strum::{AsRefStr, EnumString};

use crate::{Decrypter, Error, Getter, ProviderSettings, Result};

const _IN_GUEST_DEFAULT_KEY_PATH: &str = "/run/confidential-containers/cdh/kms-credential";
Expand All @@ -12,27 +14,41 @@ pub mod aliyun;

pub mod kbs;

#[derive(AsRefStr, EnumString)]
pub enum DecryptorProvider {
#[cfg(feature = "aliyun")]
Aliyun,
}

/// Create a new [`Decrypter`] by given provider name and [`ProviderSettings`]
pub async fn new_decryptor(
provider: &str,
provider_name: &str,
_provider_settings: ProviderSettings,
) -> Result<Box<dyn Decrypter>> {
let provider = DecryptorProvider::try_from(provider_name)
.map_err(|_| Error::UnsupportedProvider(provider_name.to_string()))?;
match provider {
#[cfg(feature = "aliyun")]
"aliyun" => Ok(Box::new(
DecryptorProvider::Aliyun => Ok(Box::new(
aliyun::AliyunKmsClient::from_provider_settings(&_provider_settings).await?,
) as Box<dyn Decrypter>),
p => Err(Error::UnsupportedProvider(p.to_string())),
}
}

#[derive(AsRefStr, EnumString)]
pub enum VaultProvider {
#[cfg(feature = "kbs")]
Kbs,
}

/// Create a new [`Getter`] by given provider name and [`ProviderSettings`]
pub async fn new_getter(
provider: &str,
provider_name: &str,
_provider_settings: ProviderSettings,
) -> Result<Box<dyn Getter>> {
let provider = VaultProvider::try_from(provider_name)
.map_err(|_| Error::UnsupportedProvider(provider_name.to_string()))?;
match provider {
"kbs" => Ok(Box::new(kbs::KbcClient::new().await?) as Box<dyn Getter>),
p => Err(Error::UnsupportedProvider(p.to_string())),
VaultProvider::Kbs => Ok(Box::new(kbs::KbcClient::new().await?) as Box<dyn Getter>),
}
}

0 comments on commit 7601425

Please sign in to comment.