mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 12:25:53 +02:00
fix: error check on postoverflow config (#3576)
* fix error check on postoverflow config * lint
This commit is contained in:
parent
922c29983f
commit
7e280b23af
1 changed files with 33 additions and 20 deletions
|
@ -33,25 +33,31 @@ type Parsers struct {
|
||||||
EnricherCtx EnricherCtx
|
EnricherCtx EnricherCtx
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init(c map[string]interface{}) (*UnixParserCtx, error) {
|
func Init(c map[string]any) (*UnixParserCtx, error) {
|
||||||
r := UnixParserCtx{}
|
r := UnixParserCtx{}
|
||||||
r.Grok = grokky.NewBase()
|
r.Grok = grokky.NewBase()
|
||||||
r.Grok.UseRe2 = fflag.Re2GrokSupport.IsEnabled()
|
r.Grok.UseRe2 = fflag.Re2GrokSupport.IsEnabled()
|
||||||
|
|
||||||
files, err := os.ReadDir(c["patterns"].(string))
|
files, err := os.ReadDir(c["patterns"].(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r.DataFolder = c["data"].(string)
|
r.DataFolder = c["data"].(string)
|
||||||
for _, f := range files {
|
|
||||||
if strings.Contains(f.Name(), ".") || f.IsDir() {
|
for _, file := range files {
|
||||||
|
if strings.Contains(file.Name(), ".") || file.IsDir() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := r.Grok.AddFromFile(filepath.Join(c["patterns"].(string), f.Name())); err != nil {
|
|
||||||
log.Errorf("failed to load pattern %s : %v", f.Name(), err)
|
if err := r.Grok.AddFromFile(filepath.Join(c["patterns"].(string), file.Name())); err != nil {
|
||||||
|
log.Errorf("failed to load pattern %s: %v", file.Name(), err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("Loaded %d pattern files", len(files))
|
log.Debugf("Loaded %d pattern files", len(files))
|
||||||
|
|
||||||
return &r, nil
|
return &r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,9 +77,11 @@ func NewParsers(hub *cwhub.Hub) *Parsers {
|
||||||
Filename: hubParserItem.State.LocalPath,
|
Filename: hubParserItem.State.LocalPath,
|
||||||
Stage: hubParserItem.Stage,
|
Stage: hubParserItem.Stage,
|
||||||
}
|
}
|
||||||
|
|
||||||
if itemType == cwhub.PARSERS {
|
if itemType == cwhub.PARSERS {
|
||||||
parsers.StageFiles = append(parsers.StageFiles, stagefile)
|
parsers.StageFiles = append(parsers.StageFiles, stagefile)
|
||||||
}
|
}
|
||||||
|
|
||||||
if itemType == cwhub.POSTOVERFLOWS {
|
if itemType == cwhub.POSTOVERFLOWS {
|
||||||
parsers.PovfwStageFiles = append(parsers.PovfwStageFiles, stagefile)
|
parsers.PovfwStageFiles = append(parsers.PovfwStageFiles, stagefile)
|
||||||
}
|
}
|
||||||
|
@ -85,6 +93,7 @@ func NewParsers(hub *cwhub.Hub) *Parsers {
|
||||||
return parsers.StageFiles[i].Filename < parsers.StageFiles[j].Filename
|
return parsers.StageFiles[i].Filename < parsers.StageFiles[j].Filename
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if parsers.PovfwStageFiles != nil {
|
if parsers.PovfwStageFiles != nil {
|
||||||
sort.Slice(parsers.PovfwStageFiles, func(i, j int) bool {
|
sort.Slice(parsers.PovfwStageFiles, func(i, j int) bool {
|
||||||
return parsers.PovfwStageFiles[i].Filename < parsers.PovfwStageFiles[j].Filename
|
return parsers.PovfwStageFiles[i].Filename < parsers.PovfwStageFiles[j].Filename
|
||||||
|
@ -99,30 +108,32 @@ func LoadParsers(cConfig *csconfig.Config, parsers *Parsers) (*Parsers, error) {
|
||||||
|
|
||||||
patternsDir := cConfig.ConfigPaths.PatternDir
|
patternsDir := cConfig.ConfigPaths.PatternDir
|
||||||
log.Infof("Loading grok library %s", patternsDir)
|
log.Infof("Loading grok library %s", patternsDir)
|
||||||
|
|
||||||
/* load base regexps for two grok parsers */
|
/* load base regexps for two grok parsers */
|
||||||
parsers.Ctx, err = Init(map[string]interface{}{
|
parsers.Ctx, err = Init(map[string]any{
|
||||||
"patterns": patternsDir,
|
"patterns": patternsDir,
|
||||||
"data": cConfig.ConfigPaths.DataDir,
|
"data": cConfig.ConfigPaths.DataDir,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return parsers, fmt.Errorf("failed to load parser patterns : %v", err)
|
return parsers, fmt.Errorf("failed to load parser patterns: %w", err)
|
||||||
}
|
}
|
||||||
parsers.Povfwctx, err = Init(map[string]interface{}{
|
|
||||||
|
parsers.Povfwctx, err = Init(map[string]any{
|
||||||
"patterns": patternsDir,
|
"patterns": patternsDir,
|
||||||
"data": cConfig.ConfigPaths.DataDir,
|
"data": cConfig.ConfigPaths.DataDir,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return parsers, fmt.Errorf("failed to load postovflw parser patterns : %v", err)
|
return parsers, fmt.Errorf("failed to load postovflw parser patterns: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Load enrichers
|
Load enrichers
|
||||||
*/
|
*/
|
||||||
log.Infof("Loading enrich plugins")
|
log.Info("Loading enrich plugins")
|
||||||
|
|
||||||
parsers.EnricherCtx, err = Loadplugin()
|
parsers.EnricherCtx, err = Loadplugin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return parsers, fmt.Errorf("failed to load enrich plugin : %v", err)
|
return parsers, fmt.Errorf("failed to load enrich plugin: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -133,19 +144,20 @@ func LoadParsers(cConfig *csconfig.Config, parsers *Parsers) (*Parsers, error) {
|
||||||
|
|
||||||
parsers.Nodes, err = LoadStages(parsers.StageFiles, parsers.Ctx, parsers.EnricherCtx)
|
parsers.Nodes, err = LoadStages(parsers.StageFiles, parsers.Ctx, parsers.EnricherCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return parsers, fmt.Errorf("failed to load parser config : %v", err)
|
return parsers, fmt.Errorf("failed to load parser config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(parsers.PovfwStageFiles) > 0 {
|
if len(parsers.PovfwStageFiles) > 0 {
|
||||||
log.Infof("Loading postoverflow parsers")
|
log.Info("Loading postoverflow parsers")
|
||||||
parsers.Povfwnodes, err = LoadStages(parsers.PovfwStageFiles, parsers.Povfwctx, parsers.EnricherCtx)
|
|
||||||
} else {
|
|
||||||
log.Infof("No postoverflow parsers to load")
|
|
||||||
parsers.Povfwnodes = []Node{}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
parsers.Povfwnodes, err = LoadStages(parsers.PovfwStageFiles, parsers.Povfwctx, parsers.EnricherCtx)
|
||||||
return parsers, fmt.Errorf("failed to load postoverflow config : %v", err)
|
if err != nil {
|
||||||
|
return parsers, fmt.Errorf("failed to load postoverflow config: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Info("No postoverflow parsers to load")
|
||||||
|
|
||||||
|
parsers.Povfwnodes = []Node{}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cConfig.Prometheus != nil && cConfig.Prometheus.Enabled {
|
if cConfig.Prometheus != nil && cConfig.Prometheus.Enabled {
|
||||||
|
@ -159,5 +171,6 @@ func LoadParsers(cConfig *csconfig.Config, parsers *Parsers) (*Parsers, error) {
|
||||||
parsers.Povfwctx.Grok = grokky.Host{}
|
parsers.Povfwctx.Grok = grokky.Host{}
|
||||||
parsers.StageFiles = []Stagefile{}
|
parsers.StageFiles = []Stagefile{}
|
||||||
parsers.PovfwStageFiles = []Stagefile{}
|
parsers.PovfwStageFiles = []Stagefile{}
|
||||||
|
|
||||||
return parsers, nil
|
return parsers, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue