-
Notifications
You must be signed in to change notification settings - Fork 64
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
Implement Cryptography API #133
Comments
@mikeee Its a pretty basic example but I am not getting back the decode response. use tonic::{Request, Status};
use tonic::codegen::tokio_stream;
use crate::Client;
use crate::client::TonicClient;
use crate::dapr::dapr::proto::common::v1::StreamPayload;
use crate::dapr::dapr::proto::runtime::v1::{
DecryptRequest, DecryptRequestOptions, EncryptRequest, EncryptRequestOptions,
};
impl Client<TonicClient> {
pub async fn encrypt(
&mut self,
payload: String,
request_options: EncryptRequestOptions,
) -> Result<StreamPayload, Status> {
let stream_payload = StreamPayload {
data: payload.as_bytes().to_vec(),
seq: 0,
};
let request = EncryptRequest {
options: Some(request_options),
payload: Some(stream_payload),
};
let request = Request::new(tokio_stream::iter([request]));
let stream = self.0.encrypt_alpha1(request).await?;
Ok(stream
.into_inner()
.message()
.await?
.unwrap()
.payload
.unwrap())
}
pub async fn decrypt<T>(
&mut self,
encrypted: T,
options: DecryptRequestOptions,
) -> Result<Vec<u8>, Status>
where
T: Into<Vec<u8>>,
{
let payload = StreamPayload {
data: encrypted.into(),
seq: 0,
};
let request = DecryptRequest {
options: Some(options),
payload: Some(payload),
};
let request = Request::new(tokio_stream::iter([request]));
let stream = self.0.decrypt_alpha1(request).await?;
Ok(stream
.into_inner()
.message()
.await?
.unwrap()
.payload
.unwrap()
.data)
}
} Sample output:
Sample code use tokio::time::sleep;
use dapr::dapr::dapr::proto::runtime::v1::{DecryptRequestOptions, EncryptRequestOptions};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// TODO: Handle this issue in the sdk
// Introduce delay so that dapr grpc port is assigned before app tries to connect
sleep(std::time::Duration::new(2, 0)).await;
// Get the Dapr port and create a connection
let port: u16 = std::env::var("DAPR_GRPC_PORT")?.parse()?;
let addr = format!("https://127.0.0.1:{}", port);
// Create the client
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr).await?;
let encrypted = client
.encrypt(
"Test".to_string(),
EncryptRequestOptions {
component_name: "localstorage".to_string(),
key_name: "rsa-private-key.pem".to_string(),
key_wrap_algorithm: "RSA".to_string(),
data_encryption_cipher: "aes-gcm".to_string(),
omit_decryption_key_name: false,
decryption_key_name: "rsa-private-key.pem".to_string(),
},
)
.await
.unwrap();
println!("{}", String::from_utf8(encrypted.clone().data).unwrap());
let decrypted = client
.decrypt(
encrypted.clone().data,
DecryptRequestOptions {
component_name: "localstorage".to_string(),
key_name: "rsa-private-key.pem".to_string(),
},
)
.await
.unwrap();
assert_eq!("Test".to_string(), String::from_utf8(decrypted).unwrap());
Ok(())
} |
@zedgell I believe you'll need to implement bidirectional streams in the client methods which you can then use to send/receive and await responses. |
I have done that base off the tonic example here use futures::StreamExt;
use tonic::codegen::tokio_stream;
use tonic::{Request, Status};
use crate::client::TonicClient;
use crate::dapr::dapr::proto::common::v1::StreamPayload;
use crate::dapr::dapr::proto::runtime::v1::{
DecryptRequest, DecryptRequestOptions, EncryptRequest, EncryptRequestOptions,
};
use crate::Client;
impl Client<TonicClient> {
pub async fn encrypt(
&mut self,
payload: String,
request_options: EncryptRequestOptions,
) -> Result<StreamPayload, Status> {
let stream_payload = StreamPayload {
data: payload.as_bytes().to_vec(),
seq: 0,
};
let request = EncryptRequest {
options: Some(request_options),
payload: Some(stream_payload),
};
let request = Request::new(tokio_stream::iter([request]));
let stream = self.0.encrypt_alpha1(request).await?;
Ok(stream
.into_inner()
.take(1)
.next()
.await
.unwrap()
.unwrap()
.payload
.unwrap())
}
pub async fn decrypt<T>(
&mut self,
encrypted: T,
options: DecryptRequestOptions,
) -> Result<Vec<u8>, Status>
where
T: Into<Vec<u8>>,
{
let payload = StreamPayload {
data: encrypted.into(),
seq: 0,
};
let request = DecryptRequest {
options: Some(options),
payload: Some(payload),
};
let request = Request::new(tokio_stream::iter([request]));
let stream = self.0.decrypt_alpha1(request).await?;
Ok(stream
.into_inner()
.take(1)
.next()
.await
.unwrap()
.unwrap()
.payload
.unwrap()
.data)
}
} |
This block client.rs#L34 is similar to what the methods should resemble |
|
@mikeee scratch the above. I see what I got wrong. I will get that a PR made come morning. |
Great stuff, have a great evening! |
Describe the feature/proposal
Implement access to the cryptography API which will include the methods:
Reference:
Release Note
RELEASE NOTE: ADD Cryptography API support to the client
The text was updated successfully, but these errors were encountered: