Commit 531c5793 by Arve Knudsen Committed by GitHub

Plugins: Let descendant plugins inherit their root's signature (#27970)

* plugins: Let descendant plugins inherit their root's signature

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
parent 65d04688
...@@ -58,12 +58,12 @@ type JwtTokenAuth struct { ...@@ -58,12 +58,12 @@ type JwtTokenAuth struct {
Params map[string]string `json:"params"` Params map[string]string `json:"params"`
} }
func (app *AppPlugin) Load(decoder *json.Decoder, pluginDir string, backendPluginManager backendplugin.Manager) error { func (app *AppPlugin) Load(decoder *json.Decoder, base *PluginBase, backendPluginManager backendplugin.Manager) error {
if err := decoder.Decode(app); err != nil { if err := decoder.Decode(app); err != nil {
return err return err
} }
if err := app.registerPlugin(pluginDir); err != nil { if err := app.registerPlugin(base); err != nil {
return err return err
} }
......
...@@ -34,12 +34,12 @@ type DataSourcePlugin struct { ...@@ -34,12 +34,12 @@ type DataSourcePlugin struct {
SDK bool `json:"sdk,omitempty"` SDK bool `json:"sdk,omitempty"`
} }
func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string, backendPluginManager backendplugin.Manager) error { func (p *DataSourcePlugin) Load(decoder *json.Decoder, base *PluginBase, backendPluginManager backendplugin.Manager) error {
if err := decoder.Decode(p); err != nil { if err := decoder.Decode(p); err != nil {
return errutil.Wrapf(err, "Failed to decode datasource plugin") return errutil.Wrapf(err, "Failed to decode datasource plugin")
} }
if err := p.registerPlugin(pluginDir); err != nil { if err := p.registerPlugin(base); err != nil {
return errutil.Wrapf(err, "Failed to register plugin") return errutil.Wrapf(err, "Failed to register plugin")
} }
......
...@@ -84,16 +84,18 @@ func readPluginManifest(body []byte) (*pluginManifest, error) { ...@@ -84,16 +84,18 @@ func readPluginManifest(body []byte) (*pluginManifest, error) {
// getPluginSignatureState returns the signature state for a plugin. // getPluginSignatureState returns the signature state for a plugin.
func getPluginSignatureState(log log.Logger, plugin *PluginBase) PluginSignature { func getPluginSignatureState(log log.Logger, plugin *PluginBase) PluginSignature {
log.Debug("Getting signature state of plugin", "plugin", plugin.Id) log.Debug("Getting signature state of plugin", "plugin", plugin.Id, "isBackend", plugin.Backend)
manifestPath := filepath.Join(plugin.PluginDir, "MANIFEST.txt") manifestPath := filepath.Join(plugin.PluginDir, "MANIFEST.txt")
byteValue, err := ioutil.ReadFile(manifestPath) byteValue, err := ioutil.ReadFile(manifestPath)
if err != nil || len(byteValue) < 10 { if err != nil || len(byteValue) < 10 {
log.Debug("Plugin is unsigned", "id", plugin.Id)
return PluginSignatureUnsigned return PluginSignatureUnsigned
} }
manifest, err := readPluginManifest(byteValue) manifest, err := readPluginManifest(byteValue)
if err != nil { if err != nil {
log.Debug("Plugin signature invalid", "id", plugin.Id)
return PluginSignatureInvalid return PluginSignatureInvalid
} }
...@@ -126,5 +128,6 @@ func getPluginSignatureState(log log.Logger, plugin *PluginBase) PluginSignature ...@@ -126,5 +128,6 @@ func getPluginSignatureState(log log.Logger, plugin *PluginBase) PluginSignature
} }
// Everything OK // Everything OK
log.Debug("Plugin signature valid", "id", plugin.Id)
return PluginSignatureValid return PluginSignatureValid
} }
...@@ -42,10 +42,13 @@ func (e PluginNotFoundError) Error() string { ...@@ -42,10 +42,13 @@ func (e PluginNotFoundError) Error() string {
return fmt.Sprintf("Plugin with id %s not found", e.PluginId) return fmt.Sprintf("Plugin with id %s not found", e.PluginId)
} }
// PluginLoader can load a plugin.
type PluginLoader interface { type PluginLoader interface {
Load(decoder *json.Decoder, pluginDir string, backendPluginManager backendplugin.Manager) error // Load loads a plugin and registers it with the manager.
Load(decoder *json.Decoder, base *PluginBase, backendPluginManager backendplugin.Manager) error
} }
// PluginBase is the base plugin type.
type PluginBase struct { type PluginBase struct {
Type string `json:"type"` Type string `json:"type"`
Name string `json:"name"` Name string `json:"name"`
...@@ -69,14 +72,16 @@ type PluginBase struct { ...@@ -69,14 +72,16 @@ type PluginBase struct {
GrafanaNetVersion string `json:"-"` GrafanaNetVersion string `json:"-"`
GrafanaNetHasUpdate bool `json:"-"` GrafanaNetHasUpdate bool `json:"-"`
Root *PluginBase
} }
func (pb *PluginBase) registerPlugin(pluginDir string) error { func (pb *PluginBase) registerPlugin(base *PluginBase) error {
if _, exists := Plugins[pb.Id]; exists { if _, exists := Plugins[pb.Id]; exists {
return fmt.Errorf("Plugin with ID %q already exists", pb.Id) return fmt.Errorf("Plugin with ID %q already exists", pb.Id)
} }
if !strings.HasPrefix(pluginDir, setting.StaticRootPath) { if !strings.HasPrefix(base.PluginDir, setting.StaticRootPath) {
plog.Info("Registering plugin", "name", pb.Name) plog.Info("Registering plugin", "name", pb.Name)
} }
...@@ -94,7 +99,10 @@ func (pb *PluginBase) registerPlugin(pluginDir string) error { ...@@ -94,7 +99,10 @@ func (pb *PluginBase) registerPlugin(pluginDir string) error {
} }
} }
pb.PluginDir = pluginDir // Copy relevant fields from the base
pb.PluginDir = base.PluginDir
pb.Signature = base.Signature
Plugins[pb.Id] = pb Plugins[pb.Id] = pb
return nil return nil
} }
......
...@@ -11,12 +11,12 @@ type PanelPlugin struct { ...@@ -11,12 +11,12 @@ type PanelPlugin struct {
SkipDataQuery bool `json:"skipDataQuery"` SkipDataQuery bool `json:"skipDataQuery"`
} }
func (p *PanelPlugin) Load(decoder *json.Decoder, pluginDir string, backendPluginManager backendplugin.Manager) error { func (p *PanelPlugin) Load(decoder *json.Decoder, base *PluginBase, backendPluginManager backendplugin.Manager) error {
if err := decoder.Decode(p); err != nil { if err := decoder.Decode(p); err != nil {
return err return err
} }
if err := p.registerPlugin(pluginDir); err != nil { if err := p.registerPlugin(base); err != nil {
return err return err
} }
......
...@@ -22,12 +22,12 @@ type RendererPlugin struct { ...@@ -22,12 +22,12 @@ type RendererPlugin struct {
backendPluginManager backendplugin.Manager backendPluginManager backendplugin.Manager
} }
func (r *RendererPlugin) Load(decoder *json.Decoder, pluginDir string, backendPluginManager backendplugin.Manager) error { func (r *RendererPlugin) Load(decoder *json.Decoder, base *PluginBase, backendPluginManager backendplugin.Manager) error {
if err := decoder.Decode(r); err != nil { if err := decoder.Decode(r); err != nil {
return err return err
} }
if err := r.registerPlugin(pluginDir); err != nil { if err := r.registerPlugin(base); err != nil {
return err return err
} }
......
...@@ -28,12 +28,12 @@ type TransformPlugin struct { ...@@ -28,12 +28,12 @@ type TransformPlugin struct {
*TransformWrapper *TransformWrapper
} }
func (p *TransformPlugin) Load(decoder *json.Decoder, pluginDir string, backendPluginManager backendplugin.Manager) error { func (p *TransformPlugin) Load(decoder *json.Decoder, base *PluginBase, backendPluginManager backendplugin.Manager) error {
if err := decoder.Decode(p); err != nil { if err := decoder.Decode(p); err != nil {
return err return err
} }
if err := p.registerPlugin(pluginDir); err != nil { if err := p.registerPlugin(base); err != nil {
return err return err
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment