Skip to content

Commit

Permalink
feat: improved custom components handling (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronsteers authored Mar 3, 2025
1 parent 61a9ee3 commit 40e5002
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions airbyte_cdk/sources/declarative/parsers/custom_code_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self) -> None:
def _hash_text(input_text: str, hash_type: str = "md5") -> str:
"""Return the hash of the input text using the specified hash type."""
if not input_text:
raise ValueError("Input text cannot be empty.")
raise ValueError("Hash input text cannot be empty.")

hash_object = CHECKSUM_FUNCTIONS[hash_type]()
hash_object.update(input_text.encode())
Expand All @@ -68,6 +68,10 @@ def validate_python_code(
Currently we fail if no checksums are provided, although this may change in the future.
"""
if not code_text:
# No code provided, nothing to validate.
return

if not checksums:
raise ValueError(f"A checksum is required to validate the code. Received: {checksums}")

Expand All @@ -77,8 +81,18 @@ def validate_python_code(
f"Unsupported checksum type: {checksum_type}. Supported checksum types are: {CHECKSUM_FUNCTIONS.keys()}"
)

if _hash_text(code_text, checksum_type) != checksum:
raise AirbyteCodeTamperedError(f"{checksum_type} checksum does not match.")
calculated_checksum = _hash_text(code_text, checksum_type)
if calculated_checksum != checksum:
raise AirbyteCodeTamperedError(
f"{checksum_type} checksum does not match."
+ str(
{
"expected_checksum": checksum,
"actual_checksum": calculated_checksum,
"code_text": code_text,
}
),
)


def get_registered_components_module(
Expand All @@ -94,7 +108,7 @@ def get_registered_components_module(
Returns `None` if no components is provided and the `components` module is not found.
"""
if config and INJECTED_COMPONENTS_PY in config:
if config and config.get(INJECTED_COMPONENTS_PY, None):
if not custom_code_execution_permitted():
raise AirbyteCustomCodeNotPermittedError

Expand Down

0 comments on commit 40e5002

Please sign in to comment.