| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2020-03-18 19:08:20 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2022-02-05 01:14:53 +08:00
										 |  |  | 	"path" | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-13 16:30:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/accesscontrol" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/datasources" | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/org" | 
					
						
							| 
									
										
										
										
											2022-05-13 16:30:26 +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" | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/fs" | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2021-05-13 02:05:16 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins/manager/installer" | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											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") | 
					
						
							| 
									
										
										
										
											2016-04-09 04:42:33 +08:00
										 |  |  | 	coreFilter := c.Query("core") | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-13 16:30:26 +08:00
										 |  |  | 	// When using access control anyone that can create a data source should be able to list all data sources installed
 | 
					
						
							|  |  |  | 	// Fallback to only letting admins list non-core plugins
 | 
					
						
							|  |  |  | 	hasAccess := accesscontrol.HasAccess(hs.AccessControl, c) | 
					
						
							| 
									
										
										
										
											2022-08-10 17:56:48 +08:00
										 |  |  | 	if !hasAccess(accesscontrol.ReqOrgAdmin, accesscontrol.EvalPermission(datasources.ActionCreate)) && !c.HasRole(org.RoleAdmin) { | 
					
						
							| 
									
										
										
										
											2020-03-16 22:40:46 +08:00
										 |  |  | 		coreFilter = "1" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to get list of plugins", err) | 
					
						
							| 
									
										
										
										
											2016-02-25 21:55:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 22:52:29 +08:00
										 |  |  | 	result := make(dtos.PluginList, 0) | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	for _, pluginDef := range hs.pluginStore.Plugins(c.Req.Context()) { | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-23 22:45:43 +08:00
										 |  |  | 		// filter out disabled plugins
 | 
					
						
							| 
									
										
										
										
											2016-03-09 01:17:47 +08:00
										 |  |  | 		if enabledFilter == "1" && !listItem.Enabled { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		// filter out built in plugins
 | 
					
						
							|  |  |  | 		if pluginDef.BuiltIn { | 
					
						
							|  |  |  | 			continue | 
					
						
							| 
									
										
										
										
											2016-03-14 19:00:56 +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() { | 
					
						
							|  |  |  | 		hasAccess := accesscontrol.HasAccess(hs.AccessControl, c) | 
					
						
							|  |  |  | 		if !hasAccess(accesscontrol.ReqSignedIn, | 
					
						
							|  |  |  | 			accesscontrol.EvalPermission(plugins.ActionAppAccess, plugins.ScopeProvider.GetResourceScope(plugin.ID))) { | 
					
						
							|  |  |  | 			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{ | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +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, | 
					
						
							| 
									
										
										
										
											2022-02-05 01:14:53 +08:00
										 |  |  | 		DefaultNavUrl: path.Join(hs.Cfg.AppSubURL, plugin.DefaultNavURL), | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 		State:         plugin.State, | 
					
						
							|  |  |  | 		Signature:     plugin.Signature, | 
					
						
							|  |  |  | 		SignatureType: plugin.SignatureType, | 
					
						
							|  |  |  | 		SignatureOrg:  plugin.SignatureOrg, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +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
 | 
					
						
							|  |  |  | 	requestedFile := filepath.Clean(filepath.Join("/", web.Params(c.Req)["*"])) | 
					
						
							|  |  |  | 	rel, err := filepath.Rel("/", requestedFile) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		// slash is prepended above therefore this is not expected to fail
 | 
					
						
							|  |  |  | 		c.JsonApiErr(500, "Failed to get the relative path", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-08 01:15:53 +08:00
										 |  |  | 	if !plugin.IncludedInSignature(rel) { | 
					
						
							| 
									
										
										
										
											2021-09-08 14:49:05 +08:00
										 |  |  | 		hs.log.Warn("Access to requested plugin file will be forbidden in upcoming Grafana versions as the file "+ | 
					
						
							|  |  |  | 			"is not included in the plugin signature", "file", requestedFile) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-08 01:15:53 +08:00
										 |  |  | 	absPluginDir, err := filepath.Abs(plugin.PluginDir) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		c.JsonApiErr(500, "Failed to get plugin absolute path", nil) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pluginFilePath := filepath.Join(absPluginDir, rel) | 
					
						
							| 
									
										
										
										
											2021-04-21 21:17:23 +08:00
										 |  |  | 	// It's safe to ignore gosec warning G304 since we already clean the requested file path and subsequently
 | 
					
						
							|  |  |  | 	// use this with a prefix of the plugin's directory, which is set during plugin loading
 | 
					
						
							|  |  |  | 	// nolint:gosec
 | 
					
						
							|  |  |  | 	f, err := os.Open(pluginFilePath) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		if os.IsNotExist(err) { | 
					
						
							| 
									
										
										
										
											2021-07-15 14:56:11 +08:00
										 |  |  | 			c.JsonApiErr(404, "Plugin file not found", err) | 
					
						
							| 
									
										
										
										
											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() { | 
					
						
							|  |  |  | 		if err := f.Close(); err != nil { | 
					
						
							|  |  |  | 			hs.log.Error("Failed to close file", "err", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-01 17:18:30 +08:00
										 |  |  | 	http.ServeContent(c.Resp, c.Req, pluginFilePath, fi.ModTime(), f) | 
					
						
							| 
									
										
										
										
											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-06-07 23:51:00 +08:00
										 |  |  | 	err := hs.pluginManager.Add(c.Req.Context(), pluginID, dto.Version) | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		var versionUnsupportedErr installer.ErrVersionUnsupported | 
					
						
							|  |  |  | 		if errors.As(err, &versionUnsupportedErr) { | 
					
						
							|  |  |  | 			return response.Error(http.StatusConflict, "Plugin version not supported", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		var versionNotFoundErr installer.ErrVersionNotFound | 
					
						
							|  |  |  | 		if errors.As(err, &versionNotFoundErr) { | 
					
						
							|  |  |  | 			return response.Error(http.StatusNotFound, "Plugin version not found", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-07-13 15:58:46 +08:00
										 |  |  | 		var clientError installer.Response4xxError | 
					
						
							|  |  |  | 		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-06-07 23:51:00 +08:00
										 |  |  | 	err := hs.pluginManager.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) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if errors.Is(err, plugins.ErrUninstallOutsideOfPluginDir) { | 
					
						
							|  |  |  | 			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} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// nolint:gosec
 | 
					
						
							| 
									
										
										
										
											2021-12-10 23:10:04 +08:00
										 |  |  | 	// We can ignore the gosec G304 warning since we have cleaned the requested file path and subsequently
 | 
					
						
							|  |  |  | 	// use this with a prefix of the plugin's directory, which is set during plugin loading
 | 
					
						
							|  |  |  | 	path := filepath.Join(plugin.PluginDir, mdFilepath(strings.ToUpper(name))) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	exists, err := fs.Exists(path) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if !exists { | 
					
						
							| 
									
										
										
										
											2021-12-10 23:10:04 +08:00
										 |  |  | 		path = filepath.Join(plugin.PluginDir, mdFilepath(strings.ToLower(name))) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	exists, err = fs.Exists(path) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if !exists { | 
					
						
							|  |  |  | 		return make([]byte, 0), nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// nolint:gosec
 | 
					
						
							| 
									
										
										
										
											2021-12-10 23:10:04 +08:00
										 |  |  | 	// We can ignore the gosec G304 warning since we have cleaned the requested file path and subsequently
 | 
					
						
							|  |  |  | 	// use this with a prefix of the plugin's directory, which is set during plugin loading
 | 
					
						
							| 
									
										
										
										
											2022-08-10 21:37:51 +08:00
										 |  |  | 	data, err := os.ReadFile(path) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return data, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-12-10 23:10:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func mdFilepath(mdFilename string) string { | 
					
						
							|  |  |  | 	return filepath.Clean(filepath.Join("/", fmt.Sprintf("%s.md", mdFilename))) | 
					
						
							|  |  |  | } |