Skip to content

feat(crypto): decrypt secret objects #482

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/apify/_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import base64
import hashlib
import hmac
import json
import string
from typing import Any

Expand Down Expand Up @@ -154,6 +155,23 @@ def decrypt_input_secrets(private_key: rsa.RSAPrivateKey, input_data: Any) -> An
encrypted_value,
private_key=private_key,
)
# TODO reuse the decryption logic for string and objects
elif isinstance(value, dict) and isinstance(value.get("secret"), str):
match = ENCRYPTED_INPUT_VALUE_REGEXP.fullmatch(value["secret"])
if match:
encrypted_password = match.group(1)
encrypted_value = match.group(2)
try:
decrypted_json_str = private_decrypt(
encrypted_password,
encrypted_value,
private_key=private_key,
)
input_data[key] = json.loads(decrypted_json_str)
except Exception as err:
raise ValueError(
f'The input field "{key}" could not be parsed as JSON after decryption: {err}'
)

return input_data

Expand Down
Loading