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

Fix CDH & kbs_protocol #381

Merged
merged 4 commits into from
Oct 30, 2023
Merged
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
3 changes: 2 additions & 1 deletion attestation-agent/kbs_protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ serde.workspace = true
serde_json.workspace = true
sha2.workspace = true
thiserror.workspace = true
tokio.workspace = true
ttrpc = { workspace = true, optional = true}
url.workspace = true
zeroize.workspace = true
Expand All @@ -42,7 +43,7 @@ passport = []
# use a client of attestation-agent to get token for kbs
aa_token = ["ttrpc-codegen", "passport", "ttrpc/async", "protobuf"]

background_check = []
background_check = ["tokio/time"]
all-attesters = ["attester/all-attesters"]
tdx-attester = ["attester/tdx-attester"]
sgx-attester = ["attester/sgx-attester"]
Expand Down
55 changes: 49 additions & 6 deletions attestation-agent/kbs_protocol/src/client/rcar_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// SPDX-License-Identifier: Apache-2.0
//

use std::time::Duration;

use anyhow::{bail, Context};
use async_trait::async_trait;
use kbs_types::{Attestation, Challenge, ErrorInformation, Request, Response};
Expand All @@ -22,6 +24,13 @@ use crate::{
Error, Result,
};

/// When executing get token, RCAR handshake should retry if failed to
/// make the logic robust. This constant is the max retry times.
const RCAR_MAX_ATTEMPT: i32 = 5;

/// The interval (seconds) between RCAR handshake retries.
const RCAR_RETRY_TIMEOUT_SECOND: u64 = 1;

#[derive(Deserialize, Debug, Clone)]
struct AttestationResponseData {
// Attestation token in JWT format
Expand All @@ -36,14 +45,48 @@ impl KbsClient<Box<dyn EvidenceProvider>> {
pub async fn get_token(&mut self) -> Result<(Token, TeeKeyPair)> {
if let Some(token) = &self.token {
if token.check_valid().is_err() {
self.rcar_handshake()
.await
.map_err(|e| Error::RcarHandshake(e.to_string()))?;
let mut retry_times = 1;
loop {
let res = self
.rcar_handshake()
.await
.map_err(|e| Error::RcarHandshake(e.to_string()));
match res {
Ok(_) => break,
Err(e) => {
if retry_times >= RCAR_MAX_ATTEMPT {
return Err(Error::RcarHandshake(format!("Get token failed because of RCAR handshake retried {RCAR_MAX_ATTEMPT} times.")));
} else {
warn!("RCAR handshake failed: {e}, retry {retry_times}...");
retry_times += 1;
tokio::time::sleep(Duration::from_secs(RCAR_RETRY_TIMEOUT_SECOND))
.await;
}
}
}
}
}
} else {
self.rcar_handshake()
.await
.map_err(|e| Error::RcarHandshake(e.to_string()))?;
let mut retry_times = 1;
loop {
let res = self
.rcar_handshake()
.await
.map_err(|e| Error::RcarHandshake(e.to_string()));
match res {
Ok(_) => break,
Err(e) => {
if retry_times >= RCAR_MAX_ATTEMPT {
return Err(Error::RcarHandshake(format!("Get token failed because of RCAR handshake retried {RCAR_MAX_ATTEMPT} times.")));
} else {
warn!("RCAR handshake failed: {e}, retry {retry_times}...");
retry_times += 1;
tokio::time::sleep(Duration::from_secs(RCAR_RETRY_TIMEOUT_SECOND))
.await;
}
}
}
}
}

assert!(self.token.is_some());
Expand Down
5 changes: 4 additions & 1 deletion confidential-data-hub/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ endif
ifdef PROVIDER
features += $(PROVIDER)
else
features += aliyun
ifneq ($(ARCH), s390x)
$(info INFO: Aliyun KMS plugins will be built in by default)
features += aliyun
endif
endif

ifeq ($(LIBC), musl)
Expand Down
2 changes: 1 addition & 1 deletion confidential-data-hub/docs/kms-providers/alibaba.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Then, let's
# define the parameters
KEY_ID=$(cat kms-key-id.txt)
KMS_INSTANCE_ID=$(cat kms-instance-id.txt)
CLIENT_KEY_PASSWORD_FILE_PATH=$(pwd)/ClientKeyPassword.txt
CLIENT_KEY_PASSWORD_FILE_PATH=$(pwd)/ClientKeyPassword.json
CERT_PATH=$(pwd)/ca.pem
CLIENT_KEY_FILE_PATH=$(pwd)/ClientKeyContent.json

Expand Down
1 change: 1 addition & 0 deletions confidential-data-hub/kms/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod kbs;
#[derive(AsRefStr, EnumString)]
pub enum DecryptorProvider {
#[cfg(feature = "aliyun")]
#[strum(ascii_case_insensitive)]
Aliyun,
}

Expand Down
Loading