actions_test.go 5.3 KB

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