diff --git a/pkg/acquisition/acquisition.go b/pkg/acquisition/acquisition.go index 8cbc82bb4..b3fb34f08 100644 --- a/pkg/acquisition/acquisition.go +++ b/pkg/acquisition/acquisition.go @@ -67,7 +67,7 @@ type DataSource interface { Configure([]byte, *log.Entry) error // Configure the datasource ConfigureByDSN(string, *log.Entry) error // Configure the datasource GetMode() string // Get the mode (TAIL, CAT or SERVER) - SupportedModes() []string // Returns the mode supported by the datasource + SupportedModes() []string // TO REMOVE : Returns the mode supported by the datasource SupportedDSN() []string // Returns the list of supported URI schemes (file:// s3:// ...) OneShotAcquisition(chan types.Event, *tomb.Tomb) error // Start one shot acquisition(eg, cat a file) LiveAcquisition(chan types.Event, *tomb.Tomb) error // Start live acquisition (eg, tail a file) @@ -111,17 +111,6 @@ func DataSourceConfigure(yamlConfig []byte, commonConfig configuration.DataSourc if err := dataSrc.CanRun(); err != nil { return nil, errors.Wrapf(err, "datasource %s cannot be run", commonConfig.Source) } - - /* check that mode is supported */ - found := false - for _, submode := range dataSrc.SupportedModes() { - if submode == commonConfig.Mode { - found = true - } - } - if !found { - return nil, fmt.Errorf("%s mode is not supported by %s", commonConfig.Mode, commonConfig.Source) - } /* configure the actual datasource */ if err := dataSrc.Configure(yamlConfig, subLogger); err != nil { return nil, errors.Wrapf(err, "failed to configure datasource %s", commonConfig.Source) @@ -172,7 +161,7 @@ func LoadAcquisitionFromFile(config *csconfig.CrowdsecServiceCfg) ([]DataSource, log.Tracef("End of yaml file") break } - return nil, errors.Wrapf(err, "failed to yaml decode %s", sub.ConfigFile) + return nil, errors.Wrapf(err, "failed to yaml decode %s", acquisFile) } //we dump it back to []byte, because we want to decode the yaml blob twice : @@ -185,7 +174,10 @@ func LoadAcquisitionFromFile(config *csconfig.CrowdsecServiceCfg) ([]DataSource, if err := yaml.Unmarshal(inBytes, &sub); err != nil { return nil, errors.Wrapf(err, "configuration isn't valid config in %s : %s", acquisFile, string(inBytes)) } - sub.ConfigFile = acquisFile + //for backward compat ('type' was not mandatory, detect it) + if guessType := detectBackwardCompatAcquis(inBytes); guessType != "" { + sub.Source = guessType + } //it's an empty item, skip it if len(sub.Labels) == 0 { if sub.Source == "" { @@ -194,17 +186,13 @@ func LoadAcquisitionFromFile(config *csconfig.CrowdsecServiceCfg) ([]DataSource, } return nil, fmt.Errorf("missing labels in %s", acquisFile) } - //for backward compat ('type' was not mandatory, detect it) - if guessType := detectBackwardCompatAcquis(inBytes); guessType != "" { - sub.Source = guessType - } if GetDataSourceIface(sub.Source) == nil { - return nil, fmt.Errorf("unknown data source %s in %s", sub.Source, sub.ConfigFile) + return nil, fmt.Errorf("unknown data source %s in %s", sub.Source, acquisFile) } src, err := DataSourceConfigure(inBytes, sub) if err != nil { - return nil, errors.Wrapf(err, "while configuring datasource of type %s from %s", sub.Source, sub.ConfigFile) + return nil, errors.Wrapf(err, "while configuring datasource of type %s from %s", sub.Source, acquisFile) } sources = append(sources, *src) } diff --git a/pkg/acquisition/acquisition_test.go b/pkg/acquisition/acquisition_test.go index f83758ae5..d1ae6d62d 100644 --- a/pkg/acquisition/acquisition_test.go +++ b/pkg/acquisition/acquisition_test.go @@ -31,6 +31,9 @@ func (f *MockSource) Configure(cfg []byte, logger *log.Entry) error { if f.Mode == "" { f.Mode = configuration.CAT_MODE } + if f.Mode != configuration.CAT_MODE && f.Mode != configuration.TAIL_MODE { + return fmt.Errorf("mode %s is not supported", f.Mode) + } if f.Toto == "" { return fmt.Errorf("expect non-empty toto") } @@ -94,7 +97,7 @@ mode: cat labels: test: foobar log_level: info -type: mock +source: mock toto: test_value1 `), }, @@ -105,7 +108,7 @@ mode: cat labels: test: foobar log_level: debug -type: mock +source: mock toto: test_value1 `), }, @@ -116,7 +119,7 @@ mode: tail labels: test: foobar log_level: debug -type: mock +source: mock toto: test_value1 `), }, @@ -127,19 +130,19 @@ mode: ratata labels: test: foobar log_level: debug -type: mock +source: mock toto: test_value1 `), - ExpectedError: "ratata mode is not supported by mock", + ExpectedError: "failed to configure datasource mock: mode ratata is not supported", }, { TestName: "bad_type_config", RawBytes: []byte(` -mode: ratata +mode: cat labels: test: foobar log_level: debug -type: tutu +source: tutu `), ExpectedError: "cannot find source tutu", }, @@ -150,7 +153,7 @@ mode: cat labels: test: foobar log_level: debug -type: mock +source: mock wowo: ajsajasjas `), ExpectedError: "field wowo not found in type acquisition.MockSource", @@ -162,7 +165,7 @@ mode: cat labels: test: foobar log_level: debug -type: mock_cant_run +source: mock_cant_run wowo: ajsajasjas `), ExpectedError: "datasource mock_cant_run cannot be run: can't run bro", @@ -317,6 +320,9 @@ func (f *MockCat) Configure(cfg []byte, logger *log.Entry) error { if f.Mode == "" { f.Mode = configuration.CAT_MODE } + if f.Mode != configuration.CAT_MODE { + return fmt.Errorf("mode %s is not supported", f.Mode) + } return nil } func (f *MockCat) GetMode() string { return "cat" } @@ -350,6 +356,9 @@ func (f *MockTail) Configure(cfg []byte, logger *log.Entry) error { if f.Mode == "" { f.Mode = configuration.TAIL_MODE } + if f.Mode != configuration.TAIL_MODE { + return fmt.Errorf("mode %s is not supported", f.Mode) + } return nil } func (f *MockTail) GetMode() string { return "tail" } diff --git a/pkg/acquisition/configuration/configuration.go b/pkg/acquisition/configuration/configuration.go index e1873c40b..f5f079c4d 100644 --- a/pkg/acquisition/configuration/configuration.go +++ b/pkg/acquisition/configuration/configuration.go @@ -5,19 +5,12 @@ import ( ) type DataSourceCommonCfg struct { - Mode string `yaml:"mode,omitempty"` - Labels map[string]string `yaml:"labels,omitempty"` - LogLevel *log.Level `yaml:"log_level,omitempty"` - Source string `yaml:"source,omitempty"` - ConfigFile string `yaml:"-"` //filled at run time : the filepath from which the config was unmarshaled - Logger *log.Entry `yaml:"-"` + Mode string `yaml:"mode,omitempty"` + Labels map[string]string `yaml:"labels,omitempty"` + LogLevel *log.Level `yaml:"log_level,omitempty"` + Source string `yaml:"source,omitempty"` } -// func (d *DataSourceCommonCfg) SetDefaults(cfg []byte) error { -// //if mode == "" -> tail -// //if source == "" && filename || filenames -> -// } - var TAIL_MODE = "tail" var CAT_MODE = "cat" var SERVER_MODE = "server" // No difference with tail, just a bit more verbose diff --git a/pkg/acquisition/test_files/bad_source.yaml b/pkg/acquisition/test_files/bad_source.yaml index 596fcd744..fdf6b3260 100644 --- a/pkg/acquisition/test_files/bad_source.yaml +++ b/pkg/acquisition/test_files/bad_source.yaml @@ -1,4 +1,4 @@ -type: does_not_exist +source: does_not_exist labels: type: syslog foobar: toto diff --git a/pkg/acquisition/test_files/basic_filemode.yaml b/pkg/acquisition/test_files/basic_filemode.yaml index 795f4b62e..78cc9d1e0 100644 --- a/pkg/acquisition/test_files/basic_filemode.yaml +++ b/pkg/acquisition/test_files/basic_filemode.yaml @@ -1,9 +1,9 @@ -type: file +#type: file filename: /tmp/test.log labels: type: syslog --- -type: file +#type: file filenames: - /tmp/test*.log labels: