2020-07-24 21:39:38 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
2021-12-04 16:27:24 +00:00
|
|
|
"github.com/lithammer/shortuuid/v3"
|
|
|
|
"github.com/rs/xid"
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
|
|
|
"github.com/sftpgo/sdk/plugin/notifier"
|
2020-07-24 21:39:38 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
2022-01-05 10:37:45 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/plugin"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/vfs"
|
2020-07-24 21:39:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewActionNotification(t *testing.T) {
|
|
|
|
user := &dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "username",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.LocalFilesystemProvider
|
2020-07-24 21:39:38 +00:00
|
|
|
user.FsConfig.S3Config = vfs.S3FsConfig{
|
2022-01-06 09:11:47 +00:00
|
|
|
BaseS3FsConfig: sdk.BaseS3FsConfig{
|
2021-07-11 13:26:51 +00:00
|
|
|
Bucket: "s3bucket",
|
|
|
|
Endpoint: "endpoint",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
user.FsConfig.GCSConfig = vfs.GCSFsConfig{
|
2022-01-06 09:11:47 +00:00
|
|
|
BaseGCSFsConfig: sdk.BaseGCSFsConfig{
|
2021-07-11 13:26:51 +00:00
|
|
|
Bucket: "gcsbucket",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2020-10-25 07:18:48 +00:00
|
|
|
user.FsConfig.AzBlobConfig = vfs.AzBlobFsConfig{
|
2022-01-06 09:11:47 +00:00
|
|
|
BaseAzBlobFsConfig: sdk.BaseAzBlobFsConfig{
|
2021-07-11 13:26:51 +00:00
|
|
|
Container: "azcontainer",
|
|
|
|
Endpoint: "azendpoint",
|
|
|
|
},
|
2020-10-25 07:18:48 +00:00
|
|
|
}
|
2021-05-26 05:48:37 +00:00
|
|
|
user.FsConfig.SFTPConfig = vfs.SFTPFsConfig{
|
2022-01-06 09:11:47 +00:00
|
|
|
BaseSFTPFsConfig: sdk.BaseSFTPFsConfig{
|
2021-07-11 13:26:51 +00:00
|
|
|
Endpoint: "sftpendpoint",
|
|
|
|
},
|
2021-05-26 05:48:37 +00:00
|
|
|
}
|
2021-12-04 16:27:24 +00:00
|
|
|
sessionID := xid.New().String()
|
|
|
|
a := newActionNotification(user, operationDownload, "path", "vpath", "target", "", "", ProtocolSFTP, "", sessionID,
|
|
|
|
123, 0, errors.New("fake error"))
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Equal(t, user.Username, a.Username)
|
|
|
|
assert.Equal(t, 0, len(a.Bucket))
|
|
|
|
assert.Equal(t, 0, len(a.Endpoint))
|
2021-10-20 17:39:49 +00:00
|
|
|
assert.Equal(t, 2, a.Status)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.S3FilesystemProvider
|
2021-12-04 16:27:24 +00:00
|
|
|
a = newActionNotification(user, operationDownload, "path", "vpath", "target", "", "", ProtocolSSH, "", sessionID,
|
|
|
|
123, 0, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Equal(t, "s3bucket", a.Bucket)
|
|
|
|
assert.Equal(t, "endpoint", a.Endpoint)
|
|
|
|
assert.Equal(t, 1, a.Status)
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.GCSFilesystemProvider
|
2021-12-04 16:27:24 +00:00
|
|
|
a = newActionNotification(user, operationDownload, "path", "vpath", "target", "", "", ProtocolSCP, "", sessionID,
|
|
|
|
123, 0, ErrQuotaExceeded)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Equal(t, "gcsbucket", a.Bucket)
|
|
|
|
assert.Equal(t, 0, len(a.Endpoint))
|
2021-10-20 17:39:49 +00:00
|
|
|
assert.Equal(t, 3, a.Status)
|
2020-10-25 07:18:48 +00:00
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.AzureBlobFilesystemProvider
|
2021-12-04 16:27:24 +00:00
|
|
|
a = newActionNotification(user, operationDownload, "path", "vpath", "target", "", "", ProtocolSCP, "", sessionID,
|
|
|
|
123, 0, nil)
|
2020-10-25 07:18:48 +00:00
|
|
|
assert.Equal(t, "azcontainer", a.Bucket)
|
2021-06-11 20:27:36 +00:00
|
|
|
assert.Equal(t, "azendpoint", a.Endpoint)
|
2020-10-25 07:18:48 +00:00
|
|
|
assert.Equal(t, 1, a.Status)
|
|
|
|
|
2021-12-04 16:27:24 +00:00
|
|
|
a = newActionNotification(user, operationDownload, "path", "vpath", "target", "", "", ProtocolSCP, "", sessionID,
|
|
|
|
123, os.O_APPEND, nil)
|
2020-10-25 07:18:48 +00:00
|
|
|
assert.Equal(t, "azcontainer", a.Bucket)
|
|
|
|
assert.Equal(t, "azendpoint", a.Endpoint)
|
|
|
|
assert.Equal(t, 1, a.Status)
|
2021-05-31 20:33:23 +00:00
|
|
|
assert.Equal(t, os.O_APPEND, a.OpenFlags)
|
2021-05-26 05:48:37 +00:00
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
user.FsConfig.Provider = sdk.SFTPFilesystemProvider
|
2021-12-04 16:27:24 +00:00
|
|
|
a = newActionNotification(user, operationDownload, "path", "vpath", "target", "", "", ProtocolSFTP, "", sessionID,
|
|
|
|
123, 0, nil)
|
2021-05-26 05:48:37 +00:00
|
|
|
assert.Equal(t, "sftpendpoint", a.Endpoint)
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestActionHTTP(t *testing.T) {
|
|
|
|
actionsCopy := Config.Actions
|
|
|
|
|
|
|
|
Config.Actions = ProtocolActions{
|
|
|
|
ExecuteOn: []string{operationDownload},
|
|
|
|
Hook: fmt.Sprintf("http://%v", httpAddr),
|
|
|
|
}
|
|
|
|
user := &dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "username",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-12-04 16:27:24 +00:00
|
|
|
a := newActionNotification(user, operationDownload, "path", "vpath", "target", "", "", ProtocolSFTP, "",
|
|
|
|
xid.New().String(), 123, 0, nil)
|
2020-10-19 23:46:51 +00:00
|
|
|
err := actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
Config.Actions.Hook = "http://invalid:1234"
|
2020-10-19 23:46:51 +00:00
|
|
|
err = actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
Config.Actions.Hook = fmt.Sprintf("http://%v/404", httpAddr)
|
2020-10-19 23:46:51 +00:00
|
|
|
err = actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.EqualError(t, err, errUnexpectedHTTResponse.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
Config.Actions = actionsCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActionCMD(t *testing.T) {
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
actionsCopy := Config.Actions
|
|
|
|
|
|
|
|
hookCmd, err := exec.LookPath("true")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
Config.Actions = ProtocolActions{
|
|
|
|
ExecuteOn: []string{operationDownload},
|
|
|
|
Hook: hookCmd,
|
|
|
|
}
|
|
|
|
user := &dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "username",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
2021-12-04 16:27:24 +00:00
|
|
|
sessionID := shortuuid.New()
|
|
|
|
a := newActionNotification(user, operationDownload, "path", "vpath", "target", "", "", ProtocolSFTP, "", sessionID,
|
|
|
|
123, 0, nil)
|
2020-10-19 23:46:51 +00:00
|
|
|
err = actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-12-04 16:27:24 +00:00
|
|
|
c := NewBaseConnection("id", ProtocolSFTP, "", "", *user)
|
|
|
|
ExecuteActionNotification(c, OperationSSHCmd, "path", "vpath", "target", "vtarget", "sha1sum", 0, nil)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
ExecuteActionNotification(c, operationDownload, "path", "vpath", "", "", "", 0, nil)
|
|
|
|
|
2020-07-24 21:39:38 +00:00
|
|
|
Config.Actions = actionsCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrongActions(t *testing.T) {
|
|
|
|
actionsCopy := Config.Actions
|
|
|
|
|
|
|
|
badCommand := "/bad/command"
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
badCommand = "C:\\bad\\command"
|
|
|
|
}
|
|
|
|
Config.Actions = ProtocolActions{
|
|
|
|
ExecuteOn: []string{operationUpload},
|
|
|
|
Hook: badCommand,
|
|
|
|
}
|
|
|
|
user := &dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "username",
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2021-12-04 16:27:24 +00:00
|
|
|
a := newActionNotification(user, operationUpload, "", "", "", "", "", ProtocolSFTP, "", xid.New().String(),
|
|
|
|
123, 0, nil)
|
2020-10-19 23:46:51 +00:00
|
|
|
err := actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Error(t, err, "action with bad command must fail")
|
|
|
|
|
|
|
|
a.Action = operationDelete
|
2020-10-19 23:46:51 +00:00
|
|
|
err = actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.EqualError(t, err, errUnconfiguredAction.Error())
|
|
|
|
|
|
|
|
Config.Actions.Hook = "http://foo\x7f.com/"
|
|
|
|
a.Action = operationUpload
|
2020-10-19 23:46:51 +00:00
|
|
|
err = actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.Error(t, err, "action with bad url must fail")
|
|
|
|
|
|
|
|
Config.Actions.Hook = ""
|
2020-10-19 23:46:51 +00:00
|
|
|
err = actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.EqualError(t, err, errNoHook.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
Config.Actions.Hook = "relative path"
|
2020-10-19 23:46:51 +00:00
|
|
|
err = actionHandler.Handle(a)
|
2020-07-24 21:39:38 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.EqualError(t, err, fmt.Sprintf("invalid notification command %#v", Config.Actions.Hook))
|
|
|
|
}
|
|
|
|
|
|
|
|
Config.Actions = actionsCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPreDeleteAction(t *testing.T) {
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
actionsCopy := Config.Actions
|
|
|
|
|
|
|
|
hookCmd, err := exec.LookPath("true")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
Config.Actions = ProtocolActions{
|
|
|
|
ExecuteOn: []string{operationPreDelete},
|
|
|
|
Hook: hookCmd,
|
|
|
|
}
|
|
|
|
homeDir := filepath.Join(os.TempDir(), "test_user")
|
|
|
|
err = os.MkdirAll(homeDir, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: "username",
|
|
|
|
HomeDir: homeDir,
|
|
|
|
},
|
2020-07-24 21:39:38 +00:00
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = []string{dataprovider.PermAny}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs := vfs.NewOsFs("id", homeDir, "")
|
2021-07-24 18:11:17 +00:00
|
|
|
c := NewBaseConnection("id", ProtocolSFTP, "", "", user)
|
2020-07-24 21:39:38 +00:00
|
|
|
|
|
|
|
testfile := filepath.Join(user.HomeDir, "testfile")
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(testfile, []byte("test"), os.ModePerm)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
info, err := os.Stat(testfile)
|
|
|
|
assert.NoError(t, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
err = c.RemoveFile(fs, testfile, "testfile", info)
|
2020-07-24 21:39:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.FileExists(t, testfile)
|
|
|
|
|
|
|
|
os.RemoveAll(homeDir)
|
|
|
|
|
|
|
|
Config.Actions = actionsCopy
|
|
|
|
}
|
2020-10-20 20:51:58 +00:00
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func TestUnconfiguredHook(t *testing.T) {
|
|
|
|
actionsCopy := Config.Actions
|
|
|
|
|
|
|
|
Config.Actions = ProtocolActions{
|
|
|
|
ExecuteOn: []string{operationDownload},
|
|
|
|
Hook: "",
|
|
|
|
}
|
|
|
|
pluginsConfig := []plugin.Config{
|
|
|
|
{
|
|
|
|
Type: "notifier",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := plugin.Initialize(pluginsConfig, true)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, plugin.Handler.HasNotifiers())
|
|
|
|
|
|
|
|
c := NewBaseConnection("id", ProtocolSFTP, "", "", dataprovider.User{})
|
|
|
|
err = ExecutePreAction(c, OperationPreDownload, "", "", 0, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ExecutePreAction(c, operationPreDelete, "", "", 0, 0)
|
|
|
|
assert.ErrorIs(t, err, errUnconfiguredAction)
|
|
|
|
|
|
|
|
ExecuteActionNotification(c, operationDownload, "", "", "", "", "", 0, nil)
|
|
|
|
|
|
|
|
err = plugin.Initialize(nil, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, plugin.Handler.HasNotifiers())
|
|
|
|
|
|
|
|
Config.Actions = actionsCopy
|
|
|
|
}
|
|
|
|
|
2020-10-20 20:51:58 +00:00
|
|
|
type actionHandlerStub struct {
|
|
|
|
called bool
|
|
|
|
}
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
func (h *actionHandlerStub) Handle(event *notifier.FsEvent) error {
|
2020-10-20 20:51:58 +00:00
|
|
|
h.called = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitializeActionHandler(t *testing.T) {
|
|
|
|
handler := &actionHandlerStub{}
|
|
|
|
|
|
|
|
InitializeActionHandler(handler)
|
|
|
|
t.Cleanup(func() {
|
2021-02-12 20:42:49 +00:00
|
|
|
InitializeActionHandler(&defaultActionHandler{})
|
2020-10-20 20:51:58 +00:00
|
|
|
})
|
|
|
|
|
2022-01-02 14:16:35 +00:00
|
|
|
err := actionHandler.Handle(¬ifier.FsEvent{})
|
2020-10-20 20:51:58 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, handler.called)
|
|
|
|
}
|