mirror of
https://github.com/grafana/grafana.git
synced 2025-02-14 09:33:34 -06:00
* 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>
197 lines
5.4 KiB
Go
197 lines
5.4 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/grafana/grafana/pkg/login/social"
|
|
"github.com/grafana/grafana/pkg/plugins/config"
|
|
"github.com/grafana/grafana/pkg/plugins/pluginscdn"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
accesscontrolmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
|
"github.com/grafana/grafana/pkg/services/licensing"
|
|
pluginSettings "github.com/grafana/grafana/pkg/services/pluginsettings/service"
|
|
"github.com/grafana/grafana/pkg/services/rendering"
|
|
"github.com/grafana/grafana/pkg/services/secrets/fakes"
|
|
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
|
|
"github.com/grafana/grafana/pkg/services/updatechecker"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
"github.com/grafana/grafana/pkg/web"
|
|
)
|
|
|
|
func setupTestEnvironment(t *testing.T, cfg *setting.Cfg, features *featuremgmt.FeatureManager) (*web.Mux, *HTTPServer) {
|
|
t.Helper()
|
|
db.InitTestDB(t)
|
|
cfg.IsFeatureToggleEnabled = features.IsEnabled
|
|
|
|
{
|
|
oldVersion := setting.BuildVersion
|
|
oldCommit := setting.BuildCommit
|
|
oldEnv := setting.Env
|
|
t.Cleanup(func() {
|
|
setting.BuildVersion = oldVersion
|
|
setting.BuildCommit = oldCommit
|
|
setting.Env = oldEnv
|
|
})
|
|
}
|
|
|
|
sqlStore := db.InitTestDB(t)
|
|
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())
|
|
|
|
hs := &HTTPServer{
|
|
Cfg: cfg,
|
|
Features: features,
|
|
License: &licensing.OSSLicensingService{Cfg: cfg},
|
|
RenderService: &rendering.RenderingService{
|
|
Cfg: cfg,
|
|
RendererPluginManager: &fakeRendererManager{},
|
|
},
|
|
SQLStore: sqlStore,
|
|
SettingsProvider: setting.ProvideProvider(cfg),
|
|
pluginStore: &plugins.FakePluginStore{},
|
|
grafanaUpdateChecker: &updatechecker.GrafanaService{},
|
|
AccessControl: accesscontrolmock.New().WithDisabled(),
|
|
PluginSettings: pluginSettings.ProvideService(sqlStore, secretsService),
|
|
pluginsCDNService: pluginscdn.ProvideService(&config.Cfg{
|
|
PluginsCDNURLTemplate: cfg.PluginsCDNURLTemplate,
|
|
PluginSettings: cfg.PluginSettings,
|
|
}),
|
|
SocialService: social.ProvideService(cfg, features),
|
|
}
|
|
|
|
m := web.New()
|
|
m.Use(getContextHandler(t, cfg).Middleware)
|
|
m.UseMiddleware(web.Renderer(filepath.Join(setting.StaticRootPath, "views"), "[[", "]]"))
|
|
m.Get("/api/frontend/settings/", hs.GetFrontendSettings)
|
|
|
|
return m, hs
|
|
}
|
|
|
|
func TestHTTPServer_GetFrontendSettings_hideVersionAnonymous(t *testing.T) {
|
|
type buildInfo struct {
|
|
Version string `json:"version"`
|
|
Commit string `json:"commit"`
|
|
Env string `json:"env"`
|
|
}
|
|
type settings struct {
|
|
BuildInfo buildInfo `json:"buildInfo"`
|
|
}
|
|
|
|
cfg := setting.NewCfg()
|
|
cfg.Env = "testing"
|
|
cfg.BuildVersion = "7.8.9"
|
|
cfg.BuildCommit = "01234567"
|
|
|
|
m, hs := setupTestEnvironment(t, cfg, featuremgmt.WithFeatures())
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/frontend/settings", nil)
|
|
|
|
// TODO: Remove
|
|
setting.BuildVersion = cfg.BuildVersion
|
|
setting.BuildCommit = cfg.BuildCommit
|
|
setting.Env = cfg.Env
|
|
|
|
tests := []struct {
|
|
desc string
|
|
hideVersion bool
|
|
expected settings
|
|
}{
|
|
{
|
|
desc: "Not hiding version",
|
|
hideVersion: false,
|
|
expected: settings{
|
|
BuildInfo: buildInfo{
|
|
Version: setting.BuildVersion,
|
|
Commit: setting.BuildCommit,
|
|
Env: setting.Env,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
desc: "Hiding version",
|
|
hideVersion: true,
|
|
expected: settings{
|
|
BuildInfo: buildInfo{
|
|
Version: "",
|
|
Commit: "",
|
|
Env: setting.Env,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
hs.Cfg.AnonymousHideVersion = test.hideVersion
|
|
expected := test.expected
|
|
|
|
recorder := httptest.NewRecorder()
|
|
m.ServeHTTP(recorder, req)
|
|
got := settings{}
|
|
err := json.Unmarshal(recorder.Body.Bytes(), &got)
|
|
require.NoError(t, err)
|
|
require.GreaterOrEqual(t, 400, recorder.Code, "status codes higher than 400 indicate a failure")
|
|
|
|
assert.EqualValues(t, expected, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHTTPServer_GetFrontendSettings_pluginsCDNBaseURL(t *testing.T) {
|
|
type settings struct {
|
|
PluginsCDNBaseURL string `json:"pluginsCDNBaseURL"`
|
|
}
|
|
|
|
tests := []struct {
|
|
desc string
|
|
mutateCfg func(*setting.Cfg)
|
|
expected settings
|
|
}{
|
|
{
|
|
desc: "With CDN",
|
|
mutateCfg: func(cfg *setting.Cfg) {
|
|
cfg.PluginsCDNURLTemplate = "https://cdn.example.com/{id}/{version}/public/plugins/{id}/{assetPath}"
|
|
},
|
|
expected: settings{PluginsCDNBaseURL: "https://cdn.example.com"},
|
|
},
|
|
{
|
|
desc: "Without CDN",
|
|
mutateCfg: func(cfg *setting.Cfg) {
|
|
cfg.PluginsCDNURLTemplate = ""
|
|
},
|
|
expected: settings{PluginsCDNBaseURL: ""},
|
|
},
|
|
{
|
|
desc: "CDN is disabled by default",
|
|
expected: settings{PluginsCDNBaseURL: ""},
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
cfg := setting.NewCfg()
|
|
if test.mutateCfg != nil {
|
|
test.mutateCfg(cfg)
|
|
}
|
|
m, _ := setupTestEnvironment(t, cfg, featuremgmt.WithFeatures())
|
|
req := httptest.NewRequest(http.MethodGet, "/api/frontend/settings", nil)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
m.ServeHTTP(recorder, req)
|
|
var got settings
|
|
err := json.Unmarshal(recorder.Body.Bytes(), &got)
|
|
require.NoError(t, err)
|
|
require.Equal(t, http.StatusOK, recorder.Code)
|
|
require.EqualValues(t, test.expected, got)
|
|
})
|
|
}
|
|
}
|