Plugins: Fix loading of dist folders (#80015)

* end to end

* tidy

* fix whitespace

* remove unused code

* fix linter

* fix gosec + add sort

* fix test

* apply cr feedback
This commit is contained in:
Will Browne
2024-01-08 11:45:03 +01:00
committed by GitHub
parent 0440b29ebf
commit 78ae795e06
6 changed files with 114 additions and 93 deletions
+55 -25
View File
@@ -2,34 +2,77 @@ package sources
import (
"context"
"os"
"path/filepath"
"slices"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/config"
"github.com/grafana/grafana/pkg/plugins/log"
"github.com/grafana/grafana/pkg/setting"
)
type Service struct {
gCfg *setting.Cfg
cfg *config.Cfg
log log.Logger
cfg *setting.Cfg
log log.Logger
}
func ProvideService(gCfg *setting.Cfg, cfg *config.Cfg) *Service {
func ProvideService(cfg *setting.Cfg) *Service {
return &Service{
gCfg: gCfg,
cfg: cfg,
log: log.New("plugin.sources"),
cfg: cfg,
log: log.New("plugin.sources"),
}
}
func (s *Service) List(_ context.Context) []plugins.PluginSource {
return []plugins.PluginSource{
NewLocalSource(plugins.ClassCore, corePluginPaths(s.gCfg.StaticRootPath)),
NewLocalSource(plugins.ClassBundled, []string{s.gCfg.BundledPluginsPath}),
NewLocalSource(plugins.ClassExternal, append([]string{s.cfg.PluginsPath}, pluginFSPaths(s.cfg.PluginSettings)...)),
r := []plugins.PluginSource{
NewLocalSource(plugins.ClassCore, corePluginPaths(s.cfg.StaticRootPath)),
NewLocalSource(plugins.ClassBundled, []string{s.cfg.BundledPluginsPath}),
}
r = append(r, s.externalPluginSources()...)
r = append(r, s.pluginSettingSources()...)
return r
}
func (s *Service) externalPluginSources() []plugins.PluginSource {
var sources []plugins.PluginSource
if s.cfg.PluginsPath == "" {
return sources
}
pluginsPath := s.cfg.PluginsPath
// It's safe to ignore gosec warning G304 since the variable part of the file path comes from a configuration
// variable.
// nolint:gosec
d, err := os.ReadDir(pluginsPath)
if err != nil {
s.log.Error("Failed to open plugins path", "path", pluginsPath, "error", err)
return sources
}
var pluginDirs []string
for _, dir := range d {
if dir.IsDir() {
pluginDirs = append(pluginDirs, filepath.Join(pluginsPath, dir.Name()))
}
}
slices.Sort(pluginDirs)
for _, dir := range pluginDirs {
sources = append(sources, NewLocalSource(plugins.ClassExternal, []string{dir}))
}
return sources
}
func (s *Service) pluginSettingSources() []plugins.PluginSource {
var sources []plugins.PluginSource
for _, ps := range s.cfg.PluginSettings {
path, exists := ps["path"]
if !exists || path == "" {
continue
}
sources = append(sources, NewLocalSource(plugins.ClassExternal, []string{path}))
}
return sources
}
// corePluginPaths provides a list of the Core plugin file system paths
@@ -38,16 +81,3 @@ func corePluginPaths(staticRootPath string) []string {
panelsPath := filepath.Join(staticRootPath, "app/plugins/panel")
return []string{datasourcePaths, panelsPath}
}
// pluginSettingPaths provides plugin file system paths defined in cfg.PluginSettings
func pluginFSPaths(ps map[string]map[string]string) []string {
var pluginSettingDirs []string
for _, s := range ps {
path, exists := s["path"]
if !exists || path == "" {
continue
}
pluginSettingDirs = append(pluginSettingDirs, path)
}
return pluginSettingDirs
}