Skip to content

Commit 80bf6a9

Browse files
christophe-papazianavara1986ericlazromainkomorn-exdatadogbrettlangdon
authored
fix(asm): fix config in api_manager (#7678)
- fix config use in api_manager for api security. - ensure api security global callback is only called once in all supported frameworks. No release note necessary as the feature is private and experimental for now. This will be covered later by system-tests ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [ ] Title is accurate. - [ ] No unnecessary changes are introduced. - [ ] Description motivates each change. - [ ] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [ ] Testing strategy adequately addresses listed risk(s). - [ ] Change is maintainable (easy to change, telemetry, documentation). - [ ] Release note makes sense to a user of the library. - [ ] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [ ] Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) - [ ] If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. - [ ] This PR doesn't touch any of that. --------- Co-authored-by: Alberto Vara <[email protected]> Co-authored-by: Eric Navarro <[email protected]> Co-authored-by: Romain Komorn <[email protected]> Co-authored-by: Brett Langdon <[email protected]>
1 parent 1c9765f commit 80bf6a9

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

ddtrace/appsec/_api_security/api_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import sys
55
from typing import TYPE_CHECKING
66

7-
from ddtrace import config
87
from ddtrace._tracing._limits import MAX_SPAN_META_VALUE_LEN
98
from ddtrace.appsec import _processor as appsec_processor
109
from ddtrace.appsec._asm_request_context import add_context_callback
@@ -15,6 +14,7 @@
1514
from ddtrace.internal.logger import get_logger
1615
from ddtrace.internal.metrics import Metrics
1716
from ddtrace.internal.service import Service
17+
from ddtrace.settings.asm import config as asm_config
1818

1919

2020
if TYPE_CHECKING:
@@ -90,7 +90,7 @@ def _start_service(self):
9090
def _should_collect_schema(self, env):
9191
method = env.waf_addresses.get(SPAN_DATA_NAMES.REQUEST_METHOD)
9292
route = env.waf_addresses.get(SPAN_DATA_NAMES.REQUEST_ROUTE)
93-
sample_rate = config._api_security_sample_rate
93+
sample_rate = asm_config._api_security_sample_rate
9494
# Framework is not fully supported
9595
if not method or not route:
9696
log.debug("unsupported groupkey for api security [method %s] [route %s]", bool(method), bool(route))

ddtrace/appsec/_asm_request_context.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def __init__(self, active: bool = False):
5353
self.callbacks: Dict[str, Any] = {}
5454
self.telemetry: Dict[str, Any] = {}
5555
self.addresses_sent: Set[str] = set()
56+
self.must_call_globals: bool = True
5657

5758

5859
def _get_asm_context() -> ASM_Environment:
@@ -96,10 +97,11 @@ def unregister(span: Span) -> None:
9697
env = _get_asm_context()
9798
if env.span_asm_context is not None and env.span is span:
9899
env.span_asm_context.__exit__(None, None, None)
99-
elif env.span is span:
100+
elif env.span is span and env.must_call_globals:
100101
# needed for api security flushing information before end of the span
101102
for function in GLOBAL_CALLBACKS.get(_CONTEXT_CALL, []):
102103
function(env)
104+
env.must_call_globals = False
103105

104106

105107
class _DataHandler:
@@ -125,7 +127,8 @@ def __init__(self):
125127
def finalise(self):
126128
if self.active:
127129
env = self.execution_context.get_item("asm_env")
128-
callbacks = GLOBAL_CALLBACKS.get(_CONTEXT_CALL, [])
130+
callbacks = GLOBAL_CALLBACKS.get(_CONTEXT_CALL, []) if env.must_call_globals else []
131+
env.must_call_globals = False
129132
if env is not None and env.callbacks is not None and env.callbacks.get(_CONTEXT_CALL):
130133
callbacks += env.callbacks.get(_CONTEXT_CALL)
131134
if callbacks:

0 commit comments

Comments
 (0)