Skip to content

Commit 3e39c6c

Browse files
fix(rcm): ensure proper validation of responses from the agent [backport #4970 to 1.8] (#4974)
## Description Backport of #4970, #4979, #4980, and #4988 Add more validations to Remote Config response from agent --------- Co-authored-by: Brett Langdon <[email protected]>
1 parent dbddf76 commit 3e39c6c

File tree

5 files changed

+850
-17
lines changed

5 files changed

+850
-17
lines changed

ddtrace/internal/remoteconfig/client.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,9 @@ class RemoteConfigClient(object):
187187
and dispatches configurations to registered products.
188188
"""
189189

190-
def __init__(self):
191-
# type: () -> None
192-
self.id = str(uuid.uuid4())
193-
self.agent_url = agent_url = agent.get_trace_url()
194-
self._conn = agent.get_connection(agent_url, timeout=agent.get_trace_agent_timeout())
195-
self._headers = {"content-type": "application/json"}
196-
197-
container_info = container.get_container_info()
198-
if container_info is not None:
199-
container_id = container_info.container_id
200-
if container_id is not None:
201-
self._headers["Datadog-Container-Id"] = container_id
202-
190+
@staticmethod
191+
def _get_version():
192+
# type: () -> str
203193
# The library uses a PEP 440-compliant versioning scheme, but the
204194
# RCM spec requires that we use a SemVer-compliant version.
205195
#
@@ -222,11 +212,25 @@ def __init__(self):
222212
tracer_version = tracer_version.replace("rc", "-rc", 1)
223213
elif ".dev" in tracer_version:
224214
tracer_version = tracer_version.replace(".dev", "-dev", 1)
215+
return tracer_version
216+
217+
def __init__(self):
218+
# type: () -> None
219+
self.id = str(uuid.uuid4())
220+
self.agent_url = agent_url = agent.get_trace_url()
221+
self._conn = agent.get_connection(agent_url, timeout=agent.get_trace_agent_timeout())
222+
self._headers = {"content-type": "application/json"}
223+
224+
container_info = container.get_container_info()
225+
if container_info is not None:
226+
container_id = container_info.container_id
227+
if container_id is not None:
228+
self._headers["Datadog-Container-Id"] = container_id
225229

226230
self._client_tracer = dict(
227231
runtime_id=runtime.get_runtime_id(),
228232
language="python",
229-
tracer_version=tracer_version,
233+
tracer_version=self._get_version(),
230234
service=ddtrace.config.service,
231235
env=ddtrace.config.env,
232236
app_version=ddtrace.config.version,
@@ -341,6 +345,7 @@ def _process_response(self, data):
341345
paths = {_.path for _ in payload.target_files}
342346
paths = paths.union({_["path"] for _ in self.cached_target_files})
343347

348+
# !(payload.client_configs is a subset of paths or payload.client_configs is equal to paths)
344349
if not set(payload.client_configs) <= paths:
345350
raise RemoteConfigError("Not all client configurations have target files")
346351

@@ -353,7 +358,15 @@ def _process_response(self, data):
353358
return
354359

355360
client_configs = {k: v for k, v in targets.items() if k in payload.client_configs}
356-
log.debug("Retrieved client configs: %s", client_configs)
361+
log.debug("Retrieved client configs last version %s: %s", last_targets_version, client_configs)
362+
363+
for target in payload.target_files:
364+
if (payload.targets.signed.targets and not payload.targets.signed.targets.get(target.path)) and (
365+
client_configs and not client_configs.get(target.path)
366+
):
367+
raise RemoteConfigError(
368+
"target file %s not exists in client_config and signed targets" % (target.path,)
369+
)
357370

358371
# 2. Remove previously applied configurations
359372
applied_configs = dict()
@@ -418,8 +431,8 @@ def request(self):
418431
# type: () -> None
419432
try:
420433
state = self._build_state()
421-
payload = json.dumps(self._build_payload(state), indent=2)
422-
# log.debug("request payload: %r", payload)
434+
payload = json.dumps(self._build_payload(state))
435+
423436
response = self._send_request(payload)
424437
if response is None:
425438
return
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
remote config: ensure proper validation of responses from the agent.

0 commit comments

Comments
 (0)