2016-02-25 07:55:31 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-12-02 06:46:55 -06:00
|
|
|
"bytes"
|
2021-11-17 05:04:22 -06:00
|
|
|
"context"
|
2020-04-21 09:16:41 -05:00
|
|
|
"encoding/json"
|
2020-03-13 06:31:44 -05:00
|
|
|
"errors"
|
2021-11-01 04:53:33 -05:00
|
|
|
"fmt"
|
2022-12-02 06:46:55 -06:00
|
|
|
"io"
|
2020-03-18 06:08:20 -05:00
|
|
|
"net/http"
|
2022-02-04 11:14:53 -06:00
|
|
|
"path"
|
2021-04-21 08:17:23 -05:00
|
|
|
"path/filepath"
|
2022-08-23 04:50:50 -05:00
|
|
|
"runtime"
|
2016-03-15 09:52:29 -05:00
|
|
|
"sort"
|
2021-11-01 04:53:33 -05:00
|
|
|
"strings"
|
2016-03-15 09:52:29 -05:00
|
|
|
|
2021-04-21 08:17:23 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2023-01-23 12:56:20 -06:00
|
|
|
|
2016-02-25 07:55:31 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2020-02-19 12:17:05 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-02-25 07:55:31 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2020-03-03 04:45:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
2022-08-23 04:50:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/repo"
|
|
|
|
"github.com/grafana/grafana/pkg/plugins/storage"
|
2022-09-09 02:44:50 -05:00
|
|
|
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
2022-10-28 06:33:37 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2022-09-09 02:44:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2022-03-18 14:49:13 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
2021-05-12 13:05:16 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2022-12-02 06:46:55 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2016-02-25 07:55:31 -06:00
|
|
|
)
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) GetPluginList(c *models.ReqContext) response.Response {
|
2016-03-08 11:17:47 -06:00
|
|
|
typeFilter := c.Query("type")
|
|
|
|
enabledFilter := c.Query("enabled")
|
|
|
|
embeddedFilter := c.Query("embedded")
|
2022-09-09 02:44:50 -05:00
|
|
|
// "" => no filter
|
|
|
|
// "0" => filter out core plugins
|
|
|
|
// "1" => filter out non-core plugins
|
2016-04-08 15:42:33 -05:00
|
|
|
coreFilter := c.Query("core")
|
2016-03-08 11:17:47 -06:00
|
|
|
|
2022-09-09 02:44:50 -05: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 03:30:26 -05:00
|
|
|
// Fallback to only letting admins list non-core plugins
|
2022-09-09 02:44:50 -05: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 09:40:46 -05:00
|
|
|
|
2022-08-11 06:28:55 -05:00
|
|
|
pluginSettingsMap, err := hs.pluginSettings(c.Req.Context(), c.OrgID)
|
2016-02-25 07:55:31 -06:00
|
|
|
if err != nil {
|
2022-09-09 02:44:50 -05:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to get list of plugins", err)
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
|
|
|
|
2022-09-09 02:44:50 -05:00
|
|
|
// Filter plugins
|
|
|
|
pluginDefinitions := hs.pluginStore.Plugins(c.Req.Context())
|
|
|
|
filteredPluginDefinitions := []plugins.PluginDTO{}
|
|
|
|
filteredPluginIDs := map[string]bool{}
|
|
|
|
for _, pluginDef := range pluginDefinitions {
|
2016-03-08 11:17:47 -06:00
|
|
|
// filter out app sub plugins
|
2021-11-01 04:53:33 -05:00
|
|
|
if embeddedFilter == "0" && pluginDef.IncludedInAppID != "" {
|
2016-03-08 11:17:47 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-04-08 15:42:33 -05:00
|
|
|
// filter out core plugins
|
2021-11-01 04:53:33 -05:00
|
|
|
if (coreFilter == "0" && pluginDef.IsCorePlugin()) || (coreFilter == "1" && !pluginDef.IsCorePlugin()) {
|
2016-04-08 15:42:33 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-09 02:44:50 -05: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-08 11:17:47 -06:00
|
|
|
// filter on type
|
2021-11-01 04:53:33 -05:00
|
|
|
if typeFilter != "" && typeFilter != string(pluginDef.Type) {
|
2016-03-08 04:29:36 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
if pluginDef.State == plugins.AlphaRelease && !hs.Cfg.PluginsEnableAlpha {
|
2018-11-15 04:10:47 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-09 02:44:50 -05: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 06:33:37 -05:00
|
|
|
if (pluginDef.ID == "parca" || pluginDef.ID == "phlare") && !hs.Features.IsEnabled(featuremgmt.FlagFlameGraph) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-09 02:44:50 -05: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 09:52:29 -05:00
|
|
|
listItem := dtos.PluginListItem{
|
2021-11-01 04:53:33 -05:00
|
|
|
Id: pluginDef.ID,
|
2016-04-11 11:47:04 -05:00
|
|
|
Name: pluginDef.Name,
|
2021-11-01 04:53:33 -05:00
|
|
|
Type: string(pluginDef.Type),
|
2019-05-09 04:45:39 -05:00
|
|
|
Category: pluginDef.Category,
|
2021-11-17 05:04:22 -06:00
|
|
|
Info: pluginDef.Info,
|
|
|
|
Dependencies: pluginDef.Dependencies,
|
2022-02-04 11:14:53 -06:00
|
|
|
DefaultNavUrl: path.Join(hs.Cfg.AppSubURL, pluginDef.DefaultNavURL),
|
2017-04-07 05:00:03 -05:00
|
|
|
State: pluginDef.State,
|
2020-04-09 02:00:16 -05:00
|
|
|
Signature: pluginDef.Signature,
|
2020-12-11 05:57:57 -06:00
|
|
|
SignatureType: pluginDef.SignatureType,
|
|
|
|
SignatureOrg: pluginDef.SignatureOrg,
|
2022-09-09 02:44:50 -05:00
|
|
|
AccessControl: pluginsMetadata[pluginDef.ID],
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
|
|
|
|
2022-01-31 09:06:16 -06:00
|
|
|
update, exists := hs.pluginsUpdateChecker.HasUpdate(c.Req.Context(), pluginDef.ID)
|
|
|
|
if exists {
|
|
|
|
listItem.LatestVersion = update
|
|
|
|
listItem.HasUpdate = true
|
|
|
|
}
|
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
if pluginSetting, exists := pluginSettingsMap[pluginDef.ID]; exists {
|
2016-02-25 07:55:31 -06:00
|
|
|
listItem.Enabled = pluginSetting.Enabled
|
|
|
|
listItem.Pinned = pluginSetting.Pinned
|
|
|
|
}
|
|
|
|
|
2016-05-03 12:00:42 -05:00
|
|
|
if listItem.DefaultNavUrl == "" || !listItem.Enabled {
|
2021-03-10 05:41:29 -06:00
|
|
|
listItem.DefaultNavUrl = hs.Cfg.AppSubURL + "/plugins/" + listItem.Id + "/"
|
2016-05-03 12:00:42 -05:00
|
|
|
}
|
|
|
|
|
2016-02-25 07:55:31 -06:00
|
|
|
result = append(result, listItem)
|
|
|
|
}
|
|
|
|
|
2016-03-15 09:52:29 -05:00
|
|
|
sort.Sort(result)
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, result)
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
func (hs *HTTPServer) GetPluginSettingByID(c *models.ReqContext) response.Response {
|
2021-10-11 07:30:59 -05:00
|
|
|
pluginID := web.Params(c.Req)[":pluginId"]
|
2016-02-25 07:55:31 -06:00
|
|
|
|
2021-11-17 05:04:22 -06:00
|
|
|
plugin, exists := hs.pluginStore.Plugin(c.Req.Context(), pluginID)
|
|
|
|
if !exists {
|
2022-07-08 06:24:09 -05: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 02:44:50 -05:00
|
|
|
hasAccess := ac.HasAccess(hs.AccessControl, c)
|
|
|
|
if !hasAccess(ac.ReqSignedIn,
|
|
|
|
ac.EvalPermission(plugins.ActionAppAccess, plugins.ScopeProvider.GetResourceScope(plugin.ID))) {
|
2022-07-08 06:24:09 -05:00
|
|
|
return response.Error(http.StatusForbidden, "Access Denied", nil)
|
|
|
|
}
|
2018-03-22 06:37:35 -05:00
|
|
|
}
|
2016-03-08 11:17:47 -06:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
dto := &dtos.PluginSetting{
|
2022-09-21 05:20:11 -05: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 05:04:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if plugin.IsApp() {
|
|
|
|
dto.Enabled = plugin.AutoEnabled
|
|
|
|
dto.Pinned = plugin.AutoEnabled
|
2021-02-25 03:00:21 -06:00
|
|
|
}
|
|
|
|
|
2022-03-18 14:49:13 -05:00
|
|
|
ps, err := hs.PluginSettings.GetPluginSettingByPluginID(c.Req.Context(), &pluginsettings.GetByPluginIDArgs{
|
|
|
|
PluginID: pluginID,
|
2022-08-11 06:28:55 -05:00
|
|
|
OrgID: c.OrgID,
|
2022-03-18 14:49:13 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-01-23 12:56:20 -06:00
|
|
|
if !errors.Is(err, pluginsettings.ErrPluginSettingNotFound) {
|
2022-03-18 14:49:13 -05:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to get plugin settings", nil)
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
2018-03-22 06:37:35 -05:00
|
|
|
} else {
|
2022-03-18 14:49:13 -05:00
|
|
|
dto.Enabled = ps.Enabled
|
|
|
|
dto.Pinned = ps.Pinned
|
|
|
|
dto.JsonData = ps.JSONData
|
2022-09-21 05:20:11 -05:00
|
|
|
|
|
|
|
for k, v := range hs.PluginSettings.DecryptedValues(ps) {
|
|
|
|
if len(v) > 0 {
|
|
|
|
dto.SecureJsonFields[k] = true
|
|
|
|
}
|
|
|
|
}
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
2018-03-22 06:37:35 -05:00
|
|
|
|
2022-01-31 09:06:16 -06:00
|
|
|
update, exists := hs.pluginsUpdateChecker.HasUpdate(c.Req.Context(), plugin.ID)
|
|
|
|
if exists {
|
|
|
|
dto.LatestVersion = update
|
|
|
|
dto.HasUpdate = true
|
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, dto)
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
|
|
|
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) UpdatePluginSetting(c *models.ReqContext) response.Response {
|
2023-01-23 12:56:20 -06:00
|
|
|
cmd := pluginsettings.UpdatePluginSettingCmd{}
|
2021-11-29 03:18:01 -06:00
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2021-10-11 07:30:59 -05:00
|
|
|
pluginID := web.Params(c.Req)[":pluginId"]
|
2016-02-25 07:55:31 -06:00
|
|
|
|
2021-11-17 05:04:22 -06:00
|
|
|
if _, exists := hs.pluginStore.Plugin(c.Req.Context(), pluginID); !exists {
|
2021-03-18 07:53:01 -05:00
|
|
|
return response.Error(404, "Plugin not installed", nil)
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
|
|
|
|
2022-08-11 06:28:55 -05:00
|
|
|
cmd.OrgId = c.OrgID
|
2021-03-18 07:53:01 -05:00
|
|
|
cmd.PluginId = pluginID
|
2022-03-18 14:49:13 -05: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 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update plugin setting", err)
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Plugin settings updated")
|
2016-02-25 07:55:31 -06:00
|
|
|
}
|
2016-03-11 02:57:20 -06:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
func (hs *HTTPServer) GetPluginMarkdown(c *models.ReqContext) response.Response {
|
2021-10-11 07:30:59 -05:00
|
|
|
pluginID := web.Params(c.Req)[":pluginId"]
|
|
|
|
name := web.Params(c.Req)[":name"]
|
2016-03-13 13:21:44 -05:00
|
|
|
|
2021-11-17 05:04:22 -06:00
|
|
|
content, err := hs.pluginMarkdown(c.Req.Context(), pluginID, name)
|
2018-03-22 06:37:35 -05:00
|
|
|
if err != nil {
|
2021-11-01 04:53:33 -05:00
|
|
|
var notFound plugins.NotFoundError
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.As(err, ¬Found) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, notFound.Error(), nil)
|
2016-03-13 13:21:44 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Could not get markdown file", err)
|
2016-03-13 13:21:44 -05:00
|
|
|
}
|
2018-03-22 06:37:35 -05:00
|
|
|
|
2018-12-19 03:53:14 -06:00
|
|
|
// fallback try readme
|
|
|
|
if len(content) == 0 {
|
2023-01-03 07:20:27 -06:00
|
|
|
content, err = hs.pluginMarkdown(c.Req.Context(), pluginID, "readme")
|
2018-12-19 03:53:14 -06:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(501, "Could not get markdown file", err)
|
2018-12-19 03:53:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
resp := response.Respond(http.StatusOK, content)
|
2021-03-17 12:12:28 -05:00
|
|
|
resp.SetHeader("Content-Type", "text/plain; charset=utf-8")
|
2018-03-22 06:37:35 -05:00
|
|
|
return resp
|
2016-03-13 13:21:44 -05:00
|
|
|
}
|
|
|
|
|
2020-03-18 06:08:20 -05:00
|
|
|
// CollectPluginMetrics collect metrics from a plugin.
|
|
|
|
//
|
|
|
|
// /api/plugins/:pluginId/metrics
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) CollectPluginMetrics(c *models.ReqContext) response.Response {
|
2021-10-11 07:30:59 -05:00
|
|
|
pluginID := web.Params(c.Req)[":pluginId"]
|
2022-02-09 10:36:53 -06:00
|
|
|
resp, err := hs.pluginClient.CollectMetrics(c.Req.Context(), &backend.CollectMetricsRequest{PluginContext: backend.PluginContext{PluginID: pluginID}})
|
2020-03-18 06:08:20 -05:00
|
|
|
if err != nil {
|
2020-06-11 09:14:05 -05:00
|
|
|
return translatePluginRequestErrorToAPIError(err)
|
2020-03-18 06:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
headers := make(http.Header)
|
|
|
|
headers.Set("Content-Type", "text/plain")
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.CreateNormalResponse(headers, resp.PrometheusMetrics, http.StatusOK)
|
2020-03-18 06:08:20 -05:00
|
|
|
}
|
|
|
|
|
2021-09-08 01:49:05 -05:00
|
|
|
// getPluginAssets returns public plugin assets (images, JS, etc.)
|
2021-04-21 08:17:23 -05:00
|
|
|
//
|
|
|
|
// /public/plugins/:pluginId/*
|
2021-09-08 01:49:05 -05:00
|
|
|
func (hs *HTTPServer) getPluginAssets(c *models.ReqContext) {
|
2021-10-11 07:30:59 -05:00
|
|
|
pluginID := web.Params(c.Req)[":pluginId"]
|
2021-11-17 05:04:22 -06:00
|
|
|
plugin, exists := hs.pluginStore.Plugin(c.Req.Context(), pluginID)
|
|
|
|
if !exists {
|
2021-07-15 01:56:11 -05:00
|
|
|
c.JsonApiErr(404, "Plugin not found", nil)
|
2021-07-14 02:38:49 -05:00
|
|
|
return
|
2021-04-21 08:17:23 -05:00
|
|
|
}
|
|
|
|
|
2021-12-07 11:15:53 -06:00
|
|
|
// prepend slash for cleaning relative paths
|
2022-12-02 06:46:55 -06:00
|
|
|
requestedFile, err := util.CleanRelativePath(web.Params(c.Req)["*"])
|
2021-12-07 11:15:53 -06:00
|
|
|
if err != nil {
|
|
|
|
// slash is prepended above therefore this is not expected to fail
|
2022-12-02 06:46:55 -06:00
|
|
|
c.JsonApiErr(500, "Failed to clean relative file path", err)
|
2021-12-07 11:15:53 -06:00
|
|
|
return
|
|
|
|
}
|
2021-04-21 08:17:23 -05:00
|
|
|
|
2022-12-02 06:46:55 -06:00
|
|
|
f, err := plugin.File(requestedFile)
|
2021-04-21 08:17:23 -05:00
|
|
|
if err != nil {
|
2022-12-02 06:46:55 -06:00
|
|
|
if errors.Is(err, plugins.ErrFileNotExist) {
|
|
|
|
c.JsonApiErr(404, "Plugin file not found", nil)
|
2021-07-14 02:38:49 -05:00
|
|
|
return
|
2021-04-21 08:17:23 -05:00
|
|
|
}
|
2021-07-15 01:56:11 -05:00
|
|
|
c.JsonApiErr(500, "Could not open plugin file", err)
|
2021-07-14 02:38:49 -05:00
|
|
|
return
|
2021-04-21 08:17:23 -05:00
|
|
|
}
|
|
|
|
defer func() {
|
2022-12-02 06:46:55 -06:00
|
|
|
if err = f.Close(); err != nil {
|
|
|
|
hs.log.Error("Failed to close plugin file", "err", err)
|
2021-04-21 08:17:23 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
2021-07-15 01:56:11 -05:00
|
|
|
c.JsonApiErr(500, "Plugin file exists but could not open", err)
|
2021-07-14 02:38:49 -05:00
|
|
|
return
|
2021-04-21 08:17:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if hs.Cfg.Env == setting.Dev {
|
2021-07-14 02:38:49 -05: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 08:17:23 -05:00
|
|
|
}
|
|
|
|
|
2022-12-02 06:46:55 -06: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 08:17:23 -05:00
|
|
|
}
|
|
|
|
|
2020-03-06 07:37:36 -06:00
|
|
|
// CheckHealth returns the health of a plugin.
|
2020-01-31 04:15:50 -06:00
|
|
|
// /api/plugins/:pluginId/health
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) CheckHealth(c *models.ReqContext) response.Response {
|
2021-10-11 07:30:59 -05:00
|
|
|
pluginID := web.Params(c.Req)[":pluginId"]
|
2020-03-13 06:31:44 -05:00
|
|
|
|
2022-05-06 03:58:02 -05:00
|
|
|
pCtx, found, err := hs.PluginContextProvider.Get(c.Req.Context(), pluginID, c.SignedInUser)
|
2020-03-13 06:31:44 -05:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get plugin settings", err)
|
2020-03-13 06:31:44 -05:00
|
|
|
}
|
2021-03-23 12:24:08 -05:00
|
|
|
if !found {
|
|
|
|
return response.Error(404, "Plugin not found", nil)
|
|
|
|
}
|
2020-03-13 06:31:44 -05:00
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
resp, err := hs.pluginClient.CheckHealth(c.Req.Context(), &backend.CheckHealthRequest{
|
|
|
|
PluginContext: pCtx,
|
2022-05-31 10:58:06 -05:00
|
|
|
Headers: map[string]string{},
|
2021-11-01 04:53:33 -05:00
|
|
|
})
|
2020-01-31 04:15:50 -06:00
|
|
|
if err != nil {
|
2020-06-11 09:14:05 -05:00
|
|
|
return translatePluginRequestErrorToAPIError(err)
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
payload := map[string]interface{}{
|
2020-04-21 09:16:41 -05: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 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to unmarshal detailed response from backend plugin", err)
|
2020-04-21 09:16:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
payload["details"] = jsonDetails
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:14:05 -05:00
|
|
|
if resp.Status != backend.HealthStatusOk {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(503, payload)
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, payload)
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
|
|
|
|
2021-03-23 12:24:08 -05:00
|
|
|
func (hs *HTTPServer) GetPluginErrorsList(_ *models.ReqContext) response.Response {
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, hs.pluginErrorResolver.PluginErrors())
|
2020-10-23 09:45:43 -05:00
|
|
|
}
|
|
|
|
|
2021-11-29 03:18:01 -06: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 07:30:59 -05:00
|
|
|
pluginID := web.Params(c.Req)[":pluginId"]
|
2021-05-12 13:05:16 -05:00
|
|
|
|
2022-09-23 07:27:01 -05:00
|
|
|
err := hs.pluginInstaller.Add(c.Req.Context(), pluginID, dto.Version, plugins.CompatOpts{
|
2022-08-23 04:50:50 -05:00
|
|
|
GrafanaVersion: hs.Cfg.BuildVersion,
|
|
|
|
OS: runtime.GOOS,
|
|
|
|
Arch: runtime.GOARCH,
|
|
|
|
})
|
2021-05-12 13:05:16 -05:00
|
|
|
if err != nil {
|
2021-11-01 04:53:33 -05:00
|
|
|
var dupeErr plugins.DuplicateError
|
2021-05-12 13:05:16 -05:00
|
|
|
if errors.As(err, &dupeErr) {
|
|
|
|
return response.Error(http.StatusConflict, "Plugin already installed", err)
|
|
|
|
}
|
2022-08-23 04:50:50 -05:00
|
|
|
var versionUnsupportedErr repo.ErrVersionUnsupported
|
2021-05-12 13:05:16 -05:00
|
|
|
if errors.As(err, &versionUnsupportedErr) {
|
|
|
|
return response.Error(http.StatusConflict, "Plugin version not supported", err)
|
|
|
|
}
|
2022-08-23 04:50:50 -05:00
|
|
|
var versionNotFoundErr repo.ErrVersionNotFound
|
2021-05-12 13:05:16 -05:00
|
|
|
if errors.As(err, &versionNotFoundErr) {
|
|
|
|
return response.Error(http.StatusNotFound, "Plugin version not found", err)
|
|
|
|
}
|
2022-08-23 04:50:50 -05:00
|
|
|
var clientError repo.Response4xxError
|
2021-07-13 02:58:46 -05:00
|
|
|
if errors.As(err, &clientError) {
|
|
|
|
return response.Error(clientError.StatusCode, clientError.Message, err)
|
2021-05-12 13:05:16 -05: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 07:30:59 -05:00
|
|
|
pluginID := web.Params(c.Req)[":pluginId"]
|
2021-05-12 13:05:16 -05:00
|
|
|
|
2022-09-23 07:27:01 -05:00
|
|
|
err := hs.pluginInstaller.Remove(c.Req.Context(), pluginID)
|
2021-05-12 13:05:16 -05: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 04:50:50 -05:00
|
|
|
if errors.Is(err, storage.ErrUninstallOutsideOfPluginDir) {
|
2021-05-12 13:05:16 -05: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 07:43:20 -06:00
|
|
|
func translatePluginRequestErrorToAPIError(err error) response.Response {
|
2020-06-11 09:14:05 -05:00
|
|
|
if errors.Is(err, backendplugin.ErrPluginNotRegistered) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Plugin not found", err)
|
2020-06-11 09:14:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, backendplugin.ErrMethodNotImplemented) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Not found", err)
|
2020-06-11 09:14:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, backendplugin.ErrHealthCheckFailed) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Plugin health check failed", err)
|
2020-06-11 09:14:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, backendplugin.ErrPluginUnavailable) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(503, "Plugin unavailable", err)
|
2020-06-11 09:14:05 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Plugin request failed", err)
|
2020-06-11 09:14:05 -05:00
|
|
|
}
|
2021-11-01 04:53:33 -05:00
|
|
|
|
2021-11-17 05:04:22 -06: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 04:53:33 -05:00
|
|
|
return nil, plugins.NotFoundError{PluginID: pluginId}
|
|
|
|
}
|
|
|
|
|
2022-12-02 06:46:55 -06:00
|
|
|
md, err := plugin.File(mdFilepath(strings.ToUpper(name)))
|
2021-11-01 04:53:33 -05:00
|
|
|
if err != nil {
|
2022-12-02 06:46:55 -06:00
|
|
|
md, err = plugin.File(mdFilepath(strings.ToUpper(name)))
|
|
|
|
if err != nil {
|
|
|
|
return make([]byte, 0), nil
|
|
|
|
}
|
2021-11-01 04:53:33 -05:00
|
|
|
}
|
2022-12-02 06:46:55 -06:00
|
|
|
defer func() {
|
|
|
|
if err = md.Close(); err != nil {
|
|
|
|
hs.log.Error("Failed to close plugin markdown file", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-11-01 04:53:33 -05:00
|
|
|
|
2022-12-02 06:46:55 -06:00
|
|
|
d, err := io.ReadAll(md)
|
2021-11-01 04:53:33 -05:00
|
|
|
if err != nil {
|
|
|
|
return make([]byte, 0), nil
|
|
|
|
}
|
2022-12-02 06:46:55 -06:00
|
|
|
return d, nil
|
2021-11-01 04:53:33 -05:00
|
|
|
}
|
2021-12-10 09:10:04 -06:00
|
|
|
|
|
|
|
func mdFilepath(mdFilename string) string {
|
|
|
|
return filepath.Clean(filepath.Join("/", fmt.Sprintf("%s.md", mdFilename)))
|
|
|
|
}
|