2020-11-30 09:37:17 +00:00
package csconfig
import (
2022-05-18 08:08:37 +00:00
"bytes"
2023-06-23 12:04:58 +00:00
"errors"
2020-11-30 09:37:17 +00:00
"fmt"
"io"
2024-03-04 13:22:53 +00:00
"gopkg.in/yaml.v3"
2023-10-09 09:10:51 +00:00
2023-07-28 14:35:08 +00:00
"github.com/crowdsecurity/go-cs-lib/yamlpatch"
2023-05-23 08:52:47 +00:00
2020-11-30 09:37:17 +00:00
"github.com/crowdsecurity/crowdsec/pkg/models"
)
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" `
2024-03-04 13:22:53 +00:00
Filters [ ] string ` yaml:"filters,omitempty" ` // A list of OR'ed expressions. the models.Alert object
2022-06-22 09:29:52 +00:00
Decisions [ ] models . Decision ` yaml:"decisions,omitempty" `
DurationExpr string ` yaml:"duration_expr,omitempty" `
2024-03-04 13:22:53 +00:00
OnSuccess string ` yaml:"on_success,omitempty" ` // continue or break
OnFailure string ` yaml:"on_failure,omitempty" ` // continue or break
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 == "" {
2024-03-04 13:22:53 +00:00
return errors . New ( "empty profiles path" )
2020-11-30 09:37:17 +00:00
}
2022-05-18 08:08:37 +00:00
patcher := yamlpatch . NewPatcher ( c . ProfilesPath , ".local" )
2024-03-04 13:22:53 +00:00
2022-05-18 08:08:37 +00:00
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
}
2024-03-04 13:22:53 +00:00
2022-05-18 08:08:37 +00:00
reader := bytes . NewReader ( fcontent )
2020-11-30 09:37:17 +00:00
2022-05-18 08:08:37 +00:00
dec := yaml . NewDecoder ( reader )
2024-03-04 13:22:53 +00:00
dec . KnownFields ( true )
2020-11-30 09:37:17 +00:00
for {
t := ProfileCfg { }
2024-03-04 13:22:53 +00:00
2020-11-30 09:37:17 +00:00
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
}
2024-03-04 13:22:53 +00:00
2023-06-23 12:04:58 +00:00
return fmt . Errorf ( "while decoding %s: %w" , c . ProfilesPath , err )
2020-11-30 09:37:17 +00:00
}
2024-03-04 13:22:53 +00:00
2020-11-30 09:37:17 +00:00
c . Profiles = append ( c . Profiles , & t )
}
if len ( c . Profiles ) == 0 {
2024-03-04 13:22:53 +00:00
return errors . New ( "zero profiles loaded for LAPI" )
2020-11-30 09:37:17 +00:00
}
2024-03-04 13:22:53 +00:00
2020-11-30 09:37:17 +00:00
return nil
}