Skip to content

Commit 153e96a

Browse files
committed
cdh/kms: add aliyun KMS support for GetPublicKey
Signed-off-by: Xynnn007 <[email protected]>
1 parent 640e176 commit 153e96a

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};
@@ -223,6 +225,40 @@ impl Decrypter for AliyunKmsClient {
223225
}
224226
}
225227

228+
#[async_trait]
229+
impl PubkeyProvider for AliyunKmsClient {
230+
/// a typical key id of aliyun KMS is like
231+
/// `key-shh65012xxxmpi4oxtxxx`
232+
async fn get_public_key(&mut self, key_id: &str) -> Result<Vec<u8>> {
233+
let get_public_key_request = GetPublicKeyRequest {
234+
key_id: key_id.into(),
235+
};
236+
let mut body = Vec::new();
237+
get_public_key_request.encode(&mut body).map_err(|e| {
238+
Error::AliyunKmsError(format!(
239+
"encode get public key request using protobuf failed: {e}"
240+
))
241+
})?;
242+
let headers = self.build_headers("GetPublicKey", &body).map_err(|e| {
243+
Error::AliyunKmsError(format!(
244+
"build get public key request http header failed: {e}"
245+
))
246+
})?;
247+
248+
let res = self
249+
.do_request(body, headers)
250+
.await
251+
.map_err(|e| Error::AliyunKmsError(format!("do request to kms server failed: {e}")))?;
252+
253+
let decrypt_response = dkms_api::GetPublicKeyResponse::decode(&res[..]).map_err(|e| {
254+
Error::AliyunKmsError(format!(
255+
"decode decrypt response using protobuf failed: {e}"
256+
))
257+
})?;
258+
Ok(decrypt_response.public_key.into())
259+
}
260+
}
261+
226262
impl AliyunKmsClient {
227263
const API_VERSION: &str = "dkms-gcs-0.2";
228264
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
@@ -60,6 +60,9 @@ pub enum PublicKeyProvider {
6060
#[cfg(feature = "kbs")]
6161
#[strum(ascii_case_insensitive)]
6262
Kbs,
63+
#[cfg(feature = "aliyun")]
64+
#[strum(ascii_case_insensitive)]
65+
Aliyun,
6366
}
6467

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

7683
/// Get the public key due to the given `key_id`.
77-
/// For example:
84+
/// For example `key_id`:
7885
///
79-
/// public key from KBS: `kbs:///default/key/1`
86+
/// - KBS: `kbs:///default/key/1`
87+
/// - Aliyun KMS: `aliyun://key-shh65012626mpi4oxxxxx`
8088
pub async fn get_public_key(key_id: &str) -> Result<Vec<u8>> {
8189
let (provider, keyid) = key_id
8290
.split_once("://")

0 commit comments

Comments
 (0)