Skip to content

Commit f9978db

Browse files
authored
chore(asm): enable ip collection if not disabled and appsec is enabled (#4333)
Enable IP collection if `DD_TRACE_CLIENT_IP_HEADER_DISABLED` is not set but appsec is enabled as per https://datadoghq.atlassian.net/wiki/spaces/APS/pages/2118779066/Client+IP+addresses+resolution#Activation Signed-off-by: Juanjo Alvarez <[email protected]> ## Checklist - [ ] Title must conform to [conventional commit](https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional). - [ ] Add additional sections for `feat` and `fix` pull requests. - [ ] Ensure tests are passing for affected code. - [ ] [Library documentation](https://github.com/DataDog/dd-trace-py/tree/1.x/docs) and/or [Datadog's documentation site](https://github.com/DataDog/documentation/) is updated. Link to doc PR in description. ## Motivation ## Design ## Testing strategy ## Relevant issue(s) ## Testing strategy ## Reviewer Checklist - [ ] Title is accurate. - [ ] Description motivates each change. - [ ] No unnecessary changes were introduced in this PR. - [ ] PR cannot be broken up into smaller PRs. - [ ] Avoid breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [ ] Tests provided or description of manual testing performed is included in the code or PR. - [ ] Release note has been added for fixes and features, or else `changelog/no-changelog` label added. - [ ] All relevant GitHub issues are correctly linked. - [ ] Backports are identified and tagged with Mergifyio. - [ ] Add to milestone.
1 parent 5175651 commit f9978db

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

ddtrace/contrib/trace_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ def _get_request_header_client_ip(span, headers, peer_ip=None, headers_are_case_
177177
# type: (Span, Mapping[str, str], Optional[str], bool) -> str
178178
global _USED_IP_HEADER
179179

180-
if asbool(os.getenv("DD_TRACE_CLIENT_IP_HEADER_DISABLED", default=False)):
180+
ip_collection_disabled = os.getenv("DD_TRACE_CLIENT_IP_HEADER_DISABLED", default=None)
181+
if (ip_collection_disabled is None and not config._appsec_enabled) or asbool(ip_collection_disabled):
182+
# IP Collection will honor the environment var is set. Otherwise it will be enabled
183+
# only if appsec is enabled
181184
return ""
182185

183186
def get_header_value(key): # type: (str) -> Optional[str]

docs/spelling_wordlist.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ agentless
2222
analytics
2323
api
2424
app
25+
appsec
2526
aredis
2627
args
2728
asgi
@@ -198,4 +199,4 @@ libddwaf
198199
Serverless
199200
serverless
200201
cattrs
201-
IAST
202+
IAST
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
features:
3+
- |
4+
ASM: ip address collection will be enabled if not explicitly disabled and appsec is enabled.

tests/contrib/django/test_django_appsec.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def test_django_useragent(client, test_spans, tracer):
248248
assert root_span.get_tag(http.USER_AGENT) == "test/1.2.3"
249249

250250

251-
def test_django_client_ip_disabled(client, test_spans, tracer):
251+
def test_django_client_ip_env_var_disabled_appsec_enabled_must_be_disabled(client, test_spans, tracer):
252252
with override_global_config(dict(_appsec_enabled=True)), override_env(
253253
dict(DD_TRACE_CLIENT_IP_HEADER_DISABLED="True")
254254
):
@@ -257,6 +257,29 @@ def test_django_client_ip_disabled(client, test_spans, tracer):
257257
assert not root_span.get_tag(http.CLIENT_IP)
258258

259259

260+
def test_django_client_ip_env_var_not_disabled_appsec_disabled_must_be_enabled(client, test_spans, tracer):
261+
with override_global_config(dict(_appsec_enabled=False)), override_env(
262+
dict(DD_TRACE_CLIENT_IP_HEADER_DISABLED="False")
263+
):
264+
client.get("/?a=1&b&c=d", HTTP_X_REAL_IP="8.8.8.8")
265+
root_span = test_spans.spans[0]
266+
assert root_span.get_tag(http.CLIENT_IP)
267+
268+
269+
def test_django_client_ip_env_var_missing_appsec_enabled_must_be_enabled(client, test_spans, tracer):
270+
with override_global_config(dict(_appsec_enabled=True)):
271+
client.get("/?a=1&b&c=d", HTTP_X_REAL_IP="8.8.8.8")
272+
root_span = test_spans.spans[0]
273+
assert root_span.get_tag(http.CLIENT_IP)
274+
275+
276+
def test_django_client_ip_env_var_missing_appsec_disabled_must_be_disabled(client, test_spans, tracer):
277+
with override_global_config(dict(_appsec_enabled=False)):
278+
client.get("/?a=1&b&c=d", HTTP_X_REAL_IP="8.8.8.8")
279+
root_span = test_spans.spans[0]
280+
assert not root_span.get_tag(http.CLIENT_IP)
281+
282+
260283
def test_django_client_ip_header_set_by_env_var_empty(client, test_spans, tracer):
261284
with override_global_config(dict(_appsec_enabled=True)), override_env(
262285
dict(DD_TRACE_CLIENT_IP_HEADER="Fooipheader")

tests/contrib/flask/test_flask_appsec.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ def test_flask_useragent(self):
126126
assert root_span.get_tag(http.USER_AGENT) == "test/1.2.3"
127127

128128
def test_flask_client_ip_header_set_by_env_var_valid(self):
129-
with override_env(dict(DD_TRACE_CLIENT_IP_HEADER="X-Use-This")):
129+
with override_global_config(dict(_appsec_enabled=True)), override_env(
130+
dict(DD_TRACE_CLIENT_IP_HEADER="X-Use-This")
131+
):
130132
self.client.get("/?a=1&b&c=d", headers={"HTTP_CLIENT_IP": "8.8.8.8", "X-Use-This": "4.4.4.4"})
131133
spans = self.pop_spans()
132134
root_span = spans[0]

0 commit comments

Comments
 (0)