github.com/crowdsecurity/crowdsec@v1.6.1/cmd/notification-dummy/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/crowdsecurity/crowdsec/pkg/protobufs"
     9  	"github.com/hashicorp/go-hclog"
    10  	plugin "github.com/hashicorp/go-plugin"
    11  	"gopkg.in/yaml.v2"
    12  )
    13  
    14  type PluginConfig struct {
    15  	Name       string  `yaml:"name"`
    16  	LogLevel   *string `yaml:"log_level"`
    17  	OutputFile *string `yaml:"output_file"`
    18  }
    19  
    20  type DummyPlugin struct {
    21  	PluginConfigByName map[string]PluginConfig
    22  }
    23  
    24  var logger hclog.Logger = hclog.New(&hclog.LoggerOptions{
    25  	Name:       "dummy-plugin",
    26  	Level:      hclog.LevelFromString("INFO"),
    27  	Output:     os.Stderr,
    28  	JSONFormat: true,
    29  })
    30  
    31  func (s *DummyPlugin) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
    32  	if _, ok := s.PluginConfigByName[notification.Name]; !ok {
    33  		return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
    34  	}
    35  	cfg := s.PluginConfigByName[notification.Name]
    36  
    37  	if cfg.LogLevel != nil && *cfg.LogLevel != "" {
    38  		logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
    39  	}
    40  
    41  	logger.Info(fmt.Sprintf("received signal for %s config", notification.Name))
    42  	logger.Debug(notification.Text)
    43  
    44  	if cfg.OutputFile != nil && *cfg.OutputFile != "" {
    45  		f, err := os.OpenFile(*cfg.OutputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    46  		if err != nil {
    47  			logger.Error(fmt.Sprintf("Cannot open notification file: %s", err))
    48  		}
    49  		if _, err := f.WriteString(notification.Text + "\n"); err != nil {
    50  			f.Close()
    51  			logger.Error(fmt.Sprintf("Cannot write notification to file: %s", err))
    52  		}
    53  		err = f.Close()
    54  		if err != nil {
    55  			logger.Error(fmt.Sprintf("Cannot close notification file: %s", err))
    56  		}
    57  	}
    58  	fmt.Println(notification.Text)
    59  
    60  	return &protobufs.Empty{}, nil
    61  }
    62  
    63  func (s *DummyPlugin) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
    64  	d := PluginConfig{}
    65  	err := yaml.Unmarshal(config.Config, &d)
    66  	s.PluginConfigByName[d.Name] = d
    67  	return &protobufs.Empty{}, err
    68  }
    69  
    70  func main() {
    71  	var handshake = plugin.HandshakeConfig{
    72  		ProtocolVersion:  1,
    73  		MagicCookieKey:   "CROWDSEC_PLUGIN_KEY",
    74  		MagicCookieValue: os.Getenv("CROWDSEC_PLUGIN_KEY"),
    75  	}
    76  
    77  	sp := &DummyPlugin{PluginConfigByName: make(map[string]PluginConfig)}
    78  	plugin.Serve(&plugin.ServeConfig{
    79  		HandshakeConfig: handshake,
    80  		Plugins: map[string]plugin.Plugin{
    81  			"dummy": &protobufs.NotifierPlugin{
    82  				Impl: sp,
    83  			},
    84  		},
    85  		GRPCServer: plugin.DefaultGRPCServer,
    86  		Logger:     logger,
    87  	})
    88  }