mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-08 23:28:39 +03:00
plugins: improve notifier and searcher
This commit is contained in:
@@ -4,13 +4,11 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"github.com/drakkan/sftpgo/v2/sdk/plugin/eventsearcher/proto"
|
||||
)
|
||||
|
||||
const (
|
||||
rpcTimeout = 30 * time.Second
|
||||
rpcTimeout = 20 * time.Second
|
||||
)
|
||||
|
||||
// GRPCClient is an implementation of Notifier interface that talks over RPC.
|
||||
@@ -19,54 +17,58 @@ type GRPCClient struct {
|
||||
}
|
||||
|
||||
// SearchFsEvents implements the Searcher interface
|
||||
func (c *GRPCClient) SearchFsEvents(startTimestamp, endTimestamp time.Time, action, username, ip, sshCmd, protocol,
|
||||
instanceID, continuationToken string, status, limit int) (string, []byte, error) {
|
||||
func (c *GRPCClient) SearchFsEvents(startTimestamp, endTimestamp int64, username, ip, sshCmd string, actions,
|
||||
protocols, instanceIDs, excludeIDs []string, statuses []int32, limit, order int,
|
||||
) ([]byte, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), rpcTimeout)
|
||||
defer cancel()
|
||||
|
||||
resp, err := c.client.SearchFsEvents(ctx, &proto.FsEventsFilter{
|
||||
StartTimestamp: timestamppb.New(startTimestamp),
|
||||
EndTimestamp: timestamppb.New(endTimestamp),
|
||||
Action: action,
|
||||
Username: username,
|
||||
Ip: ip,
|
||||
SshCmd: sshCmd,
|
||||
Protocol: protocol,
|
||||
InstanceId: instanceID,
|
||||
ContinuationToken: continuationToken,
|
||||
Status: int32(status),
|
||||
Limit: int32(limit),
|
||||
StartTimestamp: startTimestamp,
|
||||
EndTimestamp: endTimestamp,
|
||||
Actions: actions,
|
||||
Username: username,
|
||||
Ip: ip,
|
||||
SshCmd: sshCmd,
|
||||
Protocols: protocols,
|
||||
InstanceIds: instanceIDs,
|
||||
Statuses: statuses,
|
||||
Limit: int32(limit),
|
||||
Order: proto.FsEventsFilter_Order(order),
|
||||
ExcludeIds: excludeIDs,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
return nil, err
|
||||
}
|
||||
return resp.ContinuationToken, resp.ResponseData, nil
|
||||
return resp.Data, nil
|
||||
}
|
||||
|
||||
// SearchProviderEvents implements the Searcher interface
|
||||
func (c *GRPCClient) SearchProviderEvents(startTimestamp, endTimestamp time.Time, action, username, ip, objectType,
|
||||
objectName, instanceID, continuationToken string, limit int) (string, []byte, error) {
|
||||
func (c *GRPCClient) SearchProviderEvents(startTimestamp, endTimestamp int64, username, ip, objectName string,
|
||||
limit, order int, actions, objectTypes, instanceIDs, excludeIDs []string,
|
||||
) ([]byte, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), rpcTimeout)
|
||||
defer cancel()
|
||||
|
||||
resp, err := c.client.SearchProviderEvents(ctx, &proto.ProviderEventsFilter{
|
||||
StartTimestamp: timestamppb.New(startTimestamp),
|
||||
EndTimestamp: timestamppb.New(endTimestamp),
|
||||
Action: action,
|
||||
Username: username,
|
||||
Ip: ip,
|
||||
ObjectType: objectType,
|
||||
ObjectName: objectName,
|
||||
InstanceId: instanceID,
|
||||
ContinuationToken: continuationToken,
|
||||
Limit: int32(limit),
|
||||
StartTimestamp: startTimestamp,
|
||||
EndTimestamp: endTimestamp,
|
||||
Actions: actions,
|
||||
Username: username,
|
||||
Ip: ip,
|
||||
ObjectTypes: objectTypes,
|
||||
ObjectName: objectName,
|
||||
InstanceIds: instanceIDs,
|
||||
Limit: int32(limit),
|
||||
Order: proto.ProviderEventsFilter_Order(order),
|
||||
ExcludeIds: excludeIDs,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
return nil, err
|
||||
}
|
||||
return resp.ContinuationToken, resp.ResponseData, nil
|
||||
return resp.Data, nil
|
||||
}
|
||||
|
||||
// GRPCServer defines the gRPC server that GRPCClient talks to.
|
||||
@@ -76,24 +78,26 @@ type GRPCServer struct {
|
||||
|
||||
// SearchFsEvents implement the server side fs events search method
|
||||
func (s *GRPCServer) SearchFsEvents(ctx context.Context, req *proto.FsEventsFilter) (*proto.SearchResponse, error) {
|
||||
continuationToken, responseData, err := s.Impl.SearchFsEvents(req.StartTimestamp.AsTime(),
|
||||
req.EndTimestamp.AsTime(), req.Action, req.Username, req.Ip, req.SshCmd, req.Protocol, req.InstanceId,
|
||||
req.ContinuationToken, int(req.Status), int(req.Limit))
|
||||
responseData, sameTsAtStart, sameTsAtEnd, err := s.Impl.SearchFsEvents(req.StartTimestamp,
|
||||
req.EndTimestamp, req.Username, req.Ip, req.SshCmd, req.Actions, req.Protocols, req.InstanceIds,
|
||||
req.ExcludeIds, req.Statuses, int(req.Limit), int(req.Order))
|
||||
|
||||
return &proto.SearchResponse{
|
||||
ContinuationToken: continuationToken,
|
||||
ResponseData: responseData,
|
||||
Data: responseData,
|
||||
SameTsAtStart: sameTsAtStart,
|
||||
SameTsAtEnd: sameTsAtEnd,
|
||||
}, err
|
||||
}
|
||||
|
||||
// SearchProviderEvents implement the server side provider events search method
|
||||
func (s *GRPCServer) SearchProviderEvents(ctx context.Context, req *proto.ProviderEventsFilter) (*proto.SearchResponse, error) {
|
||||
continuationToken, responseData, err := s.Impl.SearchProviderEvents(req.StartTimestamp.AsTime(),
|
||||
req.EndTimestamp.AsTime(), req.Action, req.Username, req.Ip, req.ObjectType, req.ObjectName,
|
||||
req.InstanceId, req.ContinuationToken, int(req.Limit))
|
||||
responseData, sameTsAtStart, sameTsAtEnd, err := s.Impl.SearchProviderEvents(req.StartTimestamp,
|
||||
req.EndTimestamp, req.Username, req.Ip, req.ObjectName, int(req.Limit),
|
||||
int(req.Order), req.Actions, req.ObjectTypes, req.InstanceIds, req.ExcludeIds)
|
||||
|
||||
return &proto.SearchResponse{
|
||||
ContinuationToken: continuationToken,
|
||||
ResponseData: responseData,
|
||||
Data: responseData,
|
||||
SameTsAtStart: sameTsAtStart,
|
||||
SameTsAtEnd: sameTsAtEnd,
|
||||
}, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user