mirror of
https://github.com/grafana/grafana.git
synced 2025-09-19 21:24:56 +08:00
Plugins: Allow loading panel plugins from a CDN (#59096)
* POC: Plugins CDN reverse proxy * CDN proxy POC: changed env var names * Add authorization: false for /public path in frontend plugin loader * Moved CDN settings to Cfg, add some comments * Fix error 500 in asset fetch if plugin is not using CDN * Fix EnterpriseLicensePath declared twice * Fix linter complaining about whitespaces * Plugins CDN: Skip signature verification for CDN plugins * Plugins CDN: Skip manifest and signature check for cdn plugins * Plugins: use IsValid() and IsInternal() rather than equality checks * Plugins CDN: remove comment * Plugins CDN: Fix seeker can't seek when serving plugins from local fs * Plugins CDN: add back error codes in getLocalPluginAssets * Plugins CDN: call asset.Close() rather than asset.readSeekCloser.Close() * Plugins CDN: Fix panic in JsonApiErr when errorMessageCoder wraps a nil error * Plugins CDN: Add error handling to proxyCDNPluginAsset * Plugins CDN: replace errorMessageCoder with errutil * Plugins CDN POC: expose cdn plugin paths to frontend for system.js * Plugins CDN: Fix cdn plugins showing as unsigned in frontend * WIP: Add support for formatted URL * Fix missing cdnPluginsBaseURLs in GrafanaConfig * Plugins CDN: Remove reverse proxy mode and reverse proxy references * Plugins CDN: Simplify asset serving logic * Plugins CDN: sanitize redirect path * Plugins CDN: Removed unused pluginAsset type * Plugins CDN: Removed system.js changes * Plugins CDN: Return different system.js baseURL and module for cdn plugins * Plugins CDN: Ensure CDN is disabled for non-external plugins * lint * Plugins CDN: serve images and screenshots from CDN, refactoring * Lint * Plugins CDN: Fix URLs for system.js (baseUrl and module) * Plugins CDN: Add more tests for RelativeURLForSystemJS * Plugins CDN: Iterate only on apps when preloading * Plugins CDN: Refactoring * Plugins CDN: Add comments to url_constructor.go * Plugins CDN: Update defaultHGPluginsCDNBaseURL * Plugins CDN: undo extract meta from system js config * refactor(plugins): migrate systemjs css plugin to typescript * feat(plugins): introduce systemjs cdn loader plugin * feat(plugins): add systemjs load type * Plugins CDN: Removed RelativeURLForSystemJS * Plugins CDN: Log backend redirect hits along with plugin info * Plugins CDN: Add pluginsCDNBasePath to getFrontendSettingsMap * feat(plugins): introduce cdn loading for angular plugins * refactor(plugins): move systemjs cache buster into systemjsplugins directory * Plugins CDN: Rename pluginsCDNBasePath to pluginsCDNBaseURL * refactor(plugins): introduce pluginsCDNBaseURL to the frontend * Plugins CDN: Renamed "cdn base path" to "cdn url template" in backend * Plugins CDN: lint * merge with main * Instrumentation: Add prometheus counter for backend hits, log from Info to Warn * Config: Changed key from plugins_cdn.url to plugins.plugins_cdn_base_url * CDN: Add backend tests * Lint: goimports * Default CDN URL to empty string, * Do not use CDN in setImages and module if the url template is empty * CDN: Backend: Add test for frontend settings * CDN: Do not log missing module.js warn if plugin is being loaded from CDN * CDN: Add backend test for CDN plugin loader * Removed 'cdn' signature level, switch to 'valid' * Fix pfs.TestParseTreeTestdata for cdn plugin testdata dir * Fix TestLoader_Load * Fix gocyclo complexity of loadPlugins * Plugins CDN: Moved prometheus metric to api package, removed asset_path label * Fix missing in config * Changes after review * Add pluginscdn.Service * Fix tests * Refactoring * Moved all remaining CDN checks inside pluginscdn.Service * CDN url constructor: Renamed stringURLFor to stringPath * CDN: Moved asset URL functionality to assetpath service * CDN: Renamed HasCDN() to IsEnabled() * CDN: Replace assert with require * CDN: Changes after review * Assetpath: Handle url.Parse error * Fix plugin_resource_test * CDN: Change fallback redirect from 302 to 307 * goimports * Fix tests * Switch to contextmodel.ReqContext in plugins.go Co-authored-by: Will Browne <will.browne@grafana.com> Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
This commit is contained in:
@ -12,7 +12,9 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
dto "github.com/prometheus/client_model/go"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
@ -21,6 +23,8 @@ import (
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/infra/log/logtest"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/config"
|
||||
"github.com/grafana/grafana/pkg/plugins/pluginscdn"
|
||||
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
@ -139,7 +143,7 @@ func Test_PluginsInstallAndUninstall_AccessControl(t *testing.T) {
|
||||
req := webtest.RequestWithSignedInUser(server.NewPostRequest("/api/plugins/test/install", input), userWithPermissions(1, tc.permissions))
|
||||
res, err := server.SendJSON(req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tc.expectedCode, res.StatusCode)
|
||||
require.Equal(t, tc.expectedCode, res.StatusCode)
|
||||
require.NoError(t, res.Body.Close())
|
||||
})
|
||||
|
||||
@ -148,12 +152,105 @@ func Test_PluginsInstallAndUninstall_AccessControl(t *testing.T) {
|
||||
req := webtest.RequestWithSignedInUser(server.NewPostRequest("/api/plugins/test/uninstall", input), userWithPermissions(1, tc.permissions))
|
||||
res, err := server.SendJSON(req)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tc.expectedCode, res.StatusCode)
|
||||
require.Equal(t, tc.expectedCode, res.StatusCode)
|
||||
require.NoError(t, res.Body.Close())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_GetPluginAssetCDNRedirect(t *testing.T) {
|
||||
const cdnPluginID = "cdn-plugin"
|
||||
const nonCDNPluginID = "non-cdn-plugin"
|
||||
t.Run("Plugin CDN asset redirect", func(t *testing.T) {
|
||||
cdnPlugin := &plugins.Plugin{
|
||||
JSONData: plugins.JSONData{ID: cdnPluginID, Info: plugins.Info{Version: "1.0.0"}},
|
||||
}
|
||||
nonCdnPlugin := &plugins.Plugin{
|
||||
JSONData: plugins.JSONData{ID: nonCDNPluginID, Info: plugins.Info{Version: "2.0.0"}},
|
||||
}
|
||||
service := &plugins.FakePluginStore{
|
||||
PluginList: []plugins.PluginDTO{
|
||||
cdnPlugin.ToDTO(),
|
||||
nonCdnPlugin.ToDTO(),
|
||||
},
|
||||
}
|
||||
cfg := setting.NewCfg()
|
||||
cfg.PluginsCDNURLTemplate = "https://cdn.example.com/{id}/{version}/public/plugins/{id}/{assetPath}"
|
||||
cfg.PluginSettings = map[string]map[string]string{
|
||||
cdnPluginID: {"cdn": "true"},
|
||||
}
|
||||
|
||||
const cdnFolderBaseURL = "https://cdn.example.com/cdn-plugin/1.0.0/public/plugins/cdn-plugin"
|
||||
|
||||
type tc struct {
|
||||
assetURL string
|
||||
expRelativeURL string
|
||||
}
|
||||
for _, cas := range []tc{
|
||||
{"module.js", "module.js"},
|
||||
{"other/folder/file.js", "other/folder/file.js"},
|
||||
{"double////slashes/file.js", "double/slashes/file.js"},
|
||||
} {
|
||||
pluginAssetScenario(
|
||||
t,
|
||||
"When calling GET for a CDN plugin on",
|
||||
fmt.Sprintf("/public/plugins/%s/%s", cdnPluginID, cas.assetURL),
|
||||
"/public/plugins/:pluginId/*",
|
||||
cfg, service, func(sc *scenarioContext) {
|
||||
// Get the prometheus metric (to test that the handler is instrumented correctly)
|
||||
counter := pluginsCDNFallbackRedirectRequests.With(prometheus.Labels{
|
||||
"plugin_id": cdnPluginID,
|
||||
"plugin_version": "1.0.0",
|
||||
})
|
||||
|
||||
// Encode the prometheus metric and get its value
|
||||
var m dto.Metric
|
||||
require.NoError(t, counter.Write(&m))
|
||||
before := m.Counter.GetValue()
|
||||
|
||||
// Call handler
|
||||
callGetPluginAsset(sc)
|
||||
|
||||
// Check redirect code + location
|
||||
require.Equal(t, http.StatusTemporaryRedirect, sc.resp.Code, "wrong status code")
|
||||
require.Equal(t, cdnFolderBaseURL+"/"+cas.expRelativeURL, sc.resp.Header().Get("Location"), "wrong location header")
|
||||
|
||||
// Check metric
|
||||
require.NoError(t, counter.Write(&m))
|
||||
require.Equal(t, before+1, m.Counter.GetValue(), "prometheus metric not incremented")
|
||||
},
|
||||
)
|
||||
}
|
||||
pluginAssetScenario(
|
||||
t,
|
||||
"When calling GET for a non-CDN plugin on",
|
||||
fmt.Sprintf("/public/plugins/%s/%s", nonCDNPluginID, "module.js"),
|
||||
"/public/plugins/:pluginId/*",
|
||||
cfg, service, func(sc *scenarioContext) {
|
||||
// Here the metric should not increment
|
||||
var m dto.Metric
|
||||
counter := pluginsCDNFallbackRedirectRequests.With(prometheus.Labels{
|
||||
"plugin_id": nonCDNPluginID,
|
||||
"plugin_version": "2.0.0",
|
||||
})
|
||||
require.NoError(t, counter.Write(&m))
|
||||
require.Zero(t, m.Counter.GetValue())
|
||||
|
||||
// Call handler
|
||||
callGetPluginAsset(sc)
|
||||
|
||||
// 404 implies access to fs
|
||||
require.Equal(t, http.StatusNotFound, sc.resp.Code)
|
||||
require.Empty(t, sc.resp.Header().Get("Location"))
|
||||
|
||||
// Ensure the metric did not change
|
||||
require.NoError(t, counter.Write(&m))
|
||||
require.Zero(t, m.Counter.GetValue())
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_GetPluginAssets(t *testing.T) {
|
||||
pluginID := "test-plugin"
|
||||
pluginDir := "."
|
||||
@ -185,8 +282,8 @@ func Test_GetPluginAssets(t *testing.T) {
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("/public/plugins/%s/%s", pluginID, requestedFile)
|
||||
pluginAssetScenario(t, "When calling GET on", url, "/public/plugins/:pluginId/*", service,
|
||||
func(sc *scenarioContext) {
|
||||
pluginAssetScenario(t, "When calling GET on", url, "/public/plugins/:pluginId/*",
|
||||
setting.NewCfg(), service, func(sc *scenarioContext) {
|
||||
callGetPluginAsset(sc)
|
||||
|
||||
require.Equal(t, 200, sc.resp.Code)
|
||||
@ -201,8 +298,8 @@ func Test_GetPluginAssets(t *testing.T) {
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("/public/plugins/%s/%s", pluginID, tmpFileInParentDir.Name())
|
||||
pluginAssetScenario(t, "When calling GET on", url, "/public/plugins/:pluginId/*", service,
|
||||
func(sc *scenarioContext) {
|
||||
pluginAssetScenario(t, "When calling GET on", url, "/public/plugins/:pluginId/*",
|
||||
setting.NewCfg(), service, func(sc *scenarioContext) {
|
||||
callGetPluginAsset(sc)
|
||||
|
||||
require.Equal(t, 404, sc.resp.Code)
|
||||
@ -217,8 +314,8 @@ func Test_GetPluginAssets(t *testing.T) {
|
||||
|
||||
requestedFile := "nonExistent"
|
||||
url := fmt.Sprintf("/public/plugins/%s/%s", pluginID, requestedFile)
|
||||
pluginAssetScenario(t, "When calling GET on", url, "/public/plugins/:pluginId/*", service,
|
||||
func(sc *scenarioContext) {
|
||||
pluginAssetScenario(t, "When calling GET on", url, "/public/plugins/:pluginId/*",
|
||||
setting.NewCfg(), service, func(sc *scenarioContext) {
|
||||
callGetPluginAsset(sc)
|
||||
|
||||
var respJson map[string]interface{}
|
||||
@ -237,8 +334,8 @@ func Test_GetPluginAssets(t *testing.T) {
|
||||
|
||||
requestedFile := "nonExistent"
|
||||
url := fmt.Sprintf("/public/plugins/%s/%s", pluginID, requestedFile)
|
||||
pluginAssetScenario(t, "When calling GET on", url, "/public/plugins/:pluginId/*", service,
|
||||
func(sc *scenarioContext) {
|
||||
pluginAssetScenario(t, "When calling GET on", url, "/public/plugins/:pluginId/*",
|
||||
setting.NewCfg(), service, func(sc *scenarioContext) {
|
||||
callGetPluginAsset(sc)
|
||||
|
||||
var respJson map[string]interface{}
|
||||
@ -262,8 +359,8 @@ func Test_GetPluginAssets(t *testing.T) {
|
||||
l := &logtest.Fake{}
|
||||
|
||||
url := fmt.Sprintf("/public/plugins/%s/%s", pluginID, requestedFile)
|
||||
pluginAssetScenario(t, "When calling GET on", url, "/public/plugins/:pluginId/*", service,
|
||||
func(sc *scenarioContext) {
|
||||
pluginAssetScenario(t, "When calling GET on", url, "/public/plugins/:pluginId/*",
|
||||
setting.NewCfg(), service, func(sc *scenarioContext) {
|
||||
callGetPluginAsset(sc)
|
||||
|
||||
require.Equal(t, 200, sc.resp.Code)
|
||||
@ -383,12 +480,18 @@ func callGetPluginAsset(sc *scenarioContext) {
|
||||
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
||||
}
|
||||
|
||||
func pluginAssetScenario(t *testing.T, desc string, url string, urlPattern string, pluginStore plugins.Store,
|
||||
fn scenarioFunc) {
|
||||
func pluginAssetScenario(t *testing.T, desc string, url string, urlPattern string,
|
||||
cfg *setting.Cfg, pluginStore plugins.Store, fn scenarioFunc) {
|
||||
t.Run(fmt.Sprintf("%s %s", desc, url), func(t *testing.T) {
|
||||
cfg.IsFeatureToggleEnabled = func(_ string) bool { return false }
|
||||
hs := HTTPServer{
|
||||
Cfg: setting.NewCfg(),
|
||||
Cfg: cfg,
|
||||
pluginStore: pluginStore,
|
||||
log: log.NewNopLogger(),
|
||||
pluginsCDNService: pluginscdn.ProvideService(&config.Cfg{
|
||||
PluginsCDNURLTemplate: cfg.PluginsCDNURLTemplate,
|
||||
PluginSettings: cfg.PluginSettings,
|
||||
}),
|
||||
}
|
||||
|
||||
sc := setupScenarioContext(t, url)
|
||||
|
Reference in New Issue
Block a user