2020-07-24 21:39:38 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/httpclient"
|
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
2022-01-05 10:37:45 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/plugin"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/sdk"
|
2022-01-02 14:16:35 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/sdk/plugin/notifier"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2020-07-24 21:39:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errUnconfiguredAction = errors.New("no hook is configured for this action")
|
|
|
|
errNoHook = errors.New("unable to execute action, no hook defined")
|
|
|
|
errUnexpectedHTTResponse = errors.New("unexpected HTTP response code")
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProtocolActions defines the action to execute on file operations and SSH commands
|
|
|
|
type ProtocolActions struct {
|
|
|
|
// Valid values are download, upload, pre-delete, delete, rename, ssh_cmd. Empty slice to disable
|
|
|
|
ExecuteOn []string `json:"execute_on" mapstructure:"execute_on"`
|
2021-05-11 10:45:14 +00:00
|
|
|
// Actions to be performed synchronously.
|
|
|
|
// The pre-delete action is always executed synchronously while the other ones are asynchronous.
|
|
|
|
// Executing an action synchronously means that SFTPGo will not return a result code to the client
|
|
|
|
// (which is waiting for it) until your hook have completed its execution.
|
|
|
|
ExecuteSync []string `json:"execute_sync" mapstructure:"execute_sync"`
|
2020-07-24 21:39:38 +00:00
|
|
|
// Absolute path to an external program or an HTTP URL
|
|
|
|
Hook string `json:"hook" mapstructure:"hook"`
|
|
|
|
}
|
|
|
|
|
2021-02-12 20:42:49 +00:00
|
|
|
var actionHandler ActionHandler = &defaultActionHandler{}
|
2020-10-19 23:46:51 +00:00
|
|
|
|
|
|
|
// InitializeActionHandler lets the user choose an action handler implementation.
|
|
|
|
//
|
|
|
|
// Do NOT call this function after application initialization.
|
|
|
|
func InitializeActionHandler(handler ActionHandler) {
|
|
|
|
actionHandler = handler
|
|
|
|
}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func handleUnconfiguredPreAction(operation string) error {
|
|
|
|
// for pre-delete we execute the internal handling on error, so we must return errUnconfiguredAction.
|
|
|
|
// Other pre action will deny the operation on error so if we have no configuration we must return
|
|
|
|
// a nil error
|
|
|
|
if operation == operationPreDelete {
|
|
|
|
return errUnconfiguredAction
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 05:48:37 +00:00
|
|
|
// ExecutePreAction executes a pre-* action and returns the result
|
2021-12-04 16:27:24 +00:00
|
|
|
func ExecutePreAction(conn *BaseConnection, operation, filePath, virtualPath string, fileSize int64, openFlags int) error {
|
2022-01-02 14:16:35 +00:00
|
|
|
var event *notifier.FsEvent
|
|
|
|
hasNotifiersPlugin := plugin.Handler.HasNotifiers()
|
|
|
|
hasHook := util.IsStringInSlice(operation, Config.Actions.ExecuteOn)
|
|
|
|
if !hasHook && !hasNotifiersPlugin {
|
|
|
|
return handleUnconfiguredPreAction(operation)
|
|
|
|
}
|
|
|
|
event = newActionNotification(&conn.User, operation, filePath, virtualPath, "", "", "",
|
|
|
|
conn.protocol, conn.GetRemoteIP(), conn.ID, fileSize, openFlags, nil)
|
|
|
|
if hasNotifiersPlugin {
|
|
|
|
plugin.Handler.NotifyFsEvent(event)
|
2021-05-26 05:48:37 +00:00
|
|
|
}
|
2022-01-02 14:16:35 +00:00
|
|
|
if !hasHook {
|
|
|
|
return handleUnconfiguredPreAction(operation)
|
|
|
|
}
|
|
|
|
return actionHandler.Handle(event)
|
2021-05-26 05:48:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 10:45:14 +00:00
|
|
|
// ExecuteActionNotification executes the defined hook, if any, for the specified action
|
2021-12-04 16:27:24 +00:00
|
|
|
func ExecuteActionNotification(conn *BaseConnection, operation, filePath, virtualPath, target, virtualTarget, sshCmd string,
|
|
|
|
fileSize int64, err error,
|
2021-10-10 11:08:05 +00:00
|
|
|
) {
|
2022-01-02 14:16:35 +00:00
|
|
|
hasNotifiersPlugin := plugin.Handler.HasNotifiers()
|
|
|
|
hasHook := util.IsStringInSlice(operation, Config.Actions.ExecuteOn)
|
|
|
|
if !hasHook && !hasNotifiersPlugin {
|
2021-05-11 10:45:14 +00:00
|
|
|
return
|
|
|
|
}
|
2022-01-02 14:16:35 +00:00
|
|
|
notification := newActionNotification(&conn.User, operation, filePath, virtualPath, target, virtualTarget, sshCmd,
|
|
|
|
conn.protocol, conn.GetRemoteIP(), conn.ID, fileSize, 0, err)
|
|
|
|
if hasNotifiersPlugin {
|
|
|
|
plugin.Handler.NotifyFsEvent(notification)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasHook {
|
|
|
|
if util.IsStringInSlice(operation, Config.Actions.ExecuteSync) {
|
|
|
|
actionHandler.Handle(notification) //nolint:errcheck
|
|
|
|
return
|
|
|
|
}
|
2021-05-11 10:45:14 +00:00
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
go actionHandler.Handle(notification) //nolint:errcheck
|
|
|
|
}
|
2020-10-19 23:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ActionHandler handles a notification for a Protocol Action.
|
|
|
|
type ActionHandler interface {
|
2022-01-02 14:16:35 +00:00
|
|
|
Handle(notification *notifier.FsEvent) error
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 23:46:51 +00:00
|
|
|
func newActionNotification(
|
|
|
|
user *dataprovider.User,
|
2021-12-04 16:27:24 +00:00
|
|
|
operation, filePath, virtualPath, target, virtualTarget, sshCmd, protocol, ip, sessionID string,
|
2020-10-19 23:46:51 +00:00
|
|
|
fileSize int64,
|
2021-05-31 20:33:23 +00:00
|
|
|
openFlags int,
|
2020-10-19 23:46:51 +00:00
|
|
|
err error,
|
2022-01-02 14:16:35 +00:00
|
|
|
) *notifier.FsEvent {
|
2020-10-19 23:46:51 +00:00
|
|
|
var bucket, endpoint string
|
2020-07-24 21:39:38 +00:00
|
|
|
status := 1
|
2020-10-19 23:46:51 +00:00
|
|
|
|
2021-05-26 05:48:37 +00:00
|
|
|
fsConfig := user.GetFsConfigForPath(virtualPath)
|
|
|
|
|
|
|
|
switch fsConfig.Provider {
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.S3FilesystemProvider:
|
2021-05-26 05:48:37 +00:00
|
|
|
bucket = fsConfig.S3Config.Bucket
|
|
|
|
endpoint = fsConfig.S3Config.Endpoint
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.GCSFilesystemProvider:
|
2021-05-26 05:48:37 +00:00
|
|
|
bucket = fsConfig.GCSConfig.Bucket
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.AzureBlobFilesystemProvider:
|
2021-05-26 05:48:37 +00:00
|
|
|
bucket = fsConfig.AzBlobConfig.Container
|
2021-06-11 20:27:36 +00:00
|
|
|
if fsConfig.AzBlobConfig.Endpoint != "" {
|
2021-05-26 05:48:37 +00:00
|
|
|
endpoint = fsConfig.AzBlobConfig.Endpoint
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.SFTPFilesystemProvider:
|
2021-05-26 05:48:37 +00:00
|
|
|
endpoint = fsConfig.SFTPConfig.Endpoint
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2020-10-19 23:46:51 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
if err == ErrQuotaExceeded {
|
2021-10-20 17:39:49 +00:00
|
|
|
status = 3
|
2020-07-24 21:39:38 +00:00
|
|
|
} else if err != nil {
|
2021-10-20 17:39:49 +00:00
|
|
|
status = 2
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2020-10-19 23:46:51 +00:00
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
return ¬ifier.FsEvent{
|
2021-10-10 11:08:05 +00:00
|
|
|
Action: operation,
|
|
|
|
Username: user.Username,
|
|
|
|
Path: filePath,
|
|
|
|
TargetPath: target,
|
|
|
|
VirtualPath: virtualPath,
|
|
|
|
VirtualTargetPath: virtualTarget,
|
|
|
|
SSHCmd: sshCmd,
|
|
|
|
FileSize: fileSize,
|
|
|
|
FsProvider: int(fsConfig.Provider),
|
|
|
|
Bucket: bucket,
|
|
|
|
Endpoint: endpoint,
|
|
|
|
Status: status,
|
|
|
|
Protocol: protocol,
|
|
|
|
IP: ip,
|
2021-12-04 16:27:24 +00:00
|
|
|
SessionID: sessionID,
|
2021-10-10 11:08:05 +00:00
|
|
|
OpenFlags: openFlags,
|
2021-10-20 17:39:49 +00:00
|
|
|
Timestamp: time.Now().UnixNano(),
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 23:46:51 +00:00
|
|
|
type defaultActionHandler struct{}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func (h *defaultActionHandler) Handle(event *notifier.FsEvent) error {
|
|
|
|
if !util.IsStringInSlice(event.Action, Config.Actions.ExecuteOn) {
|
2020-10-19 23:46:51 +00:00
|
|
|
return errUnconfiguredAction
|
|
|
|
}
|
|
|
|
|
|
|
|
if Config.Actions.Hook == "" {
|
2022-01-02 14:16:35 +00:00
|
|
|
logger.Warn(event.Protocol, "", "Unable to send notification, no hook is defined")
|
2020-10-19 23:46:51 +00:00
|
|
|
|
|
|
|
return errNoHook
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(Config.Actions.Hook, "http") {
|
2022-01-02 14:16:35 +00:00
|
|
|
return h.handleHTTP(event)
|
2020-10-19 23:46:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
return h.handleCommand(event)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func (h *defaultActionHandler) handleHTTP(event *notifier.FsEvent) error {
|
2020-10-19 23:46:51 +00:00
|
|
|
u, err := url.Parse(Config.Actions.Hook)
|
|
|
|
if err != nil {
|
2022-01-02 14:16:35 +00:00
|
|
|
logger.Error(event.Protocol, "", "Invalid hook %#v for operation %#v: %v",
|
|
|
|
Config.Actions.Hook, event.Action, err)
|
2020-10-19 23:46:51 +00:00
|
|
|
return err
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2020-10-19 23:46:51 +00:00
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
respCode := 0
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
2022-01-02 14:16:35 +00:00
|
|
|
_ = json.NewEncoder(&b).Encode(event)
|
2020-10-19 23:46:51 +00:00
|
|
|
|
2021-05-25 06:36:01 +00:00
|
|
|
resp, err := httpclient.RetryablePost(Config.Actions.Hook, "application/json", &b)
|
2020-10-19 23:46:51 +00:00
|
|
|
if err == nil {
|
|
|
|
respCode = resp.StatusCode
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
if respCode != http.StatusOK {
|
|
|
|
err = errUnexpectedHTTResponse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
logger.Debug(event.Protocol, "", "notified operation %#v to URL: %v status code: %v, elapsed: %v err: %v",
|
|
|
|
event.Action, u.Redacted(), respCode, time.Since(startTime), err)
|
2020-10-19 23:46:51 +00:00
|
|
|
|
|
|
|
return err
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func (h *defaultActionHandler) handleCommand(event *notifier.FsEvent) error {
|
2020-07-24 21:39:38 +00:00
|
|
|
if !filepath.IsAbs(Config.Actions.Hook) {
|
|
|
|
err := fmt.Errorf("invalid notification command %#v", Config.Actions.Hook)
|
2022-01-02 14:16:35 +00:00
|
|
|
logger.Warn(event.Protocol, "", "unable to execute notification command: %v", err)
|
2020-10-19 23:46:51 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-10-19 23:46:51 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
2020-10-19 23:46:51 +00:00
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
cmd := exec.CommandContext(ctx, Config.Actions.Hook)
|
2022-01-02 14:16:35 +00:00
|
|
|
cmd.Env = append(os.Environ(), notificationAsEnvVars(event)...)
|
2020-10-19 23:46:51 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
err := cmd.Run()
|
2020-10-19 23:46:51 +00:00
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
logger.Debug(event.Protocol, "", "executed command %#v, elapsed: %v, error: %v",
|
2021-10-10 11:08:05 +00:00
|
|
|
Config.Actions.Hook, time.Since(startTime), err)
|
2020-10-19 23:46:51 +00:00
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func notificationAsEnvVars(event *notifier.FsEvent) []string {
|
2020-10-19 23:46:51 +00:00
|
|
|
return []string{
|
2022-01-02 14:16:35 +00:00
|
|
|
fmt.Sprintf("SFTPGO_ACTION=%v", event.Action),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_USERNAME=%v", event.Username),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_PATH=%v", event.Path),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_TARGET=%v", event.TargetPath),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_VIRTUAL_PATH=%v", event.VirtualPath),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_VIRTUAL_TARGET=%v", event.VirtualTargetPath),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_SSH_CMD=%v", event.SSHCmd),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_FILE_SIZE=%v", event.FileSize),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_FS_PROVIDER=%v", event.FsProvider),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_BUCKET=%v", event.Bucket),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_ENDPOINT=%v", event.Endpoint),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_STATUS=%v", event.Status),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_PROTOCOL=%v", event.Protocol),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_IP=%v", event.IP),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_SESSION_ID=%v", event.SessionID),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_OPEN_FLAGS=%v", event.OpenFlags),
|
|
|
|
fmt.Sprintf("SFTPGO_ACTION_TIMESTAMP=%v", event.Timestamp),
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
}
|