[v10.2.x] Plugins: Don't auto prepend app sub url to plugin asset paths (#82146)
Plugins: Don't auto prepend app sub url to plugin asset paths (#81658)
* don't prepend app sub url to paths
* simplify logo path
* fix(plugins): dynamically prepend appSubUrl for System module resolving to work
* fix(sandbox): support dynamic appSuburl prepend when loading plugin module.js
* fix tests
* update test name
* fix tests
* update fe + add some tests
* refactor(plugins): move wrangleurl to utils, rename to resolveModulePath, update usage
* chore: fix a typo
* test(plugins): add missing name to utils test
* reset test flag
---------
Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
(cherry picked from commit 99feb928cf)
Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>
This commit is contained in:
co-authored by
Will Browne
parent
147e7b0613
commit
dfb6c28197
@@ -47,12 +47,12 @@ func DefaultService(cfg *config.Cfg) *Service {
|
||||
func (s *Service) Base(n PluginInfo) (string, error) {
|
||||
if n.class == plugins.ClassCore {
|
||||
baseDir := getBaseDir(n.dir)
|
||||
return path.Join("/", s.cfg.GrafanaAppSubURL, "/public/app/plugins", string(n.pluginJSON.Type), baseDir), nil
|
||||
return path.Join("public/app/plugins", string(n.pluginJSON.Type), baseDir), nil
|
||||
}
|
||||
if s.cdn.PluginSupported(n.pluginJSON.ID) {
|
||||
return s.cdn.AssetURL(n.pluginJSON.ID, n.pluginJSON.Info.Version, "")
|
||||
}
|
||||
return path.Join("/", s.cfg.GrafanaAppSubURL, "/public/plugins", n.pluginJSON.ID), nil
|
||||
return path.Join("public/plugins", n.pluginJSON.ID), nil
|
||||
}
|
||||
|
||||
// Module returns the module.js path for the specified plugin.
|
||||
@@ -70,7 +70,7 @@ func (s *Service) Module(n PluginInfo) (string, error) {
|
||||
if s.cdn.PluginSupported(n.pluginJSON.ID) {
|
||||
return s.cdn.AssetURL(n.pluginJSON.ID, n.pluginJSON.Info.Version, "module.js")
|
||||
}
|
||||
return path.Join("/", s.cfg.GrafanaAppSubURL, "/public/plugins", n.pluginJSON.ID, "module.js"), nil
|
||||
return path.Join("public/plugins", n.pluginJSON.ID, "module.js"), nil
|
||||
}
|
||||
|
||||
// RelativeURL returns the relative URL for an arbitrary plugin asset.
|
||||
@@ -101,7 +101,7 @@ func (s *Service) RelativeURL(n PluginInfo, pathStr string) (string, error) {
|
||||
|
||||
// DefaultLogoPath returns the default logo path for the specified plugin type.
|
||||
func (s *Service) DefaultLogoPath(pluginType plugins.Type) string {
|
||||
return path.Join("/", s.cfg.GrafanaAppSubURL, fmt.Sprintf("/public/img/icn-%s.svg", string(pluginType)))
|
||||
return path.Join("public/img", fmt.Sprintf("icn-%s.svg", string(pluginType)))
|
||||
}
|
||||
|
||||
func getBaseDir(pluginDir string) string {
|
||||
|
||||
@@ -69,11 +69,11 @@ func TestService(t *testing.T) {
|
||||
|
||||
base, err = svc.Base(NewPluginInfo(jsonData["two"], plugins.ClassExternal, extPath("two")))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "/public/plugins/two", base)
|
||||
require.Equal(t, "public/plugins/two", base)
|
||||
|
||||
base, err = svc.Base(NewPluginInfo(jsonData["table-old"], plugins.ClassCore, tableOldFS))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "/public/app/plugins/table-old", base)
|
||||
require.Equal(t, "public/app/plugins/table-old", base)
|
||||
})
|
||||
|
||||
t.Run("Module", func(t *testing.T) {
|
||||
@@ -86,7 +86,7 @@ func TestService(t *testing.T) {
|
||||
|
||||
module, err = svc.Module(NewPluginInfo(jsonData["two"], plugins.ClassExternal, extPath("two")))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "/public/plugins/two/module.js", module)
|
||||
require.Equal(t, "public/plugins/two/module.js", module)
|
||||
|
||||
module, err = svc.Module(NewPluginInfo(jsonData["table-old"], plugins.ClassCore, tableOldFS))
|
||||
require.NoError(t, err)
|
||||
@@ -116,16 +116,16 @@ func TestService(t *testing.T) {
|
||||
|
||||
u, err = svc.RelativeURL(NewPluginInfo(pluginsMap["two"].JSONData, plugins.ClassExternal, extPath("two")), "path/to/file.txt")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "/public/plugins/two/path/to/file.txt", u)
|
||||
require.Equal(t, "public/plugins/two/path/to/file.txt", u)
|
||||
|
||||
u, err = svc.RelativeURL(NewPluginInfo(pluginsMap["two"].JSONData, plugins.ClassExternal, extPath("two")), "default")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "/public/plugins/two/default", u)
|
||||
require.Equal(t, "public/plugins/two/default", u)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("With App Sub URL", func(t *testing.T) {
|
||||
t.Run("App Sub URL has no effect on the path", func(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
appSubURL string
|
||||
}{
|
||||
@@ -151,15 +151,15 @@ func TestService(t *testing.T) {
|
||||
|
||||
base, err := svc.Base(NewPluginInfo(p, plugins.ClassExternal, fs))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "/grafana/public/plugins/test-datasource", base)
|
||||
require.Equal(t, "public/plugins/test-datasource", base)
|
||||
|
||||
mod, err := svc.Module(NewPluginInfo(p, plugins.ClassExternal, fs))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "/grafana/public/plugins/test-datasource/module.js", mod)
|
||||
require.Equal(t, "public/plugins/test-datasource/module.js", mod)
|
||||
|
||||
base, err = svc.Base(NewPluginInfo(p, plugins.ClassCore, fs))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "/grafana/public/app/plugins/test-datasource", base)
|
||||
require.Equal(t, "public/app/plugins/test-datasource", base)
|
||||
|
||||
mod, err = svc.Module(NewPluginInfo(p, plugins.ClassCore, fs))
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -83,8 +83,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
},
|
||||
Description: "Data source for Amazon AWS monitoring service",
|
||||
Logos: plugins.Logos{
|
||||
Small: "/public/app/plugins/datasource/cloudwatch/img/amazon-web-services.png",
|
||||
Large: "/public/app/plugins/datasource/cloudwatch/img/amazon-web-services.png",
|
||||
Small: "public/app/plugins/datasource/cloudwatch/img/amazon-web-services.png",
|
||||
Large: "public/app/plugins/datasource/cloudwatch/img/amazon-web-services.png",
|
||||
},
|
||||
},
|
||||
Includes: []*plugins.Includes{
|
||||
@@ -106,9 +106,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
Backend: true,
|
||||
QueryOptions: map[string]bool{"minInterval": true},
|
||||
},
|
||||
Module: "core:plugin/cloudwatch",
|
||||
BaseURL: "/public/app/plugins/datasource/cloudwatch",
|
||||
|
||||
Module: "core:plugin/cloudwatch",
|
||||
BaseURL: "public/app/plugins/datasource/cloudwatch",
|
||||
FS: mustNewStaticFSForTests(t, filepath.Join(corePluginDir, "app/plugins/datasource/cloudwatch")),
|
||||
Signature: plugins.SignatureStatusInternal,
|
||||
Class: plugins.ClassCore,
|
||||
@@ -133,8 +132,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
},
|
||||
Version: "1.0.0",
|
||||
Logos: plugins.Logos{
|
||||
Small: "/public/img/icn-datasource.svg",
|
||||
Large: "/public/img/icn-datasource.svg",
|
||||
Small: "public/img/icn-datasource.svg",
|
||||
Large: "public/img/icn-datasource.svg",
|
||||
},
|
||||
Description: "Test",
|
||||
},
|
||||
@@ -146,8 +145,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
Backend: true,
|
||||
State: "alpha",
|
||||
},
|
||||
Module: "/public/plugins/test-datasource/module.js",
|
||||
BaseURL: "/public/plugins/test-datasource",
|
||||
Module: "public/plugins/test-datasource/module.js",
|
||||
BaseURL: "public/plugins/test-datasource",
|
||||
FS: mustNewStaticFSForTests(t, filepath.Join(parentDir, "testdata/valid-v2-signature/plugin/")),
|
||||
Signature: "valid",
|
||||
SignatureType: plugins.SignatureTypeGrafana,
|
||||
@@ -172,8 +171,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
URL: "http://test.com",
|
||||
},
|
||||
Logos: plugins.Logos{
|
||||
Small: "/public/plugins/test-app/img/logo_small.png",
|
||||
Large: "/public/plugins/test-app/img/logo_large.png",
|
||||
Small: "public/plugins/test-app/img/logo_small.png",
|
||||
Large: "public/plugins/test-app/img/logo_large.png",
|
||||
},
|
||||
Links: []plugins.InfoLink{
|
||||
{Name: "Project site", URL: "http://project.com"},
|
||||
@@ -181,8 +180,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
},
|
||||
Description: "Official Grafana Test App & Dashboard bundle",
|
||||
Screenshots: []plugins.Screenshots{
|
||||
{Path: "/public/plugins/test-app/img/screenshot1.png", Name: "img1"},
|
||||
{Path: "/public/plugins/test-app/img/screenshot2.png", Name: "img2"},
|
||||
{Path: "public/plugins/test-app/img/screenshot1.png", Name: "img1"},
|
||||
{Path: "public/plugins/test-app/img/screenshot2.png", Name: "img2"},
|
||||
},
|
||||
Version: "1.0.0",
|
||||
Updated: "2015-02-10",
|
||||
@@ -223,8 +222,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
},
|
||||
},
|
||||
Class: plugins.ClassExternal,
|
||||
Module: "/public/plugins/test-app/module.js",
|
||||
BaseURL: "/public/plugins/test-app",
|
||||
Module: "public/plugins/test-app/module.js",
|
||||
BaseURL: "public/plugins/test-app",
|
||||
FS: mustNewStaticFSForTests(t, filepath.Join(parentDir, "testdata/includes-symlinks")),
|
||||
Signature: "valid",
|
||||
SignatureType: plugins.SignatureTypeGrafana,
|
||||
@@ -251,8 +250,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
URL: "https://grafana.com",
|
||||
},
|
||||
Logos: plugins.Logos{
|
||||
Small: "/public/img/icn-datasource.svg",
|
||||
Large: "/public/img/icn-datasource.svg",
|
||||
Small: "public/img/icn-datasource.svg",
|
||||
Large: "public/img/icn-datasource.svg",
|
||||
},
|
||||
Description: "Test",
|
||||
},
|
||||
@@ -264,8 +263,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
State: plugins.ReleaseStateAlpha,
|
||||
},
|
||||
Class: plugins.ClassExternal,
|
||||
Module: "/public/plugins/test-datasource/module.js",
|
||||
BaseURL: "/public/plugins/test-datasource",
|
||||
Module: "public/plugins/test-datasource/module.js",
|
||||
BaseURL: "public/plugins/test-datasource",
|
||||
FS: mustNewStaticFSForTests(t, filepath.Join(parentDir, "testdata/unsigned-datasource/plugin")),
|
||||
Signature: "unsigned",
|
||||
},
|
||||
@@ -298,8 +297,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
URL: "https://grafana.com",
|
||||
},
|
||||
Logos: plugins.Logos{
|
||||
Small: "/public/img/icn-datasource.svg",
|
||||
Large: "/public/img/icn-datasource.svg",
|
||||
Small: "public/img/icn-datasource.svg",
|
||||
Large: "public/img/icn-datasource.svg",
|
||||
},
|
||||
Description: "Test",
|
||||
},
|
||||
@@ -311,8 +310,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
State: plugins.ReleaseStateAlpha,
|
||||
},
|
||||
Class: plugins.ClassExternal,
|
||||
Module: "/public/plugins/test-datasource/module.js",
|
||||
BaseURL: "/public/plugins/test-datasource",
|
||||
Module: "public/plugins/test-datasource/module.js",
|
||||
BaseURL: "public/plugins/test-datasource",
|
||||
FS: mustNewStaticFSForTests(t, filepath.Join(parentDir, "testdata/unsigned-datasource/plugin")),
|
||||
Signature: plugins.SignatureStatusUnsigned,
|
||||
},
|
||||
@@ -381,8 +380,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
{Name: "License & Terms", URL: "http://license.com"},
|
||||
},
|
||||
Logos: plugins.Logos{
|
||||
Small: "/public/img/icn-app.svg",
|
||||
Large: "/public/img/icn-app.svg",
|
||||
Small: "public/img/icn-app.svg",
|
||||
Large: "public/img/icn-app.svg",
|
||||
},
|
||||
Updated: "2015-02-10",
|
||||
},
|
||||
@@ -401,8 +400,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
FS: mustNewStaticFSForTests(t, filepath.Join(parentDir, "testdata/test-app-with-includes")),
|
||||
Class: plugins.ClassExternal,
|
||||
Signature: plugins.SignatureStatusUnsigned,
|
||||
Module: "/public/plugins/test-app/module.js",
|
||||
BaseURL: "/public/plugins/test-app",
|
||||
Module: "public/plugins/test-app/module.js",
|
||||
BaseURL: "public/plugins/test-app",
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -427,8 +426,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
URL: "https://grafana.com",
|
||||
},
|
||||
Logos: plugins.Logos{
|
||||
Small: "/grafana/public/img/icn-datasource.svg",
|
||||
Large: "/grafana/public/img/icn-datasource.svg",
|
||||
Small: "public/img/icn-datasource.svg",
|
||||
Large: "public/img/icn-datasource.svg",
|
||||
},
|
||||
Description: "Test",
|
||||
},
|
||||
@@ -440,8 +439,8 @@ func TestLoader_Load(t *testing.T) {
|
||||
State: plugins.ReleaseStateAlpha,
|
||||
},
|
||||
Class: plugins.ClassExternal,
|
||||
Module: "/grafana/public/plugins/test-datasource/module.js",
|
||||
BaseURL: "/grafana/public/plugins/test-datasource",
|
||||
Module: "public/plugins/test-datasource/module.js",
|
||||
BaseURL: "public/plugins/test-datasource",
|
||||
FS: mustNewStaticFSForTests(t, filepath.Join(parentDir, "testdata/unsigned-datasource/plugin")),
|
||||
Signature: plugins.SignatureStatusUnsigned,
|
||||
},
|
||||
|
||||
@@ -32,7 +32,7 @@ func DefaultDecorateFuncs(cfg *config.Cfg) []DecorateFunc {
|
||||
return []DecorateFunc{
|
||||
AppDefaultNavURLDecorateFunc,
|
||||
TemplateDecorateFunc,
|
||||
AppChildDecorateFunc(cfg),
|
||||
AppChildDecorateFunc(),
|
||||
SkipHostEnvVarsDecorateFunc(cfg),
|
||||
}
|
||||
}
|
||||
@@ -132,27 +132,28 @@ func setDefaultNavURL(p *plugins.Plugin) {
|
||||
}
|
||||
|
||||
// AppChildDecorateFunc is a DecorateFunc that configures child plugins of app plugins.
|
||||
func AppChildDecorateFunc(cfg *config.Cfg) DecorateFunc {
|
||||
func AppChildDecorateFunc() DecorateFunc {
|
||||
return func(_ context.Context, p *plugins.Plugin) (*plugins.Plugin, error) {
|
||||
if p.Parent != nil && p.Parent.IsApp() {
|
||||
configureAppChildPlugin(cfg, p.Parent, p)
|
||||
configureAppChildPlugin(p.Parent, p)
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
|
||||
func configureAppChildPlugin(cfg *config.Cfg, parent *plugins.Plugin, child *plugins.Plugin) {
|
||||
func configureAppChildPlugin(parent *plugins.Plugin, child *plugins.Plugin) {
|
||||
if !parent.IsApp() {
|
||||
return
|
||||
}
|
||||
child.IncludedInAppID = parent.ID
|
||||
child.BaseURL = parent.BaseURL
|
||||
|
||||
// TODO move this logic within assetpath package
|
||||
appSubPath := strings.ReplaceAll(strings.Replace(child.FS.Base(), parent.FS.Base(), "", 1), "\\", "/")
|
||||
if parent.IsCorePlugin() {
|
||||
child.Module = path.Join("core:plugin", parent.ID, appSubPath)
|
||||
} else {
|
||||
child.Module = path.Join("/", cfg.GrafanaAppSubURL, "/public/plugins", parent.ID, appSubPath, "module.js")
|
||||
child.Module = path.Join("public/plugins", parent.ID, appSubPath, "module.js")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -109,25 +109,17 @@ func Test_configureAppChildPlugin(t *testing.T) {
|
||||
},
|
||||
Class: plugins.ClassCore,
|
||||
FS: fakes.NewFakePluginFiles("c:\\grafana\\public\\app\\plugins\\app\\testdata-app"),
|
||||
BaseURL: "/public/app/plugins/app/testdata-app",
|
||||
BaseURL: "public/app/plugins/app/testdata-app",
|
||||
}
|
||||
|
||||
configureAppChildPlugin(&config.Cfg{}, parent, child)
|
||||
configureAppChildPlugin(parent, child)
|
||||
|
||||
require.Equal(t, "core:plugin/testdata-app/datasources/datasource", child.Module)
|
||||
require.Equal(t, "testdata-app", child.IncludedInAppID)
|
||||
require.Equal(t, "/public/app/plugins/app/testdata-app", child.BaseURL)
|
||||
|
||||
t.Run("App sub URL has no effect on Core plugins", func(t *testing.T) {
|
||||
configureAppChildPlugin(&config.Cfg{GrafanaAppSubURL: "/grafana"}, parent, child)
|
||||
|
||||
require.Equal(t, "core:plugin/testdata-app/datasources/datasource", child.Module)
|
||||
require.Equal(t, "testdata-app", child.IncludedInAppID)
|
||||
require.Equal(t, "/public/app/plugins/app/testdata-app", child.BaseURL)
|
||||
})
|
||||
require.Equal(t, "public/app/plugins/app/testdata-app", child.BaseURL)
|
||||
})
|
||||
|
||||
t.Run("When setting paths based on external plugin with app sub URL", func(t *testing.T) {
|
||||
t.Run("When setting paths based on external plugin", func(t *testing.T) {
|
||||
child := &plugins.Plugin{
|
||||
FS: fakes.NewFakePluginFiles("/plugins/parent-app/child-panel"),
|
||||
}
|
||||
@@ -138,14 +130,14 @@ func Test_configureAppChildPlugin(t *testing.T) {
|
||||
},
|
||||
Class: plugins.ClassExternal,
|
||||
FS: fakes.NewFakePluginFiles("/plugins/parent-app"),
|
||||
BaseURL: "/grafana/plugins/parent-app",
|
||||
BaseURL: "plugins/parent-app",
|
||||
}
|
||||
|
||||
configureAppChildPlugin(&config.Cfg{GrafanaAppSubURL: "/grafana"}, parent, child)
|
||||
configureAppChildPlugin(parent, child)
|
||||
|
||||
require.Equal(t, "/grafana/public/plugins/testdata-app/child-panel/module.js", child.Module)
|
||||
require.Equal(t, "public/plugins/testdata-app/child-panel/module.js", child.Module)
|
||||
require.Equal(t, "testdata-app", child.IncludedInAppID)
|
||||
require.Equal(t, "/grafana/plugins/parent-app", child.BaseURL)
|
||||
require.Equal(t, "plugins/parent-app", child.BaseURL)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user