plugin.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. // Package plugin provides support for the SFTPGo plugin system
  2. package plugin
  3. import (
  4. "crypto/x509"
  5. "errors"
  6. "fmt"
  7. "sync"
  8. "sync/atomic"
  9. "time"
  10. "github.com/hashicorp/go-hclog"
  11. "github.com/drakkan/sftpgo/v2/kms"
  12. "github.com/drakkan/sftpgo/v2/logger"
  13. "github.com/drakkan/sftpgo/v2/sdk/plugin/auth"
  14. "github.com/drakkan/sftpgo/v2/sdk/plugin/eventsearcher"
  15. kmsplugin "github.com/drakkan/sftpgo/v2/sdk/plugin/kms"
  16. "github.com/drakkan/sftpgo/v2/sdk/plugin/metadata"
  17. "github.com/drakkan/sftpgo/v2/sdk/plugin/notifier"
  18. "github.com/drakkan/sftpgo/v2/util"
  19. )
  20. const (
  21. logSender = "plugins"
  22. )
  23. var (
  24. // Handler defines the plugins manager
  25. Handler Manager
  26. pluginsLogLevel = hclog.Debug
  27. // ErrNoSearcher defines the error to return for events searches if no plugin is configured
  28. ErrNoSearcher = errors.New("no events searcher plugin defined")
  29. // ErrNoMetadater returns the error to return for metadata methods if no plugin is configured
  30. ErrNoMetadater = errors.New("no metadata plugin defined")
  31. )
  32. // Renderer defines the interface for generic objects rendering
  33. type Renderer interface {
  34. RenderAsJSON(reload bool) ([]byte, error)
  35. }
  36. // Config defines a plugin configuration
  37. type Config struct {
  38. // Plugin type
  39. Type string `json:"type" mapstructure:"type"`
  40. // NotifierOptions defines options for notifiers plugins
  41. NotifierOptions NotifierConfig `json:"notifier_options" mapstructure:"notifier_options"`
  42. // KMSOptions defines options for a KMS plugin
  43. KMSOptions KMSConfig `json:"kms_options" mapstructure:"kms_options"`
  44. // AuthOptions defines options for authentication plugins
  45. AuthOptions AuthConfig `json:"auth_options" mapstructure:"auth_options"`
  46. // Path to the plugin executable
  47. Cmd string `json:"cmd" mapstructure:"cmd"`
  48. // Args to pass to the plugin executable
  49. Args []string `json:"args" mapstructure:"args"`
  50. // SHA256 checksum for the plugin executable.
  51. // If not empty it will be used to verify the integrity of the executable
  52. SHA256Sum string `json:"sha256sum" mapstructure:"sha256sum"`
  53. // If enabled the client and the server automatically negotiate mTLS for
  54. // transport authentication. This ensures that only the original client will
  55. // be allowed to connect to the server, and all other connections will be
  56. // rejected. The client will also refuse to connect to any server that isn't
  57. // the original instance started by the client.
  58. AutoMTLS bool `json:"auto_mtls" mapstructure:"auto_mtls"`
  59. // unique identifier for kms plugins
  60. kmsID int
  61. }
  62. func (c *Config) newKMSPluginSecretProvider(base kms.BaseSecret, url, masterKey string) kms.SecretProvider {
  63. return &kmsPluginSecretProvider{
  64. BaseSecret: base,
  65. URL: url,
  66. MasterKey: masterKey,
  67. config: c,
  68. }
  69. }
  70. // Manager handles enabled plugins
  71. type Manager struct {
  72. closed int32
  73. done chan bool
  74. // List of configured plugins
  75. Configs []Config `json:"plugins" mapstructure:"plugins"`
  76. notifLock sync.RWMutex
  77. notifiers []*notifierPlugin
  78. kmsLock sync.RWMutex
  79. kms []*kmsPlugin
  80. authLock sync.RWMutex
  81. auths []*authPlugin
  82. searcherLock sync.RWMutex
  83. searcher *searcherPlugin
  84. metadaterLock sync.RWMutex
  85. metadater *metadataPlugin
  86. authScopes int
  87. hasSearcher bool
  88. hasMetadater bool
  89. hasNotifiers bool
  90. }
  91. // Initialize initializes the configured plugins
  92. func Initialize(configs []Config, logVerbose bool) error {
  93. logger.Debug(logSender, "", "initialize")
  94. Handler = Manager{
  95. Configs: configs,
  96. done: make(chan bool),
  97. closed: 0,
  98. authScopes: -1,
  99. }
  100. setLogLevel(logVerbose)
  101. if len(configs) == 0 {
  102. return nil
  103. }
  104. if err := Handler.validateConfigs(); err != nil {
  105. return err
  106. }
  107. kmsID := 0
  108. for idx, config := range Handler.Configs {
  109. switch config.Type {
  110. case notifier.PluginName:
  111. plugin, err := newNotifierPlugin(config)
  112. if err != nil {
  113. return err
  114. }
  115. Handler.notifiers = append(Handler.notifiers, plugin)
  116. case kmsplugin.PluginName:
  117. plugin, err := newKMSPlugin(config)
  118. if err != nil {
  119. return err
  120. }
  121. Handler.kms = append(Handler.kms, plugin)
  122. Handler.Configs[idx].kmsID = kmsID
  123. kmsID++
  124. kms.RegisterSecretProvider(config.KMSOptions.Scheme, config.KMSOptions.EncryptedStatus,
  125. Handler.Configs[idx].newKMSPluginSecretProvider)
  126. logger.Debug(logSender, "", "registered secret provider for scheme: %v, encrypted status: %v",
  127. config.KMSOptions.Scheme, config.KMSOptions.EncryptedStatus)
  128. case auth.PluginName:
  129. plugin, err := newAuthPlugin(config)
  130. if err != nil {
  131. return err
  132. }
  133. Handler.auths = append(Handler.auths, plugin)
  134. if Handler.authScopes == -1 {
  135. Handler.authScopes = config.AuthOptions.Scope
  136. } else {
  137. Handler.authScopes |= config.AuthOptions.Scope
  138. }
  139. case eventsearcher.PluginName:
  140. plugin, err := newSearcherPlugin(config)
  141. if err != nil {
  142. return err
  143. }
  144. Handler.searcher = plugin
  145. case metadata.PluginName:
  146. plugin, err := newMetadaterPlugin(config)
  147. if err != nil {
  148. return err
  149. }
  150. Handler.metadater = plugin
  151. default:
  152. return fmt.Errorf("unsupported plugin type: %v", config.Type)
  153. }
  154. }
  155. startCheckTicker()
  156. return nil
  157. }
  158. func (m *Manager) validateConfigs() error {
  159. kmsSchemes := make(map[string]bool)
  160. kmsEncryptions := make(map[string]bool)
  161. m.hasSearcher = false
  162. m.hasMetadater = false
  163. m.hasNotifiers = false
  164. for _, config := range m.Configs {
  165. if config.Type == kmsplugin.PluginName {
  166. if _, ok := kmsSchemes[config.KMSOptions.Scheme]; ok {
  167. return fmt.Errorf("invalid KMS configuration, duplicated scheme %#v", config.KMSOptions.Scheme)
  168. }
  169. if _, ok := kmsEncryptions[config.KMSOptions.EncryptedStatus]; ok {
  170. return fmt.Errorf("invalid KMS configuration, duplicated encrypted status %#v", config.KMSOptions.EncryptedStatus)
  171. }
  172. kmsSchemes[config.KMSOptions.Scheme] = true
  173. kmsEncryptions[config.KMSOptions.EncryptedStatus] = true
  174. }
  175. if config.Type == eventsearcher.PluginName {
  176. if m.hasSearcher {
  177. return errors.New("only one eventsearcher plugin can be defined")
  178. }
  179. m.hasSearcher = true
  180. }
  181. if config.Type == metadata.PluginName {
  182. if m.hasMetadater {
  183. return errors.New("only one metadata plugin can be defined")
  184. }
  185. m.hasMetadater = true
  186. }
  187. if config.Type == notifier.PluginName {
  188. m.hasNotifiers = true
  189. }
  190. }
  191. return nil
  192. }
  193. // HasNotifiers returns true if there is at least a notifier plugin
  194. func (m *Manager) HasNotifiers() bool {
  195. return m.hasNotifiers
  196. }
  197. // NotifyFsEvent sends the fs event notifications using any defined notifier plugins
  198. func (m *Manager) NotifyFsEvent(event *notifier.FsEvent) {
  199. m.notifLock.RLock()
  200. defer m.notifLock.RUnlock()
  201. for _, n := range m.notifiers {
  202. n.notifyFsAction(event)
  203. }
  204. }
  205. // NotifyProviderEvent sends the provider event notifications using any defined notifier plugins
  206. func (m *Manager) NotifyProviderEvent(event *notifier.ProviderEvent, object Renderer) {
  207. m.notifLock.RLock()
  208. defer m.notifLock.RUnlock()
  209. for _, n := range m.notifiers {
  210. n.notifyProviderAction(event, object)
  211. }
  212. }
  213. // SearchFsEvents returns the filesystem events matching the specified filters
  214. func (m *Manager) SearchFsEvents(searchFilters *eventsearcher.FsEventSearch) ([]byte, []string, []string, error) {
  215. if !m.hasSearcher {
  216. return nil, nil, nil, ErrNoSearcher
  217. }
  218. m.searcherLock.RLock()
  219. plugin := m.searcher
  220. m.searcherLock.RUnlock()
  221. return plugin.searchear.SearchFsEvents(searchFilters)
  222. }
  223. // SearchProviderEvents returns the provider events matching the specified filters
  224. func (m *Manager) SearchProviderEvents(searchFilters *eventsearcher.ProviderEventSearch) ([]byte, []string, []string, error) {
  225. if !m.hasSearcher {
  226. return nil, nil, nil, ErrNoSearcher
  227. }
  228. m.searcherLock.RLock()
  229. plugin := m.searcher
  230. m.searcherLock.RUnlock()
  231. return plugin.searchear.SearchProviderEvents(searchFilters)
  232. }
  233. // HasMetadater returns true if a metadata plugin is defined
  234. func (m *Manager) HasMetadater() bool {
  235. return m.hasMetadater
  236. }
  237. // SetModificationTime sets the modification time for the specified object
  238. func (m *Manager) SetModificationTime(storageID, objectPath string, mTime int64) error {
  239. if !m.hasMetadater {
  240. return ErrNoMetadater
  241. }
  242. m.metadaterLock.RLock()
  243. plugin := m.metadater
  244. m.metadaterLock.RUnlock()
  245. return plugin.metadater.SetModificationTime(storageID, objectPath, mTime)
  246. }
  247. // GetModificationTime returns the modification time for the specified path
  248. func (m *Manager) GetModificationTime(storageID, objectPath string, isDir bool) (int64, error) {
  249. if !m.hasMetadater {
  250. return 0, ErrNoMetadater
  251. }
  252. m.metadaterLock.RLock()
  253. plugin := m.metadater
  254. m.metadaterLock.RUnlock()
  255. return plugin.metadater.GetModificationTime(storageID, objectPath)
  256. }
  257. // GetModificationTimes returns the modification times for all the files within the specified folder
  258. func (m *Manager) GetModificationTimes(storageID, objectPath string) (map[string]int64, error) {
  259. if !m.hasMetadater {
  260. return nil, ErrNoMetadater
  261. }
  262. m.metadaterLock.RLock()
  263. plugin := m.metadater
  264. m.metadaterLock.RUnlock()
  265. return plugin.metadater.GetModificationTimes(storageID, objectPath)
  266. }
  267. // RemoveMetadata deletes the metadata stored for the specified object
  268. func (m *Manager) RemoveMetadata(storageID, objectPath string) error {
  269. if !m.hasMetadater {
  270. return ErrNoMetadater
  271. }
  272. m.metadaterLock.RLock()
  273. plugin := m.metadater
  274. m.metadaterLock.RUnlock()
  275. return plugin.metadater.RemoveMetadata(storageID, objectPath)
  276. }
  277. // GetMetadataFolders returns the folders that metadata is associated with
  278. func (m *Manager) GetMetadataFolders(storageID, from string, limit int) ([]string, error) {
  279. if !m.hasMetadater {
  280. return nil, ErrNoMetadater
  281. }
  282. m.metadaterLock.RLock()
  283. plugin := m.metadater
  284. m.metadaterLock.RUnlock()
  285. return plugin.metadater.GetFolders(storageID, limit, from)
  286. }
  287. func (m *Manager) kmsEncrypt(secret kms.BaseSecret, url string, masterKey string, kmsID int) (string, string, int32, error) {
  288. m.kmsLock.RLock()
  289. plugin := m.kms[kmsID]
  290. m.kmsLock.RUnlock()
  291. return plugin.Encrypt(secret, url, masterKey)
  292. }
  293. func (m *Manager) kmsDecrypt(secret kms.BaseSecret, url string, masterKey string, kmsID int) (string, error) {
  294. m.kmsLock.RLock()
  295. plugin := m.kms[kmsID]
  296. m.kmsLock.RUnlock()
  297. return plugin.Decrypt(secret, url, masterKey)
  298. }
  299. // HasAuthScope returns true if there is an auth plugin that support the specified scope
  300. func (m *Manager) HasAuthScope(scope int) bool {
  301. if m.authScopes == -1 {
  302. return false
  303. }
  304. return m.authScopes&scope != 0
  305. }
  306. // Authenticate tries to authenticate the specified user using an external plugin
  307. func (m *Manager) Authenticate(username, password, ip, protocol string, pkey string,
  308. tlsCert *x509.Certificate, authScope int, userAsJSON []byte,
  309. ) ([]byte, error) {
  310. switch authScope {
  311. case AuthScopePassword:
  312. return m.checkUserAndPass(username, password, ip, protocol, userAsJSON)
  313. case AuthScopePublicKey:
  314. return m.checkUserAndPublicKey(username, pkey, ip, protocol, userAsJSON)
  315. case AuthScopeKeyboardInteractive:
  316. return m.checkUserAndKeyboardInteractive(username, ip, protocol, userAsJSON)
  317. case AuthScopeTLSCertificate:
  318. cert, err := util.EncodeTLSCertToPem(tlsCert)
  319. if err != nil {
  320. logger.Warn(logSender, "", "unable to encode tls certificate to pem: %v", err)
  321. return nil, fmt.Errorf("unable to encode tls cert to pem: %w", err)
  322. }
  323. return m.checkUserAndTLSCert(username, cert, ip, protocol, userAsJSON)
  324. default:
  325. return nil, fmt.Errorf("unsupported auth scope: %v", authScope)
  326. }
  327. }
  328. // ExecuteKeyboardInteractiveStep executes a keyboard interactive step
  329. func (m *Manager) ExecuteKeyboardInteractiveStep(req *KeyboardAuthRequest) (*KeyboardAuthResponse, error) {
  330. var plugin *authPlugin
  331. m.authLock.Lock()
  332. for _, p := range m.auths {
  333. if p.config.AuthOptions.Scope&AuthScopePassword != 0 {
  334. plugin = p
  335. break
  336. }
  337. }
  338. m.authLock.Unlock()
  339. if plugin == nil {
  340. return nil, errors.New("no auth plugin configured for keyaboard interactive authentication step")
  341. }
  342. return plugin.sendKeyboardIteractiveRequest(req)
  343. }
  344. func (m *Manager) checkUserAndPass(username, password, ip, protocol string, userAsJSON []byte) ([]byte, error) {
  345. var plugin *authPlugin
  346. m.authLock.Lock()
  347. for _, p := range m.auths {
  348. if p.config.AuthOptions.Scope&AuthScopePassword != 0 {
  349. plugin = p
  350. break
  351. }
  352. }
  353. m.authLock.Unlock()
  354. if plugin == nil {
  355. return nil, errors.New("no auth plugin configured for password checking")
  356. }
  357. return plugin.checkUserAndPass(username, password, ip, protocol, userAsJSON)
  358. }
  359. func (m *Manager) checkUserAndPublicKey(username, pubKey, ip, protocol string, userAsJSON []byte) ([]byte, error) {
  360. var plugin *authPlugin
  361. m.authLock.Lock()
  362. for _, p := range m.auths {
  363. if p.config.AuthOptions.Scope&AuthScopePublicKey != 0 {
  364. plugin = p
  365. break
  366. }
  367. }
  368. m.authLock.Unlock()
  369. if plugin == nil {
  370. return nil, errors.New("no auth plugin configured for public key checking")
  371. }
  372. return plugin.checkUserAndPublicKey(username, pubKey, ip, protocol, userAsJSON)
  373. }
  374. func (m *Manager) checkUserAndTLSCert(username, tlsCert, ip, protocol string, userAsJSON []byte) ([]byte, error) {
  375. var plugin *authPlugin
  376. m.authLock.Lock()
  377. for _, p := range m.auths {
  378. if p.config.AuthOptions.Scope&AuthScopeTLSCertificate != 0 {
  379. plugin = p
  380. break
  381. }
  382. }
  383. m.authLock.Unlock()
  384. if plugin == nil {
  385. return nil, errors.New("no auth plugin configured for TLS certificate checking")
  386. }
  387. return plugin.checkUserAndTLSCertificate(username, tlsCert, ip, protocol, userAsJSON)
  388. }
  389. func (m *Manager) checkUserAndKeyboardInteractive(username, ip, protocol string, userAsJSON []byte) ([]byte, error) {
  390. var plugin *authPlugin
  391. m.authLock.Lock()
  392. for _, p := range m.auths {
  393. if p.config.AuthOptions.Scope&AuthScopeKeyboardInteractive != 0 {
  394. plugin = p
  395. break
  396. }
  397. }
  398. m.authLock.Unlock()
  399. if plugin == nil {
  400. return nil, errors.New("no auth plugin configured for keyboard interactive checking")
  401. }
  402. return plugin.checkUserAndKeyboardInteractive(username, ip, protocol, userAsJSON)
  403. }
  404. func (m *Manager) checkCrashedPlugins() {
  405. m.notifLock.RLock()
  406. for idx, n := range m.notifiers {
  407. if n.exited() {
  408. defer func(cfg Config, index int) {
  409. Handler.restartNotifierPlugin(cfg, index)
  410. }(n.config, idx)
  411. } else {
  412. n.sendQueuedEvents()
  413. }
  414. }
  415. m.notifLock.RUnlock()
  416. m.kmsLock.RLock()
  417. for idx, k := range m.kms {
  418. if k.exited() {
  419. defer func(cfg Config, index int) {
  420. Handler.restartKMSPlugin(cfg, index)
  421. }(k.config, idx)
  422. }
  423. }
  424. m.kmsLock.RUnlock()
  425. m.authLock.RLock()
  426. for idx, a := range m.auths {
  427. if a.exited() {
  428. defer func(cfg Config, index int) {
  429. Handler.restartAuthPlugin(cfg, index)
  430. }(a.config, idx)
  431. }
  432. }
  433. m.authLock.RUnlock()
  434. if m.hasSearcher {
  435. m.searcherLock.RLock()
  436. if m.searcher.exited() {
  437. defer func(cfg Config) {
  438. Handler.restartSearcherPlugin(cfg)
  439. }(m.searcher.config)
  440. }
  441. m.searcherLock.RUnlock()
  442. }
  443. if m.hasMetadater {
  444. m.metadaterLock.RLock()
  445. if m.metadater.exited() {
  446. defer func(cfg Config) {
  447. Handler.restartMetadaterPlugin(cfg)
  448. }(m.metadater.config)
  449. }
  450. m.metadaterLock.RUnlock()
  451. }
  452. }
  453. func (m *Manager) restartNotifierPlugin(config Config, idx int) {
  454. if atomic.LoadInt32(&m.closed) == 1 {
  455. return
  456. }
  457. logger.Info(logSender, "", "try to restart crashed notifier plugin %#v, idx: %v", config.Cmd, idx)
  458. plugin, err := newNotifierPlugin(config)
  459. if err != nil {
  460. logger.Warn(logSender, "", "unable to restart notifier plugin %#v, err: %v", config.Cmd, err)
  461. return
  462. }
  463. m.notifLock.Lock()
  464. plugin.queue = m.notifiers[idx].queue
  465. m.notifiers[idx] = plugin
  466. m.notifLock.Unlock()
  467. plugin.sendQueuedEvents()
  468. }
  469. func (m *Manager) restartKMSPlugin(config Config, idx int) {
  470. if atomic.LoadInt32(&m.closed) == 1 {
  471. return
  472. }
  473. logger.Info(logSender, "", "try to restart crashed kms plugin %#v, idx: %v", config.Cmd, idx)
  474. plugin, err := newKMSPlugin(config)
  475. if err != nil {
  476. logger.Warn(logSender, "", "unable to restart kms plugin %#v, err: %v", config.Cmd, err)
  477. return
  478. }
  479. m.kmsLock.Lock()
  480. m.kms[idx] = plugin
  481. m.kmsLock.Unlock()
  482. }
  483. func (m *Manager) restartAuthPlugin(config Config, idx int) {
  484. if atomic.LoadInt32(&m.closed) == 1 {
  485. return
  486. }
  487. logger.Info(logSender, "", "try to restart crashed auth plugin %#v, idx: %v", config.Cmd, idx)
  488. plugin, err := newAuthPlugin(config)
  489. if err != nil {
  490. logger.Warn(logSender, "", "unable to restart auth plugin %#v, err: %v", config.Cmd, err)
  491. return
  492. }
  493. m.authLock.Lock()
  494. m.auths[idx] = plugin
  495. m.authLock.Unlock()
  496. }
  497. func (m *Manager) restartSearcherPlugin(config Config) {
  498. if atomic.LoadInt32(&m.closed) == 1 {
  499. return
  500. }
  501. logger.Info(logSender, "", "try to restart crashed searcher plugin %#v", config.Cmd)
  502. plugin, err := newSearcherPlugin(config)
  503. if err != nil {
  504. logger.Warn(logSender, "", "unable to restart searcher plugin %#v, err: %v", config.Cmd, err)
  505. return
  506. }
  507. m.searcherLock.Lock()
  508. m.searcher = plugin
  509. m.searcherLock.Unlock()
  510. }
  511. func (m *Manager) restartMetadaterPlugin(config Config) {
  512. if atomic.LoadInt32(&m.closed) == 1 {
  513. return
  514. }
  515. logger.Info(logSender, "", "try to restart crashed metadater plugin %#v", config.Cmd)
  516. plugin, err := newMetadaterPlugin(config)
  517. if err != nil {
  518. logger.Warn(logSender, "", "unable to restart metadater plugin %#v, err: %v", config.Cmd, err)
  519. return
  520. }
  521. m.metadaterLock.Lock()
  522. m.metadater = plugin
  523. m.metadaterLock.Unlock()
  524. }
  525. // Cleanup releases all the active plugins
  526. func (m *Manager) Cleanup() {
  527. logger.Debug(logSender, "", "cleanup")
  528. atomic.StoreInt32(&m.closed, 1)
  529. close(m.done)
  530. m.notifLock.Lock()
  531. for _, n := range m.notifiers {
  532. logger.Debug(logSender, "", "cleanup notifier plugin %v", n.config.Cmd)
  533. n.cleanup()
  534. }
  535. m.notifLock.Unlock()
  536. m.kmsLock.Lock()
  537. for _, k := range m.kms {
  538. logger.Debug(logSender, "", "cleanup kms plugin %v", k.config.Cmd)
  539. k.cleanup()
  540. }
  541. m.kmsLock.Unlock()
  542. m.authLock.Lock()
  543. for _, a := range m.auths {
  544. logger.Debug(logSender, "", "cleanup auth plugin %v", a.config.Cmd)
  545. a.cleanup()
  546. }
  547. m.authLock.Unlock()
  548. if m.hasSearcher {
  549. m.searcherLock.Lock()
  550. logger.Debug(logSender, "", "cleanup searcher plugin %v", m.searcher.config.Cmd)
  551. m.searcher.cleanup()
  552. m.searcherLock.Unlock()
  553. }
  554. if m.hasMetadater {
  555. m.metadaterLock.Lock()
  556. logger.Debug(logSender, "", "cleanup metadater plugin %v", m.metadater.config.Cmd)
  557. m.metadater.cleanup()
  558. m.metadaterLock.Unlock()
  559. }
  560. }
  561. func setLogLevel(logVerbose bool) {
  562. if logVerbose {
  563. pluginsLogLevel = hclog.Debug
  564. } else {
  565. pluginsLogLevel = hclog.Info
  566. }
  567. }
  568. func startCheckTicker() {
  569. logger.Debug(logSender, "", "start plugins checker")
  570. checker := time.NewTicker(30 * time.Second)
  571. go func() {
  572. for {
  573. select {
  574. case <-Handler.done:
  575. logger.Debug(logSender, "", "handler done, stop plugins checker")
  576. checker.Stop()
  577. return
  578. case <-checker.C:
  579. Handler.checkCrashedPlugins()
  580. }
  581. }
  582. }()
  583. }