actions_test.go 5.9 KB

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