crowdsec/pkg/parser/enrich.go

78 lines
1.7 KiB
Go
Raw Normal View History

2020-05-15 09:39:16 +00:00
package parser
import (
log "github.com/sirupsen/logrus"
"github.com/crowdsecurity/crowdsec/pkg/types"
2020-05-15 09:39:16 +00:00
)
/* should be part of a package shared with enrich/geoip.go */
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 {
Registered map[string]*Enricher
}
type Enricher struct {
2020-05-15 09:39:16 +00:00
Name string
InitFunc InitFunc
EnrichFunc EnrichFunc
Ctx interface{}
2020-05-15 09:39:16 +00:00
}
/* mimic plugin loading */
func Loadplugin(path string) (EnricherCtx, error) {
enricherCtx := EnricherCtx{}
enricherCtx.Registered = make(map[string]*Enricher)
2020-05-15 09:39:16 +00:00
enricherConfig := map[string]string{"datadir": path}
2020-05-15 09:39:16 +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,
},
{
Name: "UnmarshalJSON",
InitFunc: unmarshalInit,
EnrichFunc: unmarshalJSON,
},
2020-05-15 09:39:16 +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
}
enricher.Ctx = pluginCtx
log.Infof("Successfully registered enricher '%s'", enricher.Name)
enricherCtx.Registered[enricher.Name] = enricher
2020-05-15 09:39:16 +00:00
}
return enricherCtx, nil
2020-05-15 09:39:16 +00:00
}