bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/csconfig/crowdsec_service.go (about)

     1  package csconfig
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/pkg/errors"
     9  	log "github.com/sirupsen/logrus"
    10  )
    11  
    12  /*Configurations needed for synsec to load parser/scenarios/... + acquisition*/
    13  type SynsecServiceCfg struct {
    14  	AcquisitionFilePath string `yaml:"acquisition_path,omitempty"`
    15  	AcquisitionDirPath  string `yaml:"acquisition_dir,omitempty"`
    16  
    17  	AcquisitionFiles     []string          `yaml:"-"`
    18  	ParserRoutinesCount  int               `yaml:"parser_routines"`
    19  	BucketsRoutinesCount int               `yaml:"buckets_routines"`
    20  	OutputRoutinesCount  int               `yaml:"output_routines"`
    21  	SimulationConfig     *SimulationConfig `yaml:"-"`
    22  	LintOnly             bool              `yaml:"-"`                          //if set to true, exit after loading configs
    23  	BucketStateFile      string            `yaml:"state_input_file,omitempty"` //if we need to unserialize buckets at start
    24  	BucketStateDumpDir   string            `yaml:"state_output_dir,omitempty"` //if we need to unserialize buckets on shutdown
    25  	BucketsGCEnabled     bool              `yaml:"-"`                          //we need to garbage collect buckets when in forensic mode
    26  
    27  	HubDir             string `yaml:"-"`
    28  	DataDir            string `yaml:"-"`
    29  	ConfigDir          string `yaml:"-"`
    30  	HubIndexFile       string `yaml:"-"`
    31  	SimulationFilePath string `yaml:"-"`
    32  }
    33  
    34  func (c *Config) LoadSynsec() error {
    35  	var err error
    36  	// Configuration paths are dependency to load synsec configuration
    37  	if err := c.LoadConfigurationPaths(); err != nil {
    38  		return err
    39  	}
    40  
    41  	if c.Synsec == nil {
    42  		log.Warningf("synsec agent is disabled")
    43  		c.DisableAgent = true
    44  		return nil
    45  	}
    46  	if c.Synsec.AcquisitionFilePath != "" {
    47  		log.Debugf("non-empty acquisition file path %s", c.Synsec.AcquisitionFilePath)
    48  		if _, err := os.Stat(c.Synsec.AcquisitionFilePath); err != nil {
    49  			return errors.Wrapf(err, "while checking acquisition path %s", c.Synsec.AcquisitionFilePath)
    50  		}
    51  		c.Synsec.AcquisitionFiles = append(c.Synsec.AcquisitionFiles, c.Synsec.AcquisitionFilePath)
    52  	}
    53  	if c.Synsec.AcquisitionDirPath != "" {
    54  		c.Synsec.AcquisitionDirPath, err = filepath.Abs(c.Synsec.AcquisitionDirPath)
    55  		if err != nil {
    56  			return errors.Wrapf(err, "can't get absolute path of '%s'", c.Synsec.AcquisitionDirPath)
    57  		}
    58  		files, err := filepath.Glob(c.Synsec.AcquisitionDirPath + "/*.yaml")
    59  		if err != nil {
    60  			return errors.Wrap(err, "while globing acquis_dir")
    61  		}
    62  		c.Synsec.AcquisitionFiles = append(c.Synsec.AcquisitionFiles, files...)
    63  	}
    64  	if c.Synsec.AcquisitionDirPath == "" && c.Synsec.AcquisitionFilePath == "" {
    65  		return fmt.Errorf("no acquisition_path nor acquisition_dir")
    66  	}
    67  
    68  	c.Synsec.ConfigDir = c.ConfigPaths.ConfigDir
    69  	c.Synsec.DataDir = c.ConfigPaths.DataDir
    70  	c.Synsec.HubDir = c.ConfigPaths.HubDir
    71  	c.Synsec.HubIndexFile = c.ConfigPaths.HubIndexFile
    72  	if c.Synsec.ParserRoutinesCount <= 0 {
    73  		c.Synsec.ParserRoutinesCount = 1
    74  	}
    75  
    76  	if c.Synsec.BucketsRoutinesCount <= 0 {
    77  		c.Synsec.BucketsRoutinesCount = 1
    78  	}
    79  
    80  	if c.Synsec.OutputRoutinesCount <= 0 {
    81  		c.Synsec.OutputRoutinesCount = 1
    82  	}
    83  
    84  	var synsecCleanup = []*string{
    85  		&c.Synsec.AcquisitionFilePath,
    86  	}
    87  	for _, k := range synsecCleanup {
    88  		if *k == "" {
    89  			continue
    90  		}
    91  		*k, err = filepath.Abs(*k)
    92  		if err != nil {
    93  			return errors.Wrapf(err, "failed to get absolute path of '%s'", *k)
    94  		}
    95  	}
    96  	for i, file := range c.Synsec.AcquisitionFiles {
    97  		f, err := filepath.Abs(file)
    98  		if err != nil {
    99  			return errors.Wrapf(err, "failed to get absolute path of '%s'", file)
   100  		}
   101  		c.Synsec.AcquisitionFiles[i] = f
   102  	}
   103  
   104  	if err := c.LoadAPIClient(); err != nil {
   105  		return fmt.Errorf("loading api client: %s", err.Error())
   106  	}
   107  	if err := c.LoadHub(); err != nil {
   108  		return fmt.Errorf("loading hub: %s", err)
   109  	}
   110  	return nil
   111  }