actions.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Copyright (C) 2019-2022 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package common
  15. import (
  16. "bytes"
  17. "context"
  18. "encoding/json"
  19. "errors"
  20. "fmt"
  21. "net/http"
  22. "net/url"
  23. "os/exec"
  24. "path"
  25. "path/filepath"
  26. "strings"
  27. "time"
  28. "github.com/sftpgo/sdk"
  29. "github.com/sftpgo/sdk/plugin/notifier"
  30. "github.com/drakkan/sftpgo/v2/internal/command"
  31. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  32. "github.com/drakkan/sftpgo/v2/internal/httpclient"
  33. "github.com/drakkan/sftpgo/v2/internal/logger"
  34. "github.com/drakkan/sftpgo/v2/internal/plugin"
  35. "github.com/drakkan/sftpgo/v2/internal/util"
  36. )
  37. var (
  38. errUnconfiguredAction = errors.New("no hook is configured for this action")
  39. errNoHook = errors.New("unable to execute action, no hook defined")
  40. errUnexpectedHTTResponse = errors.New("unexpected HTTP hook response code")
  41. hooksConcurrencyGuard = make(chan struct{}, 150)
  42. )
  43. func startNewHook() {
  44. hooksConcurrencyGuard <- struct{}{}
  45. }
  46. func hookEnded() {
  47. <-hooksConcurrencyGuard
  48. }
  49. // ProtocolActions defines the action to execute on file operations and SSH commands
  50. type ProtocolActions struct {
  51. // Valid values are download, upload, pre-delete, delete, rename, ssh_cmd. Empty slice to disable
  52. ExecuteOn []string `json:"execute_on" mapstructure:"execute_on"`
  53. // Actions to be performed synchronously.
  54. // The pre-delete action is always executed synchronously while the other ones are asynchronous.
  55. // Executing an action synchronously means that SFTPGo will not return a result code to the client
  56. // (which is waiting for it) until your hook have completed its execution.
  57. ExecuteSync []string `json:"execute_sync" mapstructure:"execute_sync"`
  58. // Absolute path to an external program or an HTTP URL
  59. Hook string `json:"hook" mapstructure:"hook"`
  60. }
  61. var actionHandler ActionHandler = &defaultActionHandler{}
  62. // InitializeActionHandler lets the user choose an action handler implementation.
  63. //
  64. // Do NOT call this function after application initialization.
  65. func InitializeActionHandler(handler ActionHandler) {
  66. actionHandler = handler
  67. }
  68. func handleUnconfiguredPreAction(operation string) error {
  69. // for pre-delete we execute the internal handling on error, so we must return errUnconfiguredAction.
  70. // Other pre action will deny the operation on error so if we have no configuration we must return
  71. // a nil error
  72. if operation == operationPreDelete {
  73. return errUnconfiguredAction
  74. }
  75. return nil
  76. }
  77. // ExecutePreAction executes a pre-* action and returns the result
  78. func ExecutePreAction(conn *BaseConnection, operation, filePath, virtualPath string, fileSize int64, openFlags int) error {
  79. var event *notifier.FsEvent
  80. hasNotifiersPlugin := plugin.Handler.HasNotifiers()
  81. hasHook := util.Contains(Config.Actions.ExecuteOn, operation)
  82. if !hasHook && !hasNotifiersPlugin {
  83. return handleUnconfiguredPreAction(operation)
  84. }
  85. event = newActionNotification(&conn.User, operation, filePath, virtualPath, "", "", "",
  86. conn.protocol, conn.GetRemoteIP(), conn.ID, fileSize, openFlags, nil)
  87. if hasNotifiersPlugin {
  88. plugin.Handler.NotifyFsEvent(event)
  89. }
  90. if !hasHook {
  91. return handleUnconfiguredPreAction(operation)
  92. }
  93. return actionHandler.Handle(event)
  94. }
  95. // ExecuteActionNotification executes the defined hook, if any, for the specified action
  96. func ExecuteActionNotification(conn *BaseConnection, operation, filePath, virtualPath, target, virtualTarget, sshCmd string,
  97. fileSize int64, err error,
  98. ) error {
  99. hasNotifiersPlugin := plugin.Handler.HasNotifiers()
  100. hasHook := util.Contains(Config.Actions.ExecuteOn, operation)
  101. hasRules := eventManager.hasFsRules()
  102. if !hasHook && !hasNotifiersPlugin && !hasRules {
  103. return nil
  104. }
  105. notification := newActionNotification(&conn.User, operation, filePath, virtualPath, target, virtualTarget, sshCmd,
  106. conn.protocol, conn.GetRemoteIP(), conn.ID, fileSize, 0, err)
  107. if hasNotifiersPlugin {
  108. plugin.Handler.NotifyFsEvent(notification)
  109. }
  110. var errRes error
  111. if hasRules {
  112. errRes = eventManager.handleFsEvent(EventParams{
  113. Name: notification.Username,
  114. Event: notification.Action,
  115. Status: notification.Status,
  116. VirtualPath: notification.VirtualPath,
  117. FsPath: notification.Path,
  118. VirtualTargetPath: notification.VirtualTargetPath,
  119. FsTargetPath: notification.TargetPath,
  120. ObjectName: path.Base(notification.VirtualPath),
  121. FileSize: notification.FileSize,
  122. Protocol: notification.Protocol,
  123. IP: notification.IP,
  124. Timestamp: notification.Timestamp,
  125. Object: nil,
  126. })
  127. }
  128. if hasHook {
  129. if util.Contains(Config.Actions.ExecuteSync, operation) {
  130. if errHook := actionHandler.Handle(notification); errHook != nil {
  131. errRes = errHook
  132. }
  133. } else {
  134. go func() {
  135. startNewHook()
  136. defer hookEnded()
  137. actionHandler.Handle(notification) //nolint:errcheck
  138. }()
  139. }
  140. }
  141. return errRes
  142. }
  143. // ActionHandler handles a notification for a Protocol Action.
  144. type ActionHandler interface {
  145. Handle(notification *notifier.FsEvent) error
  146. }
  147. func newActionNotification(
  148. user *dataprovider.User,
  149. operation, filePath, virtualPath, target, virtualTarget, sshCmd, protocol, ip, sessionID string,
  150. fileSize int64,
  151. openFlags int,
  152. err error,
  153. ) *notifier.FsEvent {
  154. var bucket, endpoint string
  155. fsConfig := user.GetFsConfigForPath(virtualPath)
  156. switch fsConfig.Provider {
  157. case sdk.S3FilesystemProvider:
  158. bucket = fsConfig.S3Config.Bucket
  159. endpoint = fsConfig.S3Config.Endpoint
  160. case sdk.GCSFilesystemProvider:
  161. bucket = fsConfig.GCSConfig.Bucket
  162. case sdk.AzureBlobFilesystemProvider:
  163. bucket = fsConfig.AzBlobConfig.Container
  164. if fsConfig.AzBlobConfig.Endpoint != "" {
  165. endpoint = fsConfig.AzBlobConfig.Endpoint
  166. }
  167. case sdk.SFTPFilesystemProvider:
  168. endpoint = fsConfig.SFTPConfig.Endpoint
  169. case sdk.HTTPFilesystemProvider:
  170. endpoint = fsConfig.HTTPConfig.Endpoint
  171. }
  172. return &notifier.FsEvent{
  173. Action: operation,
  174. Username: user.Username,
  175. Path: filePath,
  176. TargetPath: target,
  177. VirtualPath: virtualPath,
  178. VirtualTargetPath: virtualTarget,
  179. SSHCmd: sshCmd,
  180. FileSize: fileSize,
  181. FsProvider: int(fsConfig.Provider),
  182. Bucket: bucket,
  183. Endpoint: endpoint,
  184. Status: getNotificationStatus(err),
  185. Protocol: protocol,
  186. IP: ip,
  187. SessionID: sessionID,
  188. OpenFlags: openFlags,
  189. Timestamp: time.Now().UnixNano(),
  190. }
  191. }
  192. type defaultActionHandler struct{}
  193. func (h *defaultActionHandler) Handle(event *notifier.FsEvent) error {
  194. if !util.Contains(Config.Actions.ExecuteOn, event.Action) {
  195. return errUnconfiguredAction
  196. }
  197. if Config.Actions.Hook == "" {
  198. logger.Warn(event.Protocol, "", "Unable to send notification, no hook is defined")
  199. return errNoHook
  200. }
  201. if strings.HasPrefix(Config.Actions.Hook, "http") {
  202. return h.handleHTTP(event)
  203. }
  204. return h.handleCommand(event)
  205. }
  206. func (h *defaultActionHandler) handleHTTP(event *notifier.FsEvent) error {
  207. u, err := url.Parse(Config.Actions.Hook)
  208. if err != nil {
  209. logger.Error(event.Protocol, "", "Invalid hook %#v for operation %#v: %v",
  210. Config.Actions.Hook, event.Action, err)
  211. return err
  212. }
  213. startTime := time.Now()
  214. respCode := 0
  215. var b bytes.Buffer
  216. _ = json.NewEncoder(&b).Encode(event)
  217. resp, err := httpclient.RetryablePost(Config.Actions.Hook, "application/json", &b)
  218. if err == nil {
  219. respCode = resp.StatusCode
  220. resp.Body.Close()
  221. if respCode != http.StatusOK {
  222. err = errUnexpectedHTTResponse
  223. }
  224. }
  225. logger.Debug(event.Protocol, "", "notified operation %q to URL: %s status code: %d, elapsed: %s err: %v",
  226. event.Action, u.Redacted(), respCode, time.Since(startTime), err)
  227. return err
  228. }
  229. func (h *defaultActionHandler) handleCommand(event *notifier.FsEvent) error {
  230. if !filepath.IsAbs(Config.Actions.Hook) {
  231. err := fmt.Errorf("invalid notification command %#v", Config.Actions.Hook)
  232. logger.Warn(event.Protocol, "", "unable to execute notification command: %v", err)
  233. return err
  234. }
  235. timeout, env := command.GetConfig(Config.Actions.Hook)
  236. ctx, cancel := context.WithTimeout(context.Background(), timeout)
  237. defer cancel()
  238. cmd := exec.CommandContext(ctx, Config.Actions.Hook)
  239. cmd.Env = append(env, notificationAsEnvVars(event)...)
  240. startTime := time.Now()
  241. err := cmd.Run()
  242. logger.Debug(event.Protocol, "", "executed command %#v, elapsed: %v, error: %v",
  243. Config.Actions.Hook, time.Since(startTime), err)
  244. return err
  245. }
  246. func notificationAsEnvVars(event *notifier.FsEvent) []string {
  247. return []string{
  248. fmt.Sprintf("SFTPGO_ACTION=%s", event.Action),
  249. fmt.Sprintf("SFTPGO_ACTION_USERNAME=%s", event.Username),
  250. fmt.Sprintf("SFTPGO_ACTION_PATH=%s", event.Path),
  251. fmt.Sprintf("SFTPGO_ACTION_TARGET=%s", event.TargetPath),
  252. fmt.Sprintf("SFTPGO_ACTION_VIRTUAL_PATH=%s", event.VirtualPath),
  253. fmt.Sprintf("SFTPGO_ACTION_VIRTUAL_TARGET=%s", event.VirtualTargetPath),
  254. fmt.Sprintf("SFTPGO_ACTION_SSH_CMD=%s", event.SSHCmd),
  255. fmt.Sprintf("SFTPGO_ACTION_FILE_SIZE=%d", event.FileSize),
  256. fmt.Sprintf("SFTPGO_ACTION_FS_PROVIDER=%d", event.FsProvider),
  257. fmt.Sprintf("SFTPGO_ACTION_BUCKET=%s", event.Bucket),
  258. fmt.Sprintf("SFTPGO_ACTION_ENDPOINT=%s", event.Endpoint),
  259. fmt.Sprintf("SFTPGO_ACTION_STATUS=%d", event.Status),
  260. fmt.Sprintf("SFTPGO_ACTION_PROTOCOL=%s", event.Protocol),
  261. fmt.Sprintf("SFTPGO_ACTION_IP=%s", event.IP),
  262. fmt.Sprintf("SFTPGO_ACTION_SESSION_ID=%s", event.SessionID),
  263. fmt.Sprintf("SFTPGO_ACTION_OPEN_FLAGS=%d", event.OpenFlags),
  264. fmt.Sprintf("SFTPGO_ACTION_TIMESTAMP=%d", event.Timestamp),
  265. }
  266. }
  267. func getNotificationStatus(err error) int {
  268. status := 1
  269. if err == ErrQuotaExceeded {
  270. status = 3
  271. } else if err != nil {
  272. status = 2
  273. }
  274. return status
  275. }