Skip to content

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

  1. 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 the as field.

  2. Potential KeyError with change fields: The code was directly accessing change, from, and to fields without checking if they exist, which could cause KeyError exceptions.

Solutions

1. Fixed as field concatenation

# 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 is None, concatenation is skipped (no crash)
  • When as field doesn't exist, behavior remains unchanged
  • When change, from, or to 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

Merge request reports

Loading