2021-07-11 13:26:51 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2021-07-20 10:51:21 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2021-07-11 13:26:51 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/go-plugin"
|
|
|
|
|
2022-01-05 10:37:45 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/sdk/plugin/notifier"
|
2022-01-05 10:37:45 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-07-11 13:26:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NotifierConfig defines configuration parameters for notifiers plugins
|
|
|
|
type NotifierConfig struct {
|
2021-07-20 10:51:21 +00:00
|
|
|
FsEvents []string `json:"fs_events" mapstructure:"fs_events"`
|
2021-10-10 11:08:05 +00:00
|
|
|
ProviderEvents []string `json:"provider_events" mapstructure:"provider_events"`
|
|
|
|
ProviderObjects []string `json:"provider_objects" mapstructure:"provider_objects"`
|
2021-07-20 10:51:21 +00:00
|
|
|
RetryMaxTime int `json:"retry_max_time" mapstructure:"retry_max_time"`
|
|
|
|
RetryQueueMaxSize int `json:"retry_queue_max_size" mapstructure:"retry_queue_max_size"`
|
2021-07-11 13:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NotifierConfig) hasActions() bool {
|
|
|
|
if len(c.FsEvents) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
2021-10-10 11:08:05 +00:00
|
|
|
if len(c.ProviderEvents) > 0 && len(c.ProviderObjects) > 0 {
|
2021-07-11 13:26:51 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-07-20 10:51:21 +00:00
|
|
|
type eventsQueue struct {
|
|
|
|
sync.RWMutex
|
2022-01-02 14:16:35 +00:00
|
|
|
fsEvents []*notifier.FsEvent
|
|
|
|
providerEvents []*notifier.ProviderEvent
|
2021-07-20 10:51:21 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func (q *eventsQueue) addFsEvent(event *notifier.FsEvent) {
|
2021-07-20 10:51:21 +00:00
|
|
|
q.Lock()
|
|
|
|
defer q.Unlock()
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
q.fsEvents = append(q.fsEvents, event)
|
2021-07-20 10:51:21 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func (q *eventsQueue) addProviderEvent(event *notifier.ProviderEvent) {
|
2021-07-20 10:51:21 +00:00
|
|
|
q.Lock()
|
|
|
|
defer q.Unlock()
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
q.providerEvents = append(q.providerEvents, event)
|
2021-07-20 10:51:21 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func (q *eventsQueue) popFsEvent() *notifier.FsEvent {
|
2021-07-20 10:51:21 +00:00
|
|
|
q.Lock()
|
|
|
|
defer q.Unlock()
|
|
|
|
|
|
|
|
if len(q.fsEvents) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
truncLen := len(q.fsEvents) - 1
|
|
|
|
ev := q.fsEvents[truncLen]
|
|
|
|
q.fsEvents[truncLen] = nil
|
|
|
|
q.fsEvents = q.fsEvents[:truncLen]
|
|
|
|
|
|
|
|
return ev
|
|
|
|
}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func (q *eventsQueue) popProviderEvent() *notifier.ProviderEvent {
|
2021-07-20 10:51:21 +00:00
|
|
|
q.Lock()
|
|
|
|
defer q.Unlock()
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
if len(q.providerEvents) == 0 {
|
2021-07-20 10:51:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-10 11:08:05 +00:00
|
|
|
truncLen := len(q.providerEvents) - 1
|
|
|
|
ev := q.providerEvents[truncLen]
|
|
|
|
q.providerEvents[truncLen] = nil
|
|
|
|
q.providerEvents = q.providerEvents[:truncLen]
|
2021-07-20 10:51:21 +00:00
|
|
|
|
|
|
|
return ev
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *eventsQueue) getSize() int {
|
|
|
|
q.RLock()
|
|
|
|
defer q.RUnlock()
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
return len(q.providerEvents) + len(q.fsEvents)
|
2021-07-20 10:51:21 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
type notifierPlugin struct {
|
|
|
|
config Config
|
|
|
|
notifier notifier.Notifier
|
|
|
|
client *plugin.Client
|
2021-07-20 10:51:21 +00:00
|
|
|
queue *eventsQueue
|
2021-07-11 13:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newNotifierPlugin(config Config) (*notifierPlugin, error) {
|
|
|
|
p := ¬ifierPlugin{
|
|
|
|
config: config,
|
2021-07-20 10:51:21 +00:00
|
|
|
queue: &eventsQueue{},
|
2021-07-11 13:26:51 +00:00
|
|
|
}
|
|
|
|
if err := p.initialize(); err != nil {
|
2021-07-16 16:22:42 +00:00
|
|
|
logger.Warn(logSender, "", "unable to create notifier plugin: %v, config %+v", err, config)
|
2021-07-11 13:26:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *notifierPlugin) exited() bool {
|
|
|
|
return p.client.Exited()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *notifierPlugin) cleanup() {
|
|
|
|
p.client.Kill()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *notifierPlugin) initialize() error {
|
|
|
|
killProcess(p.config.Cmd)
|
2021-07-16 16:22:42 +00:00
|
|
|
logger.Debug(logSender, "", "create new notifier plugin %#v", p.config.Cmd)
|
2021-07-11 13:26:51 +00:00
|
|
|
if !p.config.NotifierOptions.hasActions() {
|
2021-07-16 16:22:42 +00:00
|
|
|
return fmt.Errorf("no actions defined for the notifier plugin %#v", p.config.Cmd)
|
2021-07-11 13:26:51 +00:00
|
|
|
}
|
|
|
|
var secureConfig *plugin.SecureConfig
|
|
|
|
if p.config.SHA256Sum != "" {
|
|
|
|
secureConfig.Checksum = []byte(p.config.SHA256Sum)
|
|
|
|
secureConfig.Hash = sha256.New()
|
|
|
|
}
|
|
|
|
client := plugin.NewClient(&plugin.ClientConfig{
|
|
|
|
HandshakeConfig: notifier.Handshake,
|
|
|
|
Plugins: notifier.PluginMap,
|
|
|
|
Cmd: exec.Command(p.config.Cmd, p.config.Args...),
|
|
|
|
AllowedProtocols: []plugin.Protocol{
|
|
|
|
plugin.ProtocolGRPC,
|
|
|
|
},
|
|
|
|
AutoMTLS: p.config.AutoMTLS,
|
|
|
|
SecureConfig: secureConfig,
|
|
|
|
Managed: false,
|
|
|
|
Logger: &logger.HCLogAdapter{
|
|
|
|
Logger: hclog.New(&hclog.LoggerOptions{
|
2021-10-23 13:47:21 +00:00
|
|
|
Name: fmt.Sprintf("%v.%v", logSender, notifier.PluginName),
|
2021-07-11 13:26:51 +00:00
|
|
|
Level: pluginsLogLevel,
|
|
|
|
DisableTime: true,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
rpcClient, err := client.Client()
|
|
|
|
if err != nil {
|
2021-07-16 16:22:42 +00:00
|
|
|
logger.Debug(logSender, "", "unable to get rpc client for plugin %#v: %v", p.config.Cmd, err)
|
2021-07-11 13:26:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
raw, err := rpcClient.Dispense(notifier.PluginName)
|
|
|
|
if err != nil {
|
2021-07-16 16:22:42 +00:00
|
|
|
logger.Debug(logSender, "", "unable to get plugin %v from rpc client for command %#v: %v",
|
2021-07-11 13:26:51 +00:00
|
|
|
notifier.PluginName, p.config.Cmd, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.client = client
|
|
|
|
p.notifier = raw.(notifier.Notifier)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-20 17:39:49 +00:00
|
|
|
func (p *notifierPlugin) canQueueEvent(timestamp int64) bool {
|
2021-07-20 10:51:21 +00:00
|
|
|
if p.config.NotifierOptions.RetryMaxTime == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2022-01-02 14:16:35 +00:00
|
|
|
if time.Now().After(time.Unix(0, timestamp).Add(time.Duration(p.config.NotifierOptions.RetryMaxTime) * time.Second)) {
|
2022-01-02 18:22:44 +00:00
|
|
|
logger.Warn(logSender, "", "dropping too late event for plugin %v, event timestamp: %v",
|
|
|
|
p.config.Cmd, time.Unix(0, timestamp))
|
2021-07-20 10:51:21 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if p.config.NotifierOptions.RetryQueueMaxSize > 0 {
|
|
|
|
return p.queue.getSize() < p.config.NotifierOptions.RetryQueueMaxSize
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func (p *notifierPlugin) notifyFsAction(event *notifier.FsEvent) {
|
|
|
|
if !util.IsStringInSlice(event.Action, p.config.NotifierOptions.FsEvents) {
|
2021-07-11 13:26:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2022-01-02 14:16:35 +00:00
|
|
|
p.sendFsEvent(event)
|
2021-07-11 13:26:51 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func (p *notifierPlugin) notifyProviderAction(event *notifier.ProviderEvent, object Renderer) {
|
|
|
|
if !util.IsStringInSlice(event.Action, p.config.NotifierOptions.ProviderEvents) ||
|
|
|
|
!util.IsStringInSlice(event.ObjectType, p.config.NotifierOptions.ProviderObjects) {
|
2021-07-11 13:26:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2022-01-02 14:16:35 +00:00
|
|
|
objectAsJSON, err := object.RenderAsJSON(event.Action != "delete")
|
2021-07-11 13:26:51 +00:00
|
|
|
if err != nil {
|
2022-01-02 14:16:35 +00:00
|
|
|
logger.Warn(logSender, "", "unable to render user as json for action %v: %v", event.Action, err)
|
2021-07-11 13:26:51 +00:00
|
|
|
return
|
|
|
|
}
|
2022-01-02 14:16:35 +00:00
|
|
|
event.ObjectData = objectAsJSON
|
|
|
|
p.sendProviderEvent(event)
|
2021-07-11 13:26:51 +00:00
|
|
|
}()
|
|
|
|
}
|
2021-07-20 10:51:21 +00:00
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func (p *notifierPlugin) sendFsEvent(event *notifier.FsEvent) {
|
|
|
|
if err := p.notifier.NotifyFsEvent(event); err != nil {
|
2021-07-20 10:51:21 +00:00
|
|
|
logger.Warn(logSender, "", "unable to send fs action notification to plugin %v: %v", p.config.Cmd, err)
|
2022-01-02 14:16:35 +00:00
|
|
|
if p.canQueueEvent(event.Timestamp) {
|
|
|
|
p.queue.addFsEvent(event)
|
2021-07-20 10:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func (p *notifierPlugin) sendProviderEvent(event *notifier.ProviderEvent) {
|
|
|
|
if err := p.notifier.NotifyProviderEvent(event); err != nil {
|
2021-07-20 10:51:21 +00:00
|
|
|
logger.Warn(logSender, "", "unable to send user action notification to plugin %v: %v", p.config.Cmd, err)
|
2022-01-02 14:16:35 +00:00
|
|
|
if p.canQueueEvent(event.Timestamp) {
|
|
|
|
p.queue.addProviderEvent(event)
|
2021-07-20 10:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *notifierPlugin) sendQueuedEvents() {
|
|
|
|
queueSize := p.queue.getSize()
|
|
|
|
if queueSize == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Debug(logSender, "", "check queued events for notifier %#v, events size: %v", p.config.Cmd, queueSize)
|
|
|
|
fsEv := p.queue.popFsEvent()
|
|
|
|
for fsEv != nil {
|
2022-01-02 14:16:35 +00:00
|
|
|
go func(ev *notifier.FsEvent) {
|
|
|
|
p.sendFsEvent(ev)
|
|
|
|
}(fsEv)
|
2021-07-20 10:51:21 +00:00
|
|
|
fsEv = p.queue.popFsEvent()
|
|
|
|
}
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
providerEv := p.queue.popProviderEvent()
|
|
|
|
for providerEv != nil {
|
2022-01-02 14:16:35 +00:00
|
|
|
go func(ev *notifier.ProviderEvent) {
|
|
|
|
p.sendProviderEvent(ev)
|
|
|
|
}(providerEv)
|
2021-10-10 11:08:05 +00:00
|
|
|
providerEv = p.queue.popProviderEvent()
|
2021-07-20 10:51:21 +00:00
|
|
|
}
|
|
|
|
logger.Debug(logSender, "", "queued events sent for notifier %#v, new events size: %v", p.config.Cmd, p.queue.getSize())
|
|
|
|
}
|