| 
									
										
										
										
											2015-11-19 19:55:13 +08:00
										 |  |  | package plugins | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-22 00:23:24 +08:00
										 |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2016-03-11 02:57:48 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2016-02-10 18:03:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-28 19:51:21 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2015-12-22 00:23:24 +08:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2015-11-19 19:55:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | const ( | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	TypeDashboard = "dashboard" | 
					
						
							| 
									
										
										
										
											2016-03-11 02:57:48 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | var ( | 
					
						
							|  |  |  | 	ErrInstallCorePlugin           = errors.New("cannot install a Core plugin") | 
					
						
							|  |  |  | 	ErrUninstallCorePlugin         = errors.New("cannot uninstall a Core plugin") | 
					
						
							|  |  |  | 	ErrUninstallOutsideOfPluginDir = errors.New("cannot uninstall a plugin outside") | 
					
						
							|  |  |  | 	ErrPluginNotInstalled          = errors.New("plugin is not installed") | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | type NotFoundError struct { | 
					
						
							| 
									
										
										
										
											2020-10-21 18:39:41 +08:00
										 |  |  | 	PluginID string | 
					
						
							| 
									
										
										
										
											2016-03-11 02:57:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | func (e NotFoundError) Error() string { | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 	return fmt.Sprintf("plugin with ID '%s' not found", e.PluginID) | 
					
						
							| 
									
										
										
										
											2020-10-21 18:39:41 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | type DuplicateError struct { | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 	PluginID          string | 
					
						
							|  |  |  | 	ExistingPluginDir string | 
					
						
							| 
									
										
										
										
											2020-10-21 18:39:41 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | func (e DuplicateError) Error() string { | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 	return fmt.Sprintf("plugin with ID '%s' already exists in '%s'", e.PluginID, e.ExistingPluginDir) | 
					
						
							| 
									
										
										
										
											2020-10-21 18:39:41 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | func (e DuplicateError) Is(err error) bool { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 	// nolint:errorlint
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	_, ok := err.(DuplicateError) | 
					
						
							| 
									
										
										
										
											2020-10-21 18:39:41 +08:00
										 |  |  | 	return ok | 
					
						
							| 
									
										
										
										
											2016-03-11 02:57:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | type SignatureError struct { | 
					
						
							|  |  |  | 	PluginID        string          `json:"pluginId"` | 
					
						
							|  |  |  | 	SignatureStatus SignatureStatus `json:"status"` | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-09-08 14:49:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | func (e SignatureError) Error() string { | 
					
						
							|  |  |  | 	switch e.SignatureStatus { | 
					
						
							|  |  |  | 	case SignatureInvalid: | 
					
						
							|  |  |  | 		return fmt.Sprintf("plugin '%s' has an invalid signature", e.PluginID) | 
					
						
							|  |  |  | 	case SignatureModified: | 
					
						
							|  |  |  | 		return fmt.Sprintf("plugin '%s' has an modified signature", e.PluginID) | 
					
						
							|  |  |  | 	case SignatureUnsigned: | 
					
						
							|  |  |  | 		return fmt.Sprintf("plugin '%s' has no signature", e.PluginID) | 
					
						
							|  |  |  | 	case SignatureInternal, SignatureValid: | 
					
						
							|  |  |  | 		return "" | 
					
						
							| 
									
										
										
										
											2021-09-08 14:49:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	return fmt.Sprintf("plugin '%s' has an unknown signature state", e.PluginID) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (e SignatureError) AsErrorCode() ErrorCode { | 
					
						
							|  |  |  | 	switch e.SignatureStatus { | 
					
						
							|  |  |  | 	case SignatureInvalid: | 
					
						
							|  |  |  | 		return signatureInvalid | 
					
						
							|  |  |  | 	case SignatureModified: | 
					
						
							|  |  |  | 		return signatureModified | 
					
						
							|  |  |  | 	case SignatureUnsigned: | 
					
						
							|  |  |  | 		return signatureMissing | 
					
						
							|  |  |  | 	case SignatureInternal, SignatureValid: | 
					
						
							|  |  |  | 		return "" | 
					
						
							| 
									
										
										
										
											2021-09-08 14:49:05 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return "" | 
					
						
							| 
									
										
										
										
											2021-09-08 14:49:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | type Dependencies struct { | 
					
						
							|  |  |  | 	GrafanaVersion string       `json:"grafanaVersion"` | 
					
						
							|  |  |  | 	Plugins        []Dependency `json:"plugins"` | 
					
						
							| 
									
										
										
										
											2016-03-07 21:31:02 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | type Includes struct { | 
					
						
							| 
									
										
										
										
											2020-02-28 19:51:21 +08: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 17:48:37 +08:00
										 |  |  | 	Icon       string          `json:"icon"` | 
					
						
							| 
									
										
										
										
											2021-07-22 15:11:33 +08:00
										 |  |  | 	UID        string          `json:"uid"` | 
					
						
							| 
									
										
										
										
											2016-03-22 02:07:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	ID string `json:"-"` | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | func (e Includes) GetSlugOrUIDLink() string { | 
					
						
							| 
									
										
										
										
											2021-07-22 15:11:33 +08:00
										 |  |  | 	if len(e.UID) > 0 { | 
					
						
							|  |  |  | 		return "/d/" + e.UID | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return "/dashboard/db/" + e.Slug | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | type Dependency struct { | 
					
						
							|  |  |  | 	ID      string `json:"id"` | 
					
						
							| 
									
										
										
										
											2018-02-16 16:49:29 +08:00
										 |  |  | 	Type    string `json:"type"` | 
					
						
							|  |  |  | 	Name    string `json:"name"` | 
					
						
							|  |  |  | 	Version string `json:"version"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | type BuildInfo struct { | 
					
						
							| 
									
										
										
										
											2019-07-19 02:52:34 +08:00
										 |  |  | 	Time   int64  `json:"time,omitempty"` | 
					
						
							|  |  |  | 	Repo   string `json:"repo,omitempty"` | 
					
						
							|  |  |  | 	Branch string `json:"branch,omitempty"` | 
					
						
							|  |  |  | 	Hash   string `json:"hash,omitempty"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | type Info struct { | 
					
						
							|  |  |  | 	Author      InfoLink      `json:"author"` | 
					
						
							|  |  |  | 	Description string        `json:"description"` | 
					
						
							|  |  |  | 	Links       []InfoLink    `json:"links"` | 
					
						
							|  |  |  | 	Logos       Logos         `json:"logos"` | 
					
						
							|  |  |  | 	Build       BuildInfo     `json:"build"` | 
					
						
							|  |  |  | 	Screenshots []Screenshots `json:"screenshots"` | 
					
						
							|  |  |  | 	Version     string        `json:"version"` | 
					
						
							|  |  |  | 	Updated     string        `json:"updated"` | 
					
						
							| 
									
										
										
										
											2015-12-22 23:32:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | type InfoLink struct { | 
					
						
							| 
									
										
										
										
											2015-12-22 23:32:17 +08:00
										 |  |  | 	Name string `json:"name"` | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	URL  string `json:"url"` | 
					
						
							| 
									
										
										
										
											2015-12-22 23:32:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | type Logos struct { | 
					
						
							| 
									
										
										
										
											2015-12-22 23:32:17 +08:00
										 |  |  | 	Small string `json:"small"` | 
					
						
							|  |  |  | 	Large string `json:"large"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | type Screenshots struct { | 
					
						
							| 
									
										
										
										
											2016-02-09 22:36:42 +08:00
										 |  |  | 	Name string `json:"name"` | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	Path string `json:"path"` | 
					
						
							| 
									
										
										
										
											2016-02-09 22:36:42 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | type StaticRoute struct { | 
					
						
							|  |  |  | 	PluginID  string | 
					
						
							| 
									
										
										
										
											2016-01-09 15:12:27 +08:00
										 |  |  | 	Directory string | 
					
						
							| 
									
										
										
										
											2015-11-19 23:50:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | type SignatureStatus string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (ss SignatureStatus) IsValid() bool { | 
					
						
							|  |  |  | 	return ss == SignatureValid | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (ss SignatureStatus) IsInternal() bool { | 
					
						
							|  |  |  | 	return ss == SignatureInternal | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	SignatureInternal SignatureStatus = "internal" // core plugin, no signature
 | 
					
						
							|  |  |  | 	SignatureValid    SignatureStatus = "valid"    // signed and accurate MANIFEST
 | 
					
						
							|  |  |  | 	SignatureInvalid  SignatureStatus = "invalid"  // invalid signature
 | 
					
						
							|  |  |  | 	SignatureModified SignatureStatus = "modified" // valid signature, but content mismatch
 | 
					
						
							|  |  |  | 	SignatureUnsigned SignatureStatus = "unsigned" // no MANIFEST file
 | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ReleaseState string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	AlphaRelease ReleaseState = "alpha" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type SignatureType string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	GrafanaSignature SignatureType = "grafana" | 
					
						
							|  |  |  | 	PrivateSignature SignatureType = "private" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type PluginFiles map[string]struct{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Signature struct { | 
					
						
							|  |  |  | 	Status     SignatureStatus | 
					
						
							|  |  |  | 	Type       SignatureType | 
					
						
							|  |  |  | 	SigningOrg string | 
					
						
							|  |  |  | 	Files      PluginFiles | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type PluginMetaDTO struct { | 
					
						
							|  |  |  | 	JSONData | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Signature SignatureStatus `json:"signature"` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Module  string `json:"module"` | 
					
						
							|  |  |  | 	BaseURL string `json:"baseUrl"` | 
					
						
							| 
									
										
										
										
											2015-12-03 15:52:37 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-07-29 17:52:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	signatureMissing  ErrorCode = "signatureMissing" | 
					
						
							|  |  |  | 	signatureModified ErrorCode = "signatureModified" | 
					
						
							|  |  |  | 	signatureInvalid  ErrorCode = "signatureInvalid" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ErrorCode string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Error struct { | 
					
						
							|  |  |  | 	ErrorCode `json:"errorCode"` | 
					
						
							|  |  |  | 	PluginID  string `json:"pluginId,omitempty"` | 
					
						
							| 
									
										
										
										
											2021-07-29 17:52:23 +08:00
										 |  |  | } |