-
Notifications
You must be signed in to change notification settings - Fork 18
feat: add a encrypt_secret helper function #279
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3e9b0de
Add encrypt_secret helper function
awalker4 6009134
Add encryption tests
awalker4 49eb79d
Throw an error if wrong key type is used
awalker4 d9a6b4c
Some linter cleanup
awalker4 558c1a5
Remove extra lines
awalker4 eec2a77
Merge branch 'main' into feat/secret-encryption
awalker4 b945912
Use the correct max size for RSA
awalker4 f7a1625
Fix lint error
awalker4 a316efc
Merge branch 'main' into feat/secret-encryption
awalker4 89b75b5
Remove a comment
awalker4 68aa549
Add decrypt_secret for reference
awalker4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,124 @@ | ||
| from cryptography import x509 | ||
| from cryptography.hazmat.primitives import serialization, hashes | ||
| from cryptography.hazmat.primitives.asymmetric import padding, rsa | ||
| from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes | ||
| from cryptography.hazmat.backends import default_backend | ||
| import os | ||
| import base64 | ||
| from typing import Optional | ||
|
|
||
| import pytest | ||
|
|
||
| from unstructured_client import UnstructuredClient | ||
|
|
||
| @pytest.fixture | ||
| def rsa_key_pair(): | ||
| private_key = rsa.generate_private_key( | ||
| public_exponent=65537, | ||
| key_size=2048, | ||
| backend=default_backend() | ||
| ) | ||
| public_key = private_key.public_key() | ||
|
|
||
| private_key_pem = private_key.private_bytes( | ||
| encoding=serialization.Encoding.PEM, | ||
| format=serialization.PrivateFormat.TraditionalOpenSSL, | ||
| encryption_algorithm=serialization.NoEncryption() | ||
| ).decode('utf-8') | ||
|
|
||
| public_key_pem = public_key.public_bytes( | ||
| encoding=serialization.Encoding.PEM, | ||
| format=serialization.PublicFormat.SubjectPublicKeyInfo | ||
| ).decode('utf-8') | ||
|
|
||
| return private_key_pem, public_key_pem | ||
|
|
||
|
|
||
| def decrypt_secret( | ||
| private_key_pem: str, | ||
| encrypted_value: str, | ||
| type: str, | ||
| encrypted_aes_key: str, | ||
| aes_iv: str, | ||
| ) -> str: | ||
| private_key = serialization.load_pem_private_key( | ||
| private_key_pem.encode('utf-8'), | ||
| password=None, | ||
| backend=default_backend() | ||
| ) | ||
|
|
||
| if type == 'rsa': | ||
| ciphertext = base64.b64decode(encrypted_value) | ||
| plaintext = private_key.decrypt( | ||
| ciphertext, | ||
| padding.OAEP( | ||
| mgf=padding.MGF1(algorithm=hashes.SHA256()), | ||
| algorithm=hashes.SHA256(), | ||
| label=None | ||
| ) | ||
| ) | ||
| return plaintext.decode('utf-8') | ||
| else: | ||
| encrypted_aes_key = base64.b64decode(encrypted_aes_key) | ||
| iv = base64.b64decode(aes_iv) | ||
| ciphertext = base64.b64decode(encrypted_value) | ||
|
|
||
| aes_key = private_key.decrypt( | ||
| encrypted_aes_key, | ||
| padding.OAEP( | ||
| mgf=padding.MGF1(algorithm=hashes.SHA256()), | ||
| algorithm=hashes.SHA256(), | ||
| label=None | ||
| ) | ||
| ) | ||
| cipher = Cipher( | ||
| algorithms.AES(aes_key), | ||
| modes.CFB(iv), | ||
| ) | ||
| decryptor = cipher.decryptor() | ||
| plaintext = decryptor.update(ciphertext) + decryptor.finalize() | ||
| return plaintext.decode('utf-8') | ||
|
|
||
|
|
||
| def test_encrypt_rsa(rsa_key_pair): | ||
| private_key_pem, public_key_pem = rsa_key_pair | ||
|
|
||
| client = UnstructuredClient() | ||
|
|
||
| plaintext = "This is a secret message." | ||
|
|
||
| secret_obj = client.users.encrypt_secret(public_key_pem, plaintext) | ||
|
|
||
| # A short payload should use direct RSA encryption | ||
| assert secret_obj["type"] == 'rsa' | ||
|
|
||
| decrypted_text = decrypt_secret( | ||
| private_key_pem, | ||
| secret_obj["encrypted_value"], | ||
| secret_obj["type"], | ||
| "", | ||
| "", | ||
| ) | ||
| assert decrypted_text == plaintext | ||
|
|
||
|
|
||
| def test_encrypt_rsa_aes(rsa_key_pair): | ||
| private_key_pem, public_key_pem = rsa_key_pair | ||
|
|
||
| client = UnstructuredClient() | ||
|
|
||
| plaintext = "This is a secret message." * 100 | ||
|
|
||
| secret_obj = client.users.encrypt_secret(public_key_pem, plaintext) | ||
|
|
||
| # A longer payload uses hybrid RSA-AES encryption | ||
| assert secret_obj["type"] == 'rsa_aes' | ||
|
|
||
| decrypted_text = decrypt_secret( | ||
| private_key_pem, | ||
| secret_obj["encrypted_value"], | ||
| secret_obj["type"], | ||
| secret_obj["encrypted_aes_key"], | ||
| secret_obj["aes_iv"], | ||
| ) | ||
| assert decrypted_text == plaintext |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.