Skip to content

Feat/brain parser - #283

Open
Leokaufi wants to merge 5 commits into
developmentfrom
feat/brain-parser
Open

Feat/brain parser#283
Leokaufi wants to merge 5 commits into
developmentfrom
feat/brain-parser

Conversation

@Leokaufi

@Leokaufi Leokaufi commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Task

Description

Integrates the Brain log-parsing algorithm (logpai/logparser, Apache-2.0)
into the library as a new BrainParser.

  • Vendored engine (parsers/brain/engine/): the Brain algorithm, file-I/O
    stripped, exposes parse(list[str]) -> list[str]. Apache-2.0 license kept
    alongside the code.
  • Wrapper (parsers/brain/parser.py): BrainParser bridges Brain (batch)
    into the streaming CoreParser lifecycle — train buffers logs,
    post_train runs Brain once and builds the TemplateMatcher, parse looks
    up each new log (falls back to <Not Found> / EventID -1 on no match).

Known limitations (open for discussion)

  • On very short / structureless log types Brain can collapse to a catch-all
    <*> template that matches everything — unknown logs then aren't flagged as
    unknown. Workaround: train on homogeneous log groups.
  • The event_id stored in the template manager (1-indexed) and the EventID
    returned by parse (0-indexed) are off by one — worth confirming whether
    intended.

How Has This Been Tested?

Unit tests in tests/test_parsers/test_brain_parser.py: multi-shape corpora,
template-building after training, and the no-match-after-training fallback.
Full suite green, all prek hooks pass. Also verified template output against
upstream Brain on real auditd logs (AIT Log Data Set).

Checklist

  • This Pull-Request goes to the development branch.
  • I have successfully run prek locally.
  • I have added tests to cover my changes.
  • I have linked the issue-id to the task-description.
  • I have performed a self-review of my own code.

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

Ports Brain's tuple-tree template-derivation core (get_frequecy_vector,
tuple_generate, tupletree.find_root/up_split/down_split, output_result)
into an in-memory, file-I/O-free form: no CSV/argparse/print, just a
LogParser.parse(contents: list[str]) -> list[str]) entry point. The
algorithm itself, including its known quirks, is kept faithful to
upstream.
Bridges Brain's batch algorithm into the streaming CoreParser lifecycle:
train() buffers each log's content (stripped of log_format headers, same
as run() does for parse()); post_train() runs Brain once over the buffer
to derive a template set and builds a TemplateMatcher from it; parse()
then looks templates up per-log via that matcher instead of re-running
Brain. No further templates are learned once training ends.
Covers config/type wiring, the pre-training fallback, the
train -> post_train -> parse lifecycle end-to-end on synthetic logs, and
the vendored Brain engine directly.
…ining

Adds a training corpus with two genuinely different log shapes and
asserts each gets its own template + EventID, and a detect-phase log
that matches neither template, asserting the "<Not Found>"/EventID=-1
fallback fires via TemplateMatcher's own no-match path (matcher already
built), not the earlier "training not finished yet" fallback branch.
@Leokaufi
Leokaufi requested a review from viktorbeck98 August 26, 2026 11:01

@ipmach ipmach left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an apache license, we cannot simple added it, also Brain from logpai is really slow and not great. We can make our own much faster in DetectMatePerformance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants