| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2022-12-02 20:46:55 +08:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2020-04-21 22:16:41 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2020-03-13 19:31:44 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2022-12-02 20:46:55 +08:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2020-03-18 19:08:20 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2022-02-05 01:14:53 +08:00
										 |  |  | 	"path" | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2022-08-23 17:50:50 +08:00
										 |  |  | 	"runtime" | 
					
						
							| 
									
										
										
										
											2016-03-15 22:52:29 +08:00
										 |  |  | 	"sort" | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2016-03-15 22:52:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | 	"github.com/grafana/grafana-plugin-sdk-go/backend" | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							| 
									
										
										
										
											2020-02-20 02:17:05 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins" | 
					
						
							| 
									
										
										
										
											2020-03-03 18:45:16 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins/backendplugin" | 
					
						
							| 
									
										
										
										
											2022-08-23 17:50:50 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins/repo" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/plugins/storage" | 
					
						
							| 
									
										
										
										
											2022-09-09 15:44:50 +08:00
										 |  |  | 	ac "github.com/grafana/grafana/pkg/services/accesscontrol" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/datasources" | 
					
						
							| 
									
										
										
										
											2022-10-28 19:33:37 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/featuremgmt" | 
					
						
							| 
									
										
										
										
											2022-09-09 15:44:50 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/org" | 
					
						
							| 
									
										
										
										
											2022-03-19 03:49:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/pluginsettings" | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2022-12-02 20:46:55 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func (hs *HTTPServer) GetPluginList(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 	typeFilter := c.Query("type") | 
					
						
							|  |  |  | 	enabledFilter := c.Query("enabled") | 
					
						
							|  |  |  | 	embeddedFilter := c.Query("embedded") | 
					
						
							| 
									
										
										
										
											2022-09-09 15:44:50 +08:00
										 |  |  | 	// "" => no filter
 | 
					
						
							|  |  |  | 	// "0" => filter out core plugins
 | 
					
						
							|  |  |  | 	// "1" => filter out non-core plugins
 | 
					
						
							| 
									
										
										
										
											2016-04-09 04:42:33 +08:00
										 |  |  | 	coreFilter := c.Query("core") | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:44:50 +08:00
										 |  |  | 	// FIXME: while we don't have permissions for listing plugins we need this complex check:
 | 
					
						
							|  |  |  | 	// When using access control, should be able to list non-core plugins:
 | 
					
						
							|  |  |  | 	//  * anyone that can create a data source
 | 
					
						
							|  |  |  | 	//  * anyone that can install a plugin
 | 
					
						
							| 
									
										
										
										
											2022-05-13 16:30:26 +08:00
										 |  |  | 	// Fallback to only letting admins list non-core plugins
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:44:50 +08:00
										 |  |  | 	reqOrgAdmin := ac.ReqHasRole(org.RoleAdmin) | 
					
						
							|  |  |  | 	hasAccess := ac.HasAccess(hs.AccessControl, c) | 
					
						
							|  |  |  | 	canListNonCorePlugins := reqOrgAdmin(c) || hasAccess(reqOrgAdmin, ac.EvalAny( | 
					
						
							|  |  |  | 		ac.EvalPermission(datasources.ActionCreate), | 
					
						
							|  |  |  | 		ac.EvalPermission(plugins.ActionInstall), | 
					
						
							|  |  |  | 	)) | 
					
						
							| 
									
										
										
										
											2020-03-16 22:40:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 	pluginSettingsMap, err := hs.pluginSettings(c.Req.Context(), c.OrgID) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-09-09 15:44:50 +08:00
										 |  |  | 		return response.Error(http.StatusInternalServerError, "Failed to get list of plugins", err) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:44:50 +08:00
										 |  |  | 	// Filter plugins
 | 
					
						
							|  |  |  | 	pluginDefinitions := hs.pluginStore.Plugins(c.Req.Context()) | 
					
						
							|  |  |  | 	filteredPluginDefinitions := []plugins.PluginDTO{} | 
					
						
							|  |  |  | 	filteredPluginIDs := map[string]bool{} | 
					
						
							|  |  |  | 	for _, pluginDef := range pluginDefinitions { | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 		// filter out app sub plugins
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		if embeddedFilter == "0" && pluginDef.IncludedInAppID != "" { | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 04:42:33 +08:00
										 |  |  | 		// filter out core plugins
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		if (coreFilter == "0" && pluginDef.IsCorePlugin()) || (coreFilter == "1" && !pluginDef.IsCorePlugin()) { | 
					
						
							| 
									
										
										
										
											2016-04-09 04:42:33 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:44:50 +08:00
										 |  |  | 		// FIXME: while we don't have permissions for listing plugins we need this complex check:
 | 
					
						
							|  |  |  | 		// When using access control, should be able to list non-core plugins:
 | 
					
						
							|  |  |  | 		//  * anyone that can create a data source
 | 
					
						
							|  |  |  | 		//  * anyone that can install a plugin
 | 
					
						
							|  |  |  | 		// Should be able to list this installed plugin:
 | 
					
						
							|  |  |  | 		//  * anyone that can edit its settings
 | 
					
						
							|  |  |  | 		if !pluginDef.IsCorePlugin() && !canListNonCorePlugins && !hasAccess(reqOrgAdmin, | 
					
						
							|  |  |  | 			ac.EvalPermission(plugins.ActionWrite, plugins.ScopeProvider.GetResourceScope(pluginDef.ID))) { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 		// filter on type
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		if typeFilter != "" && typeFilter != string(pluginDef.Type) { | 
					
						
							| 
									
										
										
										
											2016-03-08 18:29:36 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		if pluginDef.State == plugins.AlphaRelease && !hs.Cfg.PluginsEnableAlpha { | 
					
						
							| 
									
										
										
										
											2018-11-15 18:10:47 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:44:50 +08:00
										 |  |  | 		// filter out built in plugins
 | 
					
						
							|  |  |  | 		if pluginDef.BuiltIn { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// filter out disabled plugins
 | 
					
						
							|  |  |  | 		if pluginSetting, exists := pluginSettingsMap[pluginDef.ID]; exists { | 
					
						
							|  |  |  | 			if enabledFilter == "1" && !pluginSetting.Enabled { | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-28 19:33:37 +08:00
										 |  |  | 		if (pluginDef.ID == "parca" || pluginDef.ID == "phlare") && !hs.Features.IsEnabled(featuremgmt.FlagFlameGraph) { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-09 15:44:50 +08:00
										 |  |  | 		filteredPluginDefinitions = append(filteredPluginDefinitions, pluginDef) | 
					
						
							|  |  |  | 		filteredPluginIDs[pluginDef.ID] = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Compute metadata
 | 
					
						
							|  |  |  | 	pluginsMetadata := hs.getMultiAccessControlMetadata(c, c.OrgID, | 
					
						
							|  |  |  | 		plugins.ScopeProvider.GetResourceScope(""), filteredPluginIDs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Prepare DTO
 | 
					
						
							|  |  |  | 	result := make(dtos.PluginList, 0) | 
					
						
							|  |  |  | 	for _, pluginDef := range filteredPluginDefinitions { | 
					
						
							| 
									
										
										
										
											2016-03-15 22:52:29 +08:00
										 |  |  | 		listItem := dtos.PluginListItem{ | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 			Id:            pluginDef.ID, | 
					
						
							| 
									
										
										
										
											2016-04-12 00:47:04 +08:00
										 |  |  | 			Name:          pluginDef.Name, | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 			Type:          string(pluginDef.Type), | 
					
						
							| 
									
										
										
										
											2019-05-09 17:45:39 +08:00
										 |  |  | 			Category:      pluginDef.Category, | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 			Info:          pluginDef.Info, | 
					
						
							|  |  |  | 			Dependencies:  pluginDef.Dependencies, | 
					
						
							| 
									
										
										
										
											2022-02-05 01:14:53 +08:00
										 |  |  | 			DefaultNavUrl: path.Join(hs.Cfg.AppSubURL, pluginDef.DefaultNavURL), | 
					
						
							| 
									
										
										
										
											2017-04-07 18:00:03 +08:00
										 |  |  | 			State:         pluginDef.State, | 
					
						
							| 
									
										
										
										
											2020-04-09 15:00:16 +08:00
										 |  |  | 			Signature:     pluginDef.Signature, | 
					
						
							| 
									
										
										
										
											2020-12-11 19:57:57 +08:00
										 |  |  | 			SignatureType: pluginDef.SignatureType, | 
					
						
							|  |  |  | 			SignatureOrg:  pluginDef.SignatureOrg, | 
					
						
							| 
									
										
										
										
											2022-09-09 15:44:50 +08:00
										 |  |  | 			AccessControl: pluginsMetadata[pluginDef.ID], | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-31 23:06:16 +08:00
										 |  |  | 		update, exists := hs.pluginsUpdateChecker.HasUpdate(c.Req.Context(), pluginDef.ID) | 
					
						
							|  |  |  | 		if exists { | 
					
						
							|  |  |  | 			listItem.LatestVersion = update | 
					
						
							|  |  |  | 			listItem.HasUpdate = true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		if pluginSetting, exists := pluginSettingsMap[pluginDef.ID]; exists { | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 			listItem.Enabled = pluginSetting.Enabled | 
					
						
							|  |  |  | 			listItem.Pinned = pluginSetting.Pinned | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 01:00:42 +08:00
										 |  |  | 		if listItem.DefaultNavUrl == "" || !listItem.Enabled { | 
					
						
							| 
									
										
										
										
											2021-03-10 19:41:29 +08:00
										 |  |  | 			listItem.DefaultNavUrl = hs.Cfg.AppSubURL + "/plugins/" + listItem.Id + "/" | 
					
						
							| 
									
										
										
										
											2016-05-04 01:00:42 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 		result = append(result, listItem) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 22:52:29 +08:00
										 |  |  | 	sort.Sort(result) | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, result) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-17 23:06:10 +08:00
										 |  |  | func (hs *HTTPServer) GetPluginSettingByID(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	pluginID := web.Params(c.Req)[":pluginId"] | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	plugin, exists := hs.pluginStore.Plugin(c.Req.Context(), pluginID) | 
					
						
							|  |  |  | 	if !exists { | 
					
						
							| 
									
										
										
										
											2022-07-08 19:24:09 +08:00
										 |  |  | 		return response.Error(http.StatusNotFound, "Plugin not found, no installed plugin with that id", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// In a first iteration, we only have one permission for app plugins.
 | 
					
						
							|  |  |  | 	// We will need a different permission to allow users to configure the plugin without needing access to it.
 | 
					
						
							|  |  |  | 	if plugin.IsApp() { | 
					
						
							| 
									
										
										
										
											2022-09-09 15:44:50 +08:00
										 |  |  | 		hasAccess := ac.HasAccess(hs.AccessControl, c) | 
					
						
							|  |  |  | 		if !hasAccess(ac.ReqSignedIn, | 
					
						
							|  |  |  | 			ac.EvalPermission(plugins.ActionAppAccess, plugins.ScopeProvider.GetResourceScope(plugin.ID))) { | 
					
						
							| 
									
										
										
										
											2022-07-08 19:24:09 +08:00
										 |  |  | 			return response.Error(http.StatusForbidden, "Access Denied", nil) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	dto := &dtos.PluginSetting{ | 
					
						
							| 
									
										
										
										
											2022-09-21 18:20:11 +08:00
										 |  |  | 		Type:             string(plugin.Type), | 
					
						
							|  |  |  | 		Id:               plugin.ID, | 
					
						
							|  |  |  | 		Name:             plugin.Name, | 
					
						
							|  |  |  | 		Info:             plugin.Info, | 
					
						
							|  |  |  | 		Dependencies:     plugin.Dependencies, | 
					
						
							|  |  |  | 		Includes:         plugin.Includes, | 
					
						
							|  |  |  | 		BaseUrl:          plugin.BaseURL, | 
					
						
							|  |  |  | 		Module:           plugin.Module, | 
					
						
							|  |  |  | 		DefaultNavUrl:    path.Join(hs.Cfg.AppSubURL, plugin.DefaultNavURL), | 
					
						
							|  |  |  | 		State:            plugin.State, | 
					
						
							|  |  |  | 		Signature:        plugin.Signature, | 
					
						
							|  |  |  | 		SignatureType:    plugin.SignatureType, | 
					
						
							|  |  |  | 		SignatureOrg:     plugin.SignatureOrg, | 
					
						
							|  |  |  | 		SecureJsonFields: map[string]bool{}, | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if plugin.IsApp() { | 
					
						
							|  |  |  | 		dto.Enabled = plugin.AutoEnabled | 
					
						
							|  |  |  | 		dto.Pinned = plugin.AutoEnabled | 
					
						
							| 
									
										
										
										
											2021-02-25 17:00:21 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-19 03:49:13 +08:00
										 |  |  | 	ps, err := hs.PluginSettings.GetPluginSettingByPluginID(c.Req.Context(), &pluginsettings.GetByPluginIDArgs{ | 
					
						
							|  |  |  | 		PluginID: pluginID, | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 		OrgID:    c.OrgID, | 
					
						
							| 
									
										
										
										
											2022-03-19 03:49:13 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 		if !errors.Is(err, models.ErrPluginSettingNotFound) { | 
					
						
							| 
									
										
										
										
											2022-03-19 03:49:13 +08:00
										 |  |  | 			return response.Error(http.StatusInternalServerError, "Failed to get plugin settings", nil) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2022-03-19 03:49:13 +08:00
										 |  |  | 		dto.Enabled = ps.Enabled | 
					
						
							|  |  |  | 		dto.Pinned = ps.Pinned | 
					
						
							|  |  |  | 		dto.JsonData = ps.JSONData | 
					
						
							| 
									
										
										
										
											2022-09-21 18:20:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for k, v := range hs.PluginSettings.DecryptedValues(ps) { | 
					
						
							|  |  |  | 			if len(v) > 0 { | 
					
						
							|  |  |  | 				dto.SecureJsonFields[k] = true | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-31 23:06:16 +08:00
										 |  |  | 	update, exists := hs.pluginsUpdateChecker.HasUpdate(c.Req.Context(), plugin.ID) | 
					
						
							|  |  |  | 	if exists { | 
					
						
							|  |  |  | 		dto.LatestVersion = update | 
					
						
							|  |  |  | 		dto.HasUpdate = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, dto) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | func (hs *HTTPServer) UpdatePluginSetting(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	cmd := models.UpdatePluginSettingCmd{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &cmd); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	pluginID := web.Params(c.Req)[":pluginId"] | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	if _, exists := hs.pluginStore.Plugin(c.Req.Context(), pluginID); !exists { | 
					
						
							| 
									
										
										
										
											2021-03-18 20:53:01 +08:00
										 |  |  | 		return response.Error(404, "Plugin not installed", nil) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-11 19:28:55 +08:00
										 |  |  | 	cmd.OrgId = c.OrgID | 
					
						
							| 
									
										
										
										
											2021-03-18 20:53:01 +08:00
										 |  |  | 	cmd.PluginId = pluginID | 
					
						
							| 
									
										
										
										
											2022-03-19 03:49:13 +08:00
										 |  |  | 	if err := hs.PluginSettings.UpdatePluginSetting(c.Req.Context(), &pluginsettings.UpdateArgs{ | 
					
						
							|  |  |  | 		Enabled:                 cmd.Enabled, | 
					
						
							|  |  |  | 		Pinned:                  cmd.Pinned, | 
					
						
							|  |  |  | 		JSONData:                cmd.JsonData, | 
					
						
							|  |  |  | 		SecureJSONData:          cmd.SecureJsonData, | 
					
						
							|  |  |  | 		PluginVersion:           cmd.PluginVersion, | 
					
						
							|  |  |  | 		PluginID:                cmd.PluginId, | 
					
						
							|  |  |  | 		OrgID:                   cmd.OrgId, | 
					
						
							|  |  |  | 		EncryptedSecureJSONData: cmd.EncryptedSecureJsonData, | 
					
						
							|  |  |  | 	}); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to update plugin setting", err) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.Success("Plugin settings updated") | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-03-11 16:57:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 14:02:49 +08:00
										 |  |  | func (hs *HTTPServer) GetPluginMarkdown(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	pluginID := web.Params(c.Req)[":pluginId"] | 
					
						
							|  |  |  | 	name := web.Params(c.Req)[":name"] | 
					
						
							| 
									
										
										
										
											2016-03-14 02:21:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	content, err := hs.pluginMarkdown(c.Req.Context(), pluginID, name) | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		var notFound plugins.NotFoundError | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 		if errors.As(err, ¬Found) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(404, notFound.Error(), nil) | 
					
						
							| 
									
										
										
										
											2016-03-14 02:21:44 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Could not get markdown file", err) | 
					
						
							| 
									
										
										
										
											2016-03-14 02:21:44 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-19 17:53:14 +08:00
										 |  |  | 	// fallback try readme
 | 
					
						
							|  |  |  | 	if len(content) == 0 { | 
					
						
							| 
									
										
										
										
											2023-01-03 21:20:27 +08:00
										 |  |  | 		content, err = hs.pluginMarkdown(c.Req.Context(), pluginID, "readme") | 
					
						
							| 
									
										
										
										
											2018-12-19 17:53:14 +08:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(501, "Could not get markdown file", err) | 
					
						
							| 
									
										
										
										
											2018-12-19 17:53:14 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	resp := response.Respond(http.StatusOK, content) | 
					
						
							| 
									
										
										
										
											2021-03-18 01:12:28 +08:00
										 |  |  | 	resp.SetHeader("Content-Type", "text/plain; charset=utf-8") | 
					
						
							| 
									
										
										
										
											2018-03-22 19:37:35 +08:00
										 |  |  | 	return resp | 
					
						
							| 
									
										
										
										
											2016-03-14 02:21:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-18 19:08:20 +08:00
										 |  |  | // CollectPluginMetrics collect metrics from a plugin.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // /api/plugins/:pluginId/metrics
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func (hs *HTTPServer) CollectPluginMetrics(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	pluginID := web.Params(c.Req)[":pluginId"] | 
					
						
							| 
									
										
										
										
											2022-02-10 00:36:53 +08:00
										 |  |  | 	resp, err := hs.pluginClient.CollectMetrics(c.Req.Context(), &backend.CollectMetricsRequest{PluginContext: backend.PluginContext{PluginID: pluginID}}) | 
					
						
							| 
									
										
										
										
											2020-03-18 19:08:20 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | 		return translatePluginRequestErrorToAPIError(err) | 
					
						
							| 
									
										
										
										
											2020-03-18 19:08:20 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	headers := make(http.Header) | 
					
						
							|  |  |  | 	headers.Set("Content-Type", "text/plain") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.CreateNormalResponse(headers, resp.PrometheusMetrics, http.StatusOK) | 
					
						
							| 
									
										
										
										
											2020-03-18 19:08:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 14:49:05 +08:00
										 |  |  | // getPluginAssets returns public plugin assets (images, JS, etc.)
 | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // /public/plugins/:pluginId/*
 | 
					
						
							| 
									
										
										
										
											2021-09-08 14:49:05 +08:00
										 |  |  | func (hs *HTTPServer) getPluginAssets(c *models.ReqContext) { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	pluginID := web.Params(c.Req)[":pluginId"] | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	plugin, exists := hs.pluginStore.Plugin(c.Req.Context(), pluginID) | 
					
						
							|  |  |  | 	if !exists { | 
					
						
							| 
									
										
										
										
											2021-07-15 14:56:11 +08:00
										 |  |  | 		c.JsonApiErr(404, "Plugin not found", nil) | 
					
						
							| 
									
										
										
										
											2021-07-14 15:38:49 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-08 01:15:53 +08:00
										 |  |  | 	// prepend slash for cleaning relative paths
 | 
					
						
							| 
									
										
										
										
											2022-12-02 20:46:55 +08:00
										 |  |  | 	requestedFile, err := util.CleanRelativePath(web.Params(c.Req)["*"]) | 
					
						
							| 
									
										
										
										
											2021-12-08 01:15:53 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		// slash is prepended above therefore this is not expected to fail
 | 
					
						
							| 
									
										
										
										
											2022-12-02 20:46:55 +08:00
										 |  |  | 		c.JsonApiErr(500, "Failed to clean relative file path", err) | 
					
						
							| 
									
										
										
										
											2021-12-08 01:15:53 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-02 20:46:55 +08:00
										 |  |  | 	f, err := plugin.File(requestedFile) | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-12-02 20:46:55 +08:00
										 |  |  | 		if errors.Is(err, plugins.ErrFileNotExist) { | 
					
						
							|  |  |  | 			c.JsonApiErr(404, "Plugin file not found", nil) | 
					
						
							| 
									
										
										
										
											2021-07-14 15:38:49 +08:00
										 |  |  | 			return | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-07-15 14:56:11 +08:00
										 |  |  | 		c.JsonApiErr(500, "Could not open plugin file", err) | 
					
						
							| 
									
										
										
										
											2021-07-14 15:38:49 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	defer func() { | 
					
						
							| 
									
										
										
										
											2022-12-02 20:46:55 +08:00
										 |  |  | 		if err = f.Close(); err != nil { | 
					
						
							|  |  |  | 			hs.log.Error("Failed to close plugin file", "err", err) | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fi, err := f.Stat() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-07-15 14:56:11 +08:00
										 |  |  | 		c.JsonApiErr(500, "Plugin file exists but could not open", err) | 
					
						
							| 
									
										
										
										
											2021-07-14 15:38:49 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if hs.Cfg.Env == setting.Dev { | 
					
						
							| 
									
										
										
										
											2021-07-14 15:38:49 +08:00
										 |  |  | 		c.Resp.Header().Set("Cache-Control", "max-age=0, must-revalidate, no-cache") | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		c.Resp.Header().Set("Cache-Control", "public, max-age=3600") | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-02 20:46:55 +08:00
										 |  |  | 	if rs, ok := f.(io.ReadSeeker); ok { | 
					
						
							|  |  |  | 		http.ServeContent(c.Resp, c.Req, requestedFile, fi.ModTime(), rs) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		b, err := io.ReadAll(f) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			c.JsonApiErr(500, "Plugin file exists but could not read", err) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		http.ServeContent(c.Resp, c.Req, requestedFile, fi.ModTime(), bytes.NewReader(b)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 21:37:36 +08:00
										 |  |  | // CheckHealth returns the health of a plugin.
 | 
					
						
							| 
									
										
										
										
											2020-01-31 18:15:50 +08:00
										 |  |  | // /api/plugins/:pluginId/health
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func (hs *HTTPServer) CheckHealth(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	pluginID := web.Params(c.Req)[":pluginId"] | 
					
						
							| 
									
										
										
										
											2020-03-13 19:31:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-06 16:58:02 +08:00
										 |  |  | 	pCtx, found, err := hs.PluginContextProvider.Get(c.Req.Context(), pluginID, c.SignedInUser) | 
					
						
							| 
									
										
										
										
											2020-03-13 19:31:44 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to get plugin settings", err) | 
					
						
							| 
									
										
										
										
											2020-03-13 19:31:44 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-03-24 01:24:08 +08:00
										 |  |  | 	if !found { | 
					
						
							|  |  |  | 		return response.Error(404, "Plugin not found", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-13 19:31:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	resp, err := hs.pluginClient.CheckHealth(c.Req.Context(), &backend.CheckHealthRequest{ | 
					
						
							|  |  |  | 		PluginContext: pCtx, | 
					
						
							| 
									
										
										
										
											2022-05-31 23:58:06 +08:00
										 |  |  | 		Headers:       map[string]string{}, | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2020-01-31 18:15:50 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | 		return translatePluginRequestErrorToAPIError(err) | 
					
						
							| 
									
										
										
										
											2020-01-31 18:15:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	payload := map[string]interface{}{ | 
					
						
							| 
									
										
										
										
											2020-04-21 22:16:41 +08:00
										 |  |  | 		"status":  resp.Status.String(), | 
					
						
							|  |  |  | 		"message": resp.Message, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Unmarshal JSONDetails if it's not empty.
 | 
					
						
							|  |  |  | 	if len(resp.JSONDetails) > 0 { | 
					
						
							|  |  |  | 		var jsonDetails map[string]interface{} | 
					
						
							|  |  |  | 		err = json.Unmarshal(resp.JSONDetails, &jsonDetails) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(500, "Failed to unmarshal detailed response from backend plugin", err) | 
					
						
							| 
									
										
										
										
											2020-04-21 22:16:41 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		payload["details"] = jsonDetails | 
					
						
							| 
									
										
										
										
											2020-01-31 18:15:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | 	if resp.Status != backend.HealthStatusOk { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.JSON(503, payload) | 
					
						
							| 
									
										
										
										
											2020-01-31 18:15:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, payload) | 
					
						
							| 
									
										
										
										
											2020-01-31 18:15:50 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-24 01:24:08 +08:00
										 |  |  | func (hs *HTTPServer) GetPluginErrorsList(_ *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, hs.pluginErrorResolver.PluginErrors()) | 
					
						
							| 
									
										
										
										
											2020-10-23 22:45:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | func (hs *HTTPServer) InstallPlugin(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	dto := dtos.InstallPluginCommand{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &dto); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	pluginID := web.Params(c.Req)[":pluginId"] | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-23 20:27:01 +08:00
										 |  |  | 	err := hs.pluginInstaller.Add(c.Req.Context(), pluginID, dto.Version, plugins.CompatOpts{ | 
					
						
							| 
									
										
										
										
											2022-08-23 17:50:50 +08:00
										 |  |  | 		GrafanaVersion: hs.Cfg.BuildVersion, | 
					
						
							|  |  |  | 		OS:             runtime.GOOS, | 
					
						
							|  |  |  | 		Arch:           runtime.GOARCH, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		var dupeErr plugins.DuplicateError | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 		if errors.As(err, &dupeErr) { | 
					
						
							|  |  |  | 			return response.Error(http.StatusConflict, "Plugin already installed", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-08-23 17:50:50 +08:00
										 |  |  | 		var versionUnsupportedErr repo.ErrVersionUnsupported | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 		if errors.As(err, &versionUnsupportedErr) { | 
					
						
							|  |  |  | 			return response.Error(http.StatusConflict, "Plugin version not supported", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-08-23 17:50:50 +08:00
										 |  |  | 		var versionNotFoundErr repo.ErrVersionNotFound | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 		if errors.As(err, &versionNotFoundErr) { | 
					
						
							|  |  |  | 			return response.Error(http.StatusNotFound, "Plugin version not found", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-08-23 17:50:50 +08:00
										 |  |  | 		var clientError repo.Response4xxError | 
					
						
							| 
									
										
										
										
											2021-07-13 15:58:46 +08:00
										 |  |  | 		if errors.As(err, &clientError) { | 
					
						
							|  |  |  | 			return response.Error(clientError.StatusCode, clientError.Message, err) | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if errors.Is(err, plugins.ErrInstallCorePlugin) { | 
					
						
							|  |  |  | 			return response.Error(http.StatusForbidden, "Cannot install or change a Core plugin", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return response.Error(http.StatusInternalServerError, "Failed to install plugin", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return response.JSON(http.StatusOK, []byte{}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (hs *HTTPServer) UninstallPlugin(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	pluginID := web.Params(c.Req)[":pluginId"] | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-23 20:27:01 +08:00
										 |  |  | 	err := hs.pluginInstaller.Remove(c.Req.Context(), pluginID) | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		if errors.Is(err, plugins.ErrPluginNotInstalled) { | 
					
						
							|  |  |  | 			return response.Error(http.StatusNotFound, "Plugin not installed", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if errors.Is(err, plugins.ErrUninstallCorePlugin) { | 
					
						
							|  |  |  | 			return response.Error(http.StatusForbidden, "Cannot uninstall a Core plugin", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-08-23 17:50:50 +08:00
										 |  |  | 		if errors.Is(err, storage.ErrUninstallOutsideOfPluginDir) { | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 			return response.Error(http.StatusForbidden, "Cannot uninstall a plugin outside of the plugins directory", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return response.Error(http.StatusInternalServerError, "Failed to uninstall plugin", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return response.JSON(http.StatusOK, []byte{}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func translatePluginRequestErrorToAPIError(err error) response.Response { | 
					
						
							| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | 	if errors.Is(err, backendplugin.ErrPluginNotRegistered) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(404, "Plugin not found", err) | 
					
						
							| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if errors.Is(err, backendplugin.ErrMethodNotImplemented) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(404, "Not found", err) | 
					
						
							| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if errors.Is(err, backendplugin.ErrHealthCheckFailed) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Plugin health check failed", err) | 
					
						
							| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if errors.Is(err, backendplugin.ErrPluginUnavailable) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(503, "Plugin unavailable", err) | 
					
						
							| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.Error(500, "Plugin request failed", err) | 
					
						
							| 
									
										
										
										
											2020-06-11 22:14:05 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | func (hs *HTTPServer) pluginMarkdown(ctx context.Context, pluginId string, name string) ([]byte, error) { | 
					
						
							|  |  |  | 	plugin, exists := hs.pluginStore.Plugin(ctx, pluginId) | 
					
						
							|  |  |  | 	if !exists { | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		return nil, plugins.NotFoundError{PluginID: pluginId} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-02 20:46:55 +08:00
										 |  |  | 	md, err := plugin.File(mdFilepath(strings.ToUpper(name))) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2022-12-02 20:46:55 +08:00
										 |  |  | 		md, err = plugin.File(mdFilepath(strings.ToUpper(name))) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return make([]byte, 0), nil | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-12-02 20:46:55 +08:00
										 |  |  | 	defer func() { | 
					
						
							|  |  |  | 		if err = md.Close(); err != nil { | 
					
						
							|  |  |  | 			hs.log.Error("Failed to close plugin markdown file", "err", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-02 20:46:55 +08:00
										 |  |  | 	d, err := io.ReadAll(md) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return make([]byte, 0), nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-12-02 20:46:55 +08:00
										 |  |  | 	return d, nil | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-12-10 23:10:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func mdFilepath(mdFilename string) string { | 
					
						
							|  |  |  | 	return filepath.Clean(filepath.Join("/", fmt.Sprintf("%s.md", mdFilename))) | 
					
						
							|  |  |  | } |