Fix TypeError and improve error handling in audit event processing
Summary
This MR fixes multiple TypeError
issues and improves error handling when processing audit events with missing or None
field values.
Problems Fixed
-
TypeError with
as
field: The audit log compiler was crashing with:TypeError: can only concatenate str (not "NoneType") to str
This happened when trying to concatenate a
None
value from theas
field. -
Potential KeyError with change fields: The code was directly accessing
change
,from
, andto
fields without checking if they exist, which could cause KeyError exceptions.
Solutions
as
field concatenation
1. Fixed # Before
if "as" in event["details"]:
flat_event["action"] += " as " + event["details"]["as"]
# After
if "as" in event["details"] and event["details"]["as"] is not None:
flat_event["action"] += " as " + event["details"]["as"]
2. Added defensive handling for change event fields
# Before
flat_event["action"] = "%s from %s to %s" % (event["details"]["change"],event["details"]["from"],event["details"]["to"])
# After
change = event["details"].get("change", "unknown")
from_value = event["details"].get("from", "unknown")
to_value = event["details"].get("to", "unknown")
flat_event["action"] = "%s from %s to %s" % (change, from_value, to_value)
Testing
These changes ensure that:
- When
as
field exists and has a value, it's concatenated as before - When
as
field exists but isNone
, concatenation is skipped (no crash) - When
as
field doesn't exist, behavior remains unchanged - When
change
,from
, orto
fields are missing, "unknown" is used as a fallback - All audit events can be processed without TypeErrors or KeyErrors
Related Issues
Closes #3 (closed)
Edited by Ricardo Amarilla