Skip to content

Commit

Permalink
Merge pull request #2907 from DataDog/cbeauchesne/python-2799
Browse files Browse the repository at this point in the history
[python] Use healthcheck to get info #2799
  • Loading branch information
cbeauchesne committed Aug 26, 2024
2 parents 4046f64 + a523e38 commit 0a114f4
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 33 deletions.
5 changes: 3 additions & 2 deletions utils/_context/_scenarios/parametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def configure(self, config):
self._clean_networks()

# https://github.com/DataDog/system-tests/issues/2799
if library in ("nodejs",):
if library in ("nodejs", "python"):
output = _get_client().containers.run(
self.apm_test_server_definition.container_tag,
remove=True,
Expand Down Expand Up @@ -338,7 +338,8 @@ def python_library_factory() -> APMLibraryTestServer:
WORKDIR /app
RUN pyenv global 3.9.16
RUN python3.9 -m pip install fastapi==0.89.1 uvicorn==0.20.0
COPY utils/build/docker/python/install_ddtrace.sh utils/build/docker/python/get_appsec_rules_version.py binaries* /binaries/
COPY utils/build/docker/python/parametric/system_tests_library_version.sh system_tests_library_version.sh
COPY utils/build/docker/python/install_ddtrace.sh binaries* /binaries/
RUN /binaries/install_ddtrace.sh
ENV DD_PATCH_MODULES="fastapi:false"
""",
Expand Down
14 changes: 7 additions & 7 deletions utils/_context/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ def configure(self, replay):
)

# https://github.com/DataDog/system-tests/issues/2799
if self.library in ("nodejs",):
if self.library in ("nodejs", "python"):
self.healthcheck = {
"test": f"curl --fail --silent --show-error localhost:{self.port}/healthcheck",
"retries": 60,
Expand All @@ -685,18 +685,14 @@ def configure(self, replay):
else:
self.appsec_rules_file = (self.image.env | self.environment).get("DD_APPSEC_RULES", None)

if self.weblog_variant == "python3.12":
if self.library < "[email protected]": # profiling causes a seg fault on 2.0.0
self.environment["DD_PROFILING_ENABLED"] = "false"

def post_start(self):
from utils import weblog

logger.debug(f"Docker host is {weblog.domain}")

# new way of getting info from the weblog. Only working for nodejs right now
# new way of getting info from the weblog. Only working for nodejs and python right now
# https://github.com/DataDog/system-tests/issues/2799
if self.library == "nodejs":
if self.library in ("nodejs", "python"):
with open(self.healthcheck_log_file, mode="r", encoding="utf-8") as f:
data = json.load(f)
lib = data["library"]
Expand Down Expand Up @@ -749,6 +745,10 @@ def __init__(self, host_log_folder) -> None:
image_name="postgres:alpine",
name="postgres",
host_log_folder=host_log_folder,
# healthcheck={
# "test": "pg_isready -q -U postgres -d system_tests_dbname",
# "retries": 30,
# },
user="postgres",
environment={"POSTGRES_PASSWORD": "password", "PGPORT": "5433"},
volumes={
Expand Down
2 changes: 1 addition & 1 deletion utils/build/docker/python/django-poc.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM datadog/system-tests:django-poc.base-v2
WORKDIR /app


COPY utils/build/docker/python/install_ddtrace.sh utils/build/docker/python/get_appsec_rules_version.py binaries* /binaries/
COPY utils/build/docker/python/install_ddtrace.sh binaries* /binaries/
RUN /binaries/install_ddtrace.sh

COPY utils/build/docker/python/django /app
Expand Down
25 changes: 25 additions & 0 deletions utils/build/docker/python/django/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
weak_hash_secure_algorithm,
)

import ddtrace
from ddtrace import Pin, tracer, patch_all
from ddtrace.appsec import trace_utils as appsec_trace_utils

Expand Down Expand Up @@ -63,6 +64,29 @@ def sample_rate(request, i):
_TRACK_CUSTOM_APPSEC_EVENT_NAME = "system_tests_appsec_event"


@csrf_exempt
def healthcheck(request):
with open(ddtrace.appsec.__path__[0] + "/rules.json", encoding="utf-8") as f:
data = json.load(f)

if "metadata" not in data:
appsec_event_rules_version = "1.2.5"
else:
appsec_event_rules_version = data["metadata"]["rules_version"]

result = {
"status": "ok",
"library": {
"language": "python",
"version": ddtrace.__version__,
"libddwaf_version": ddtrace.appsec._ddwaf.ddwaf_get_version().decode(),
"appsec_event_rules_version": appsec_event_rules_version,
},
}

return HttpResponse(json.dumps(result), content_type="application/json")


@csrf_exempt
def waf(request, *args, **kwargs):
if "tag_value" in kwargs:
Expand Down Expand Up @@ -662,6 +686,7 @@ def create_extra_service(request):
urlpatterns = [
path("", hello_world),
path("sample_rate_route/<int:i>", sample_rate),
path("healthcheck", healthcheck),
path("waf", waf),
path("waf/", waf),
path("waf/<url>", waf),
Expand Down
2 changes: 1 addition & 1 deletion utils/build/docker/python/fastapi.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM datadog/system-tests:fastapi.base-v1

WORKDIR /app

COPY utils/build/docker/python/install_ddtrace.sh utils/build/docker/python/get_appsec_rules_version.py binaries* /binaries/
COPY utils/build/docker/python/install_ddtrace.sh binaries* /binaries/
RUN /binaries/install_ddtrace.sh
RUN python3.10 -m pip install --upgrade pip
RUN python3.10 -m pip install PyYAML uvicorn requests psycopg2-binary python-multipart
Expand Down
22 changes: 22 additions & 0 deletions utils/build/docker/python/fastapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import urllib3
import xmltodict

import ddtrace
from ddtrace import Pin
from ddtrace import patch_all
from ddtrace import tracer
Expand Down Expand Up @@ -64,6 +65,27 @@ async def root():
return "Hello, World!"


@app.get("/healthcheck")
async def healthcheck():
with open(ddtrace.appsec.__path__[0] + "/rules.json", encoding="utf-8") as f:
data = json.load(f)

if "metadata" not in data:
appsec_event_rules_version = "1.2.5"
else:
appsec_event_rules_version = data["metadata"]["rules_version"]

return {
"status": "ok",
"library": {
"language": "python",
"version": ddtrace.__version__,
"libddwaf_version": ddtrace.appsec._ddwaf.ddwaf_get_version().decode(),
"appsec_event_rules_version": appsec_event_rules_version,
},
}


@app.get("/sample_rate_route/{i}", response_class=PlainTextResponse)
async def sample_rate(i):
return "OK"
Expand Down
2 changes: 1 addition & 1 deletion utils/build/docker/python/flask-poc.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM datadog/system-tests:flask-poc.base-v6

WORKDIR /app

COPY utils/build/docker/python/install_ddtrace.sh utils/build/docker/python/get_appsec_rules_version.py binaries* /binaries/
COPY utils/build/docker/python/install_ddtrace.sh binaries* /binaries/
RUN /binaries/install_ddtrace.sh

COPY utils/build/docker/python/flask /app
Expand Down
22 changes: 22 additions & 0 deletions utils/build/docker/python/flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,28 @@ def hello_world():
return "Hello, World!\\n"


@app.route("/healthcheck")
def healthcheck():
path = ddtrace.appsec.__path__[0] + "/rules.json"
with open(path, encoding="utf-8") as f:
data = json.load(f)

if "metadata" not in data:
appsec_event_rules_version = "1.2.5"
else:
appsec_event_rules_version = data["metadata"]["rules_version"]

return {
"status": "ok",
"library": {
"language": "python",
"version": ddtrace.__version__,
"libddwaf_version": ddtrace.appsec._ddwaf.ddwaf_get_version().decode(),
"appsec_event_rules_version": appsec_event_rules_version,
},
}


@app.route("/sample_rate_route/<i>")
def sample_rate(i):
return "OK"
Expand Down
11 changes: 0 additions & 11 deletions utils/build/docker/python/get_appsec_rules_version.py

This file was deleted.

10 changes: 4 additions & 6 deletions utils/build/docker/python/install_ddtrace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ fi

cd -

python -c "import ddtrace; print(ddtrace.__version__)" > SYSTEM_TESTS_LIBRARY_VERSION
python /binaries/get_appsec_rules_version.py > SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION
touch SYSTEM_TESTS_LIBDDWAF_VERSION

echo "dd-trace version is $(cat SYSTEM_TESTS_LIBRARY_VERSION)"
echo "appsec rules version is $(cat SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION)"
# python uses the next API to get the library version. See https://github.com/DataDog/system-tests/issues/2799
echo "0.0.0" > SYSTEM_TESTS_LIBRARY_VERSION
echo "0.0.0" > SYSTEM_TESTS_LIBDDWAF_VERSION
echo "0.0.0" > SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

python -c "import ddtrace; print(ddtrace.__version__)"
2 changes: 1 addition & 1 deletion utils/build/docker/python/pylons.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ RUN python --version && curl --version
RUN pip install pylons
COPY utils/build/docker/python/pylons/app /app

COPY utils/build/docker/python/install_ddtrace.sh utils/build/docker/python/get_appsec_rules_version.py binaries* /binaries/
COPY utils/build/docker/python/install_ddtrace.sh binaries* /binaries/
RUN /binaries/install_ddtrace.sh

ENV DD_TRACE_HEADER_TAGS='user-agent:http.request.headers.user-agent'
Expand Down
2 changes: 1 addition & 1 deletion utils/build/docker/python/python3.12.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM datadog/system-tests:python3.12.base-v3

WORKDIR /app

COPY utils/build/docker/python/install_ddtrace.sh utils/build/docker/python/get_appsec_rules_version.py binaries* /binaries/
COPY utils/build/docker/python/install_ddtrace.sh binaries* /binaries/
RUN /binaries/install_ddtrace.sh

COPY utils/build/docker/python/django /app
Expand Down
2 changes: 1 addition & 1 deletion utils/build/docker/python/uds-flask.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM datadog/system-tests:flask-poc.base-v6

WORKDIR /app

COPY utils/build/docker/python/install_ddtrace.sh utils/build/docker/python/get_appsec_rules_version.py binaries* /binaries/
COPY utils/build/docker/python/install_ddtrace.sh binaries* /binaries/
RUN /binaries/install_ddtrace.sh

COPY utils/build/docker/python/flask /app
Expand Down
2 changes: 1 addition & 1 deletion utils/build/docker/python/uwsgi-poc.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM datadog/system-tests:uwsgi-poc.base-v3

WORKDIR /app

COPY utils/build/docker/python/install_ddtrace.sh utils/build/docker/python/get_appsec_rules_version.py binaries* /binaries/
COPY utils/build/docker/python/install_ddtrace.sh binaries* /binaries/
RUN /binaries/install_ddtrace.sh

COPY utils/build/docker/python/flask /app
Expand Down

0 comments on commit 0a114f4

Please sign in to comment.