Skip to content

Commit e975809

Browse files
committed
[python_lambda] fix issues preventing some tests
1 parent 526c5d0 commit e975809

File tree

8 files changed

+68
-36
lines changed

8 files changed

+68
-36
lines changed

docs/scenarios/aws_lambda.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,28 @@ flowchart TD
3636
TESTS[Tests Container] -->|Send Requests| LambdaProxy
3737
LambdaProxy[Lambda Proxy] -->|Send Lambda Event| Application
3838
subgraph APP[Application Container]
39-
Extension[Extension *:8126]
39+
socat[socat *:8127] --> Extension
40+
Extension[Extension localhost:8126]
4041
Application[Application *:8080]
4142
end
4243
Application --> | Send Traces | APPPROXY
43-
APPPROXY[Application Proxy] --> | Send back traces | Extension
44+
APPPROXY[Application Proxy] --> | Send back traces | socat
4445
APPPROXY -->|mitmdump| TESTS
4546
Extension --> AGENTPROXY
4647
AGENTPROXY[Agent Proxy] -->|remote request| BACKEND
4748
AGENTPROXY -->|mitmdump| TESTS
4849
BACKEND[Datadog] -->|trace API| TESTS
4950
```
51+
52+
## Specific considerations for the weblogs
53+
54+
On top of responding to the regular [`/healthcheck`](../weblog/README.md#get-healthcheck) endpoint.
55+
56+
Lambda Weblogs should respond the same JSON dict response to the non HTTP event:
57+
```json
58+
{
59+
"healthcheck": true
60+
}
61+
```
62+
63+
This is because the healthcheck is sent by the Lambda Weblog container itself which has no knowledge of how to serialize it as the event type expected by the weblog.

tests/appsec/test_only_python.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
@scenarios.default
1313
@scenarios.appsec_lambda_default
1414
@features.language_specifics
15-
@irrelevant(context.library != "python", reason="specific tests for python tracer")
15+
@irrelevant(context.library not in ("python", "python_lambda"), reason="specific tests for python tracer")
1616
class Test_ImportError:
1717
"""Tests to verify that we don't have import errors due to tracer instrumentation."""
1818

1919
@flaky(context.library == "python@3.2.1" and "flask" in context.weblog_variant, reason="APMRP-360")
2020
def test_circular_import(self):
2121
"""Test to verify that we don't have a circular import in the weblog."""
22-
assert context.library == "python"
22+
assert context.library in ("python", "python_lambda")
2323
interfaces.library_stdout.assert_absence("most likely due to a circular import")

utils/_context/_scenarios/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,7 @@ class _Scenarios:
10831083
appsec_lambda_default = LambdaScenario(
10841084
"APPSEC_LAMBDA_DEFAULT",
10851085
doc="Default Lambda scenario",
1086+
scenario_groups=[scenario_groups.appsec],
10861087
)
10871088

10881089

utils/_context/_scenarios/aws_lambda.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def __init__(
2929
weblog_volumes: dict[str, dict[str, str]] | None = None,
3030
):
3131
scenario_groups = [
32-
all_scenario_groups.appsec,
3332
all_scenario_groups.tracer_release,
3433
] + (scenario_groups or [])
3534

@@ -53,7 +52,7 @@ def __init__(
5352
self.proxy_container.environment.update(
5453
{
5554
"PROXY_TRACING_AGENT_TARGET_HOST": self.lambda_weblog.name,
56-
"PROXY_TRACING_AGENT_TARGET_PORT": "8126",
55+
"PROXY_TRACING_AGENT_TARGET_PORT": "8127",
5756
}
5857
)
5958

utils/build/docker/lambda_proxy/main.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ def invoke_lambda_function():
2323
This function is used to invoke the Lambda function with the provided event.
2424
It constructs a v1 event from the Flask request and sends it to the RIE URL.
2525
"""
26-
converted_event = construct_v1_event(request, PORT, binary_types=["application/octet-stream"], stage_name="Prod")
26+
converted_event = construct_v1_event(
27+
request,
28+
PORT,
29+
binary_types=["application/octet-stream"],
30+
stage_name="Prod",
31+
)
2732

2833
response = post(
2934
RIE_URL,
@@ -32,22 +37,33 @@ def invoke_lambda_function():
3237
)
3338

3439
(status_code, headers, body) = LocalApigwService._parse_v1_payload_format_lambda_output(
35-
response.content.decode("utf-8"), binary_types=[], flask_request=request, event_type="Api"
40+
response.content.decode("utf-8"),
41+
binary_types=[],
42+
flask_request=request,
43+
event_type="Api",
3644
)
3745

3846
return app.response_class(response=body, status=status_code, headers=headers)
3947

4048

41-
@app.route("/", methods=["GET", "POST", "OPTIONS"])
42-
@app.route("/finger_print")
43-
@app.get("/headers")
44-
@app.get("/healthcheck")
45-
@app.route("/params/<path>/", methods=["GET", "POST", "OPTIONS"])
46-
@app.route("/tag_value/<string:tag_value>/<int:status_code>", methods=["GET", "POST", "OPTIONS"])
47-
@app.get("/users")
48-
@app.route("/waf", methods=["GET", "POST", "OPTIONS"])
49-
@app.route("/waf/", methods=["GET", "POST", "OPTIONS"])
50-
@app.route("/waf/<path:url>", methods=["GET", "POST", "OPTIONS"])
51-
@app.get("/.git")
52-
def main(**kwargs):
53-
return invoke_lambda_function()
49+
ROUTES = [
50+
("/", ["GET", "POST", "OPTIONS"]),
51+
("/finger_print", ["GET"]),
52+
("/headers", ["GET"]),
53+
("/healthcheck", ["GET"]),
54+
("/params/<path>/", ["GET", "POST", "OPTIONS"]),
55+
("/tag_value/<tag_value>/<status_code>", ["GET", "POST", "OPTIONS"]),
56+
("/users", ["GET"]),
57+
("/waf", ["GET", "POST", "OPTIONS"]),
58+
("/waf/", ["GET", "POST", "OPTIONS"]),
59+
("/waf/<path>", ["GET", "POST", "OPTIONS"]),
60+
("/.git", ["GET"]),
61+
]
62+
63+
for endpoint, methods in ROUTES:
64+
app.add_url_rule(
65+
endpoint,
66+
endpoint,
67+
lambda **kwargs: invoke_lambda_function(),
68+
methods=methods,
69+
)

utils/build/docker/python_lambda/apigw-rest.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM public.ecr.aws/lambda/python:3.13
22

3-
RUN dnf install -y unzip findutils
3+
RUN dnf install -y unzip findutils socat
44

55
# Add the Datadog Extension
66
RUN mkdir -p /opt/extensions

utils/build/docker/python_lambda/function/app.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ set -eu
44

55
export DD_LAMBDA_HANDLER=handler.lambda_handler
66

7+
socat TCP-LISTEN:8127,reuseaddr,fork,bind=0.0.0.0 TCP:127.0.0.1:8126 &
8+
79
exec /lambda-entrypoint.sh datadog_lambda.handler.handler

utils/build/docker/python_lambda/function/handler.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,6 @@ def version_info():
3232
}
3333

3434

35-
@app.get("/tag_value/<tag_value>/<status_code>")
36-
@app.route("/tag_value/<tag_value>/<status_code>", method="OPTIONS")
37-
def tag_value(tag_value: str, status_code: int):
38-
appsec_trace_utils.track_custom_event(
39-
tracer, event_name=_TRACK_CUSTOM_APPSEC_EVENT_NAME, metadata={"value": tag_value}
40-
)
41-
return Response(
42-
status_code=status_code,
43-
content_type="text/plain",
44-
body="Value tagged",
45-
headers=app.current_event.query_string_parameters,
46-
)
47-
48-
4935
@app.get("/")
5036
@app.post("/")
5137
@app.route("/", method="OPTIONS")
@@ -83,6 +69,20 @@ def waf_params(path: str = ""):
8369
)
8470

8571

72+
@app.get("/tag_value/<tag_value>/<status_code>")
73+
@app.route("/tag_value/<tag_value>/<status_code>", method="OPTIONS")
74+
def tag_value(tag_value: str, status_code: int):
75+
appsec_trace_utils.track_custom_event(
76+
tracer, event_name=_TRACK_CUSTOM_APPSEC_EVENT_NAME, metadata={"value": tag_value}
77+
)
78+
return Response(
79+
status_code=status_code,
80+
content_type="text/plain",
81+
body="Value tagged",
82+
headers=app.current_event.query_string_parameters,
83+
)
84+
85+
8686
@app.post("/tag_value/<tag_value>/<status_code>")
8787
def tag_value_post(tag_value: str, status_code: int):
8888
appsec_trace_utils.track_custom_event(

0 commit comments

Comments
 (0)