Skip to content

Commit 0dd944f

Browse files
chore(asm): migrate appsec capabilities to drop python2 compatibility (#7135)
drop python 2 and `ddtrace.internal.compat` dependency in `appsec/_capabilities.py` simplify some imports follows #7121 ## 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.
1 parent 06ec846 commit 0dd944f

File tree

1 file changed

+5
-20
lines changed

1 file changed

+5
-20
lines changed

ddtrace/appsec/_capabilities.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
import base64
22
import os
3-
import sys
43
from typing import Optional
54

6-
from ddtrace import Tracer
7-
from ddtrace import config as ddconfig
5+
import ddtrace
86
from ddtrace.appsec._utils import _appsec_rc_features_is_enabled
9-
from ddtrace.internal.compat import to_bytes_py2
107

118

129
def _appsec_rc_file_is_not_static():
1310
return "DD_APPSEC_RULES" not in os.environ
1411

1512

16-
def _appsec_rc_capabilities(test_tracer=None):
17-
# type: (Optional[Tracer]) -> str
13+
def _appsec_rc_capabilities(test_tracer: Optional[ddtrace.Tracer] = None) -> str:
1814
r"""return the bit representation of the composed capabilities in base64
1915
bit 0: Reserved
2016
bit 1: ASM 1-click Activation
@@ -30,14 +26,10 @@ def _appsec_rc_capabilities(test_tracer=None):
3026
...
3127
256 -> 100000000 -> b'\x01\x00' -> b'AQA='
3228
"""
33-
if test_tracer is None:
34-
from ddtrace import tracer
35-
else:
36-
tracer = test_tracer
37-
29+
tracer = ddtrace.tracer if test_tracer is None else test_tracer
3830
value = 0b0
3931
result = ""
40-
if ddconfig._remote_config_enabled:
32+
if ddtrace.config._remote_config_enabled:
4133
if _appsec_rc_features_is_enabled():
4234
value |= 1 << 1 # Enable ASM_ACTIVATION
4335
if tracer._appsec_processor and _appsec_rc_file_is_not_static():
@@ -50,12 +42,5 @@ def _appsec_rc_capabilities(test_tracer=None):
5042
value |= 1 << 8 # Enable ASM_CUSTOM_RULES
5143
value |= 1 << 9 # Enable ASM_CUSTOM_BLOCKING_RESPONSE
5244
value |= 1 << 10 # Enable ASM_TRUSTED_IPS
53-
54-
if sys.version_info.major < 3:
55-
bytes_res = to_bytes_py2(value, (value.bit_length() + 7) // 8, "big")
56-
# "type: ignore" because mypy does not notice this is for Python2 b64encode
57-
result = str(base64.b64encode(bytes_res)) # type: ignore
58-
else:
59-
result = str(base64.b64encode(value.to_bytes((value.bit_length() + 7) // 8, "big")), encoding="utf-8")
60-
45+
result = base64.b64encode(value.to_bytes((value.bit_length() + 7) // 8, "big")).decode()
6146
return result

0 commit comments

Comments
 (0)