2020-11-30 09:37:17 +00:00
|
|
|
package acquisition
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2021-06-11 07:53:53 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
|
|
|
|
cloudwatchacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/cloudwatch"
|
2021-12-02 14:55:50 +00:00
|
|
|
dockeracquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/docker"
|
2021-06-11 07:53:53 +00:00
|
|
|
fileacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/file"
|
|
|
|
journalctlacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/journalctl"
|
2022-08-30 15:03:45 +00:00
|
|
|
kafkaacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/kafka"
|
2022-01-11 13:19:43 +00:00
|
|
|
kinesisacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/kinesis"
|
2021-06-11 07:53:53 +00:00
|
|
|
syslogacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/syslog"
|
2022-05-17 10:14:59 +00:00
|
|
|
wineventlogacquisition "github.com/crowdsecurity/crowdsec/pkg/acquisition/modules/wineventlog"
|
2020-11-30 09:37:17 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
|
|
|
tomb "gopkg.in/tomb.v2"
|
|
|
|
)
|
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
// The interface each datasource must implement
|
|
|
|
type DataSource interface {
|
2021-12-02 14:55:50 +00:00
|
|
|
GetMetrics() []prometheus.Collector // Returns pointers to metrics that are managed by the module
|
|
|
|
GetAggregMetrics() []prometheus.Collector // Returns pointers to metrics that are managed by the module (aggregated mode, limits cardinality)
|
|
|
|
Configure([]byte, *log.Entry) error // Configure the datasource
|
|
|
|
ConfigureByDSN(string, map[string]string, *log.Entry) error // Configure the datasource
|
|
|
|
GetMode() string // Get the mode (TAIL, CAT or SERVER)
|
|
|
|
GetName() string // Get the name of the module
|
|
|
|
OneShotAcquisition(chan types.Event, *tomb.Tomb) error // Start one shot acquisition(eg, cat a file)
|
|
|
|
StreamingAcquisition(chan types.Event, *tomb.Tomb) error // Start live acquisition (eg, tail a file)
|
|
|
|
CanRun() error // Whether the datasource can run or not (eg, journalctl on BSD is a non-sense)
|
2021-06-11 07:53:53 +00:00
|
|
|
Dump() interface{}
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
var AcquisitionSources = []struct {
|
|
|
|
name string
|
|
|
|
iface func() DataSource
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "file",
|
|
|
|
iface: func() DataSource { return &fileacquisition.FileSource{} },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "journalctl",
|
|
|
|
iface: func() DataSource { return &journalctlacquisition.JournalCtlSource{} },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "cloudwatch",
|
|
|
|
iface: func() DataSource { return &cloudwatchacquisition.CloudwatchSource{} },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "syslog",
|
|
|
|
iface: func() DataSource { return &syslogacquisition.SyslogSource{} },
|
|
|
|
},
|
2021-12-02 14:55:50 +00:00
|
|
|
{
|
|
|
|
name: "docker",
|
|
|
|
iface: func() DataSource { return &dockeracquisition.DockerSource{} },
|
|
|
|
},
|
2022-01-11 13:19:43 +00:00
|
|
|
{
|
|
|
|
name: "kinesis",
|
|
|
|
iface: func() DataSource { return &kinesisacquisition.KinesisSource{} },
|
|
|
|
},
|
2022-05-17 10:14:59 +00:00
|
|
|
{
|
|
|
|
name: "wineventlog",
|
|
|
|
iface: func() DataSource { return &wineventlogacquisition.WinEventLogSource{} },
|
|
|
|
},
|
2022-08-30 15:03:45 +00:00
|
|
|
{
|
|
|
|
name: "kafka",
|
|
|
|
iface: func() DataSource { return &kafkaacquisition.KafkaSource{} },
|
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
func GetDataSourceIface(dataSourceType string) DataSource {
|
|
|
|
for _, source := range AcquisitionSources {
|
|
|
|
if source.name == dataSourceType {
|
|
|
|
return source.iface()
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2021-06-11 07:53:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
func DataSourceConfigure(commonConfig configuration.DataSourceCommonCfg) (*DataSource, error) {
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
//we dump it back to []byte, because we want to decode the yaml blob twice :
|
|
|
|
//once to DataSourceCommonCfg, and then later to the dedicated type of the datasource
|
|
|
|
yamlConfig, err := yaml.Marshal(commonConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "unable to marshal back interface")
|
|
|
|
}
|
|
|
|
if dataSrc := GetDataSourceIface(commonConfig.Source); dataSrc != nil {
|
|
|
|
/* this logger will then be used by the datasource at runtime */
|
|
|
|
clog := log.New()
|
|
|
|
if err := types.ConfigureLogger(clog); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "while configuring datasource logger")
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2021-06-11 07:53:53 +00:00
|
|
|
if commonConfig.LogLevel != nil {
|
|
|
|
clog.SetLevel(*commonConfig.LogLevel)
|
|
|
|
}
|
|
|
|
customLog := log.Fields{
|
|
|
|
"type": commonConfig.Source,
|
|
|
|
}
|
|
|
|
if commonConfig.Name != "" {
|
|
|
|
customLog["name"] = commonConfig.Name
|
|
|
|
}
|
|
|
|
subLogger := clog.WithFields(customLog)
|
|
|
|
/* check eventual dependencies are satisfied (ie. journald will check journalctl availability) */
|
|
|
|
if err := dataSrc.CanRun(); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "datasource %s cannot be run", 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)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
}
|
2021-06-11 07:53:53 +00:00
|
|
|
return &dataSrc, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("cannot find source %s", commonConfig.Source)
|
|
|
|
}
|
|
|
|
|
|
|
|
//detectBackwardCompatAcquis : try to magically detect the type for backward compat (type was not mandatory then)
|
|
|
|
func detectBackwardCompatAcquis(sub configuration.DataSourceCommonCfg) string {
|
|
|
|
|
|
|
|
if _, ok := sub.Config["filename"]; ok {
|
|
|
|
return "file"
|
|
|
|
}
|
|
|
|
if _, ok := sub.Config["filenames"]; ok {
|
|
|
|
return "file"
|
|
|
|
}
|
|
|
|
if _, ok := sub.Config["journalctl_filter"]; ok {
|
|
|
|
return "journalctl"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-11-17 09:08:46 +00:00
|
|
|
func LoadAcquisitionFromDSN(dsn string, labels map[string]string) ([]DataSource, error) {
|
2021-06-11 07:53:53 +00:00
|
|
|
var sources []DataSource
|
|
|
|
|
|
|
|
frags := strings.Split(dsn, ":")
|
|
|
|
if len(frags) == 1 {
|
|
|
|
return nil, fmt.Errorf("%s isn't valid dsn (no protocol)", dsn)
|
|
|
|
}
|
|
|
|
dataSrc := GetDataSourceIface(frags[0])
|
|
|
|
if dataSrc == nil {
|
|
|
|
return nil, fmt.Errorf("no acquisition for protocol %s://", frags[0])
|
|
|
|
}
|
|
|
|
/* this logger will then be used by the datasource at runtime */
|
|
|
|
clog := log.New()
|
|
|
|
if err := types.ConfigureLogger(clog); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "while configuring datasource logger")
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2021-06-11 07:53:53 +00:00
|
|
|
subLogger := clog.WithFields(log.Fields{
|
|
|
|
"type": dsn,
|
|
|
|
})
|
2021-11-17 09:08:46 +00:00
|
|
|
err := dataSrc.ConfigureByDSN(dsn, labels, subLogger)
|
2021-06-11 07:53:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "while configuration datasource for %s", dsn)
|
|
|
|
}
|
|
|
|
sources = append(sources, dataSrc)
|
|
|
|
return sources, nil
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
// LoadAcquisitionFromFile unmarshals the configuration item and checks its availability
|
2020-11-30 09:37:17 +00:00
|
|
|
func LoadAcquisitionFromFile(config *csconfig.CrowdsecServiceCfg) ([]DataSource, error) {
|
|
|
|
|
|
|
|
var sources []DataSource
|
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
for _, acquisFile := range config.AcquisitionFiles {
|
2021-02-17 12:55:36 +00:00
|
|
|
log.Infof("loading acquisition file : %s", acquisFile)
|
|
|
|
yamlFile, err := os.Open(acquisFile)
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
2022-10-17 15:32:08 +00:00
|
|
|
return nil, err
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2021-02-17 12:55:36 +00:00
|
|
|
dec := yaml.NewDecoder(yamlFile)
|
|
|
|
dec.SetStrict(true)
|
|
|
|
for {
|
2021-06-11 07:53:53 +00:00
|
|
|
var sub configuration.DataSourceCommonCfg
|
2022-05-17 10:14:59 +00:00
|
|
|
var idx int
|
2021-02-17 12:55:36 +00:00
|
|
|
err = dec.Decode(&sub)
|
|
|
|
if err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
if ! errors.Is(err, io.EOF) {
|
|
|
|
return nil, errors.Wrapf(err, "failed to yaml decode %s", acquisFile)
|
2021-02-17 12:55:36 +00:00
|
|
|
}
|
2022-11-29 08:16:07 +00:00
|
|
|
log.Tracef("End of yaml file")
|
|
|
|
break
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//for backward compat ('type' was not mandatory, detect it)
|
|
|
|
if guessType := detectBackwardCompatAcquis(sub); guessType != "" {
|
|
|
|
sub.Source = guessType
|
|
|
|
}
|
|
|
|
//it's an empty item, skip it
|
|
|
|
if len(sub.Labels) == 0 {
|
|
|
|
if sub.Source == "" {
|
|
|
|
log.Debugf("skipping empty item in %s", acquisFile)
|
2022-05-17 10:14:59 +00:00
|
|
|
idx += 1
|
2021-06-11 07:53:53 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-05-17 10:14:59 +00:00
|
|
|
return nil, fmt.Errorf("missing labels in %s (position: %d)", acquisFile, idx)
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
if sub.Source == "" {
|
2022-05-17 10:14:59 +00:00
|
|
|
return nil, fmt.Errorf("data source type is empty ('source') in %s (position: %d)", acquisFile, idx)
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
if GetDataSourceIface(sub.Source) == nil {
|
2022-05-17 10:14:59 +00:00
|
|
|
return nil, fmt.Errorf("unknown data source %s in %s (position: %d)", sub.Source, acquisFile, idx)
|
2021-02-17 12:55:36 +00:00
|
|
|
}
|
|
|
|
src, err := DataSourceConfigure(sub)
|
|
|
|
if err != nil {
|
2022-05-17 10:14:59 +00:00
|
|
|
return nil, errors.Wrapf(err, "while configuring datasource of type %s from %s (position: %d)", sub.Source, acquisFile, idx)
|
2021-02-17 12:55:36 +00:00
|
|
|
}
|
2021-06-11 07:53:53 +00:00
|
|
|
sources = append(sources, *src)
|
2022-05-17 10:14:59 +00:00
|
|
|
idx += 1
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return sources, nil
|
|
|
|
}
|
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
func GetMetrics(sources []DataSource, aggregated bool) error {
|
|
|
|
var metrics []prometheus.Collector
|
|
|
|
for i := 0; i < len(sources); i++ {
|
|
|
|
if aggregated {
|
|
|
|
metrics = sources[i].GetMetrics()
|
|
|
|
} else {
|
|
|
|
metrics = sources[i].GetAggregMetrics()
|
|
|
|
}
|
|
|
|
for _, metric := range metrics {
|
|
|
|
if err := prometheus.Register(metric); err != nil {
|
2022-02-01 21:08:06 +00:00
|
|
|
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
|
2021-06-11 07:53:53 +00:00
|
|
|
return errors.Wrapf(err, "could not register metrics for datasource %s", sources[i].GetName())
|
|
|
|
}
|
2022-02-01 21:08:06 +00:00
|
|
|
//ignore the error
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2021-06-11 07:53:53 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartAcquisition(sources []DataSource, output chan types.Event, AcquisTomb *tomb.Tomb) error {
|
2020-11-30 09:37:17 +00:00
|
|
|
for i := 0; i < len(sources); i++ {
|
|
|
|
subsrc := sources[i] //ensure its a copy
|
|
|
|
log.Debugf("starting one source %d/%d ->> %T", i, len(sources), subsrc)
|
2021-06-11 07:53:53 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
AcquisTomb.Go(func() error {
|
|
|
|
defer types.CatchPanic("crowdsec/acquis")
|
2021-06-11 07:53:53 +00:00
|
|
|
var err error
|
|
|
|
if subsrc.GetMode() == configuration.TAIL_MODE {
|
|
|
|
err = subsrc.StreamingAcquisition(output, AcquisTomb)
|
|
|
|
} else {
|
|
|
|
err = subsrc.OneShotAcquisition(output, AcquisTomb)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
//if one of the acqusition returns an error, we kill the others to properly shutdown
|
|
|
|
AcquisTomb.Kill(err)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2021-07-28 06:58:44 +00:00
|
|
|
// Don't wait if we have no sources, as it will hang forever
|
|
|
|
if len(sources) > 0 {
|
|
|
|
/*return only when acquisition is over (cat) or never (tail)*/
|
|
|
|
err := AcquisTomb.Wait()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|