|
15 | 15 | from apify_shared.utils import ignore_docs
|
16 | 16 | from crawlee._utils.crypto import crypto_random_object_id
|
17 | 17 |
|
18 |
| -from apify._consts import ENCRYPTED_INPUT_VALUE_REGEXP |
| 18 | +from apify._consts import ENCRYPTED_INPUT_VALUE_REGEXP, ENCRYPTED_STRING_VALUE_PREFIX, ENCRYPTED_JSON_VALUE_PREFIX |
19 | 19 |
|
20 | 20 | ENCRYPTION_KEY_LENGTH = 32
|
21 | 21 | ENCRYPTION_IV_LENGTH = 16
|
@@ -148,30 +148,19 @@ def decrypt_input_secrets(private_key: rsa.RSAPrivateKey, input_data: Any) -> An
|
148 | 148 | if isinstance(value, str):
|
149 | 149 | match = ENCRYPTED_INPUT_VALUE_REGEXP.fullmatch(value)
|
150 | 150 | if match:
|
151 |
| - encrypted_password = match.group(1) |
152 |
| - encrypted_value = match.group(2) |
153 |
| - input_data[key] = private_decrypt( |
| 151 | + prefix = match.group(1) |
| 152 | + encrypted_password = match.group(3) |
| 153 | + encrypted_value = match.group(4) |
| 154 | + decrypted_value = private_decrypt( |
154 | 155 | encrypted_password,
|
155 | 156 | encrypted_value,
|
156 | 157 | private_key=private_key,
|
157 | 158 | )
|
158 |
| - # TODO reuse the decryption logic for string and objects |
159 |
| - elif isinstance(value, dict) and isinstance(value.get("secret"), str): |
160 |
| - match = ENCRYPTED_INPUT_VALUE_REGEXP.fullmatch(value["secret"]) |
161 |
| - if match: |
162 |
| - encrypted_password = match.group(1) |
163 |
| - encrypted_value = match.group(2) |
164 |
| - try: |
165 |
| - decrypted_json_str = private_decrypt( |
166 |
| - encrypted_password, |
167 |
| - encrypted_value, |
168 |
| - private_key=private_key, |
169 |
| - ) |
170 |
| - input_data[key] = json.loads(decrypted_json_str) |
171 |
| - except Exception as err: |
172 |
| - raise ValueError( |
173 |
| - f'The input field "{key}" could not be parsed as JSON after decryption: {err}' |
174 |
| - ) |
| 159 | + |
| 160 | + if prefix == ENCRYPTED_STRING_VALUE_PREFIX: |
| 161 | + input_data[key] = decrypted_value |
| 162 | + elif prefix == ENCRYPTED_JSON_VALUE_PREFIX: |
| 163 | + input_data[key] = json.loads(decrypted_value) |
175 | 164 |
|
176 | 165 | return input_data
|
177 | 166 |
|
|
0 commit comments