github.com/crowdsecurity/crowdsec@v1.6.1/pkg/csconfig/profiles.go (about)

     1  package csconfig
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  	"io"
     8  
     9  	"gopkg.in/yaml.v3"
    10  
    11  	"github.com/crowdsecurity/go-cs-lib/yamlpatch"
    12  
    13  	"github.com/crowdsecurity/crowdsec/pkg/models"
    14  )
    15  
    16  // var OnErrorDefault = OnErrorIgnore
    17  // var OnErrorContinue = "continue"
    18  // var OnErrorBreak = "break"
    19  // var OnErrorApply = "apply"
    20  // var OnErrorIgnore = "ignore"
    21  
    22  // 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
    23  type ProfileCfg struct {
    24  	Name          string            `yaml:"name,omitempty"`
    25  	Debug         *bool             `yaml:"debug,omitempty"`
    26  	Filters       []string          `yaml:"filters,omitempty"` // A list of OR'ed expressions. the models.Alert object
    27  	Decisions     []models.Decision `yaml:"decisions,omitempty"`
    28  	DurationExpr  string            `yaml:"duration_expr,omitempty"`
    29  	OnSuccess     string            `yaml:"on_success,omitempty"` // continue or break
    30  	OnFailure     string            `yaml:"on_failure,omitempty"` // continue or break
    31  	OnError       string            `yaml:"on_error,omitempty"`   // continue, break, error, report, apply, ignore
    32  	Notifications []string          `yaml:"notifications,omitempty"`
    33  }
    34  
    35  func (c *LocalApiServerCfg) LoadProfiles() error {
    36  	if c.ProfilesPath == "" {
    37  		return errors.New("empty profiles path")
    38  	}
    39  
    40  	patcher := yamlpatch.NewPatcher(c.ProfilesPath, ".local")
    41  
    42  	fcontent, err := patcher.PrependedPatchContent()
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	reader := bytes.NewReader(fcontent)
    48  
    49  	dec := yaml.NewDecoder(reader)
    50  	dec.KnownFields(true)
    51  
    52  	for {
    53  		t := ProfileCfg{}
    54  
    55  		err = dec.Decode(&t)
    56  		if err != nil {
    57  			if errors.Is(err, io.EOF) {
    58  				break
    59  			}
    60  
    61  			return fmt.Errorf("while decoding %s: %w", c.ProfilesPath, err)
    62  		}
    63  
    64  		c.Profiles = append(c.Profiles, &t)
    65  	}
    66  
    67  	if len(c.Profiles) == 0 {
    68  		return errors.New("zero profiles loaded for LAPI")
    69  	}
    70  
    71  	return nil
    72  }