2020-05-15 09:39:16 +00:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-06-13 11:16:13 +00:00
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
2020-05-15 09:39:16 +00:00
|
|
|
)
|
|
|
|
|
2023-06-13 11:16:13 +00:00
|
|
|
/* should be part of a package shared with enrich/geoip.go */
|
2022-07-28 14:41:41 +00:00
|
|
|
type EnrichFunc func(string, *types.Event, interface{}, *log.Entry) (map[string]string, error)
|
2020-05-15 09:39:16 +00:00
|
|
|
type InitFunc func(map[string]string) (interface{}, error)
|
|
|
|
|
|
|
|
type EnricherCtx struct {
|
2021-09-09 14:27:30 +00:00
|
|
|
Registered map[string]*Enricher
|
|
|
|
}
|
|
|
|
|
|
|
|
type Enricher struct {
|
2020-05-15 09:39:16 +00:00
|
|
|
Name string
|
2021-09-09 14:27:30 +00:00
|
|
|
InitFunc InitFunc
|
|
|
|
EnrichFunc EnrichFunc
|
|
|
|
Ctx interface{}
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* mimic plugin loading */
|
2021-09-09 14:27:30 +00:00
|
|
|
func Loadplugin(path string) (EnricherCtx, error) {
|
|
|
|
enricherCtx := EnricherCtx{}
|
|
|
|
enricherCtx.Registered = make(map[string]*Enricher)
|
2020-05-15 09:39:16 +00:00
|
|
|
|
2021-09-09 14:27:30 +00:00
|
|
|
enricherConfig := map[string]string{"datadir": path}
|
2020-05-15 09:39:16 +00:00
|
|
|
|
2021-09-09 14:27:30 +00:00
|
|
|
EnrichersList := []*Enricher{
|
|
|
|
{
|
|
|
|
Name: "GeoIpCity",
|
|
|
|
InitFunc: GeoIPCityInit,
|
|
|
|
EnrichFunc: GeoIpCity,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "GeoIpASN",
|
|
|
|
InitFunc: GeoIPASNInit,
|
|
|
|
EnrichFunc: GeoIpASN,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "IpToRange",
|
|
|
|
InitFunc: IpToRangeInit,
|
|
|
|
EnrichFunc: IpToRange,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "reverse_dns",
|
|
|
|
InitFunc: reverseDNSInit,
|
|
|
|
EnrichFunc: reverse_dns,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "ParseDate",
|
|
|
|
InitFunc: parseDateInit,
|
|
|
|
EnrichFunc: ParseDate,
|
|
|
|
},
|
2022-12-06 12:47:29 +00:00
|
|
|
{
|
|
|
|
Name: "UnmarshalJSON",
|
|
|
|
InitFunc: unmarshalInit,
|
|
|
|
EnrichFunc: unmarshalJSON,
|
|
|
|
},
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 14:27:30 +00:00
|
|
|
for _, enricher := range EnrichersList {
|
|
|
|
log.Debugf("Initiating enricher '%s'", enricher.Name)
|
|
|
|
pluginCtx, err := enricher.InitFunc(enricherConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to register plugin '%s': %v", enricher.Name, err)
|
|
|
|
continue
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
2021-09-09 14:27:30 +00:00
|
|
|
enricher.Ctx = pluginCtx
|
|
|
|
log.Infof("Successfully registered enricher '%s'", enricher.Name)
|
|
|
|
enricherCtx.Registered[enricher.Name] = enricher
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
2021-04-16 17:13:48 +00:00
|
|
|
|
2021-09-09 14:27:30 +00:00
|
|
|
return enricherCtx, nil
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|