plugin.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Package plugin provides support for the SFTPGo plugin system
  2. package plugin
  3. import (
  4. "fmt"
  5. "sync"
  6. "github.com/hashicorp/go-hclog"
  7. "github.com/drakkan/sftpgo/v2/logger"
  8. "github.com/drakkan/sftpgo/v2/sdk/plugin/notifier"
  9. )
  10. const (
  11. logSender = "plugins"
  12. )
  13. var (
  14. // Handler defines the plugins manager
  15. Handler Manager
  16. pluginsLogLevel = hclog.Debug
  17. )
  18. // Renderer defines the interface for generic objects rendering
  19. type Renderer interface {
  20. RenderAsJSON(reload bool) ([]byte, error)
  21. }
  22. // Config defines a plugin configuration
  23. type Config struct {
  24. // Plugin type
  25. Type string `json:"type" mapstructure:"type"`
  26. // NotifierOptions defines additional options for notifiers plugins
  27. NotifierOptions NotifierConfig `json:"notifier_options" mapstructure:"notifier_options"`
  28. // Path to the plugin executable
  29. Cmd string `json:"cmd" mapstructure:"cmd"`
  30. // Args to pass to the plugin executable
  31. Args []string `json:"args" mapstructure:"args"`
  32. // SHA256 checksum for the plugin executable.
  33. // If not empty it will be used to verify the integrity of the executable
  34. SHA256Sum string `json:"sha256sum" mapstructure:"sha256sum"`
  35. // If enabled the client and the server automatically negotiate mTLS for
  36. // transport authentication. This ensures that only the original client will
  37. // be allowed to connect to the server, and all other connections will be
  38. // rejected. The client will also refuse to connect to any server that isn't
  39. // the original instance started by the client.
  40. AutoMTLS bool `json:"auto_mtls" mapstructure:"auto_mtls"`
  41. }
  42. // Manager handles enabled plugins
  43. type Manager struct {
  44. // List of configured plugins
  45. Configs []Config `json:"plugins" mapstructure:"plugins"`
  46. mu sync.RWMutex
  47. notifiers []*notifierPlugin
  48. }
  49. // Initialize initializes the configured plugins
  50. func Initialize(configs []Config, logVerbose bool) error {
  51. Handler = Manager{
  52. Configs: configs,
  53. }
  54. if logVerbose {
  55. pluginsLogLevel = hclog.Debug
  56. } else {
  57. pluginsLogLevel = hclog.Info
  58. }
  59. for _, config := range configs {
  60. switch config.Type {
  61. case notifier.PluginName:
  62. plugin, err := newNotifierPlugin(config)
  63. if err != nil {
  64. return err
  65. }
  66. Handler.notifiers = append(Handler.notifiers, plugin)
  67. default:
  68. return fmt.Errorf("unsupported plugin type: %v", config.Type)
  69. }
  70. }
  71. return nil
  72. }
  73. // NotifyFsEvent sends the fs event notifications using any defined notifier plugins
  74. func (m *Manager) NotifyFsEvent(action, username, fsPath, fsTargetPath, sshCmd, protocol string, fileSize int64, err error) {
  75. m.mu.RLock()
  76. var crashedIdxs []int
  77. for idx, n := range m.notifiers {
  78. if n.exited() {
  79. crashedIdxs = append(crashedIdxs, idx)
  80. } else {
  81. n.notifyFsAction(action, username, fsPath, fsTargetPath, sshCmd, protocol, fileSize, err)
  82. }
  83. }
  84. m.mu.RUnlock()
  85. if len(crashedIdxs) > 0 {
  86. m.restartCrashedNotifiers(crashedIdxs)
  87. m.mu.RLock()
  88. defer m.mu.RUnlock()
  89. for idx := range crashedIdxs {
  90. if !m.notifiers[idx].exited() {
  91. m.notifiers[idx].notifyFsAction(action, username, fsPath, fsTargetPath, sshCmd, protocol, fileSize, err)
  92. }
  93. }
  94. }
  95. }
  96. // NotifyUserEvent sends the user event notifications using any defined notifier plugins
  97. func (m *Manager) NotifyUserEvent(action string, user Renderer) {
  98. m.mu.RLock()
  99. var crashedIdxs []int
  100. for idx, n := range m.notifiers {
  101. if n.exited() {
  102. crashedIdxs = append(crashedIdxs, idx)
  103. } else {
  104. n.notifyUserAction(action, user)
  105. }
  106. }
  107. m.mu.RUnlock()
  108. if len(crashedIdxs) > 0 {
  109. m.restartCrashedNotifiers(crashedIdxs)
  110. m.mu.RLock()
  111. defer m.mu.RUnlock()
  112. for idx := range crashedIdxs {
  113. if !m.notifiers[idx].exited() {
  114. m.notifiers[idx].notifyUserAction(action, user)
  115. }
  116. }
  117. }
  118. }
  119. func (m *Manager) restartCrashedNotifiers(crashedIdxs []int) {
  120. for _, idx := range crashedIdxs {
  121. m.mu.Lock()
  122. defer m.mu.Unlock()
  123. if m.notifiers[idx].exited() {
  124. logger.Info(logSender, "", "try to restart crashed plugin %v", m.Configs[idx].Cmd)
  125. plugin, err := newNotifierPlugin(m.Configs[idx])
  126. if err == nil {
  127. m.notifiers[idx] = plugin
  128. } else {
  129. logger.Warn(logSender, "", "plugin %v crashed and restart failed: %v", m.Configs[idx].Cmd, err)
  130. }
  131. }
  132. }
  133. }
  134. // Cleanup releases all the active plugins
  135. func (m *Manager) Cleanup() {
  136. for _, n := range m.notifiers {
  137. logger.Debug(logSender, "", "cleanup plugin %v", n.config.Cmd)
  138. n.cleanup()
  139. }
  140. }