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

Method LoadByPath

pkg/appsec/appsec.go:379–480  ·  view source on GitHub ↗
(file string)

Source from the content-addressed store, hash-verified

377}
378
379func (wc *AppsecConfig) LoadByPath(file string) error {
380 wc.Logger.Debugf("loading config %s", file)
381
382 yamlFile, err := os.ReadFile(file)
383 if err != nil {
384 return fmt.Errorf("unable to read file %s : %w", file, err)
385 }
386
387 // as LoadByPath can be called several time, we append rules/hooks, but override other options
388 var tmp AppsecConfig
389
390 err = yaml.UnmarshalStrict(yamlFile, &tmp)
391 if err != nil {
392 return fmt.Errorf("unable to parse yaml file %s : %w", file, err)
393 }
394
395 // Normalize phase-scoped sections: merge rules, options, and variables_tracking
396 // into flat fields. Hooks stay in the phase sections for Build() to compile separately.
397 tmp.normalizePhaseScoped()
398
399 if wc.Name == "" && tmp.Name != "" {
400 wc.Name = tmp.Name
401 }
402
403 // We can append rules/hooks
404 if tmp.OutOfBandRules != nil {
405 wc.OutOfBandRules = append(wc.OutOfBandRules, tmp.OutOfBandRules...)
406 }
407
408 if tmp.InBandRules != nil {
409 wc.InBandRules = append(wc.InBandRules, tmp.InBandRules...)
410 }
411
412 if tmp.OnLoad != nil {
413 wc.OnLoad = append(wc.OnLoad, tmp.OnLoad...)
414 }
415
416 if tmp.PreEval != nil {
417 wc.PreEval = append(wc.PreEval, tmp.PreEval...)
418 }
419
420 if tmp.PostEval != nil {
421 wc.PostEval = append(wc.PostEval, tmp.PostEval...)
422 }
423
424 if tmp.OnMatch != nil {
425 wc.OnMatch = append(wc.OnMatch, tmp.OnMatch...)
426 }
427
428 if tmp.VariablesTracking != nil {
429 wc.VariablesTracking = append(wc.VariablesTracking, tmp.VariablesTracking...)
430 }
431
432 // Append phase-scoped hooks
433 if tmp.InBand != nil {
434 if wc.InBand == nil {
435 wc.InBand = &AppsecPhaseConfig{}
436 }

Calls 1

normalizePhaseScopedMethod · 0.95