forked from dapr/rust-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dapr#144 from zedgell/feature/crypto
Implement Cryptography API
- Loading branch information
Showing
9 changed files
with
318 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Crypto Example | ||
|
||
This is a simple example that demonstrates Dapr's Cryptography capabilities. | ||
|
||
> **Note:** Make sure to use latest version of proto bindings. | ||
## Running | ||
|
||
To run this example: | ||
|
||
1. Generate keys in examples/crypto/keys directory: | ||
<!-- STEP | ||
name: Generate keys | ||
background: false | ||
sleep: 5 | ||
timeout_seconds: 30 | ||
--> | ||
```bash | ||
mkdir -p keys | ||
# Generate a private RSA key, 4096-bit keys | ||
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out keys/rsa-private-key.pem | ||
# Generate a 256-bit key for AES | ||
openssl rand -out keys/symmetric-key-256 32 | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
2. Run the multi-app run template: | ||
|
||
<!-- STEP | ||
name: Run multi-app | ||
output_match_mode: substring | ||
match_order: none | ||
expected_stdout_lines: | ||
- '== APP - crypto-example == Successfully Decrypted String' | ||
- '== APP - crypto-example == Successfully Decrypted Image' | ||
background: true | ||
sleep: 30 | ||
timeout_seconds: 90 | ||
--> | ||
|
||
```bash | ||
dapr run -f . | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
2. Stop with `ctrl + c` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: localstorage | ||
spec: | ||
type: crypto.dapr.localstorage | ||
version: v1 | ||
metadata: | ||
- name: path | ||
# Path is relative to the folder where the example is located | ||
value: ./keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version: 1 | ||
common: | ||
daprdLogDestination: console | ||
apps: | ||
- appID: crypto-example | ||
appDirPath: ./ | ||
daprGRPCPort: 35002 | ||
logLevel: debug | ||
command: [ "cargo", "run", "--example", "crypto" ] | ||
resourcesPath: ./components |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use std::fs; | ||
|
||
use tokio::fs::File; | ||
use tokio::time::sleep; | ||
|
||
use dapr::client::ReaderStream; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
sleep(std::time::Duration::new(2, 0)).await; | ||
let port: u16 = std::env::var("DAPR_GRPC_PORT")?.parse()?; | ||
let addr = format!("https://127.0.0.1:{}", port); | ||
|
||
let mut client = dapr::Client::<dapr::client::TonicClient>::connect(addr).await?; | ||
|
||
let encrypted = client | ||
.encrypt( | ||
ReaderStream::new("Test".as_bytes()), | ||
dapr::client::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(); | ||
|
||
let decrypted = client | ||
.decrypt( | ||
encrypted, | ||
dapr::client::DecryptRequestOptions { | ||
component_name: "localstorage".to_string(), | ||
key_name: "rsa-private-key.pem".to_string(), | ||
}, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(String::from_utf8(decrypted).unwrap().as_str(), "Test"); | ||
|
||
println!("Successfully Decrypted String"); | ||
|
||
let image = File::open("./image.png").await.unwrap(); | ||
|
||
let encrypted = client | ||
.encrypt( | ||
ReaderStream::new(image), | ||
dapr::client::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(); | ||
|
||
let decrypted = client | ||
.decrypt( | ||
encrypted, | ||
dapr::client::DecryptRequestOptions { | ||
component_name: "localstorage".to_string(), | ||
key_name: "rsa-private-key.pem".to_string(), | ||
}, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let image = fs::read("./image.png").unwrap(); | ||
|
||
assert_eq!(decrypted, image); | ||
|
||
println!("Successfully Decrypted Image"); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.