Skip to content

Commit a6a6160

Browse files
committed
CDH: return formatted brief error message to RPC caller
This commit defines a more formatted error printing when RPC fails to be called by a remote caller. Also, it includes a simple error reason for image pulling failures. Signed-off-by: Xynnn007 <[email protected]>
1 parent 21053c6 commit a6a6160

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

confidential-data-hub/hub/src/bin/grpc_server/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl SealedSecretService for Arc<Cdh> {
5353
.map_err(|e| {
5454
let detailed_error = format_error!(e);
5555
error!("[gRPC CDH] Call CDH to unseal secret failed:\n{detailed_error}");
56-
Status::internal(format!("[ERROR] CDH unseal secret failed: {}", e))
56+
Status::internal(format!("[CDH] [ERROR]: {e}"))
5757
})?;
5858

5959
debug!("[gRPC CDH] Unseal secret successfully!");
@@ -80,7 +80,7 @@ impl GetResourceService for Arc<Cdh> {
8080
.map_err(|e| {
8181
let detailed_error = format_error!(e);
8282
error!("[gRPC CDH] Call CDH to get resource failed:\n{detailed_error}");
83-
Status::internal(format!("[ERROR] CDH get resource failed: {}", e))
83+
Status::internal(format!("[CDH] [ERROR]: {e}"))
8484
})?;
8585

8686
debug!("[gRPC CDH] Get resource successfully!");
@@ -109,7 +109,7 @@ impl SecureMountService for Arc<Cdh> {
109109
let mount_path = self.inner.secure_mount(storage).await.map_err(|e| {
110110
let detailed_error = format_error!(e);
111111
error!("[gRPC CDH] Call CDH to secure mount failed:\n{detailed_error}");
112-
Status::internal(format!("[ERROR] CDH secure mount failed: {}", e))
112+
Status::internal(format!("[CDH] [ERROR]: {e}"))
113113
})?;
114114

115115
debug!("[gRPC CDH] Secure mount successfully!");
@@ -136,7 +136,7 @@ impl ImagePullService for Arc<Cdh> {
136136
.map_err(|e| {
137137
let detailed_error = format_error!(e);
138138
error!("[gRPC CDH] Call CDH to pull image failed:\n{detailed_error}");
139-
Status::internal(format!("[ERROR] CDH image pulling failed: {}", e))
139+
Status::internal(format!("[CDH] [ERROR]: {e}"))
140140
})?;
141141

142142
debug!("[gRPC CDH] Pull image successfully!");
@@ -185,7 +185,7 @@ impl KeyProviderService for Arc<Cdh> {
185185
.map_err(|e| {
186186
let detailed_error = format_error!(e);
187187
error!("[gRPC CDH] Call CDH to Unwrap Key failed:\n{detailed_error}");
188-
Status::internal(format!("[ERROR] CDH Unwrap Key failed: {}", e))
188+
Status::internal(format!("[CDH] [ERROR]: {e}"))
189189
})?;
190190

191191
// Construct output structure and serialize it as the return value of gRPC

confidential-data-hub/hub/src/bin/ttrpc_server/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl SealedSecretService for Server {
5555
error!("[ttRPC CDH] UnsealSecret :\n{detailed_error}");
5656
let mut status = Status::new();
5757
status.set_code(Code::INTERNAL);
58-
status.set_message("[CDH] [ERROR]: Unseal Secret failed".into());
58+
status.set_message(format!("[CDH] [ERROR]: {e}"));
5959
Error::RpcStatus(status)
6060
})?;
6161

@@ -79,7 +79,7 @@ impl GetResourceService for Server {
7979
error!("[ttRPC CDH] GetResource :\n{detailed_error}");
8080
let mut status = Status::new();
8181
status.set_code(Code::INTERNAL);
82-
status.set_message("[CDH] [ERROR]: Get Resource failed".into());
82+
status.set_message(format!("[CDH] [ERROR]: {e}"));
8383
Error::RpcStatus(status)
8484
})?;
8585

@@ -121,7 +121,7 @@ impl KeyProviderService for Server {
121121
error!("[ttRPC CDH] UnWrapKey :\n{detailed_error}");
122122
let mut status = Status::new();
123123
status.set_code(Code::INTERNAL);
124-
status.set_message("[CDH] [ERROR]: UnwrapKey failed".to_string());
124+
status.set_message(format!("[CDH] [ERROR]: {e}"));
125125
Error::RpcStatus(status)
126126
})?;
127127

@@ -167,7 +167,7 @@ impl SecureMountService for Server {
167167
error!("[ttRPC CDH] Secure Mount :\n{detailed_error}");
168168
let mut status = Status::new();
169169
status.set_code(Code::INTERNAL);
170-
status.set_message("[CDH] [ERROR]: secure mount failed".to_string());
170+
status.set_message(format!("[CDH] [ERROR]: {e}"));
171171
Error::RpcStatus(status)
172172
})?;
173173

@@ -195,7 +195,7 @@ impl ImagePullService for Server {
195195
error!("[ttRPC CDH] Pull Image :\n{detailed_error}");
196196
let mut status = Status::new();
197197
status.set_code(Code::INTERNAL);
198-
status.set_message("[CDH] [ERROR]: pull image failed".to_string());
198+
status.set_message(format!("[CDH] [ERROR]: {e}"));
199199
Error::RpcStatus(status)
200200
})?;
201201

confidential-data-hub/hub/src/error.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,52 @@ pub enum Error {
1616
source: kms::Error,
1717
},
1818

19-
#[error("get resource failed")]
19+
#[error("Get Resource failed")]
2020
GetResource {
2121
#[source]
2222
source: kms::Error,
2323
},
2424

25-
#[error("decrypt image (unwrap key) failed")]
25+
#[error("Decrypt Image (UnwrapKey) failed")]
2626
ImageDecryption(#[from] image::Error),
2727

2828
#[error("init Hub failed: {0}")]
2929
InitializationFailed(String),
3030

31-
#[error("unseal secret failed")]
31+
#[error("Unseal Secret failed")]
3232
UnsealSecret(#[from] secret::SecretError),
3333

34-
#[error("secure mount failed")]
34+
#[error("Secure Mount failed")]
3535
SecureMount(#[from] storage::Error),
3636

37-
#[error("image pull failed")]
37+
#[error("Image Pull failed: {source}")]
3838
ImagePull {
3939
#[source]
40-
source: anyhow::Error,
40+
source: image_rs::PullImageError,
4141
},
4242
}
43+
#[cfg(test)]
44+
mod tests {
45+
use super::*;
46+
use anyhow::anyhow;
47+
use rstest::rstest;
48+
49+
#[rstest]
50+
#[case(Error::KbsClient { source: kms::Error::KbsClientError("details".into()) }, "kbs client initialization failed")]
51+
#[case(Error::GetResource { source: kms::Error::KbsClientError("details".into()) }, "Get Resource failed")]
52+
#[case(
53+
Error::UnsealSecret(secret::SecretError::VersionError),
54+
"Unseal Secret failed"
55+
)]
56+
#[case(
57+
Error::SecureMount(storage::Error::StorageTypeNotRecognized(
58+
strum::ParseError::VariantNotFound
59+
)),
60+
"Secure Mount failed"
61+
)]
62+
#[case(Error::ImagePull {source: image_rs::PullImageError::SignatureValidationFailed{source: anyhow!("details")}}, "Image Pull failed: Image policy rejected")]
63+
fn test_brief_message(#[case] error: Error, #[case] expected: &str) {
64+
let brief_message = error.to_string();
65+
assert_eq!(brief_message, expected);
66+
}
67+
}

0 commit comments

Comments
 (0)