Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat | Implement initdata for bare-metal/qemu hypervisor #10163

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/agent/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ regorus = { version = "0.1.4", default-features = false, features = [
"regex",
], optional = true }

# Initdata
base64 = "0.22"
sha2 = "0.10.8"

[dev-dependencies]
tempfile = "3.1.0"
test-utils = { path = "../libs/test-utils" }
Expand Down
78 changes: 47 additions & 31 deletions src/agent/src/cdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,30 @@
// https://github.com/confidential-containers/guest-components/tree/main/confidential-data-hub

use crate::AGENT_CONFIG;
use crate::CDH_SOCKET_URI;
use anyhow::{Context, Result};
use anyhow::Result;
use derivative::Derivative;
use protocols::{
confidential_data_hub, confidential_data_hub_ttrpc_async,
confidential_data_hub_ttrpc_async::{SealedSecretServiceClient, SecureMountServiceClient},
};
use tokio::sync::OnceCell;

use crate::initdata::CDH_SOCKET_URI;

// Nanoseconds
lazy_static! {
static ref CDH_API_TIMEOUT: i64 = AGENT_CONFIG.cdh_api_timeout.as_nanos() as i64;
pub static ref CDH_CLIENT: OnceCell<CDHClient> = OnceCell::new();
}

const SEALED_SECRET_PREFIX: &str = "sealed.";

pub static CDH_CLIENT: OnceCell<CDHClient> = OnceCell::const_new();

// Convenience function to obtain the scope logger.
fn sl() -> slog::Logger {
slog_scope::logger()
}

#[derive(Derivative)]
#[derivative(Clone, Debug)]
pub struct CDHClient {
Expand Down Expand Up @@ -78,32 +86,29 @@ impl CDHClient {
}
}

pub async fn init_cdh_client() -> Result<()> {
CDH_CLIENT
.get_or_try_init(|| async { CDHClient::new().context("Failed to create CDH Client") })
.await?;
Ok(())
}

/// Check if the CDH client is initialized
pub async fn is_cdh_client_initialized() -> bool {
CDH_CLIENT.get().is_some() // Returns true if CDH_CLIENT is initialized, false otherwise
}

pub async fn unseal_env(env: &str) -> Result<String> {
let cdh_client = CDH_CLIENT
.get()
.expect("Confidential Data Hub not initialized");

if let Some((key, value)) = env.split_once('=') {
if value.starts_with(SEALED_SECRET_PREFIX) {
let unsealed_value = cdh_client.unseal_secret_async(value).await?;
let unsealed_env = format!("{}={}", key, std::str::from_utf8(&unsealed_value)?);
match CDH_CLIENT.get() {
Some(client) => {
if let Some((key, value)) = env.split_once('=') {
if value.starts_with(SEALED_SECRET_PREFIX) {
let unsealed_value = client.unseal_secret_async(value).await?;
let unsealed_env = format!("{}={}", key, std::str::from_utf8(&unsealed_value)?);

info!(sl(), "Unseal secret of key `{key}` successfully");
return Ok(unsealed_env);
}
}

return Ok(unsealed_env);
Ok(env.to_string())
}
None => {
info!(
sl(),
"No CDH client initialized, env `{env}` will be used without unsealing."
);
Ok(env.to_string())
}
}
Ok((*env.to_owned()).to_string())
}

pub async fn secure_mount(
Expand All @@ -112,13 +117,24 @@ pub async fn secure_mount(
flags: Vec<String>,
mount_point: &str,
) -> Result<()> {
let cdh_client = CDH_CLIENT
.get()
.expect("Confidential Data Hub not initialized");
match CDH_CLIENT.get() {
Some(cdh_client) => {
cdh_client
.secure_mount(volume_type, options, flags, mount_point)
.await?;
info!(
sl(),
"Secure mount of volume type `{volume_type}` successfully"
);
}
None => {
warn!(
sl(),
"No CDH client initialized, secure mount of volume type `{volume_type}` will be ignored"
);
}
}

cdh_client
.secure_mount(volume_type, options, flags, mount_point)
.await?;
Ok(())
}

Expand Down
Loading
Loading