2020-11-30 09:37:17 +00:00
package csconfig
import (
2022-05-18 08:08:37 +00:00
"bytes"
2020-11-30 09:37:17 +00:00
"fmt"
"io"
2023-05-23 08:52:47 +00:00
"github.com/crowdsecurity/go-cs-lib/pkg/yamlpatch"
2020-11-30 09:37:17 +00:00
"github.com/crowdsecurity/crowdsec/pkg/models"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
2023-01-19 07:45:50 +00:00
// var OnErrorDefault = OnErrorIgnore
// var OnErrorContinue = "continue"
// var OnErrorBreak = "break"
// var OnErrorApply = "apply"
// var OnErrorIgnore = "ignore"
// Profile structure(s) are used by the local API to "decide" what kind of decision should be applied when a scenario with an active remediation has been triggered
2020-11-30 09:37:17 +00:00
type ProfileCfg struct {
2022-06-22 09:29:52 +00:00
Name string ` yaml:"name,omitempty" `
Debug * bool ` yaml:"debug,omitempty" `
Filters [ ] string ` yaml:"filters,omitempty" ` //A list of OR'ed expressions. the models.Alert object
Decisions [ ] models . Decision ` yaml:"decisions,omitempty" `
DurationExpr string ` yaml:"duration_expr,omitempty" `
OnSuccess string ` yaml:"on_success,omitempty" ` //continue or break
OnFailure string ` yaml:"on_failure,omitempty" ` //continue or break
2023-01-19 07:45:50 +00:00
OnError string ` yaml:"on_error,omitempty" ` //continue, break, error, report, apply, ignore
2022-06-22 09:29:52 +00:00
Notifications [ ] string ` yaml:"notifications,omitempty" `
2020-11-30 09:37:17 +00:00
}
func ( c * LocalApiServerCfg ) LoadProfiles ( ) error {
if c . ProfilesPath == "" {
return fmt . Errorf ( "empty profiles path" )
}
2022-05-18 08:08:37 +00:00
patcher := yamlpatch . NewPatcher ( c . ProfilesPath , ".local" )
fcontent , err := patcher . PrependedPatchContent ( )
2020-11-30 09:37:17 +00:00
if err != nil {
2022-05-18 08:08:37 +00:00
return err
2020-11-30 09:37:17 +00:00
}
2022-05-18 08:08:37 +00:00
reader := bytes . NewReader ( fcontent )
2020-11-30 09:37:17 +00:00
//process the yaml
2022-05-18 08:08:37 +00:00
dec := yaml . NewDecoder ( reader )
2020-11-30 09:37:17 +00:00
dec . SetStrict ( true )
for {
t := ProfileCfg { }
err = dec . Decode ( & t )
if err != nil {
2022-11-29 08:16:07 +00:00
if errors . Is ( err , io . EOF ) {
2020-11-30 09:37:17 +00:00
break
}
return errors . Wrapf ( err , "while decoding %s" , c . ProfilesPath )
}
c . Profiles = append ( c . Profiles , & t )
}
if len ( c . Profiles ) == 0 {
return fmt . Errorf ( "zero profiles loaded for LAPI" )
}
return nil
}