agent:cdh: Initialize CDH client as a global asynchronous instance

Introduced a global `CDH_CLIENT` instance to hold the cdh client and
implemented `init_cdh_client` function to initialize the cdh client if not already set.

Fixes: #10231

Signed-off-by: ChengyuZhu6 <chengyu.zhu@intel.com>
This commit is contained in:
ChengyuZhu6 2024-09-02 07:52:28 +08:00
parent 4698235e59
commit 0ad35dc91b

View File

@ -7,19 +7,20 @@
// Confidential Data Hub is a service running inside guest to provide resource related APIs.
// https://github.com/confidential-containers/guest-components/tree/main/confidential-data-hub
use anyhow::Result;
use crate::AGENT_CONFIG;
use crate::CDH_SOCKET_URI;
use anyhow::{Context, Result};
use derivative::Derivative;
use protocols::{
confidential_data_hub, confidential_data_hub_ttrpc_async,
confidential_data_hub_ttrpc_async::{SealedSecretServiceClient, SecureMountServiceClient},
};
use crate::AGENT_CONFIG;
use crate::CDH_SOCKET_URI;
use tokio::sync::OnceCell;
// 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.";
@ -90,6 +91,13 @@ 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(())
}
#[cfg(test)]
#[cfg(feature = "sealed-secret")]
mod tests {