eventsearcher.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Package eventsearcher defines the implementation for events search plugins.
  2. // Events search plugins allow to search for filesystem and provider events.
  3. package eventsearcher
  4. import (
  5. "context"
  6. "github.com/hashicorp/go-plugin"
  7. "google.golang.org/grpc"
  8. "github.com/drakkan/sftpgo/v2/sdk/plugin/eventsearcher/proto"
  9. )
  10. const (
  11. // PluginName defines the name for an events search plugin
  12. PluginName = "eventsearcher"
  13. )
  14. // Handshake is a common handshake that is shared by plugin and host.
  15. var Handshake = plugin.HandshakeConfig{
  16. ProtocolVersion: 1,
  17. MagicCookieKey: "SFTPGO_PLUGIN_EVENTSEARCHER",
  18. MagicCookieValue: "2b523805-0279-471c-895e-6c0d39002ca4",
  19. }
  20. // PluginMap is the map of plugins we can dispense.
  21. var PluginMap = map[string]plugin.Plugin{
  22. PluginName: &Plugin{},
  23. }
  24. // CommonSearchParams defines common parameters for both filesystem and provider search
  25. type CommonSearchParams struct {
  26. StartTimestamp int64
  27. EndTimestamp int64
  28. Actions []string
  29. Username string
  30. IP string
  31. InstanceIDs []string
  32. ExcludeIDs []string
  33. Limit int
  34. Order int
  35. }
  36. // FsEventSearch defines the fields for a filesystem event search
  37. type FsEventSearch struct {
  38. CommonSearchParams
  39. SSHCmd string
  40. Protocols []string
  41. Statuses []int32
  42. FsProvider int
  43. Bucket string
  44. Endpoint string
  45. }
  46. // ProviderEventSearch defines the fields for a provider event search
  47. type ProviderEventSearch struct {
  48. CommonSearchParams
  49. ObjectName string
  50. ObjectTypes []string
  51. }
  52. // Searcher defines the interface for events search plugins
  53. type Searcher interface {
  54. SearchFsEvents(searchFilters *FsEventSearch) ([]byte, []string, []string, error)
  55. SearchProviderEvents(searchFilters *ProviderEventSearch) ([]byte, []string, []string, error)
  56. }
  57. // Plugin defines the implementation to serve/connect to a event search plugin
  58. type Plugin struct {
  59. plugin.Plugin
  60. Impl Searcher
  61. }
  62. // GRPCServer defines the GRPC server implementation for this plugin
  63. func (p *Plugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
  64. proto.RegisterSearcherServer(s, &GRPCServer{
  65. Impl: p.Impl,
  66. })
  67. return nil
  68. }
  69. // GRPCClient defines the GRPC client implementation for this plugin
  70. func (p *Plugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
  71. return &GRPCClient{
  72. client: proto.NewSearcherClient(c),
  73. }, nil
  74. }