Skip to content
Draft
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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,15 @@ custom:

### Enable in-tracer App and API Protection

The [Datadog Lambda Library for Python][8] (version `8.114.0` or later) can run [App and API Protection][19] inside your function, giving the security engine more request context. This complements the in-extension approach provided by the [Datadog Lambda Extension][12]. Support for additional runtimes is coming.
The [Datadog Lambda Library for Python][8] (version `8.114.0` or later) can run [App and API Protection][19] inside your function, giving the security engine more request context. This complements the in-extension approach provided by the [Datadog Lambda Extension][12]. Java tracer-based support described below is not yet released. It requires a plugin release bundling Java layer `31` or later (with tracer `1.69.0` or later) and extension `102` or later.

`appSecMode` controls which implementation runs:

* **`on` (recommended):** Enable App and API Protection.
- If you’re on a supported runtime and using the plugin-provided layers (`addLayers: true`), enable the library implementation.
- For Python, enable the library implementation when `addLayers: true`.
- For Java, enable the tracer implementation when both `addLayers: true` and `addExtension: true`. Otherwise, use the **extension** implementation.
- For other runtimes, enable the **extension** implementation.
* **`tracer`:** Force the library implementation. Every function must be Python and use the Python library at version `8.114.0` or newer.
* **`tracer`:** Force the library implementation. Every function must be Python or Java. Python requires library version `8.114.0` or newer. Java requires tracer `1.69.0` or newer and extension `102` or newer.
* **`extension`:** Force the **extension** implementation, even if a library with support is present.
* **`off`:** Disable App and API Protection.

Expand Down
74 changes: 74 additions & 0 deletions src/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2367,3 +2367,77 @@ describe("setEnvConfiguration", () => {
});
});
});

describe("Java tracer AppSec", () => {
const config = { ...defaultConfiguration, appSecMode: "on" as const };
function java(environment: Record<string, any> = {}): FunctionInfo[] {
return [{ name: "java", type: RuntimeType.JAVA, runtime: "java21", handler: { events: [], environment } }];
}
it.each(["on", "tracer"] as const)("enables tracer WAF and disables extension WAF in %s mode", (appSecMode) => {
const handlers = java();
setEnvConfiguration({ ...config, appSecMode }, handlers);
expect(handlers[0].handler.environment).toMatchObject({
DD_APPSEC_ENABLED: true,
DD_SERVERLESS_APPSEC_ENABLED: false,
AWS_LAMBDA_EXEC_WRAPPER: "/opt/datadog_wrapper",
});
});
it.each([{ addLayers: false }, { addExtension: false }, { addLayers: false, addExtension: false }])(
"keeps automatic Java mode on the extension when instrumentation is manual: %j",
(manual) => {
const handlers = java();
setEnvConfiguration({ ...config, ...manual }, handlers);
expect(handlers[0].handler.environment).toMatchObject({ DD_SERVERLESS_APPSEC_ENABLED: true });
expect(handlers[0].handler.environment).not.toHaveProperty("DD_APPSEC_ENABLED");
},
);
it.each([{ addLayers: false }, { addExtension: false }, { useLayersFromAccount: "123456789012" }])(
"allows explicit tracer mode with user-managed instrumentation, like Python: %j",
(manual) => {
const handlers = java();
setEnvConfiguration({ ...config, ...manual, appSecMode: "tracer" }, handlers);
expect(handlers[0].handler.environment).toMatchObject({
DD_APPSEC_ENABLED: true,
DD_SERVERLESS_APPSEC_ENABLED: false,
});
},
);
it.each([{ useLayersFromAccount: "123456789012" }])(
"allows plugin-managed layers from another account: %j",
(overrides) => {
const handlers = java();
setEnvConfiguration({ ...config, ...overrides }, handlers);
expect(handlers[0].handler.environment).toMatchObject({
DD_APPSEC_ENABLED: true,
DD_SERVERLESS_APPSEC_ENABLED: false,
});
},
);
it.each([true, "true", false, "false", { Ref: "ExtensionWaf" }])(
"overrides function and provider extension WAF settings: %j",
(value) => {
for (const handlers of [java({ DD_SERVERLESS_APPSEC_ENABLED: value }), java()]) {
setEnvConfiguration(config, handlers, { DD_SERVERLESS_APPSEC_ENABLED: value });
expect(handlers[0].handler.environment).toMatchObject({
DD_APPSEC_ENABLED: true,
DD_SERVERLESS_APPSEC_ENABLED: false,
});
}
},
);
it.each([{ appSecMode: "extension" as const }, { appSecMode: undefined, enableASM: true }])(
"preserves explicit legacy mode: %j",
(legacy) => {
const handlers = java();
setEnvConfiguration({ ...config, ...legacy }, handlers);
expect(handlers[0].handler.environment).toMatchObject({ DD_SERVERLESS_APPSEC_ENABLED: true });
expect(handlers[0].handler.environment).not.toHaveProperty("DD_APPSEC_ENABLED");
},
);
it("keeps disabled AppSec untouched and rejects disabled tracing", () => {
const handlers = java();
setEnvConfiguration({ ...config, appSecMode: "off" }, handlers);
expect(handlers[0].handler.environment).not.toHaveProperty("DD_APPSEC_ENABLED");
expect(() => setEnvConfiguration({ ...config, enableDDTracing: false }, java())).toThrow("enableDDTracing");
});
});
16 changes: 11 additions & 5 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export const ddTagsEnvVar = "DD_TAGS";
const AWS_LAMBDA_EXEC_WRAPPER_VAR = "AWS_LAMBDA_EXEC_WRAPPER";
const AWS_LAMBDA_EXEC_WRAPPER = "/opt/datadog_wrapper";

