Skip to content

feat: enable tracer-based AppSec for Java Lambda - #791

Open
claponcet wants to merge 2 commits into
DataDog:mainfrom
claponcet:clara.poncet/java-lambda-appsec-cdk
Open

claponcet wants to merge 2 commits into
DataDog:mainfrom
claponcet:clara.poncet/java-lambda-appsec-cdk

Conversation

@claponcet

@claponcet claponcet commented Nov 18, 2026 •

Copy link
Copy Markdown

What does this PR do?

Enable tracer-based App and API Protection for Java Lambda functions. With datadogAppSecMode: ON, select the tracer when javaLayerVersion >= 31 and extensionLayerVersion >= 102; otherwise retain extension mode. Explicit TRACER selects tracer mode without version checks, matching Python behavior.

Disable extension AppSec when enabling the Java tracer, while preserving explicit AppSec environment overrides, including extension enablement. Configure the Java execution wrapper even when handler redirection is disabled. Preserve existing Python and legacy extension behavior, and document Java support in the README.

Motivation

Support Java Lambda AppSec onboarding through the existing CDK options and automatically select tracer mode as customers upgrade their configured layers.

Testing Guidelines

  • All 505 unit tests pass locally on the latest commit.
  • The Python-wheel suite contains five synthesis tests; validation against a rebuilt wheel is pending for the latest commit.
  • No AWS resources were deployed.

Additional Notes

Do not merge until Java layer 31 is published with tracer 1.69.0+ and end-to-end onboarding validation passes.

Types of Changes

  • Bug fix
  • New feature
  • Breaking change
  • Misc (docs, refactoring, dependency upgrade, etc.)

Check all that apply

  • This PR's description is comprehensive
  • This PR contains breaking changes that are documented in the description
  • This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
  • This PR impacts documentation, and it has been updated (or a ticket has been logged)
  • This PR's changes are covered by the automated tests
  • This PR collects user input/sensitive content into Datadog

@claponcet
claponcet marked this pull request as ready for review November 20, 2026 10:49
@claponcet
claponcet requested review from a team as code owners November 20, 2026 10:49

@ava-silver ava-silver left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Automated AI Review (human curated)

Left a few inline comments on simplifying the Java branch in src/env.ts (avoiding src/env-tracker.ts changes and preserving redirectHandler: false), removing the unexecuted Python test script, and adding end-to-end validation evidence once Java layer v31 is published.

Comment thread src/env.ts
) {
setEnvIfUnset(ENABLE_DD_APPSEC_ENV_VAR, "true");
if (runtimeType === RuntimeType.JAVA) {
setEnvIfUnset(AWS_LAMBDA_EXEC_WRAPPER_KEY, AWS_LAMBDA_EXEC_WRAPPER_VAL);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Setting AWS_LAMBDA_EXEC_WRAPPER here breaks redirectHandler: false for Java tracer-based AppSec.

When redirectHandler is true (the default), redirectHandlers in src/redirect.ts already sets AWS_LAMBDA_EXEC_WRAPPER=/opt/datadog_wrapper whenever useExtension is true -- and validateProps in src/datadog-lambda.ts already enforces that extensionLayerVersion or extensionLayerArn is set whenever AppSec is enabled. So this line only takes effect when a user explicitly sets redirectHandler: false, clobbering setups where they opt out of the Datadog wrapper (for example, configuring -javaagent via JAVA_TOOL_OPTIONS or setting a custom wrapper via fn.addEnvironment).

Python tracer AppSec respects redirectHandler: false; Java should too by dropping this line.

Comment thread src/env.ts
Comment on lines +151 to +156
// Explicit extension configuration takes precedence over automatic tracer selection.
const extensionEnabled = /^(1|t|true)$/i.test(getTrackedEnv(lam, ENABLE_DD_SERVERLESS_APPSEC_ENV_VAR) ?? "");
if (!extensionEnabled) {
setEnvIfUnset(ENABLE_DD_APPSEC_ENV_VAR, "true");
setEnvIfUnset(ENABLE_DD_SERVERLESS_APPSEC_ENV_VAR, "false");
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we need this Java-only branch or getTrackedEnv in src/env-tracker.ts?

Unlike datadog-ci (which mutates existing remote Lambda state), CDK synthesizes CloudFormation templates declaratively from scratch, and src/env.ts is the only place DD_SERVERLESS_APPSEC_ENABLED is written. Writing DD_SERVERLESS_APPSEC_ENABLED="false" on every Java tracer-AppSec function:

  • Diverges from Python (which only sets DD_APPSEC_ENABLED="true") and sets both AppSec env vars on every fresh Java Lambda.
  • Forces adding getTrackedEnv to src/env-tracker.ts (which only exists to avoid reading aws-cdk-lib's private Function.environment field for setEnvIfUnset, not to inspect sibling env var values).
  • Silently suppresses explicit datadogAppSecMode: DatadogAppSecMode.TRACER as well as ON when DD_SERVERLESS_APPSEC_ENABLED is tracked as truthy (even though the comment mentions only automatic selection).

If Java follows the same path as Python -- collapsing lines 149-159 down to setEnvIfUnset(ENABLE_DD_APPSEC_ENV_VAR, "true") -- we keep both runtimes consistent, respect redirectHandler: false, and don't need to touch src/env-tracker.ts.

Comment on lines +1 to +5
"""Run against an installed local Python wheel; synthesizes only, never deploys.

After building and installing the wheel in a virtual environment, run:
python integration_tests/java_appsec_python.py
"""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nothing in .github/workflows/tests.yml or scripts/run_integration_tests.sh executes this file (run_integration_tests.sh only synthesizes stacks listed in STACK_CONFIG_PATHS under integration_tests/stacks/ against integration_tests/snapshots/), so it will rot over time.

Since Python bindings are generated from TypeScript via JSII and test/java-appsec.spec.ts already covers the behavior, consider removing this script -- or, if Python JSII coverage for Java AppSec is desired, adding it to the existing snapshot harness under integration_tests/stacks/python/ and scripts/run_integration_tests.sh.

Comment thread src/env.ts
Comment on lines +130 to +133
const javaTracerSupported =
runtimeType === RuntimeType.JAVA &&
(baseProps.javaLayerVersion ?? 0) >= 31 &&
(baseProps.extensionLayerVersion ?? 0) >= 102;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Per the PR description, Java layer v31 is still pending release and no AWS resources have been deployed yet. I'd like to see end-to-end validation with the published v31 layer (for example, the resulting Lambda config and telemetry showing client_tracer_based:true) documented in the PR description before approving.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants