@cryptotaxi247 / CoPilot / commits / e37e7ce7

fix(agents): coerce wazuh_last_seen fallback to datetime on insert (#874)

Wazuh agents in `never_connected` state report `agent_last_seen="Unknown"`, which the create paths were passing through to MySQL as the raw string `"1970-01-01T00:00:00+00:00"`. MySQL DATETIME rejects ISO-8601 with a timezone offset, so INSERT failed with error 1292. Update paths already strptime the same fallback; mirror that in `create_from_model` and `create_wazuh_agent_from_model` and strip tz info so the value matches what `agent_last_seen_as_datetime` produces. Co-authored-by: taylor_socfortress <taylor.walton@socfortress.co> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

taylorcopilot committed May 15, 2026 at 15:02 UTC e37e7ce70d84f459407cf4ac13c7427ed6c15d1f
1 file changed +10 -4
backend/app/db/universal_models.py
+10 -4
@@ -117,8 +117,11 @@ class Agents(SQLModel, table=True):
117 @classmethod
118 def create_from_model(cls, wazuh_agent, velociraptor_agent, customer_code):
119 # Check if agent_last_seen is 'Unknown' and set wazuh_last_seen accordingly
120 - if wazuh_agent.agent_last_seen == "Unknown":
121 - wazuh_last_seen_value = "1970-01-01T00:00:00+00:00" # default datetime value
120 + if wazuh_agent.agent_last_seen in ("Unknown", "1970-01-01T00:00:00+00:00"):
121 + wazuh_last_seen_value = datetime.strptime(
122 + "1970-01-01T00:00:00+00:00",
123 + "%Y-%m-%dT%H:%M:%S%z",
124 + ).replace(tzinfo=None)
125 else:
126 wazuh_last_seen_value = wazuh_agent.agent_last_seen_as_datetime
127
@@ -144,8 +147,11 @@ class Agents(SQLModel, table=True):
147
148 @classmethod
149 def create_wazuh_agent_from_model(cls, wazuh_agent, customer_code):
147 - if wazuh_agent.agent_last_seen == "Unknown":
148 - wazuh_last_seen_value = "1970-01-01T00:00:00+00:00"
150 + if wazuh_agent.agent_last_seen in ("Unknown", "1970-01-01T00:00:00+00:00"):
151 + wazuh_last_seen_value = datetime.strptime(
152 + "1970-01-01T00:00:00+00:00",
153 + "%Y-%m-%dT%H:%M:%S%z",
154 + ).replace(tzinfo=None)
155 else:
156 wazuh_last_seen_value = wazuh_agent.agent_last_seen_as_datetime
157