plugin.go 21 KB

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