2021-10-23 13:47:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-plugin"
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk/plugin/eventsearcher"
|
2021-10-23 13:47:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errNotSupported = errors.New("unsupported parameter")
|
|
|
|
)
|
|
|
|
|
|
|
|
type fsEvent struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Timestamp int64 `json:"timestamp"`
|
|
|
|
Action string `json:"action"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
FsPath string `json:"fs_path"`
|
|
|
|
FsTargetPath string `json:"fs_target_path,omitempty"`
|
|
|
|
VirtualPath string `json:"virtual_path"`
|
|
|
|
VirtualTargetPath string `json:"virtual_target_path,omitempty"`
|
|
|
|
SSHCmd string `json:"ssh_cmd,omitempty"`
|
|
|
|
FileSize int64 `json:"file_size,omitempty"`
|
|
|
|
Status int `json:"status"`
|
|
|
|
Protocol string `json:"protocol"`
|
|
|
|
IP string `json:"ip,omitempty"`
|
2021-12-04 17:18:59 +00:00
|
|
|
SessionID string `json:"session_id"`
|
2022-01-03 16:02:52 +00:00
|
|
|
FsProvider int `json:"fs_provider"`
|
|
|
|
Bucket string `json:"bucket,omitempty"`
|
|
|
|
Endpoint string `json:"endpoint,omitempty"`
|
|
|
|
OpenFlags int `json:"open_flags,omitempty"`
|
2022-12-03 10:45:27 +00:00
|
|
|
Role string `json:"role,omitempty"`
|
2021-10-23 13:47:21 +00:00
|
|
|
InstanceID string `json:"instance_id,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type providerEvent struct {
|
|
|
|
ID string `json:"id" gorm:"primaryKey"`
|
|
|
|
Timestamp int64 `json:"timestamp"`
|
|
|
|
Action string `json:"action"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
IP string `json:"ip,omitempty"`
|
|
|
|
ObjectType string `json:"object_type"`
|
|
|
|
ObjectName string `json:"object_name"`
|
2022-12-08 09:02:12 +00:00
|
|
|
ObjectData []byte `json:"object_data,omitempty"`
|
2022-12-03 10:45:27 +00:00
|
|
|
Role string `json:"role,omitempty"`
|
2021-10-23 13:47:21 +00:00
|
|
|
InstanceID string `json:"instance_id,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Searcher struct{}
|
|
|
|
|
2022-01-03 16:02:52 +00:00
|
|
|
func (s *Searcher) SearchFsEvents(filters *eventsearcher.FsEventSearch) ([]byte, []string, []string, error) {
|
|
|
|
if filters.StartTimestamp < 0 {
|
2021-10-23 13:47:21 +00:00
|
|
|
return nil, nil, nil, errNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
results := []fsEvent{
|
|
|
|
{
|
|
|
|
ID: "1",
|
|
|
|
Timestamp: 100,
|
|
|
|
Action: "upload",
|
|
|
|
Username: "username1",
|
|
|
|
FsPath: "/tmp/file.txt",
|
|
|
|
FsTargetPath: "/tmp/target.txt",
|
|
|
|
VirtualPath: "file.txt",
|
|
|
|
VirtualTargetPath: "target.txt",
|
|
|
|
SSHCmd: "scp",
|
|
|
|
FileSize: 123,
|
|
|
|
Status: 1,
|
|
|
|
Protocol: "SFTP",
|
|
|
|
IP: "::1",
|
2021-12-04 17:18:59 +00:00
|
|
|
SessionID: "1234",
|
2021-10-23 13:47:21 +00:00
|
|
|
InstanceID: "instance1",
|
2022-01-03 16:02:52 +00:00
|
|
|
FsProvider: 0,
|
|
|
|
Bucket: "bucket",
|
|
|
|
Endpoint: "endpoint",
|
|
|
|
OpenFlags: 512,
|
2022-12-03 10:45:27 +00:00
|
|
|
Role: "role1",
|
2021-10-23 13:47:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(results)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2022-01-03 16:02:52 +00:00
|
|
|
func (s *Searcher) SearchProviderEvents(filters *eventsearcher.ProviderEventSearch) ([]byte, []string, []string, error) {
|
|
|
|
if filters.StartTimestamp < 0 {
|
2021-10-23 13:47:21 +00:00
|
|
|
return nil, nil, nil, errNotSupported
|
|
|
|
}
|
|
|
|
|
2022-12-08 09:02:12 +00:00
|
|
|
var objectData []byte
|
|
|
|
if !filters.OmitObjectData {
|
|
|
|
objectData = []byte("data")
|
|
|
|
}
|
|
|
|
|
2021-10-23 13:47:21 +00:00
|
|
|
results := []providerEvent{
|
|
|
|
{
|
|
|
|
ID: "1",
|
|
|
|
Timestamp: 100,
|
|
|
|
Action: "add",
|
|
|
|
Username: "username1",
|
|
|
|
IP: "127.0.0.1",
|
|
|
|
ObjectType: "api_key",
|
|
|
|
ObjectName: "123",
|
2022-12-08 09:02:12 +00:00
|
|
|
ObjectData: objectData,
|
2022-12-03 10:45:27 +00:00
|
|
|
Role: "role2",
|
2021-10-23 13:47:21 +00:00
|
|
|
InstanceID: "instance1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(results)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
plugin.Serve(&plugin.ServeConfig{
|
|
|
|
HandshakeConfig: eventsearcher.Handshake,
|
|
|
|
Plugins: map[string]plugin.Plugin{
|
|
|
|
eventsearcher.PluginName: &eventsearcher.Plugin{Impl: &Searcher{}},
|
|
|
|
},
|
|
|
|
GRPCServer: plugin.DefaultGRPCServer,
|
|
|
|
})
|
|
|
|
}
|