Skip to content

Commit 42a99c2

Browse files
authored
add python-sdk in cryptography (#1093)
Signed-off-by: KentHsu <[email protected]>
1 parent c32f421 commit 42a99c2

File tree

6 files changed

+186
-0
lines changed

6 files changed

+186
-0
lines changed

cryptography/python/sdk/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Dapr cryptography (Dapr SDK)
2+
3+
In this quickstart, you'll create an application that encrypts, and then decrypts, data using the Dapr cryptography APIs (high-level). We will:
4+
5+
- Encrypt and then decrypt a short string, reading the result in-memory
6+
- Encrypt and then decrypt a large file, storing the encrypted and decrypted data to files
7+
8+
Visit the documentation to learn more about the [Cryptography building block](https://v1-11.docs.dapr.io/developing-applications/building-blocks/cryptography/) in Dapr.
9+
10+
> **Note:** This example uses the Dapr SDK. Using the Dapr SDK, which leverages gRPC internally, is **strongly** recommended when using the high-level cryptography APIs (to encrypt and decrypt messages).
11+
12+
This quickstart includes one application:
13+
14+
- Python application `crypto-quickstart`
15+
16+
### Run Python service with Dapr
17+
18+
> In order to run this sample, make sure that OpenSSL is available on your system.
19+
20+
1. Navigate into the folder with the source code:
21+
22+
<!-- STEP
23+
name: Navigate into folder
24+
expected_stdout_lines:
25+
expected_stderr_lines:
26+
-->
27+
28+
```bash
29+
cd ./crypto-quickstart
30+
pip3 install -r requirements.txt
31+
```
32+
33+
<!-- END_STEP -->
34+
35+
2. This sample requires a private RSA key and a 256-bit symmetric (AES) key. We will generate them using OpenSSL:
36+
37+
<!-- STEP
38+
name: Generate keys
39+
working_dir: crypto-quickstart
40+
expected_stdout_lines:
41+
expected_stderr_lines:
42+
-->
43+
44+
```bash
45+
mkdir -p keys
46+
# Generate a private RSA key, 4096-bit keys
47+
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out keys/rsa-private-key.pem
48+
# Generate a 256-bit key for AES
49+
openssl rand -out keys/symmetric-key-256 32
50+
```
51+
52+
<!-- END_STEP -->
53+
54+
3. Run the Python service app with Dapr:
55+
56+
<!-- STEP
57+
name: Run order-processor service
58+
working_dir: crypto-quickstart
59+
expected_stdout_lines:
60+
- '== APP == Encrypted the message, got 856 bytes'
61+
- '== APP == Decrypted the message, got 24 bytes'
62+
- '== APP == The secret is "passw0rd"'
63+
- '== APP == Wrote decrypted data to encrypted.out'
64+
- '== APP == Wrote decrypted data to decrypted.out.jpg'
65+
- "Exited App successfully"
66+
expected_stderr_lines:
67+
output_match_mode: substring
68+
-->
69+
70+
```bash
71+
dapr run --app-id crypto-quickstart --resources-path ../../../components/ -- python3 app.py
72+
```
73+
74+
<!-- END_STEP -->
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Output files
2+
encrypted.out
3+
decrypted.out.jpg
4+
5+
# Generated keys
6+
keys/
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from dapr.clients import DaprClient
2+
from dapr.clients.grpc._crypto import EncryptOptions, DecryptOptions
3+
4+
# Name of the crypto component to use
5+
CRYPTO_COMPONENT_NAME = 'localstorage'
6+
# Name of the RSA private key to use
7+
RSA_KEY_NAME = 'rsa-private-key.pem'
8+
# Name of the symmetric (AES) key to use
9+
SYMMETRIC_KEY_NAME = 'symmetric-key-256'
10+
11+
12+
def main():
13+
print('Running gRPC client synchronous API')
14+
15+
with DaprClient() as dapr:
16+
# Step 1: encrypt a string using the RSA key, then decrypt it and show the output in the terminal
17+
print('Running encrypt/decrypt operation on string')
18+
encrypt_decrypt_string(dapr)
19+
20+
# Step 2: encrypt a large file and then decrypt it, using the AES key
21+
print('Running encrypt/decrypt operation on file')
22+
encrypt_decrypt_file(dapr)
23+
24+
25+
def encrypt_decrypt_string(dapr: DaprClient):
26+
message = 'The secret is "passw0rd"'
27+
28+
# Encrypt the message
29+
resp = dapr.encrypt(
30+
data=message.encode(),
31+
options=EncryptOptions(
32+
component_name=CRYPTO_COMPONENT_NAME,
33+
key_name=RSA_KEY_NAME,
34+
key_wrap_algorithm='RSA',
35+
),
36+
)
37+
38+
# The method returns a readable stream, which we read in full in memory
39+
encrypt_bytes = resp.read()
40+
print(f'Encrypted the message, got {len(encrypt_bytes)} bytes')
41+
42+
# Decrypt the encrypted data
43+
resp = dapr.decrypt(
44+
data=encrypt_bytes,
45+
options=DecryptOptions(
46+
component_name=CRYPTO_COMPONENT_NAME,
47+
key_name=RSA_KEY_NAME,
48+
),
49+
)
50+
51+
# The method returns a readable stream, which we read in full in memory
52+
decrypt_bytes = resp.read()
53+
print(f'Decrypted the message, got {len(decrypt_bytes)} bytes')
54+
55+
print(decrypt_bytes.decode())
56+
assert message == decrypt_bytes.decode()
57+
58+
59+
def encrypt_decrypt_file(dapr: DaprClient):
60+
file_name = 'desert.jpg'
61+
62+
# Encrypt the file
63+
with open(file_name, 'r+b') as target_file:
64+
encrypt_stream = dapr.encrypt(
65+
data=target_file.read(),
66+
options=EncryptOptions(
67+
component_name=CRYPTO_COMPONENT_NAME,
68+
key_name=SYMMETRIC_KEY_NAME,
69+
key_wrap_algorithm='AES',
70+
),
71+
)
72+
73+
# Write the encrypted data to a file "encrypted.out"
74+
with open('encrypted.out', 'w+b') as encrypted_file:
75+
encrypted_file.write(encrypt_stream.read())
76+
print('Wrote encrypted data to encrypted.out')
77+
78+
# Decrypt the encrypted data
79+
with open('encrypted.out', 'r+b') as encrypted_file:
80+
decrypt_stream = dapr.decrypt(
81+
data=encrypted_file.read(),
82+
options=DecryptOptions(
83+
component_name=CRYPTO_COMPONENT_NAME,
84+
key_name=SYMMETRIC_KEY_NAME,
85+
),
86+
)
87+
88+
# Write the decrypted data to a file "decrypted.out.jpg"
89+
with open('decrypted.out.jpg', 'w+b') as decrypted_file:
90+
decrypted_file.write(decrypt_stream.read())
91+
print('Wrote decrypted data to decrypted.out.jpg')
92+
93+
94+
if __name__ == '__main__':
95+
main()
5.65 MB
Loading
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dapr>=1.13.0a,<1.14.0
2+
typing-extensions

cryptography/python/sdk/makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
include ../../../docker.mk
2+
include ../../../validate.mk
3+
4+
# Remove generated files
5+
.PHONY: clean
6+
clean:
7+
-rm -r crypto-quickstart/keys
8+
-rm crypto-quickstart/encrypted.out
9+
-rm crypto-quickstart/decrypted.out.jpg

0 commit comments

Comments
 (0)