From 99bdbe587158e9a8758770073badb8f7c42293dc Mon Sep 17 00:00:00 2001 From: Florentin Labelle Date: Thu, 29 Oct 2026 14:06:48 +0100 Subject: [PATCH 1/6] feat: initialize SCA reachability in the Lambda wrapper --- datadog_lambda/wrapper.py | 12 ++++++++++++ tests/test_wrapper.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/datadog_lambda/wrapper.py b/datadog_lambda/wrapper.py index 73057983..83e054c2 100644 --- a/datadog_lambda/wrapper.py +++ b/datadog_lambda/wrapper.py @@ -67,6 +67,18 @@ start() +if config.sca_enabled: + try: + from ddtrace.internal.sca.product import start as start_sca + from ddtrace.internal.sca.product import post_preload as instrument_sca + + # The layer patches integrations before loading the wrapper. It does not + # use ddtrace.auto, so run both SCA startup phases before importing user code. + start_sca() + instrument_sca() + except Exception: + logger.debug("Failed to initialize SCA reachability", exc_info=True) + profiler = None if config.profiling_enabled: try: diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py index 26a53b0c..ea495917 100644 --- a/tests/test_wrapper.py +++ b/tests/test_wrapper.py @@ -23,6 +23,38 @@ MAX_UINT_64BITS = (1 << 64) - 1 +@pytest.mark.parametrize("sca_enabled", [False, True]) +def test_sca_startup(sca_enabled, monkeypatch): + product = MagicMock() + monkeypatch.setattr("datadog_lambda.config.Config.sca_enabled", sca_enabled) + monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", product) + + importlib.reload(wrapper) + + assert product.mock_calls == ( + [call.start(), call.post_preload()] if sca_enabled else [] + ) + + +def test_sca_startup_with_older_tracer(monkeypatch): + monkeypatch.setattr("datadog_lambda.config.Config.sca_enabled", True) + monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", None) + + importlib.reload(wrapper) + + +def test_sca_startup_failure_preserves_handler(monkeypatch): + product = MagicMock() + product.start.side_effect = RuntimeError("SCA startup failed") + monkeypatch.setattr("datadog_lambda.config.Config.sca_enabled", True) + monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", product) + + importlib.reload(wrapper) + + product.post_preload.assert_not_called() + assert callable(wrapper.datadog_lambda_wrapper) + + class TestDatadogLambdaWrapper(unittest.TestCase): def setUp(self): # Force @datadog_lambda_wrapper to always create a real From 25eb00b5b8d6e7ea5882921b097fe279b498fbc9 Mon Sep 17 00:00:00 2001 From: Florentin Labelle Date: Thu, 29 Oct 2026 14:25:37 +0100 Subject: [PATCH 2/6] test: align SCA startup coverage with other products --- tests/test_wrapper.py | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py index ea495917..0bcede0c 100644 --- a/tests/test_wrapper.py +++ b/tests/test_wrapper.py @@ -23,38 +23,23 @@ MAX_UINT_64BITS = (1 << 64) - 1 -@pytest.mark.parametrize("sca_enabled", [False, True]) -def test_sca_startup(sca_enabled, monkeypatch): +@patch("datadog_lambda.config.Config.sca_enabled", True) +def test_sca_enabled(monkeypatch): product = MagicMock() - monkeypatch.setattr("datadog_lambda.config.Config.sca_enabled", sca_enabled) monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", product) importlib.reload(wrapper) - assert product.mock_calls == ( - [call.start(), call.post_preload()] if sca_enabled else [] - ) + assert product.mock_calls == [call.start(), call.post_preload()] -def test_sca_startup_with_older_tracer(monkeypatch): - monkeypatch.setattr("datadog_lambda.config.Config.sca_enabled", True) +@patch("datadog_lambda.config.Config.sca_enabled", True) +def test_sca_import_errors_caught(monkeypatch): monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", None) importlib.reload(wrapper) -def test_sca_startup_failure_preserves_handler(monkeypatch): - product = MagicMock() - product.start.side_effect = RuntimeError("SCA startup failed") - monkeypatch.setattr("datadog_lambda.config.Config.sca_enabled", True) - monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", product) - - importlib.reload(wrapper) - - product.post_preload.assert_not_called() - assert callable(wrapper.datadog_lambda_wrapper) - - class TestDatadogLambdaWrapper(unittest.TestCase): def setUp(self): # Force @datadog_lambda_wrapper to always create a real From 4dafd95121734c18718e11977fe0ff00da7059a5 Mon Sep 17 00:00:00 2001 From: Florentin Labelle Date: Thu, 29 Oct 2026 14:29:35 +0100 Subject: [PATCH 3/6] fix: preserve SCA modules in Lambda layers --- Dockerfile | 1 - 1 file changed, 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 8b8f9a09..57b703f6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,7 +37,6 @@ RUN rm -rf ./python/lib/$runtime/site-packages/jsonschema/tests # Remove unsupported appsec modules RUN rm -rf \ ./python/lib/$runtime/site-packages/ddtrace/appsec/_iast \ - ./python/lib/$runtime/site-packages/ddtrace/appsec/sca \ ./python/lib/$runtime/site-packages/ddtrace/appsec/_shared # CI Visibility paths/integrations From 6844e7ef7acec3ff3bcc59f27d585391f1e76f1d Mon Sep 17 00:00:00 2001 From: Florentin Labelle Date: Mon, 16 Nov 2026 14:54:48 +0100 Subject: [PATCH 4/6] fix: start SCA before Lambda handler imports --- datadog_lambda/patch.py | 25 ++++++++++- datadog_lambda/wrapper.py | 12 ----- tests/test_patch.py | 93 ++++++++++++++++++++++++++++++++++++++- tests/test_wrapper.py | 17 ------- 4 files changed, 115 insertions(+), 32 deletions(-) diff --git a/datadog_lambda/patch.py b/datadog_lambda/patch.py index c62cb7c6..8735406b 100644 --- a/datadog_lambda/patch.py +++ b/datadog_lambda/patch.py @@ -4,6 +4,7 @@ # Copyright 2019 Datadog, Inc. import sys +import os import logging import zlib import ujson as json @@ -11,8 +12,9 @@ from wrapt import wrap_function_wrapper as wrap from wrapt.importer import when_imported from ddtrace import patch_all as patch_all_dd +from ddtrace import patch as patch_dd -from datadog_lambda.config import config +from datadog_lambda.config import config, as_bool from datadog_lambda.tracing import get_dd_trace_context from collections.abc import MutableMapping @@ -33,11 +35,30 @@ def patch_all(): # unittest and selenium are CI Visibility integrations. The layer strips # their code and ddtrace.internal.ci_visibility, so leaving them enabled # logs an import error whenever the user's code imports those modules. - patch_all_dd(unittest=False, selenium=False) + patch_modules = {"unittest": False, "selenium": False} + if config.sca_enabled: + # The Lambda integration imports the handler. Defer it until SCA is ready. + patch_modules["aws_lambda"] = False + patch_all_dd(**patch_modules) else: _patch_http() _ensure_patch_requests() + if config.sca_enabled: + try: + from ddtrace.internal.sca.product import start as start_sca + from ddtrace.internal.sca.product import post_preload as instrument_sca + + start_sca() + instrument_sca() + except Exception: + logger.debug("Failed to initialize SCA reachability", exc_info=True) + + if config.trace_enabled and as_bool( + os.environ.get("DD_TRACE_AWS_LAMBDA_ENABLED", "true") + ): + patch_dd(raise_errors=False, aws_lambda=True) + def _patch_for_integration_tests(): """ diff --git a/datadog_lambda/wrapper.py b/datadog_lambda/wrapper.py index 83e054c2..73057983 100644 --- a/datadog_lambda/wrapper.py +++ b/datadog_lambda/wrapper.py @@ -67,18 +67,6 @@ start() -if config.sca_enabled: - try: - from ddtrace.internal.sca.product import start as start_sca - from ddtrace.internal.sca.product import post_preload as instrument_sca - - # The layer patches integrations before loading the wrapper. It does not - # use ddtrace.auto, so run both SCA startup phases before importing user code. - start_sca() - instrument_sca() - except Exception: - logger.debug("Failed to initialize SCA reachability", exc_info=True) - profiler = None if config.profiling_enabled: try: diff --git a/tests/test_patch.py b/tests/test_patch.py index dec539e1..8089cb7b 100644 --- a/tests/test_patch.py +++ b/tests/test_patch.py @@ -1,7 +1,10 @@ import pytest import unittest +import os +import subprocess +import sys -from unittest.mock import patch, MagicMock +from unittest.mock import patch, MagicMock, call from datadog_lambda.patch import _patch_http, _ensure_patch_requests from datadog_lambda.constants import TraceHeader @@ -103,6 +106,94 @@ def test_patch_all_disables_ci_visibility_integrations(mock_config, mock_patch_a mock_config.trace_enabled = True mock_config.integration_test = False + mock_config.sca_enabled = False patch_all() mock_patch_all_dd.assert_called_once_with(unittest=False, selenium=False) + + +@patch("datadog_lambda.config.Config.sca_enabled", True) +@patch("datadog_lambda.patch.patch_all_dd") +@patch("datadog_lambda.patch.patch_dd") +def test_sca_enabled(mock_patch_dd, mock_patch_all_dd, monkeypatch): + from datadog_lambda.patch import patch_all + + product = MagicMock() + monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", product) + monkeypatch.delenv("DD_TRACE_AWS_LAMBDA_ENABLED", raising=False) + + patch_all() + + assert product.mock_calls == [call.start(), call.post_preload()] + mock_patch_all_dd.assert_called_once_with( + unittest=False, selenium=False, aws_lambda=False + ) + mock_patch_dd.assert_called_once_with(raise_errors=False, aws_lambda=True) + + +@pytest.mark.parametrize("failure", ["import", "start", "post_preload"]) +@patch("datadog_lambda.config.Config.sca_enabled", True) +@patch("datadog_lambda.patch.patch_all_dd") +@patch("datadog_lambda.patch.patch_dd") +def test_sca_errors_caught(mock_patch_dd, mock_patch_all_dd, monkeypatch, failure): + from datadog_lambda.patch import patch_all + + product = MagicMock() + if failure == "import": + product = None + else: + getattr(product, failure).side_effect = RuntimeError("SCA startup failed") + monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", product) + monkeypatch.delenv("DD_TRACE_AWS_LAMBDA_ENABLED", raising=False) + + patch_all() + + mock_patch_dd.assert_called_once_with(raise_errors=False, aws_lambda=True) + + +@pytest.mark.parametrize("trace_enabled", ["true", "false"]) +@pytest.mark.parametrize("lambda_enabled", ["true", "false"]) +def test_sca_starts_before_handler_import(tmp_path, trace_enabled, lambda_enabled): + (tmp_path / "startup_handler.py").write_text( + "import os\n" + "sca_ready_at_import = os.environ.get('SCA_READY') == 'true'\n" + "def handler(event, context):\n" + " return {}\n" + ) + code = """ +import os +import sys +from types import ModuleType + +product = ModuleType("ddtrace.internal.sca.product") +product.start = lambda: None +product.post_preload = lambda: os.environ.__setitem__("SCA_READY", "true") +sys.modules[product.__name__] = product + +import datadog_lambda.handler as handler + +assert handler.handler_module.sca_ready_at_import +expected_patch = os.environ["DD_TRACE_ENABLED"] == "true" and os.environ["DD_TRACE_AWS_LAMBDA_ENABLED"] == "true" +assert bool(getattr(handler.handler_module, "_datadog_patch", False)) == expected_patch +""" + env = os.environ.copy() + env.update( + { + "PYTHONPATH": os.pathsep.join([str(tmp_path), os.getcwd()]), + "AWS_LAMBDA_FUNCTION_NAME": "sca-startup-test", + "DD_LAMBDA_HANDLER": "startup_handler.handler", + "DD_APPSEC_ENABLED": "false", + "DD_APPSEC_SCA_ENABLED": "true", + "DD_TRACE_ENABLED": trace_enabled, + "DD_TRACE_AWS_LAMBDA_ENABLED": lambda_enabled, + } + ) + env.pop("SCA_READY", None) + result = subprocess.run( + [sys.executable, "-c", code], + env=env, + capture_output=True, + text=True, + timeout=30, + ) + assert result.returncode == 0, result.stdout + result.stderr diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py index 0bcede0c..26a53b0c 100644 --- a/tests/test_wrapper.py +++ b/tests/test_wrapper.py @@ -23,23 +23,6 @@ MAX_UINT_64BITS = (1 << 64) - 1 -@patch("datadog_lambda.config.Config.sca_enabled", True) -def test_sca_enabled(monkeypatch): - product = MagicMock() - monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", product) - - importlib.reload(wrapper) - - assert product.mock_calls == [call.start(), call.post_preload()] - - -@patch("datadog_lambda.config.Config.sca_enabled", True) -def test_sca_import_errors_caught(monkeypatch): - monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", None) - - importlib.reload(wrapper) - - class TestDatadogLambdaWrapper(unittest.TestCase): def setUp(self): # Force @datadog_lambda_wrapper to always create a real From 1b2ec492030582c9112d8f674fa20420b18f4034 Mon Sep 17 00:00:00 2001 From: Florentin Labelle Date: Mon, 16 Nov 2026 15:02:08 +0100 Subject: [PATCH 5/6] test: consolidate Lambda SCA startup coverage --- tests/test_patch.py | 71 +++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 48 deletions(-) diff --git a/tests/test_patch.py b/tests/test_patch.py index 8089cb7b..4ba2f065 100644 --- a/tests/test_patch.py +++ b/tests/test_patch.py @@ -112,51 +112,38 @@ def test_patch_all_disables_ci_visibility_integrations(mock_config, mock_patch_a mock_patch_all_dd.assert_called_once_with(unittest=False, selenium=False) +@pytest.mark.parametrize("failure", [None, "import", "start", "post_preload"]) @patch("datadog_lambda.config.Config.sca_enabled", True) @patch("datadog_lambda.patch.patch_all_dd") @patch("datadog_lambda.patch.patch_dd") -def test_sca_enabled(mock_patch_dd, mock_patch_all_dd, monkeypatch): - from datadog_lambda.patch import patch_all - - product = MagicMock() - monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", product) - monkeypatch.delenv("DD_TRACE_AWS_LAMBDA_ENABLED", raising=False) - - patch_all() - - assert product.mock_calls == [call.start(), call.post_preload()] - mock_patch_all_dd.assert_called_once_with( - unittest=False, selenium=False, aws_lambda=False - ) - mock_patch_dd.assert_called_once_with(raise_errors=False, aws_lambda=True) - - -@pytest.mark.parametrize("failure", ["import", "start", "post_preload"]) -@patch("datadog_lambda.config.Config.sca_enabled", True) -@patch("datadog_lambda.patch.patch_all_dd") -@patch("datadog_lambda.patch.patch_dd") -def test_sca_errors_caught(mock_patch_dd, mock_patch_all_dd, monkeypatch, failure): +def test_sca_startup(mock_patch_dd, mock_patch_all_dd, monkeypatch, failure): from datadog_lambda.patch import patch_all product = MagicMock() if failure == "import": product = None - else: + elif failure: getattr(product, failure).side_effect = RuntimeError("SCA startup failed") monkeypatch.setitem(sys.modules, "ddtrace.internal.sca.product", product) monkeypatch.delenv("DD_TRACE_AWS_LAMBDA_ENABLED", raising=False) patch_all() + if failure is None: + assert product.mock_calls == [call.start(), call.post_preload()] + mock_patch_all_dd.assert_called_once_with( + unittest=False, selenium=False, aws_lambda=False + ) mock_patch_dd.assert_called_once_with(raise_errors=False, aws_lambda=True) @pytest.mark.parametrize("trace_enabled", ["true", "false"]) @pytest.mark.parametrize("lambda_enabled", ["true", "false"]) -def test_sca_starts_before_handler_import(tmp_path, trace_enabled, lambda_enabled): +def test_sca_starts_before_handler_import( + tmp_path, monkeypatch, trace_enabled, lambda_enabled +): (tmp_path / "startup_handler.py").write_text( - "import os\n" - "sca_ready_at_import = os.environ.get('SCA_READY') == 'true'\n" + "from ddtrace.internal.sca.product import ready\n" "def handler(event, context):\n" " return {}\n" ) @@ -166,34 +153,22 @@ def test_sca_starts_before_handler_import(tmp_path, trace_enabled, lambda_enable from types import ModuleType product = ModuleType("ddtrace.internal.sca.product") +product.ready = False product.start = lambda: None -product.post_preload = lambda: os.environ.__setitem__("SCA_READY", "true") +product.post_preload = lambda: setattr(product, "ready", True) sys.modules[product.__name__] = product import datadog_lambda.handler as handler -assert handler.handler_module.sca_ready_at_import +assert handler.handler_module.ready expected_patch = os.environ["DD_TRACE_ENABLED"] == "true" and os.environ["DD_TRACE_AWS_LAMBDA_ENABLED"] == "true" assert bool(getattr(handler.handler_module, "_datadog_patch", False)) == expected_patch """ - env = os.environ.copy() - env.update( - { - "PYTHONPATH": os.pathsep.join([str(tmp_path), os.getcwd()]), - "AWS_LAMBDA_FUNCTION_NAME": "sca-startup-test", - "DD_LAMBDA_HANDLER": "startup_handler.handler", - "DD_APPSEC_ENABLED": "false", - "DD_APPSEC_SCA_ENABLED": "true", - "DD_TRACE_ENABLED": trace_enabled, - "DD_TRACE_AWS_LAMBDA_ENABLED": lambda_enabled, - } - ) - env.pop("SCA_READY", None) - result = subprocess.run( - [sys.executable, "-c", code], - env=env, - capture_output=True, - text=True, - timeout=30, - ) - assert result.returncode == 0, result.stdout + result.stderr + monkeypatch.setenv("PYTHONPATH", os.pathsep.join([str(tmp_path), os.getcwd()])) + monkeypatch.setenv("AWS_LAMBDA_FUNCTION_NAME", "sca-startup-test") + monkeypatch.setenv("DD_LAMBDA_HANDLER", "startup_handler.handler") + monkeypatch.setenv("DD_APPSEC_ENABLED", "false") + monkeypatch.setenv("DD_APPSEC_SCA_ENABLED", "true") + monkeypatch.setenv("DD_TRACE_ENABLED", trace_enabled) + monkeypatch.setenv("DD_TRACE_AWS_LAMBDA_ENABLED", lambda_enabled) + subprocess.run([sys.executable, "-c", code], check=True, timeout=30) From 1ce368c30b0f925beb6977e3d237fa7090d870ec Mon Sep 17 00:00:00 2001 From: Florentin Labelle Date: Tue, 17 Nov 2026 10:23:15 +0100 Subject: [PATCH 6/6] refactor: read Lambda instrumentation setting from config --- datadog_lambda/config.py | 1 + datadog_lambda/patch.py | 7 ++----- tests/test_config.py | 1 + 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/datadog_lambda/config.py b/datadog_lambda/config.py index 6487c9e7..530c468c 100644 --- a/datadog_lambda/config.py +++ b/datadog_lambda/config.py @@ -63,6 +63,7 @@ def _resolve_env(self, key, default=None, cast=None, depends_on_tracing=False): capture_payload_enabled = _get_env("DD_CAPTURE_LAMBDA_PAYLOAD", "false", as_bool) trace_enabled = _get_env("DD_TRACE_ENABLED", "true", as_bool) + aws_lambda_enabled = _get_env("DD_TRACE_AWS_LAMBDA_ENABLED", "true", as_bool) make_inferred_span = _get_env( "DD_TRACE_MANAGED_SERVICES", "true", as_bool, depends_on_tracing=True ) diff --git a/datadog_lambda/patch.py b/datadog_lambda/patch.py index 8735406b..e3455564 100644 --- a/datadog_lambda/patch.py +++ b/datadog_lambda/patch.py @@ -4,7 +4,6 @@ # Copyright 2019 Datadog, Inc. import sys -import os import logging import zlib import ujson as json @@ -14,7 +13,7 @@ from ddtrace import patch_all as patch_all_dd from ddtrace import patch as patch_dd -from datadog_lambda.config import config, as_bool +from datadog_lambda.config import config from datadog_lambda.tracing import get_dd_trace_context from collections.abc import MutableMapping @@ -54,9 +53,7 @@ def patch_all(): except Exception: logger.debug("Failed to initialize SCA reachability", exc_info=True) - if config.trace_enabled and as_bool( - os.environ.get("DD_TRACE_AWS_LAMBDA_ENABLED", "true") - ): + if config.trace_enabled and config.aws_lambda_enabled: patch_dd(raise_errors=False, aws_lambda=True) diff --git a/tests/test_config.py b/tests/test_config.py index 9c5da63a..dcf85733 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -85,6 +85,7 @@ def _test_as_list(env_key, conf_key, default): *_test_as_bool("DD_FLUSH_TO_LOG", "flush_to_log", default=False), *_test_as_bool("DD_LOGS_INJECTION", "logs_injection", default=True), *_test_as_bool("DD_TRACE_ENABLED", "trace_enabled", default=True), + *_test_as_bool("DD_TRACE_AWS_LAMBDA_ENABLED", "aws_lambda_enabled", default=True), *_test_as_bool("DD_COLD_START_TRACING", "cold_start_tracing", default=True), *_test_as_bool("DD_TRACE_MANAGED_SERVICES", "make_inferred_span", default=True), *_test_as_bool(