forked from confidential-containers/guest-components
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cdh: support to encrypt block device
Support to encrypt block device in cdh. Fixed: confidential-containers#540 -- part II Signed-off-by: ChengyuZhu6 <[email protected]>
- Loading branch information
ChengyuZhu6
committed
Jul 17, 2024
1 parent
3cbdf1b
commit ee3da35
Showing
4 changed files
with
196 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/rbd/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, RbdError>; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum RbdError { | ||
#[error("Error when getting encrypt/decrypt keys")] | ||
GetKeysFaile(#[from] anyhow::Error), | ||
|
||
#[error("LUKSfs 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), | ||
} |
156 changes: 156 additions & 0 deletions
156
confidential-data-hub/storage/src/volume_type/rbd/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,156 @@ | ||
// 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 base64::Engine; | ||
use error::{RbdError, Result}; | ||
use log::{debug, error}; | ||
use rand::{distributions::Alphanumeric, Rng}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
use strum::{Display, EnumString}; | ||
use tokio::{ | ||
fs, | ||
io::{AsyncReadExt, AsyncWriteExt}, | ||
process::Command, | ||
}; | ||
|
||
/// LUKS encrypt storage binary | ||
const LUKS_ENCRYPT_STORAGE_BIN: &str = "/usr/local/bin/luks-encrypt-storage"; | ||
|
||
#[derive(EnumString, Serialize, Deserialize, Display, Debug, PartialEq, Eq)] | ||
pub enum RbdEncryptType { | ||
#[strum(serialize = "luks")] | ||
LUKS, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, PartialEq, Debug)] | ||
struct RbdParameters { | ||
/// 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 encrypt_type: RbdEncryptType, | ||
|
||
/// Encryption key. If not set, generate a random 4096-byte key | ||
#[serde(rename = "encryptKey")] | ||
pub encrypt_key: Option<String>, | ||
|
||
/// Indicates whether to enable dm-integrity. | ||
#[serde(rename = "dataIntegrity")] | ||
pub data_integrity: String, | ||
} | ||
|
||
pub(crate) struct Rbd; | ||
|
||
async fn random_encrypt_key() -> anyhow::Result<String> { | ||
let mut buffer = vec![0u8; 4096]; | ||
rand::thread_rng().fill(&mut buffer[..]); | ||
Ok(base64::engine::general_purpose::STANDARD.encode(&buffer)) | ||
} | ||
|
||
async fn get_plaintext_secret(secret: &str) -> anyhow::Result<String> { | ||
if secret.starts_with("sealed.") { | ||
debug!("detected sealed secret"); | ||
let unsealed = secret::unseal_secret(secret.as_bytes()).await?; | ||
|
||
String::from_utf8(unsealed).context("convert to String failed") | ||
} else { | ||
Ok(secret.into()) | ||
} | ||
} | ||
|
||
async fn create_storage_key_file(rbd_parameter: &RbdParameters) -> Result<String> { | ||
let random_string: String = rand::thread_rng() | ||
.sample_iter(&Alphanumeric) | ||
.take(5) | ||
.map(char::from) | ||
.collect(); | ||
let storage_key_path = format!("/tmp/encrypted_disk_{}", random_string); | ||
let mut storage_key_file = fs::File::create(storage_key_path).await?; | ||
|
||
let plain_key = match &rbd_parameter.encrypt_key { | ||
Some(encrypt_key) => get_plaintext_secret(encrypt_key).await?, | ||
None => random_encrypt_key().await?, | ||
}; | ||
|
||
storage_key_file.write_all(plain_key.as_bytes()).await?; | ||
storage_key_file.flush().await?; | ||
Ok(storage_key_path) | ||
} | ||
|
||
impl Rbd { | ||
async fn real_mount( | ||
&self, | ||
options: &HashMap<String, String>, | ||
_flags: &[String], | ||
mount_point: &str, | ||
) -> Result<()> { | ||
// construct RbdParameters | ||
let parameters = serde_json::to_string(options)?; | ||
let rbd_parameter: RbdParameters = serde_json::from_str(¶meters)?; | ||
|
||
if rbd_parameter.encrypt_type == RbdEncryptType::LUKS { | ||
let storage_key_path = create_storage_key_file(&rbd_parameter).await?; | ||
|
||
let parameters = vec![ | ||
rbd_parameter.device_id, | ||
mount_point.to_string(), | ||
storage_key_file, | ||
rbd_parameter.data_integrity, | ||
]; | ||
|
||
let mut encrypt_device = Command::new(LUKS_ENCRYPT_STORAGE_BIN) | ||
.args(parameters) | ||
.spawn() | ||
.map_err(|e| { | ||
error!("luks-encrypt-storage cmd fork failed: {e}"); | ||
RbdError::BlockDeviceMountFailed | ||
})?; | ||
|
||
let rbd_res = encrypt_device.wait().await?; | ||
if !rbd_res.success() { | ||
{ | ||
let mut stderr = String::new(); | ||
if let Some(mut err) = encrypt_device.stderr { | ||
err.read_to_string(&mut stderr).await?; | ||
error!("RBD mount failed with stderr: {stderr}"); | ||
} else { | ||
error!("RBD mount failed"); | ||
} | ||
|
||
return Err(RbdError::BlockDeviceMountFailed); | ||
} | ||
} | ||
}; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl SecureMount for Rbd { | ||
/// Mount the block device to the given `mount_point``. | ||
/// | ||
/// If `rbd.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()) | ||
} | ||
} |