Skip to content
Open
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Comment thread
florentinl marked this conversation as resolved.
Resolved
./python/lib/$runtime/site-packages/ddtrace/appsec/_shared

# CI Visibility paths/integrations
Expand Down
1 change: 1 addition & 0 deletions datadog_lambda/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
aws_lambda_enabled = _get_env("DD_TRACE_AWS_LAMBDA_ENABLED", "true", as_bool)
# ddtrace's DD_TRACE_<PACKAGE>_ENABLED turns instrumentation on/off per package
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
)
Expand Down
20 changes: 19 additions & 1 deletion datadog_lambda/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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.tracing import get_dd_trace_context
Expand All @@ -33,11 +34,28 @@ 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 config.aws_lambda_enabled:
patch_dd(raise_errors=False, aws_lambda=True)


def _patch_for_integration_tests():
"""
Expand Down
1 change: 1 addition & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
68 changes: 67 additions & 1 deletion tests/test_patch.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -103,6 +106,69 @@ 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)


@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_startup(mock_patch_dd, mock_patch_all_dd, monkeypatch, failure):
from datadog_lambda.patch import patch_all

product = MagicMock()
if failure == "import":
product = None
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, monkeypatch, trace_enabled, lambda_enabled
):
(tmp_path / "startup_handler.py").write_text(
"from ddtrace.internal.sca.product import ready\n"
"def handler(event, context):\n"
" return {}\n"
)
code = """
import os
import sys
from types import ModuleType

product = ModuleType("ddtrace.internal.sca.product")
product.ready = False
product.start = lambda: None
product.post_preload = lambda: setattr(product, "ready", True)
sys.modules[product.__name__] = product

import datadog_lambda.handler as handler

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
"""
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)
Loading