mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 12:25:53 +02:00
Avoid code duplication for protobuf in plugins (#918)
* Avoid code duplication for protobuf in plugins Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
This commit is contained in:
parent
0ad6165ed2
commit
899b2abae7
17 changed files with 4555 additions and 1388 deletions
|
@ -10,6 +10,7 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
|
||||
|
@ -39,9 +40,9 @@ type Payload struct {
|
|||
Event string `json:"event"`
|
||||
}
|
||||
|
||||
func (s *Splunk) Notify(ctx context.Context, notification *Notification) (*Empty, error) {
|
||||
func (s *Splunk) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
|
||||
if _, ok := s.PluginConfigByName[notification.Name]; !ok {
|
||||
return &Empty{}, fmt.Errorf("splunk invalid config name %s", notification.Name)
|
||||
return &protobufs.Empty{}, fmt.Errorf("splunk invalid config name %s", notification.Name)
|
||||
}
|
||||
cfg := s.PluginConfigByName[notification.Name]
|
||||
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
|
||||
|
@ -54,41 +55,41 @@ func (s *Splunk) Notify(ctx context.Context, notification *Notification) (*Empty
|
|||
p := Payload{Event: notification.Text}
|
||||
data, err := json.Marshal(p)
|
||||
if err != nil {
|
||||
return &Empty{}, err
|
||||
return &protobufs.Empty{}, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", cfg.URL, strings.NewReader(string(data)))
|
||||
if err != nil {
|
||||
return &Empty{}, err
|
||||
return &protobufs.Empty{}, err
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Splunk %s", cfg.Token))
|
||||
logger.Debug(fmt.Sprintf("posting event %s to %s", string(data), req.URL))
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return &Empty{}, err
|
||||
return &protobufs.Empty{}, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
content, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return &Empty{}, fmt.Errorf("got non 200 response and failed to read error %s", string(err.Error()))
|
||||
return &protobufs.Empty{}, fmt.Errorf("got non 200 response and failed to read error %s", string(err.Error()))
|
||||
}
|
||||
return &Empty{}, fmt.Errorf("got non 200 response %s", string(content))
|
||||
return &protobufs.Empty{}, fmt.Errorf("got non 200 response %s", string(content))
|
||||
}
|
||||
respData, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return &Empty{}, fmt.Errorf("failed to read response body got error %s", string(err.Error()))
|
||||
return &protobufs.Empty{}, fmt.Errorf("failed to read response body got error %s", string(err.Error()))
|
||||
}
|
||||
logger.Debug(fmt.Sprintf("got response %s", string(respData)))
|
||||
return &Empty{}, nil
|
||||
return &protobufs.Empty{}, nil
|
||||
}
|
||||
|
||||
func (s *Splunk) Configure(ctx context.Context, config *Config) (*Empty, error) {
|
||||
func (s *Splunk) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
|
||||
d := PluginConfig{}
|
||||
err := yaml.Unmarshal(config.Config, &d)
|
||||
s.PluginConfigByName[d.Name] = d
|
||||
return &Empty{}, err
|
||||
return &protobufs.Empty{}, err
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -107,7 +108,7 @@ func main() {
|
|||
plugin.Serve(&plugin.ServeConfig{
|
||||
HandshakeConfig: handshake,
|
||||
Plugins: map[string]plugin.Plugin{
|
||||
"splunk": &NotifierPlugin{
|
||||
"splunk": &protobufs.NotifierPlugin{
|
||||
Impl: sp,
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue