plugin.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. }
  90. // Initialize initializes the configured plugins
  91. func Initialize(configs []Config, logVerbose bool) error {
  92. logger.Debug(logSender, "", "initialize")
  93. Handler = Manager{
  94. Configs: configs,
  95. done: make(chan bool),
  96. closed: 0,
  97. authScopes: -1,
  98. }
  99. setLogLevel(logVerbose)
  100. if len(configs) == 0 {
  101. return nil
  102. }
  103. if err := Handler.validateConfigs(); err != nil {
  104. return err
  105. }
  106. kmsID := 0
  107. for idx, config := range Handler.Configs {
  108. switch config.Type {
  109. case notifier.PluginName:
  110. plugin, err := newNotifierPlugin(config)
  111. if err != nil {
  112. return err
  113. }
  114. Handler.notifiers = append(Handler.notifiers, plugin)
  115. case kmsplugin.PluginName:
  116. plugin, err := newKMSPlugin(config)
  117. if err != nil {
  118. return err
  119. }
  120. Handler.kms = append(Handler.kms, plugin)
  121. Handler.Configs[idx].kmsID = kmsID
  122. kmsID++
  123. kms.RegisterSecretProvider(config.KMSOptions.Scheme, config.KMSOptions.EncryptedStatus,
  124. Handler.Configs[idx].newKMSPluginSecretProvider)
  125. logger.Debug(logSender, "", "registered secret provider for scheme: %v, encrypted status: %v",
  126. config.KMSOptions.Scheme, config.KMSOptions.EncryptedStatus)
  127. case auth.PluginName:
  128. plugin, err := newAuthPlugin(config)
  129. if err != nil {
  130. return err
  131. }
  132. Handler.auths = append(Handler.auths, plugin)
  133. if Handler.authScopes == -1 {
  134. Handler.authScopes = config.AuthOptions.Scope
  135. } else {
  136. Handler.authScopes |= config.AuthOptions.Scope
  137. }
  138. case eventsearcher.PluginName:
  139. plugin, err := newSearcherPlugin(config)
  140. if err != nil {
  141. return err
  142. }
  143. Handler.searcher = plugin
  144. case metadata.PluginName:
  145. plugin, err := newMetadaterPlugin(config)
  146. if err != nil {
  147. return err
  148. }
  149. Handler.metadater = plugin
  150. default:
  151. return fmt.Errorf("unsupported plugin type: %v", config.Type)
  152. }
  153. }
  154. startCheckTicker()
  155. return nil
  156. }
  157. func (m *Manager) validateConfigs() error {
  158. kmsSchemes := make(map[string]bool)
  159. kmsEncryptions := make(map[string]bool)
  160. m.hasSearcher = false
  161. m.hasMetadater = false
  162. for _, config := range m.Configs {
  163. if config.Type == kmsplugin.PluginName {
  164. if _, ok := kmsSchemes[config.KMSOptions.Scheme]; ok {
  165. return fmt.Errorf("invalid KMS configuration, duplicated scheme %#v", config.KMSOptions.Scheme)
  166. }
  167. if _, ok := kmsEncryptions[config.KMSOptions.EncryptedStatus]; ok {
  168. return fmt.Errorf("invalid KMS configuration, duplicated encrypted status %#v", config.KMSOptions.EncryptedStatus)
  169. }
  170. kmsSchemes[config.KMSOptions.Scheme] = true
  171. kmsEncryptions[config.KMSOptions.EncryptedStatus] = true
  172. }
  173. if config.Type == eventsearcher.PluginName {
  174. if m.hasSearcher {
  175. return errors.New("only one eventsearcher plugin can be defined")
  176. }
  177. m.hasSearcher = true
  178. }
  179. if config.Type == metadata.PluginName {
  180. if m.hasMetadater {
  181. return errors.New("only one metadata plugin can be defined")
  182. }
  183. m.hasMetadater = true
  184. }
  185. }
  186. return nil
  187. }
  188. // NotifyFsEvent sends the fs event notifications using any defined notifier plugins
  189. func (m *Manager) NotifyFsEvent(timestamp int64, action, username, fsPath, fsTargetPath, sshCmd, protocol, ip,
  190. virtualPath, virtualTargetPath, sessionID string, fileSize int64, err error,
  191. ) {
  192. m.notifLock.RLock()
  193. defer m.notifLock.RUnlock()
  194. for _, n := range m.notifiers {
  195. n.notifyFsAction(timestamp, action, username, fsPath, fsTargetPath, sshCmd, protocol, ip, virtualPath,
  196. virtualTargetPath, sessionID, fileSize, err)
  197. }
  198. }
  199. // NotifyProviderEvent sends the provider event notifications using any defined notifier plugins
  200. func (m *Manager) NotifyProviderEvent(timestamp int64, action, username, objectType, objectName, ip string,
  201. object Renderer,
  202. ) {
  203. m.notifLock.RLock()
  204. defer m.notifLock.RUnlock()
  205. for _, n := range m.notifiers {
  206. n.notifyProviderAction(timestamp, action, username, objectType, objectName, ip, object)
  207. }
  208. }
  209. // SearchFsEvents returns the filesystem events matching the specified filter and a continuation token
  210. // to use for cursor based pagination
  211. func (m *Manager) SearchFsEvents(startTimestamp, endTimestamp int64, username, ip, sshCmd string, actions,
  212. protocols, instanceIDs, excludeIDs []string, statuses []int32, limit, order int,
  213. ) ([]byte, []string, []string, error) {
  214. if !m.hasSearcher {
  215. return nil, nil, nil, ErrNoSearcher
  216. }
  217. m.searcherLock.RLock()
  218. plugin := m.searcher
  219. m.searcherLock.RUnlock()
  220. return plugin.searchear.SearchFsEvents(startTimestamp, endTimestamp, username, ip, sshCmd, actions, protocols,
  221. instanceIDs, excludeIDs, statuses, limit, order)
  222. }
  223. // SearchProviderEvents returns the provider events matching the specified filter and a continuation token
  224. // to use for cursor based pagination
  225. func (m *Manager) SearchProviderEvents(startTimestamp, endTimestamp int64, username, ip, objectName string,
  226. limit, order int, actions, objectTypes, instanceIDs, excludeIDs []string,
  227. ) ([]byte, []string, []string, error) {
  228. if !m.hasSearcher {
  229. return nil, nil, nil, ErrNoSearcher
  230. }
  231. m.searcherLock.RLock()
  232. plugin := m.searcher
  233. m.searcherLock.RUnlock()
  234. return plugin.searchear.SearchProviderEvents(startTimestamp, endTimestamp, username, ip, objectName, limit,
  235. order, actions, objectTypes, instanceIDs, excludeIDs)
  236. }
  237. // HasMetadater returns true if a metadata plugin is defined
  238. func (m *Manager) HasMetadater() bool {
  239. return m.hasMetadater
  240. }
  241. // SetModificationTime sets the modification time for the specified object
  242. func (m *Manager) SetModificationTime(storageID, objectPath string, mTime int64) error {
  243. if !m.hasMetadater {
  244. return ErrNoMetadater
  245. }
  246. m.metadaterLock.RLock()
  247. plugin := m.metadater
  248. m.metadaterLock.RUnlock()
  249. return plugin.metadater.SetModificationTime(storageID, objectPath, mTime)
  250. }
  251. // GetModificationTime returns the modification time for the specified path
  252. func (m *Manager) GetModificationTime(storageID, objectPath string, isDir bool) (int64, error) {
  253. if !m.hasMetadater {
  254. return 0, ErrNoMetadater
  255. }
  256. m.metadaterLock.RLock()
  257. plugin := m.metadater
  258. m.metadaterLock.RUnlock()
  259. return plugin.metadater.GetModificationTime(storageID, objectPath)
  260. }
  261. // GetModificationTimes returns the modification times for all the files within the specified folder
  262. func (m *Manager) GetModificationTimes(storageID, objectPath string) (map[string]int64, error) {
  263. if !m.hasMetadater {
  264. return nil, ErrNoMetadater
  265. }
  266. m.metadaterLock.RLock()
  267. plugin := m.metadater
  268. m.metadaterLock.RUnlock()
  269. return plugin.metadater.GetModificationTimes(storageID, objectPath)
  270. }
  271. // RemoveMetadata deletes the metadata stored for the specified object
  272. func (m *Manager) RemoveMetadata(storageID, objectPath string) error {
  273. if !m.hasMetadater {
  274. return ErrNoMetadater
  275. }
  276. m.metadaterLock.RLock()
  277. plugin := m.metadater
  278. m.metadaterLock.RUnlock()
  279. return plugin.metadater.RemoveMetadata(storageID, objectPath)
  280. }
  281. // GetMetadataFolders returns the folders that metadata is associated with
  282. func (m *Manager) GetMetadataFolders(storageID, from string, limit int) ([]string, error) {
  283. if !m.hasMetadater {
  284. return nil, ErrNoMetadater
  285. }
  286. m.metadaterLock.RLock()
  287. plugin := m.metadater
  288. m.metadaterLock.RUnlock()
  289. return plugin.metadater.GetFolders(storageID, limit, from)
  290. }
  291. func (m *Manager) kmsEncrypt(secret kms.BaseSecret, url string, masterKey string, kmsID int) (string, string, int32, error) {
  292. m.kmsLock.RLock()
  293. plugin := m.kms[kmsID]
  294. m.kmsLock.RUnlock()
  295. return plugin.Encrypt(secret, url, masterKey)
  296. }
  297. func (m *Manager) kmsDecrypt(secret kms.BaseSecret, url string, masterKey string, kmsID int) (string, error) {
  298. m.kmsLock.RLock()
  299. plugin := m.kms[kmsID]
  300. m.kmsLock.RUnlock()
  301. return plugin.Decrypt(secret, url, masterKey)
  302. }
  303. // HasAuthScope returns true if there is an auth plugin that support the specified scope
  304. func (m *Manager) HasAuthScope(scope int) bool {
  305. if m.authScopes == -1 {
  306. return false
  307. }
  308. return m.authScopes&scope != 0
  309. }
  310. // Authenticate tries to authenticate the specified user using an external plugin
  311. func (m *Manager) Authenticate(username, password, ip, protocol string, pkey string,
  312. tlsCert *x509.Certificate, authScope int, userAsJSON []byte,
  313. ) ([]byte, error) {
  314. switch authScope {
  315. case AuthScopePassword:
  316. return m.checkUserAndPass(username, password, ip, protocol, userAsJSON)
  317. case AuthScopePublicKey:
  318. return m.checkUserAndPublicKey(username, pkey, ip, protocol, userAsJSON)
  319. case AuthScopeKeyboardInteractive:
  320. return m.checkUserAndKeyboardInteractive(username, ip, protocol, userAsJSON)
  321. case AuthScopeTLSCertificate:
  322. cert, err := util.EncodeTLSCertToPem(tlsCert)
  323. if err != nil {
  324. logger.Warn(logSender, "", "unable to encode tls certificate to pem: %v", err)
  325. return nil, fmt.Errorf("unable to encode tls cert to pem: %w", err)
  326. }
  327. return m.checkUserAndTLSCert(username, cert, ip, protocol, userAsJSON)
  328. default:
  329. return nil, fmt.Errorf("unsupported auth scope: %v", authScope)
  330. }
  331. }
  332. // ExecuteKeyboardInteractiveStep executes a keyboard interactive step
  333. func (m *Manager) ExecuteKeyboardInteractiveStep(req *KeyboardAuthRequest) (*KeyboardAuthResponse, error) {
  334. var plugin *authPlugin
  335. m.authLock.Lock()
  336. for _, p := range m.auths {
  337. if p.config.AuthOptions.Scope&AuthScopePassword != 0 {
  338. plugin = p
  339. break
  340. }
  341. }
  342. m.authLock.Unlock()
  343. if plugin == nil {
  344. return nil, errors.New("no auth plugin configured for keyaboard interactive authentication step")
  345. }
  346. return plugin.sendKeyboardIteractiveRequest(req)
  347. }
  348. func (m *Manager) checkUserAndPass(username, password, ip, protocol string, userAsJSON []byte) ([]byte, error) {
  349. var plugin *authPlugin
  350. m.authLock.Lock()
  351. for _, p := range m.auths {
  352. if p.config.AuthOptions.Scope&AuthScopePassword != 0 {
  353. plugin = p
  354. break
  355. }
  356. }
  357. m.authLock.Unlock()
  358. if plugin == nil {
  359. return nil, errors.New("no auth plugin configured for password checking")
  360. }
  361. return plugin.checkUserAndPass(username, password, ip, protocol, userAsJSON)
  362. }
  363. func (m *Manager) checkUserAndPublicKey(username, pubKey, ip, protocol string, userAsJSON []byte) ([]byte, error) {
  364. var plugin *authPlugin
  365. m.authLock.Lock()
  366. for _, p := range m.auths {
  367. if p.config.AuthOptions.Scope&AuthScopePublicKey != 0 {
  368. plugin = p
  369. break
  370. }
  371. }
  372. m.authLock.Unlock()
  373. if plugin == nil {
  374. return nil, errors.New("no auth plugin configured for public key checking")
  375. }
  376. return plugin.checkUserAndPublicKey(username, pubKey, ip, protocol, userAsJSON)
  377. }
  378. func (m *Manager) checkUserAndTLSCert(username, tlsCert, ip, protocol string, userAsJSON []byte) ([]byte, error) {
  379. var plugin *authPlugin
  380. m.authLock.Lock()
  381. for _, p := range m.auths {
  382. if p.config.AuthOptions.Scope&AuthScopeTLSCertificate != 0 {
  383. plugin = p
  384. break
  385. }
  386. }
  387. m.authLock.Unlock()
  388. if plugin == nil {
  389. return nil, errors.New("no auth plugin configured for TLS certificate checking")
  390. }
  391. return plugin.checkUserAndTLSCertificate(username, tlsCert, ip, protocol, userAsJSON)
  392. }
  393. func (m *Manager) checkUserAndKeyboardInteractive(username, ip, protocol string, userAsJSON []byte) ([]byte, error) {
  394. var plugin *authPlugin
  395. m.authLock.Lock()
  396. for _, p := range m.auths {
  397. if p.config.AuthOptions.Scope&AuthScopeKeyboardInteractive != 0 {
  398. plugin = p
  399. break
  400. }
  401. }
  402. m.authLock.Unlock()
  403. if plugin == nil {
  404. return nil, errors.New("no auth plugin configured for keyboard interactive checking")
  405. }
  406. return plugin.checkUserAndKeyboardInteractive(username, ip, protocol, userAsJSON)
  407. }
  408. func (m *Manager) checkCrashedPlugins() {
  409. m.notifLock.RLock()
  410. for idx, n := range m.notifiers {
  411. if n.exited() {
  412. defer func(cfg Config, index int) {
  413. Handler.restartNotifierPlugin(cfg, index)
  414. }(n.config, idx)
  415. } else {
  416. n.sendQueuedEvents()
  417. }
  418. }
  419. m.notifLock.RUnlock()
  420. m.kmsLock.RLock()
  421. for idx, k := range m.kms {
  422. if k.exited() {
  423. defer func(cfg Config, index int) {
  424. Handler.restartKMSPlugin(cfg, index)
  425. }(k.config, idx)
  426. }
  427. }
  428. m.kmsLock.RUnlock()
  429. m.authLock.RLock()
  430. for idx, a := range m.auths {
  431. if a.exited() {
  432. defer func(cfg Config, index int) {
  433. Handler.restartAuthPlugin(cfg, index)
  434. }(a.config, idx)
  435. }
  436. }
  437. m.authLock.RUnlock()
  438. if m.hasSearcher {
  439. m.searcherLock.RLock()
  440. if m.searcher.exited() {
  441. defer func(cfg Config) {
  442. Handler.restartSearcherPlugin(cfg)
  443. }(m.searcher.config)
  444. }
  445. m.searcherLock.RUnlock()
  446. }
  447. if m.hasMetadater {
  448. m.metadaterLock.RLock()
  449. if m.metadater.exited() {
  450. defer func(cfg Config) {
  451. Handler.restartMetadaterPlugin(cfg)
  452. }(m.metadater.config)
  453. }
  454. m.metadaterLock.RUnlock()
  455. }
  456. }
  457. func (m *Manager) restartNotifierPlugin(config Config, idx int) {
  458. if atomic.LoadInt32(&m.closed) == 1 {
  459. return
  460. }
  461. logger.Info(logSender, "", "try to restart crashed notifier plugin %#v, idx: %v", config.Cmd, idx)
  462. plugin, err := newNotifierPlugin(config)
  463. if err != nil {
  464. logger.Warn(logSender, "", "unable to restart notifier plugin %#v, err: %v", config.Cmd, err)
  465. return
  466. }
  467. m.notifLock.Lock()
  468. plugin.queue = m.notifiers[idx].queue
  469. m.notifiers[idx] = plugin
  470. m.notifLock.Unlock()
  471. plugin.sendQueuedEvents()
  472. }
  473. func (m *Manager) restartKMSPlugin(config Config, idx int) {
  474. if atomic.LoadInt32(&m.closed) == 1 {
  475. return
  476. }
  477. logger.Info(logSender, "", "try to restart crashed kms plugin %#v, idx: %v", config.Cmd, idx)
  478. plugin, err := newKMSPlugin(config)
  479. if err != nil {
  480. logger.Warn(logSender, "", "unable to restart kms plugin %#v, err: %v", config.Cmd, err)
  481. return
  482. }
  483. m.kmsLock.Lock()
  484. m.kms[idx] = plugin
  485. m.kmsLock.Unlock()
  486. }
  487. func (m *Manager) restartAuthPlugin(config Config, idx int) {
  488. if atomic.LoadInt32(&m.closed) == 1 {
  489. return
  490. }
  491. logger.Info(logSender, "", "try to restart crashed auth plugin %#v, idx: %v", config.Cmd, idx)
  492. plugin, err := newAuthPlugin(config)
  493. if err != nil {
  494. logger.Warn(logSender, "", "unable to restart auth plugin %#v, err: %v", config.Cmd, err)
  495. return
  496. }
  497. m.authLock.Lock()
  498. m.auths[idx] = plugin
  499. m.authLock.Unlock()
  500. }
  501. func (m *Manager) restartSearcherPlugin(config Config) {
  502. if atomic.LoadInt32(&m.closed) == 1 {
  503. return
  504. }
  505. logger.Info(logSender, "", "try to restart crashed searcher plugin %#v", config.Cmd)
  506. plugin, err := newSearcherPlugin(config)
  507. if err != nil {
  508. logger.Warn(logSender, "", "unable to restart searcher plugin %#v, err: %v", config.Cmd, err)
  509. return
  510. }
  511. m.searcherLock.Lock()
  512. m.searcher = plugin
  513. m.searcherLock.Unlock()
  514. }
  515. func (m *Manager) restartMetadaterPlugin(config Config) {
  516. if atomic.LoadInt32(&m.closed) == 1 {
  517. return
  518. }
  519. logger.Info(logSender, "", "try to restart crashed metadater plugin %#v", config.Cmd)
  520. plugin, err := newMetadaterPlugin(config)
  521. if err != nil {
  522. logger.Warn(logSender, "", "unable to restart metadater plugin %#v, err: %v", config.Cmd, err)
  523. return
  524. }
  525. m.metadaterLock.Lock()
  526. m.metadater = plugin
  527. m.metadaterLock.Unlock()
  528. }
  529. // Cleanup releases all the active plugins
  530. func (m *Manager) Cleanup() {
  531. logger.Debug(logSender, "", "cleanup")
  532. atomic.StoreInt32(&m.closed, 1)
  533. close(m.done)
  534. m.notifLock.Lock()
  535. for _, n := range m.notifiers {
  536. logger.Debug(logSender, "", "cleanup notifier plugin %v", n.config.Cmd)
  537. n.cleanup()
  538. }
  539. m.notifLock.Unlock()
  540. m.kmsLock.Lock()
  541. for _, k := range m.kms {
  542. logger.Debug(logSender, "", "cleanup kms plugin %v", k.config.Cmd)
  543. k.cleanup()
  544. }
  545. m.kmsLock.Unlock()
  546. m.authLock.Lock()
  547. for _, a := range m.auths {
  548. logger.Debug(logSender, "", "cleanup auth plugin %v", a.config.Cmd)
  549. a.cleanup()
  550. }
  551. m.authLock.Unlock()
  552. if m.hasSearcher {
  553. m.searcherLock.Lock()
  554. logger.Debug(logSender, "", "cleanup searcher plugin %v", m.searcher.config.Cmd)
  555. m.searcher.cleanup()
  556. m.searcherLock.Unlock()
  557. }
  558. if m.hasMetadater {
  559. m.metadaterLock.Lock()
  560. logger.Debug(logSender, "", "cleanup metadater plugin %v", m.metadater.config.Cmd)
  561. m.metadater.cleanup()
  562. m.metadaterLock.Unlock()
  563. }
  564. }
  565. func setLogLevel(logVerbose bool) {
  566. if logVerbose {
  567. pluginsLogLevel = hclog.Debug
  568. } else {
  569. pluginsLogLevel = hclog.Info
  570. }
  571. }
  572. func startCheckTicker() {
  573. logger.Debug(logSender, "", "start plugins checker")
  574. checker := time.NewTicker(30 * time.Second)
  575. go func() {
  576. for {
  577. select {
  578. case <-Handler.done:
  579. logger.Debug(logSender, "", "handler done, stop plugins checker")
  580. checker.Stop()
  581. return
  582. case <-checker.C:
  583. Handler.checkCrashedPlugins()
  584. }
  585. }
  586. }()
  587. }