2015-11-19 12:55:13 +01:00
|
|
|
package plugins
|
|
|
|
|
|
2015-12-22 00:23:24 +08:00
|
|
|
import (
|
2016-01-09 23:34:20 +01:00
|
|
|
"encoding/json"
|
2016-03-10 19:57:48 +01:00
|
|
|
"fmt"
|
2016-02-10 11:03:12 +01:00
|
|
|
"strings"
|
|
|
|
|
|
2020-02-28 12:51:21 +01:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2020-01-13 20:18:45 +01:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
2016-02-10 11:03:12 +01:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-12-22 00:23:24 +08:00
|
|
|
)
|
2015-11-19 12:55:13 +01:00
|
|
|
|
2016-03-10 19:57:48 +01:00
|
|
|
var (
|
2020-11-17 11:51:31 +01:00
|
|
|
PluginTypeApp = "app"
|
|
|
|
|
PluginTypeDashboard = "dashboard"
|
2016-03-10 19:57:48 +01:00
|
|
|
)
|
|
|
|
|
|
2018-11-15 11:10:47 +01:00
|
|
|
type PluginState string
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
PluginStateAlpha PluginState = "alpha"
|
|
|
|
|
)
|
|
|
|
|
|
2020-12-11 12:57:57 +01:00
|
|
|
type PluginSignatureState struct {
|
|
|
|
|
Status PluginSignatureStatus
|
|
|
|
|
Type PluginSignatureType
|
|
|
|
|
SigningOrg string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PluginSignatureStatus string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
pluginSignatureInternal PluginSignatureStatus = "internal" // core plugin, no signature
|
|
|
|
|
pluginSignatureValid PluginSignatureStatus = "valid" // signed and accurate MANIFEST
|
|
|
|
|
pluginSignatureInvalid PluginSignatureStatus = "invalid" // invalid signature
|
|
|
|
|
pluginSignatureModified PluginSignatureStatus = "modified" // valid signature, but content mismatch
|
|
|
|
|
pluginSignatureUnsigned PluginSignatureStatus = "unsigned" // no MANIFEST file
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type PluginSignatureType string
|
2020-04-09 00:00:16 -07:00
|
|
|
|
|
|
|
|
const (
|
2020-12-11 12:57:57 +01:00
|
|
|
grafanaType PluginSignatureType = "grafana"
|
|
|
|
|
privateType PluginSignatureType = "private"
|
2020-04-09 00:00:16 -07:00
|
|
|
)
|
|
|
|
|
|
2016-03-10 19:57:48 +01:00
|
|
|
type PluginNotFoundError struct {
|
2020-10-21 12:39:41 +02:00
|
|
|
PluginID string
|
2016-03-10 19:57:48 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-11 09:57:20 +01:00
|
|
|
func (e PluginNotFoundError) Error() string {
|
2020-10-21 12:39:41 +02:00
|
|
|
return fmt.Sprintf("plugin with ID %q not found", e.PluginID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type duplicatePluginError struct {
|
|
|
|
|
Plugin *PluginBase
|
|
|
|
|
ExistingPlugin *PluginBase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e duplicatePluginError) Error() string {
|
|
|
|
|
return fmt.Sprintf("plugin with ID %q already loaded from %q", e.Plugin.Id, e.ExistingPlugin.PluginDir)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e duplicatePluginError) Is(err error) bool {
|
2020-11-19 13:34:28 +01:00
|
|
|
// nolint:errorlint
|
2020-10-21 12:39:41 +02:00
|
|
|
_, ok := err.(duplicatePluginError)
|
|
|
|
|
return ok
|
2016-03-10 19:57:48 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-05 13:28:18 +02:00
|
|
|
// PluginLoader can load a plugin.
|
2016-01-09 23:34:20 +01:00
|
|
|
type PluginLoader interface {
|
2020-10-05 13:28:18 +02:00
|
|
|
// Load loads a plugin and registers it with the manager.
|
|
|
|
|
Load(decoder *json.Decoder, base *PluginBase, backendPluginManager backendplugin.Manager) error
|
2016-01-09 23:34:20 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-05 13:28:18 +02:00
|
|
|
// PluginBase is the base plugin type.
|
2016-01-09 23:34:20 +01:00
|
|
|
type PluginBase struct {
|
2020-12-11 12:57:57 +01:00
|
|
|
Type string `json:"type"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Id string `json:"id"`
|
|
|
|
|
Info PluginInfo `json:"info"`
|
|
|
|
|
Dependencies PluginDependencies `json:"dependencies"`
|
|
|
|
|
Includes []*PluginInclude `json:"includes"`
|
|
|
|
|
Module string `json:"module"`
|
|
|
|
|
BaseUrl string `json:"baseUrl"`
|
|
|
|
|
Category string `json:"category"`
|
|
|
|
|
HideFromList bool `json:"hideFromList,omitempty"`
|
|
|
|
|
Preload bool `json:"preload"`
|
|
|
|
|
State PluginState `json:"state,omitempty"`
|
|
|
|
|
Signature PluginSignatureStatus `json:"signature"`
|
|
|
|
|
Backend bool `json:"backend"`
|
|
|
|
|
|
|
|
|
|
IncludedInAppId string `json:"-"`
|
|
|
|
|
PluginDir string `json:"-"`
|
|
|
|
|
DefaultNavUrl string `json:"-"`
|
|
|
|
|
IsCorePlugin bool `json:"-"`
|
|
|
|
|
Files []string `json:"-"`
|
|
|
|
|
SignatureType PluginSignatureType `json:"-"`
|
|
|
|
|
SignatureOrg string `json:"-"`
|
2016-03-13 19:21:44 +01:00
|
|
|
|
2016-04-11 12:21:48 -04:00
|
|
|
GrafanaNetVersion string `json:"-"`
|
|
|
|
|
GrafanaNetHasUpdate bool `json:"-"`
|
2020-10-05 13:28:18 +02:00
|
|
|
|
|
|
|
|
Root *PluginBase
|
2016-01-08 23:15:44 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-05 13:28:18 +02:00
|
|
|
func (pb *PluginBase) registerPlugin(base *PluginBase) error {
|
2020-10-21 12:39:41 +02:00
|
|
|
if p, exists := Plugins[pb.Id]; exists {
|
|
|
|
|
return duplicatePluginError{Plugin: pb, ExistingPlugin: p}
|
2016-02-10 11:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-05 13:28:18 +02:00
|
|
|
if !strings.HasPrefix(base.PluginDir, setting.StaticRootPath) {
|
2020-10-23 16:45:43 +02:00
|
|
|
plog.Info("Registering plugin", "id", pb.Id)
|
2016-02-10 11:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
2018-02-16 09:49:29 +01:00
|
|
|
if len(pb.Dependencies.Plugins) == 0 {
|
|
|
|
|
pb.Dependencies.Plugins = []PluginDependencyItem{}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 18:03:45 +01:00
|
|
|
if pb.Dependencies.GrafanaVersion == "" {
|
|
|
|
|
pb.Dependencies.GrafanaVersion = "*"
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-25 14:00:49 +02:00
|
|
|
for _, include := range pb.Includes {
|
|
|
|
|
if include.Role == "" {
|
2020-02-28 12:51:21 +01:00
|
|
|
include.Role = models.ROLE_VIEWER
|
2016-04-25 14:00:49 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 13:28:18 +02:00
|
|
|
// Copy relevant fields from the base
|
|
|
|
|
pb.PluginDir = base.PluginDir
|
|
|
|
|
pb.Signature = base.Signature
|
2020-12-11 12:57:57 +01:00
|
|
|
pb.SignatureType = base.SignatureType
|
|
|
|
|
pb.SignatureOrg = base.SignatureOrg
|
2020-10-05 13:28:18 +02:00
|
|
|
|
2016-02-10 11:03:12 +01:00
|
|
|
Plugins[pb.Id] = pb
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 14:31:02 +01:00
|
|
|
type PluginDependencies struct {
|
2018-02-16 09:49:29 +01:00
|
|
|
GrafanaVersion string `json:"grafanaVersion"`
|
|
|
|
|
Plugins []PluginDependencyItem `json:"plugins"`
|
2016-03-07 14:31:02 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-08 18:17:47 +01:00
|
|
|
type PluginInclude struct {
|
2020-02-28 12:51:21 +01:00
|
|
|
Name string `json:"name"`
|
|
|
|
|
Path string `json:"path"`
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
Component string `json:"component"`
|
|
|
|
|
Role models.RoleType `json:"role"`
|
|
|
|
|
AddToNav bool `json:"addToNav"`
|
|
|
|
|
DefaultNav bool `json:"defaultNav"`
|
|
|
|
|
Slug string `json:"slug"`
|
2020-11-27 10:48:37 +01:00
|
|
|
Icon string `json:"icon"`
|
2016-03-21 19:07:08 +01:00
|
|
|
|
|
|
|
|
Id string `json:"-"`
|
2016-03-08 18:17:47 +01:00
|
|
|
}
|
|
|
|
|
|
2018-02-16 09:49:29 +01:00
|
|
|
type PluginDependencyItem struct {
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
Id string `json:"id"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 11:52:34 -07:00
|
|
|
type PluginBuildInfo struct {
|
|
|
|
|
Time int64 `json:"time,omitempty"`
|
|
|
|
|
Repo string `json:"repo,omitempty"`
|
|
|
|
|
Branch string `json:"branch,omitempty"`
|
|
|
|
|
Hash string `json:"hash,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-22 16:32:17 +01:00
|
|
|
type PluginInfo struct {
|
2016-02-09 22:36:42 +08:00
|
|
|
Author PluginInfoLink `json:"author"`
|
|
|
|
|
Description string `json:"description"`
|
|
|
|
|
Links []PluginInfoLink `json:"links"`
|
|
|
|
|
Logos PluginLogos `json:"logos"`
|
2019-07-19 06:21:39 -07:00
|
|
|
Build PluginBuildInfo `json:"build"`
|
2016-02-09 22:36:42 +08:00
|
|
|
Screenshots []PluginScreenshots `json:"screenshots"`
|
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
Updated string `json:"updated"`
|
2015-12-22 16:32:17 +01:00
|
|
|
}
|
|
|
|
|
|
2016-01-08 20:57:58 +01:00
|
|
|
type PluginInfoLink struct {
|
2015-12-22 16:32:17 +01:00
|
|
|
Name string `json:"name"`
|
|
|
|
|
Url string `json:"url"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PluginLogos struct {
|
|
|
|
|
Small string `json:"small"`
|
|
|
|
|
Large string `json:"large"`
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-09 22:36:42 +08:00
|
|
|
type PluginScreenshots struct {
|
|
|
|
|
Path string `json:"path"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-09 08:12:27 +01:00
|
|
|
type PluginStaticRoute struct {
|
|
|
|
|
Directory string
|
|
|
|
|
PluginId string
|
2015-11-19 16:50:17 +01:00
|
|
|
}
|
|
|
|
|
|
2015-12-03 15:52:37 +08:00
|
|
|
type EnabledPlugins struct {
|
2015-12-21 23:09:27 +01:00
|
|
|
Panels []*PanelPlugin
|
|
|
|
|
DataSources map[string]*DataSourcePlugin
|
|
|
|
|
Apps []*AppPlugin
|
2015-12-03 15:52:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewEnabledPlugins() EnabledPlugins {
|
|
|
|
|
return EnabledPlugins{
|
2015-12-21 23:09:27 +01:00
|
|
|
Panels: make([]*PanelPlugin, 0),
|
|
|
|
|
DataSources: make(map[string]*DataSourcePlugin),
|
|
|
|
|
Apps: make([]*AppPlugin, 0),
|
2015-12-03 15:52:37 +08:00
|
|
|
}
|
|
|
|
|
}
|