Skip to content

Commit 2a62292

Browse files
committed
cdh/kms: add aliyun KMS support for GetPublicKey
Signed-off-by: Xynnn007 <[email protected]>
1 parent c11afc2 commit 2a62292

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

confidential-data-hub/kms/src/plugins/aliyun/client.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ use serde_json::Value;
1818
use sha2::{Digest, Sha256};
1919
use tokio::fs;
2020

21-
use crate::plugins::aliyun::client::dkms_api::{DecryptRequest, EncryptRequest};
21+
use crate::plugins::aliyun::client::dkms_api::{
22+
DecryptRequest, EncryptRequest, GetPublicKeyRequest,
23+
};
2224
use crate::plugins::_IN_GUEST_DEFAULT_KEY_PATH;
23-
use crate::{Annotations, Decrypter, Encrypter, ProviderSettings};
25+
use crate::{Annotations, Decrypter, Encrypter, ProviderSettings, PubkeyProvider};
2426
use crate::{Error, Result};
2527

2628
use super::annotations::{AliAnnotations, AliProviderSettings};
@@ -222,6 +224,40 @@ impl Decrypter for AliyunKmsClient {
222224
}
223225
}
224226

227+
#[async_trait]
228+
impl PubkeyProvider for AliyunKmsClient {
229+
/// a typical key id of aliyun KMS is like
230+
/// `key-shh65012xxxmpi4oxtxxx`
231+
async fn get_public_key(&mut self, key_id: &str) -> Result<Vec<u8>> {
232+
let get_public_key_request = GetPublicKeyRequest {
233+
key_id: key_id.into(),
234+
};
235+
let mut body = Vec::new();
236+
get_public_key_request.encode(&mut body).map_err(|e| {
237+
Error::AliyunKmsError(format!(
238+
"encode get public key request using protobuf failed: {e}"
239+
))
240+
})?;
241+
let headers = self.build_headers("GetPublicKey", &body).map_err(|e| {
242+
Error::AliyunKmsError(format!(
243+
"build get public key request http header failed: {e}"
244+
))
245+
})?;
246+
247+
let res = self
248+
.do_request(body, headers)
249+
.await
250+
.map_err(|e| Error::AliyunKmsError(format!("do request to kms server failed: {e}")))?;
251+
252+
let decrypt_response = dkms_api::GetPublicKeyResponse::decode(&res[..]).map_err(|e| {
253+
Error::AliyunKmsError(format!(
254+
"decode decrypt response using protobuf failed: {e}"
255+
))
256+
})?;
257+
Ok(decrypt_response.public_key.into())
258+
}
259+
}
260+
225261
impl AliyunKmsClient {
226262
const API_VERSION: &str = "dkms-gcs-0.2";
227263
const SIGNATURE_METHOD: &str = "RSA_PKCS1_SHA_256";

confidential-data-hub/kms/src/plugins/aliyun/protobuf/dkms_api.proto

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ message DecryptResponse {
3737
string PaddingMode = 5;
3838
}
3939

40+
message GetPublicKeyRequest {
41+
string KeyId = 1;
42+
}
43+
44+
message GetPublicKeyResponse {
45+
string KeyId = 1;
46+
string PublicKey = 2;
47+
string RequestId = 3;
48+
}
49+
4050
message Error {
4151
int32 StatusCode = 1;
4252
string ErrorCode = 2;

confidential-data-hub/kms/src/plugins/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ pub enum PublicKeyProvider {
5959
#[cfg(feature = "kbs")]
6060
#[strum(ascii_case_insensitive)]
6161
Kbs,
62+
#[cfg(feature = "aliyun")]
63+
#[strum(ascii_case_insensitive)]
64+
Aliyun,
6265
}
6366

6467
/// Create a new [`PubkeyProvider`] by given provider name
@@ -69,13 +72,18 @@ async fn new_public_key_provider(provider_name: &str) -> Result<Box<dyn PubkeyPr
6972
PublicKeyProvider::Kbs => {
7073
Ok(Box::new(kbs::KbcClient::new().await?) as Box<dyn PubkeyProvider>)
7174
}
75+
#[cfg(feature = "aliyun")]
76+
PublicKeyProvider::Aliyun => Ok(Box::new(
77+
aliyun::AliyunKmsClient::from_provider_settings(&ProviderSettings::default()).await?,
78+
) as Box<dyn PubkeyProvider>),
7279
}
7380
}
7481

7582
/// Get the public key due to the given `key_id`.
76-
/// For example:
83+
/// For example `key_id`:
7784
///
78-
/// public key from KBS: `kbs:///default/key/1`
85+
/// - KBS: `kbs:///default/key/1`
86+
/// - Aliyun KMS: `aliyun://key-shh65012626mpi4oxxxxx`
7987
pub async fn get_public_key(key_id: &str) -> Result<Vec<u8>> {
8088
let (provider, keyid) = key_id
8189
.split_once("://")

0 commit comments

Comments
 (0)