MCPcopy
hub / github.com/crowdsecurity/crowdsec / compile

Method compile

pkg/parser/node.go:401–528  ·  view source on GitHub ↗
(pctx *UnixParserCtx, ectx EnricherCtx)

Source from the content-addressed store, hash-verified

399var dumpr = spew.ConfigState{MaxDepth: 1, DisablePointerAddresses: true}
400
401func (n *Node) compile(pctx *UnixParserCtx, ectx EnricherCtx) error {
402 var err error
403
404 valid := false
405
406 n.rn = seed.Generate()
407
408 n.EnrichFunctions = ectx
409 log.Tracef("compile, node is %s", n.Stage)
410 /* if the node has debugging enabled, create a specific logger with debug
411 that will be used only for processing this node ;) */
412
413 var clog *log.Entry
414
415 if n.Debug {
416 clog = logging.SubLogger(log.StandardLogger(), "parser", log.DebugLevel)
417 clog.Infof("%s has debug enabled", n.Name)
418 } else {
419 /* else bind it to the default one (might find something more elegant here)*/
420 clog = log.WithField("module", "parser")
421 }
422
423 n.Logger = clog.WithField("id", n.rn)
424
425 /* display info about top-level nodes, they should be the only one with explicit stage name ?*/
426 n.Logger = n.Logger.WithFields(log.Fields{"stage": n.Stage, "name": n.Name})
427
428 if n.Logger.Logger.IsLevelEnabled(log.TraceLevel) {
429 n.Logger.Tracef("Compiling: %s", dumpr.Sdump(n))
430 }
431
432 // compile filter if present
433 if n.Filter != "" {
434 n.RunTimeFilter, err = expr.Compile(n.Filter, exprhelpers.GetExprOptions(map[string]any{"evt": &pipeline.Event{}})...)
435 if err != nil {
436 return fmt.Errorf("compilation of %q failed: %v", n.Filter, err)
437 }
438 }
439
440 /* handle pattern_syntax and groks */
441 for _, pattern := range n.SubGroks {
442 n.Logger.Tracef("Adding subpattern '%s': '%s'", pattern.Key, pattern.Value)
443
444 if err = pctx.Grok.Add(pattern.Key.(string), pattern.Value.(string)); err != nil {
445 if errors.Is(err, grokky.ErrAlreadyExist) {
446 n.Logger.Warningf("grok '%s' already registred", pattern.Key)
447 continue
448 }
449
450 n.Logger.Errorf("Unable to compile subpattern %s: %v", pattern.Key, err)
451
452 return err
453 }
454 }
455
456 if n.Grok.RegexpName != "" || n.Grok.RegexpValue != "" || n.Grok.ExpValue != "" {
457 rg, err := n.Grok.Compile(pctx, n.Logger)
458 if err != nil {

Callers 2

processStageFileFunction · 0.95
TestParserConfigsFunction · 0.95

Calls 9

CompileWLsMethod · 0.95
validateMethod · 0.95
SubLoggerFunction · 0.92
GetExprOptionsFunction · 0.92
TracefMethod · 0.80
StandardLoggerMethod · 0.80
ErrorMethod · 0.65
CompileMethod · 0.45
AddMethod · 0.45

Tested by 1

TestParserConfigsFunction · 0.76