const IN_TRACER_APPSEC_SUPPORTED_RUNTIMES = [RuntimeType.PYTHON];
const IN_TRACER_APPSEC_SUPPORTED_RUNTIMES = [RuntimeType.PYTHON, RuntimeType.JAVA];

export const defaultConfiguration: Configuration = {
addLayers: true,
Expand Down Expand Up @@ -291,16 +291,22 @@ export function setEnvConfiguration(

if (config.appSecMode === "tracer" && !IN_TRACER_APPSEC_SUPPORTED_RUNTIMES.includes(type)) {
throw new Error(
`\`appSecMode\` is set to \`tracer\`. This requires the function to be a Python runtime. Found: ${type}`,
`\`appSecMode\` is set to \`tracer\`. This requires the function to be a Python or Java runtime. Found: ${type}`,
);
}

if (
(config.appSecMode === "on" && IN_TRACER_APPSEC_SUPPORTED_RUNTIMES.includes(type) && config.addLayers) ||
(config.appSecMode === "on" &&
IN_TRACER_APPSEC_SUPPORTED_RUNTIMES.includes(type) &&
config.addLayers &&
(type !== RuntimeType.JAVA || config.addExtension)) ||
config.appSecMode === "tracer"
)
) {
if (type === RuntimeType.JAVA) {
environment[ddServerlessAppsecEnabledEnvVar] = false;
}
environment[ddAppsecEnabledEnvVar] = true;
else if (config.appSecMode === "on" || config.appSecMode === "extension") {
} else if (config.appSecMode === "on" || config.appSecMode === "extension") {
environment[AWS_LAMBDA_EXEC_WRAPPER_VAR] ??= AWS_LAMBDA_EXEC_WRAPPER;
environment[ddServerlessAppsecEnabledEnvVar] = true;
}
Expand Down
79 changes: 79 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,85 @@ describe("ServerlessPlugin", () => {
mock.restore();
});

it("configures mixed Java and Python tracer AppSec while retaining Node extension AppSec", async () => {
const serverless = {
cli: { log: () => {} },
getProvider: () => awsMock(),
service: {
getServiceName: () => "dev",
provider: { region: "us-east-1" },
functions: {
java: { runtime: "java21", handler: "example.Handler" },
python: { runtime: "python3.12", handler: "app.handler" },
node: { runtime: "nodejs20.x", handler: "app.handler" },
excluded: { runtime: "java21", handler: "example.Handler" },
},
custom: { datadog: { apiKey: "1234", appSecMode: "on", exclude: ["excluded"] } },
},
};
const plugin = new ServerlessPlugin(serverless, {});
await plugin.hooks["before:package:createDeploymentArtifacts"]();
expect(serverless.service.functions.java).toMatchObject({
layers: expect.arrayContaining([
expect.stringContaining(":layer:dd-trace-java:"),
expect.stringContaining(":layer:Datadog-Extension:"),
]),
environment: { DD_APPSEC_ENABLED: true, AWS_LAMBDA_EXEC_WRAPPER: "/opt/datadog_wrapper" },
});
expect(serverless.service.functions.java).toHaveProperty("environment.DD_SERVERLESS_APPSEC_ENABLED", false);
expect(serverless.service.functions.python).toHaveProperty("environment.DD_APPSEC_ENABLED", true);
expect(serverless.service.functions.node).toHaveProperty("environment.DD_SERVERLESS_APPSEC_ENABLED", true);
expect(serverless.service.functions.excluded).not.toHaveProperty("environment");
expect(serverless.service.functions.excluded).not.toHaveProperty("layers");
});

it("preserves external layer configuration and overrides the inherited extension WAF flag", async () => {
const externalLayer = { Ref: "CustomLayer" };
const serverless = {
cli: { log: () => {} },
getProvider: () => awsMock(),
service: {
getServiceName: () => "dev",
provider: {
region: "us-east-1",
layers: [externalLayer],
environment: { DD_SERVERLESS_APPSEC_ENABLED: "true" },
},
functions: { java: { runtime: "java21", handler: "example.Handler" } },
custom: { datadog: { apiKey: "1234", appSecMode: "on" } },
},
};
const plugin = new ServerlessPlugin(serverless, {});
await plugin.hooks["before:package:createDeploymentArtifacts"]();
expect(serverless.service.functions.java).toMatchObject({
layers: expect.arrayContaining([externalLayer]),
environment: { DD_APPSEC_ENABLED: true, DD_SERVERLESS_APPSEC_ENABLED: false },
});
expect(serverless.service.provider.environment.DD_SERVERLESS_APPSEC_ENABLED).toBe("true");
});

it("keeps extension AppSec when Java instrumentation is not installed by the plugin", async () => {
const extension = "arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Extension:103";
const serverless = {
cli: { log: () => {} },
getProvider: () => awsMock(),
service: {
getServiceName: () => "dev",
provider: { region: "us-east-1", layers: [extension] },
functions: { java: { runtime: "java21", handler: "example.Handler" } },
custom: { datadog: { apiKey: "1234", appSecMode: "on", addExtension: false } },
},
};
const plugin = new ServerlessPlugin(serverless, {});
await plugin.hooks["before:package:createDeploymentArtifacts"]();
expect(serverless.service.functions.java).toMatchObject({
environment: { DD_SERVERLESS_APPSEC_ENABLED: true, AWS_LAMBDA_EXEC_WRAPPER: "/opt/datadog_wrapper" },
});
expect(serverless.service.functions.java).not.toHaveProperty("environment.DD_APPSEC_ENABLED");
expect(serverless.service.functions.java).not.toHaveProperty("layers");
expect(serverless.service.provider.layers).toEqual([extension]);
});

it("adds lambda library layers by default and doesn't change handler", async () => {
mock({});
const serverless = {
Expand Down
Loading