-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cdh: Add secure mount interface for block device
Add secure mount interface for block device. Fixed: #540 -- part II Signed-off-by: ChengyuZhu6 <[email protected]>
- Loading branch information
ChengyuZhu6
committed
Jul 18, 2024
1 parent
3cbdf1b
commit ccf7171
Showing
5 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
confidential-data-hub/storage/src/volume_type/blockdevice/error.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) 2024 Intel | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
use thiserror::Error; | ||
|
||
pub type Result<T> = std::result::Result<T, BlockDeviceError>; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum BlockDeviceError { | ||
#[error("Error when getting encrypt/decrypt keys")] | ||
GetKeysFailure(#[from] anyhow::Error), | ||
|
||
#[error("LUKS decryption mount failed")] | ||
LUKSfsMountFailed, | ||
|
||
#[error("I/O error")] | ||
IOError(#[from] std::io::Error), | ||
|
||
#[error("Failed to mount block device")] | ||
BlockDeviceMountFailed, | ||
|
||
#[error("Serialize/Deserialize failed")] | ||
SerdeError(#[from] serde_json::Error), | ||
|
||
#[error("Failed to recognize the storage type")] | ||
StorageTypeNotRecognized(#[from] strum::ParseError), | ||
} |
107 changes: 107 additions & 0 deletions
107
confidential-data-hub/storage/src/volume_type/blockdevice/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright (c) 2024 Intel | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
pub mod error; | ||
|
||
use super::SecureMount; | ||
use anyhow::Context; | ||
use async_trait::async_trait; | ||
use error::{BlockDeviceError, Result}; | ||
use kms::{Annotations, ProviderSettings}; | ||
use log::{debug, error}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
use strum::{Display, EnumString}; | ||
|
||
#[derive(EnumString, Serialize, Deserialize, Display, Debug, PartialEq, Eq)] | ||
pub enum BlockDeviceEncryptType { | ||
#[strum(serialize = "luks")] | ||
LUKS, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, PartialEq, Debug)] | ||
pub struct BlockDeviceParameters { | ||
/// The device number, formatted as "MAJ:MIN". | ||
#[serde(rename = "deviceId")] | ||
pub device_id: String, | ||
|
||
/// The encryption type. Currently, only LUKS is supported. | ||
#[serde(rename = "encryptType")] | ||
pub encryption_type: BlockDeviceEncryptType, | ||
|
||
/// Encryption key. If not set, generate a random 4096-byte key | ||
#[serde(rename = "encryptKey")] | ||
pub encryption_key: Option<String>, | ||
|
||
/// Indicates whether to enable dm-integrity. | ||
#[serde(rename = "dataIntegrity")] | ||
pub data_integrity: String, | ||
} | ||
pub(crate) struct BlockDevice; | ||
|
||
#[async_trait] | ||
pub trait Encryptor { | ||
async fn encrypt_and_mount( | ||
&self, | ||
parameters: BlockDeviceParameters, | ||
mount_point: &str, | ||
) -> Result<()>; | ||
} | ||
|
||
async fn get_plaintext_key(resource: &str) -> anyhow::Result<String> { | ||
if resource.starts_with("sealed.") { | ||
debug!("detected sealed secret"); | ||
let unsealed = secret::unseal_secret(resource.as_bytes()).await?; | ||
return String::from_utf8(unsealed).context("convert to String failed"); | ||
} | ||
|
||
if resource.starts_with("kbs://") { | ||
let secret = kms::new_getter("kbs", ProviderSettings::default()) | ||
.await? | ||
.get_secret(resource, &Annotations::default()) | ||
.await | ||
.map_err(|e| { | ||
error!("get keys from kbs failed: {e}"); | ||
BlockDeviceError::GetKeysFailure(e.into()) | ||
})?; | ||
return String::from_utf8(secret).context("convert to String failed"); | ||
} | ||
|
||
Err(BlockDeviceError::GetKeysFailure(anyhow::anyhow!("unknown resource scheme")).into()) | ||
} | ||
|
||
impl BlockDevice { | ||
async fn real_mount( | ||
&self, | ||
options: &HashMap<String, String>, | ||
_flags: &[String], | ||
mount_point: &str, | ||
) -> Result<()> { | ||
// construct BlockDeviceParameters | ||
let parameters = serde_json::to_string(options)?; | ||
let bd_parameter: BlockDeviceParameters = serde_json::from_str(¶meters)?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl SecureMount for BlockDevice { | ||
/// Mount the block device to the given `mount_point``. | ||
/// | ||
/// If `bd.encrypt_type` is set to `LUKS`, the device will be formated as a LUKS-encrypted device. | ||
/// Then use cryptsetup open the device and mount it to `mount_point` as plaintext. | ||
/// | ||
/// This is a wrapper for inner function to convert error type. | ||
async fn mount( | ||
&self, | ||
options: &HashMap<String, String>, | ||
flags: &[String], | ||
mount_point: &str, | ||
) -> super::Result<()> { | ||
self.real_mount(options, flags, mount_point) | ||
.await | ||
.map_err(|e| e.into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters