actions_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package common
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "runtime"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/drakkan/sftpgo/dataprovider"
  12. "github.com/drakkan/sftpgo/vfs"
  13. )
  14. func TestNewActionNotification(t *testing.T) {
  15. user := &dataprovider.User{
  16. Username: "username",
  17. }
  18. user.FsConfig.Provider = vfs.LocalFilesystemProvider
  19. user.FsConfig.S3Config = vfs.S3FsConfig{
  20. Bucket: "s3bucket",
  21. Endpoint: "endpoint",
  22. }
  23. user.FsConfig.GCSConfig = vfs.GCSFsConfig{
  24. Bucket: "gcsbucket",
  25. }
  26. user.FsConfig.AzBlobConfig = vfs.AzBlobFsConfig{
  27. Container: "azcontainer",
  28. Endpoint: "azendpoint",
  29. }
  30. user.FsConfig.SFTPConfig = vfs.SFTPFsConfig{
  31. Endpoint: "sftpendpoint",
  32. }
  33. a := newActionNotification(user, operationDownload, "path", "vpath", "target", "", ProtocolSFTP, 123, 0, errors.New("fake error"))
  34. assert.Equal(t, user.Username, a.Username)
  35. assert.Equal(t, 0, len(a.Bucket))
  36. assert.Equal(t, 0, len(a.Endpoint))
  37. assert.Equal(t, 0, a.Status)
  38. user.FsConfig.Provider = vfs.S3FilesystemProvider
  39. a = newActionNotification(user, operationDownload, "path", "vpath", "target", "", ProtocolSSH, 123, 0, nil)
  40. assert.Equal(t, "s3bucket", a.Bucket)
  41. assert.Equal(t, "endpoint", a.Endpoint)
  42. assert.Equal(t, 1, a.Status)
  43. user.FsConfig.Provider = vfs.GCSFilesystemProvider
  44. a = newActionNotification(user, operationDownload, "path", "vpath", "target", "", ProtocolSCP, 123, 0, ErrQuotaExceeded)
  45. assert.Equal(t, "gcsbucket", a.Bucket)
  46. assert.Equal(t, 0, len(a.Endpoint))
  47. assert.Equal(t, 2, a.Status)
  48. user.FsConfig.Provider = vfs.AzureBlobFilesystemProvider
  49. a = newActionNotification(user, operationDownload, "path", "vpath", "target", "", ProtocolSCP, 123, 0, nil)
  50. assert.Equal(t, "azcontainer", a.Bucket)
  51. assert.Equal(t, "azendpoint", a.Endpoint)
  52. assert.Equal(t, 1, a.Status)
  53. a = newActionNotification(user, operationDownload, "path", "vpath", "target", "", ProtocolSCP, 123, os.O_APPEND, nil)
  54. assert.Equal(t, "azcontainer", a.Bucket)
  55. assert.Equal(t, "azendpoint", a.Endpoint)
  56. assert.Equal(t, 1, a.Status)
  57. assert.Equal(t, os.O_APPEND, a.OpenFlags)
  58. user.FsConfig.Provider = vfs.SFTPFilesystemProvider
  59. a = newActionNotification(user, operationDownload, "path", "vpath", "target", "", ProtocolSFTP, 123, 0, nil)
  60. assert.Equal(t, "sftpendpoint", a.Endpoint)
  61. }
  62. func TestActionHTTP(t *testing.T) {
  63. actionsCopy := Config.Actions
  64. Config.Actions = ProtocolActions{
  65. ExecuteOn: []string{operationDownload},
  66. Hook: fmt.Sprintf("http://%v", httpAddr),
  67. }
  68. user := &dataprovider.User{
  69. Username: "username",
  70. }
  71. a := newActionNotification(user, operationDownload, "path", "vpath", "target", "", ProtocolSFTP, 123, 0, nil)
  72. err := actionHandler.Handle(a)
  73. assert.NoError(t, err)
  74. Config.Actions.Hook = "http://invalid:1234"
  75. err = actionHandler.Handle(a)
  76. assert.Error(t, err)
  77. Config.Actions.Hook = fmt.Sprintf("http://%v/404", httpAddr)
  78. err = actionHandler.Handle(a)
  79. if assert.Error(t, err) {
  80. assert.EqualError(t, err, errUnexpectedHTTResponse.Error())
  81. }
  82. Config.Actions = actionsCopy
  83. }
  84. func TestActionCMD(t *testing.T) {
  85. if runtime.GOOS == osWindows {
  86. t.Skip("this test is not available on Windows")
  87. }
  88. actionsCopy := Config.Actions
  89. hookCmd, err := exec.LookPath("true")
  90. assert.NoError(t, err)
  91. Config.Actions = ProtocolActions{
  92. ExecuteOn: []string{operationDownload},
  93. Hook: hookCmd,
  94. }
  95. user := &dataprovider.User{
  96. Username: "username",
  97. }
  98. a := newActionNotification(user, operationDownload, "path", "vpath", "target", "", ProtocolSFTP, 123, 0, nil)
  99. err = actionHandler.Handle(a)
  100. assert.NoError(t, err)
  101. ExecuteActionNotification(user, OperationSSHCmd, "path", "vpath", "target", "sha1sum", ProtocolSSH, 0, nil)
  102. Config.Actions = actionsCopy
  103. }
  104. func TestWrongActions(t *testing.T) {
  105. actionsCopy := Config.Actions
  106. badCommand := "/bad/command"
  107. if runtime.GOOS == osWindows {
  108. badCommand = "C:\\bad\\command"
  109. }
  110. Config.Actions = ProtocolActions{
  111. ExecuteOn: []string{operationUpload},
  112. Hook: badCommand,
  113. }
  114. user := &dataprovider.User{
  115. Username: "username",
  116. }
  117. a := newActionNotification(user, operationUpload, "", "", "", "", ProtocolSFTP, 123, 0, nil)
  118. err := actionHandler.Handle(a)
  119. assert.Error(t, err, "action with bad command must fail")
  120. a.Action = operationDelete
  121. err = actionHandler.Handle(a)
  122. assert.EqualError(t, err, errUnconfiguredAction.Error())
  123. Config.Actions.Hook = "http://foo\x7f.com/"
  124. a.Action = operationUpload
  125. err = actionHandler.Handle(a)
  126. assert.Error(t, err, "action with bad url must fail")
  127. Config.Actions.Hook = ""
  128. err = actionHandler.Handle(a)
  129. if assert.Error(t, err) {
  130. assert.EqualError(t, err, errNoHook.Error())
  131. }
  132. Config.Actions.Hook = "relative path"
  133. err = actionHandler.Handle(a)
  134. if assert.Error(t, err) {
  135. assert.EqualError(t, err, fmt.Sprintf("invalid notification command %#v", Config.Actions.Hook))
  136. }
  137. Config.Actions = actionsCopy
  138. }
  139. func TestPreDeleteAction(t *testing.T) {
  140. if runtime.GOOS == osWindows {
  141. t.Skip("this test is not available on Windows")
  142. }
  143. actionsCopy := Config.Actions
  144. hookCmd, err := exec.LookPath("true")
  145. assert.NoError(t, err)
  146. Config.Actions = ProtocolActions{
  147. ExecuteOn: []string{operationPreDelete},
  148. Hook: hookCmd,
  149. }
  150. homeDir := filepath.Join(os.TempDir(), "test_user")
  151. err = os.MkdirAll(homeDir, os.ModePerm)
  152. assert.NoError(t, err)
  153. user := dataprovider.User{
  154. Username: "username",
  155. HomeDir: homeDir,
  156. }
  157. user.Permissions = make(map[string][]string)
  158. user.Permissions["/"] = []string{dataprovider.PermAny}
  159. fs := vfs.NewOsFs("id", homeDir, "")
  160. c := NewBaseConnection("id", ProtocolSFTP, "", user)
  161. testfile := filepath.Join(user.HomeDir, "testfile")
  162. err = os.WriteFile(testfile, []byte("test"), os.ModePerm)
  163. assert.NoError(t, err)
  164. info, err := os.Stat(testfile)
  165. assert.NoError(t, err)
  166. err = c.RemoveFile(fs, testfile, "testfile", info)
  167. assert.NoError(t, err)
  168. assert.FileExists(t, testfile)
  169. os.RemoveAll(homeDir)
  170. Config.Actions = actionsCopy
  171. }
  172. type actionHandlerStub struct {
  173. called bool
  174. }
  175. func (h *actionHandlerStub) Handle(notification *ActionNotification) error {
  176. h.called = true
  177. return nil
  178. }
  179. func TestInitializeActionHandler(t *testing.T) {
  180. handler := &actionHandlerStub{}
  181. InitializeActionHandler(handler)
  182. t.Cleanup(func() {
  183. InitializeActionHandler(&defaultActionHandler{})
  184. })
  185. err := actionHandler.Handle(&ActionNotification{})
  186. assert.NoError(t, err)
  187. assert.True(t, handler.called)
  188. }