Files
NODEDC_MISSION_CORE/apps/node-agent/packaging/uplink/evidence.py
T

61 lines
3.2 KiB
Python

"""Bounded local controller evidence. Never sent as periodic WAN telemetry."""
import re
import subprocess
class Evidence:
def record_error(self, error):
# Class/step are sufficient to locate the failing boundary. Never copy
# exception text or subprocess arguments, which may contain secrets.
self.operation['failure'] = dict(
step=self.operation.get('apply_step', self.operation.get('phase', 'unknown')),
error_type=type(error).__name__[:80])
if callable(getattr(error, 'get_dbus_name', None)):
name = error.get_dbus_name()
if isinstance(name, str) and re.fullmatch(r'[A-Za-z0-9_.]{1,160}', name):
self.operation['failure']['dbus_error'] = name
if isinstance(error, subprocess.CalledProcessError):
cmd = error.cmd
if isinstance(cmd, (tuple, list)) and list(cmd[:1]) == ['/usr/sbin/ip']:
# ip route/rule commands contain no authentication material.
# Preserve the actual kernel failure locally for diagnosis.
self.operation['failure']['command'] = [str(v)[:128] for v in cmd[:32]]
self.operation['failure']['exit_code'] = error.returncode
raw = error.stderr or b''
self.operation['failure']['kernel_error'] = (
raw.decode(errors='replace') if isinstance(raw, bytes) else raw)[:512]
self.save()
def record_transition(self):
op = self.operation
state = dict(operation_id=op['id'], phase=op['phase'], target=op.get('target'),
previous=op.get('previous'), selected=self.watched_target(),
fallback_enabled=self.fallback_enabled(),
local_override=bool(op.get('local_override')),
reason=op.get('recovery_reason', ''), message=op.get('message', ''),
wait_reason=op.get('wait_reason', ''), failure=op.get('failure'))
prior = op.get('diagnostic_events', [])
if prior and prior[-1]['state'] == state:
return
sequence = op.get('diagnostic_sequence', 0) + 1
op['diagnostic_sequence'] = sequence
op['diagnostic_events'] = [*prior, dict(
sequence=sequence, at=self.wall_clock(), monotonic_ms=self.clock()*1000,
helper_instance=self.helper_instance, state=state)][-16:]
def local_evidence(self, state):
op = self.operation or {}
policy = op.get('watch_policy') or op.get('policy', {})
return dict(
schema='missioncore.uplink-evidence/v1', helper_instance=self.helper_instance,
sampled_at=self.wall_clock(), monotonic_ms=self.clock()*1000,
native_current_id=state.get('current_id', ''),
selected_id=self.watched_target() if op else '',
interface=policy.get('interface', ''), transport_mark=policy.get('mark'),
path_proven=self.path_ok, proof_age_ms=max(0, self.clock()-self.last_probe)*1000,
core_reply_age_ms=max(0, self.clock()-self.last_core)*1000,
fallback_enabled=self.fallback_enabled(), phase=op.get('phase', 'unselected'),
local_override=bool(op.get('local_override')),
events=op.get('diagnostic_events', [])[-16:])