Skip to content

Commit 3dc6cc2

Browse files
chore(asm): improve api security import condition (#8609) [backport 2.7] (#8638)
Following #8226, this PR ensure that we never import api security manager module if the WAF is unavailable or asm is disabled or api security is disabled. - Add one finite state (bool) to the asm config `_api_security_active` to keep track of API Security Manager - Update and check that state in `_default_span_processors_factory`. ## 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] Risks are described (performance impact, potential for breakage, maintainability) - [x] Change is maintainable (easy to change, telemetry, documentation) - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed or label `changelog/no-changelog` is set - [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)) - [x] If this PR changes the public interface, I've notified `@DataDog/apm-tees`. - [x] If change 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`. ## Reviewer Checklist - [x] Title is accurate - [x] All changes are related to the pull request's stated goal - [x] Description motivates each change - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - [x] Testing strategy adequately addresses listed risks - [x] Change is maintainable (easy to change, telemetry, documentation) - [x] Release note makes sense to a user of the library - [x] Author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - [x] 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)
1 parent 097267c commit 3dc6cc2

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

ddtrace/_trace/tracer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ def _default_span_processors_factory(
136136
if appsec_processor:
137137
span_processors.append(appsec_processor)
138138
else:
139-
if asm_config._api_security_enabled:
139+
# api_security_active will keep track of the service status of APIManager
140+
# we don't want to import the module if it was not started before due to
141+
# one click activation of ASM via Remote Config
142+
if asm_config._api_security_active:
140143
from ddtrace.appsec._api_security.api_manager import APIManager
141144

142145
APIManager.disable()

ddtrace/appsec/_api_security/api_manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def enable(cls):
5353
log.debug("%s already enabled", cls.__name__)
5454
return
5555

56+
asm_config._api_security_active = True
5657
log.debug("Enabling %s", cls.__name__)
5758
metrics.enable()
5859
cls._instance = cls()
@@ -66,6 +67,7 @@ def disable(cls):
6667
log.debug("%s not enabled", cls.__name__)
6768
return
6869

70+
asm_config._api_security_active = False
6971
log.debug("Disabling %s", cls.__name__)
7072
cls._instance.stop()
7173
cls._instance = None

ddtrace/appsec/_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ def _appsec_rc_features_is_enabled() -> bool:
7171

7272

7373
def _appsec_apisec_features_is_active() -> bool:
74-
return asm_config._asm_enabled and asm_config._api_security_enabled and asm_config._api_security_sample_rate > 0.0
74+
return (
75+
asm_config._asm_libddwaf_available
76+
and asm_config._asm_enabled
77+
and asm_config._api_security_enabled
78+
and asm_config._api_security_sample_rate > 0.0
79+
)
7580

7681

7782
def _safe_userid(user_id):

ddtrace/settings/asm.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class ASMConfig(Env):
4949
_api_security_enabled = Env.var(bool, API_SECURITY.ENV_VAR_ENABLED, default=True)
5050
_api_security_sample_rate = Env.var(float, API_SECURITY.SAMPLE_RATE, validator=_validate_sample_rate, default=0.1)
5151
_api_security_parse_response_body = Env.var(bool, API_SECURITY.PARSE_RESPONSE_BODY, default=True)
52+
53+
# internal state of the API security Manager service.
54+
# updated in API Manager enable/disable
55+
_api_security_active = False
5256
_asm_libddwaf = build_libddwaf_filename()
5357
_asm_libddwaf_available = os.path.exists(_asm_libddwaf)
5458

@@ -110,3 +114,4 @@ class ASMConfig(Env):
110114
config._asm_enabled = False
111115
config._asm_can_be_enabled = False
112116
config._iast_enabled = False
117+
config._api_security_enabled = False

0 commit comments

Comments
 (0)