@@ -23,8 +23,12 @@ package control
2323
2424import (
2525 "crypto/sha256"
26+ "encoding/json"
2627 "errors"
2728 "fmt"
29+ "io/ioutil"
30+ "net/http"
31+ "net/url"
2832 "os"
2933 "path/filepath"
3034 "runtime"
@@ -170,6 +174,7 @@ type pluginDetails struct {
170174 KeyPath string
171175 CACertPaths string
172176 TLSEnabled bool
177+ Uri * url.URL
173178}
174179
175180type loadedPlugin struct {
@@ -342,56 +347,83 @@ func (p *pluginManager) LoadPlugin(details *pluginDetails, emitter gomit.Emitter
342347 lPlugin .Details = details
343348 lPlugin .State = DetectedState
344349
345- pmLogger .WithFields (log.Fields {
346- "_block" : "load-plugin" ,
347- "path" : filepath .Base (lPlugin .Details .Exec [0 ]),
348- }).Info ("plugin load called" )
349-
350- // We will create commands by appending the ExecPath to the actual command.
351- // The ExecPath is a temporary location where the plugin/package will be
352- // run from.
353- commands := make ([]string , len (lPlugin .Details .Exec ))
354- for i , e := range lPlugin .Details .Exec {
355- commands [i ] = filepath .Join (lPlugin .Details .ExecPath , e )
356- }
357-
358- ePlugin , err := plugin .NewExecutablePlugin (
359- p .GenerateArgs (int (log .GetLevel ())).
360- SetCertPath (details .CertPath ).
361- SetKeyPath (details .KeyPath ).
362- SetCACertPaths (details .CACertPaths ).
363- SetTLSEnabled (details .TLSEnabled ),
364- commands ... )
365- if err != nil {
350+ var (
351+ ePlugin * plugin.ExecutablePlugin
352+ resp plugin.Response
353+ err error
354+ )
355+
356+ if lPlugin .Details .Uri == nil {
366357 pmLogger .WithFields (log.Fields {
367358 "_block" : "load-plugin" ,
368- "error" : err .Error (),
369- }).Error ("load plugin error while creating executable plugin" )
370- return nil , serror .New (err )
371- }
359+ "path" : filepath .Base (lPlugin .Details .Exec [0 ]),
360+ }).Info ("plugin load called" )
361+ // We will create commands by appending the ExecPath to the actual command.
362+ // The ExecPath is a temporary location where the plugin/package will be
363+ // run from.
364+ commands := make ([]string , len (lPlugin .Details .Exec ))
365+ for i , e := range lPlugin .Details .Exec {
366+ commands [i ] = filepath .Join (lPlugin .Details .ExecPath , e )
367+ }
372368
373- pmLogger .WithFields (log.Fields {
374- "_block" : "load-plugin" ,
375- "path" : lPlugin .Details .Exec ,
376- }).Debug (fmt .Sprintf ("plugin load timeout set to %ds" , p .pluginLoadTimeout ))
377- resp , err := ePlugin .Run (time .Second * time .Duration (p .pluginLoadTimeout ))
378- if err != nil {
369+ ePlugin , err = plugin .NewExecutablePlugin (
370+ p .GenerateArgs (int (log .GetLevel ())).
371+ SetCertPath (details .CertPath ).
372+ SetKeyPath (details .KeyPath ).
373+ SetCACertPaths (details .CACertPaths ).
374+ SetTLSEnabled (details .TLSEnabled ),
375+ commands ... )
376+ if err != nil {
377+ pmLogger .WithFields (log.Fields {
378+ "_block" : "load-plugin" ,
379+ "error" : err .Error (),
380+ }).Error ("load plugin error while creating executable plugin" )
381+ return nil , serror .New (err )
382+ }
379383 pmLogger .WithFields (log.Fields {
380384 "_block" : "load-plugin" ,
381- "error" : err .Error (),
382- }).Error ("load plugin error when starting plugin" )
383- return nil , serror .New (err )
384- }
385+ "path" : lPlugin .Details .Exec ,
386+ }).Debug (fmt .Sprintf ("plugin load timeout set to %ds" , p .pluginLoadTimeout ))
387+ resp , err = ePlugin .Run (time .Second * time .Duration (p .pluginLoadTimeout ))
388+ if err != nil {
389+ pmLogger .WithFields (log.Fields {
390+ "_block" : "load-plugin" ,
391+ "error" : err .Error (),
392+ }).Error ("load plugin error when starting plugin" )
393+ return nil , serror .New (err )
394+ }
385395
386- ePlugin .SetName (resp .Meta .Name )
396+ ePlugin .SetName (resp .Meta .Name )
387397
388- key := fmt .Sprintf ("%s" + core .Separator + "%s" + core .Separator + "%d" , resp .Meta .Type .String (), resp .Meta .Name , resp .Meta .Version )
389- if _ , exists := p .loadedPlugins .table [key ]; exists {
390- return nil , serror .New (ErrPluginAlreadyLoaded , map [string ]interface {}{
391- "plugin-name" : resp .Meta .Name ,
392- "plugin-version" : resp .Meta .Version ,
393- "plugin-type" : resp .Type .String (),
394- })
398+ key := fmt .Sprintf ("%s" + core .Separator + "%s" + core .Separator + "%d" , resp .Meta .Type .String (), resp .Meta .Name , resp .Meta .Version )
399+ if _ , exists := p .loadedPlugins .table [key ]; exists {
400+ return nil , serror .New (ErrPluginAlreadyLoaded , map [string ]interface {}{
401+ "plugin-name" : resp .Meta .Name ,
402+ "plugin-version" : resp .Meta .Version ,
403+ "plugin-type" : resp .Type .String (),
404+ })
405+ }
406+ } else {
407+ pmLogger .WithFields (log.Fields {
408+ "_block" : "load-plugin" ,
409+ "uri" : lPlugin .Details .Uri .String (),
410+ }).Info ("plugin load called" )
411+ res , err := http .Get (lPlugin .Details .Uri .String ())
412+ if err != nil {
413+ return nil , serror .New (err )
414+ }
415+
416+ body , err := ioutil .ReadAll (res .Body )
417+ if err != nil {
418+ return nil , serror .New (err )
419+ }
420+ err = json .Unmarshal (body , & resp )
421+ if err != nil {
422+ pmLogger .WithFields (log.Fields {
423+ "_block" : "load-plugin" ,
424+ "error" : err .Error (),
425+ }).Error ("error during json unmarshal" )
426+ }
395427 }
396428 ap , err := newAvailablePlugin (resp , emitter , ePlugin , p .grpcSecurity )
397429 if err != nil {
@@ -402,6 +434,10 @@ func (p *pluginManager) LoadPlugin(details *pluginDetails, emitter gomit.Emitter
402434 return nil , serror .New (err )
403435 }
404436
437+ if lPlugin .Details .Uri != nil {
438+ ap .SetIsRemote (true )
439+ }
440+
405441 if resp .Meta .Unsecure {
406442 err = ap .client .Ping ()
407443 } else {
@@ -481,6 +517,7 @@ func (p *pluginManager) LoadPlugin(details *pluginDetails, emitter gomit.Emitter
481517 }
482518
483519 colClient := ap .client .(client.PluginCollectorClient )
520+ defer ap .client .(client.PluginCollectorClient ).Close ()
484521
485522 cfg := plugin.ConfigType {
486523 ConfigDataNode : cfgNode ,
@@ -550,19 +587,21 @@ func (p *pluginManager) LoadPlugin(details *pluginDetails, emitter gomit.Emitter
550587 }
551588 }
552589
553- // Added so clients can adequately clean up connections
554- ap .client .Kill ("Retrieved necessary plugin info" )
555- err = ePlugin .Kill ()
556- if err != nil {
557- pmLogger .WithFields (log.Fields {
558- "_block" : "load-plugin" ,
559- "error" : err .Error (),
560- }).Error ("load plugin error while killing plugin executable plugin" )
561- return nil , serror .New (err )
590+ if lPlugin .Details .Uri == nil {
591+ // Added so clients can adequately clean up connections
592+ ap .client .Kill ("Retrieved necessary plugin info" )
593+ err = ePlugin .Kill ()
594+ if err != nil {
595+ pmLogger .WithFields (log.Fields {
596+ "_block" : "load-plugin" ,
597+ "error" : err .Error (),
598+ }).Error ("load plugin error while killing plugin executable plugin" )
599+ return nil , serror .New (err )
600+ }
562601 }
563602
564603 if resp .State != plugin .PluginSuccess {
565- e := fmt .Errorf ("Plugin loading did not succeed: %s\n " , resp .ErrorMessage )
604+ e := fmt .Errorf ("plugin loading did not succeed: %s\n " , resp .ErrorMessage )
566605 pmLogger .WithFields (log.Fields {
567606 "_block" : "load-plugin" ,
568607 "error" : e ,
@@ -594,6 +633,7 @@ func (p *pluginManager) UnloadPlugin(pl core.Plugin) (*loadedPlugin, serror.Snap
594633 })
595634 return nil , se
596635 }
636+
597637 pmLogger .WithFields (log.Fields {
598638 "_block" : "unload-plugin" ,
599639 "path" : plugin .Details .Exec ,
0 commit comments