tidy
This commit is contained in:
@@ -82,7 +82,7 @@ func setupTestEnvironment(t *testing.T, cfg *setting.Cfg, features featuremgmt.F
|
|||||||
var pluginsAssets = passets
|
var pluginsAssets = passets
|
||||||
if pluginsAssets == nil {
|
if pluginsAssets == nil {
|
||||||
sig := signature.ProvideService(pluginsCfg, statickey.New())
|
sig := signature.ProvideService(pluginsCfg, statickey.New())
|
||||||
calc := modulehash.NewModuleHashCalculator(pluginsCfg, registry.NewInMemory(), pluginsCDN, sig)
|
calc := modulehash.NewCalculator(pluginsCfg, registry.NewInMemory(), pluginsCDN, sig)
|
||||||
pluginsAssets = pluginassets.ProvideService(pluginsCfg, pluginsCDN, calc)
|
pluginsAssets = pluginassets.ProvideService(pluginsCfg, pluginsCDN, calc)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -718,7 +718,7 @@ func newPluginAssets() func() *pluginassets.Service {
|
|||||||
func newPluginAssetsWithConfig(pCfg *config.PluginManagementCfg) func() *pluginassets.Service {
|
func newPluginAssetsWithConfig(pCfg *config.PluginManagementCfg) func() *pluginassets.Service {
|
||||||
return func() *pluginassets.Service {
|
return func() *pluginassets.Service {
|
||||||
cdn := pluginscdn.ProvideService(pCfg)
|
cdn := pluginscdn.ProvideService(pCfg)
|
||||||
calc := modulehash.NewModuleHashCalculator(pCfg, registry.NewInMemory(), cdn, signature.ProvideService(pCfg, statickey.New()))
|
calc := modulehash.NewCalculator(pCfg, registry.NewInMemory(), cdn, signature.ProvideService(pCfg, statickey.New()))
|
||||||
return pluginassets.ProvideService(pCfg, cdn, calc)
|
return pluginassets.ProvideService(pCfg, cdn, calc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -850,7 +850,7 @@ func Test_PluginsSettings(t *testing.T) {
|
|||||||
pCfg := &config.PluginManagementCfg{}
|
pCfg := &config.PluginManagementCfg{}
|
||||||
pluginCDN := pluginscdn.ProvideService(pCfg)
|
pluginCDN := pluginscdn.ProvideService(pCfg)
|
||||||
sig := signature.ProvideService(pCfg, statickey.New())
|
sig := signature.ProvideService(pCfg, statickey.New())
|
||||||
calc := modulehash.NewModuleHashCalculator(pCfg, registry.NewInMemory(), pluginCDN, sig)
|
calc := modulehash.NewCalculator(pCfg, registry.NewInMemory(), pluginCDN, sig)
|
||||||
hs.pluginAssets = pluginassets.ProvideService(pCfg, pluginCDN, calc)
|
hs.pluginAssets = pluginassets.ProvideService(pCfg, pluginCDN, calc)
|
||||||
hs.pluginErrorResolver = pluginerrs.ProvideStore(errTracker)
|
hs.pluginErrorResolver = pluginerrs.ProvideStore(errTracker)
|
||||||
hs.pluginsUpdateChecker, err = updatemanager.ProvidePluginsService(
|
hs.pluginsUpdateChecker, err = updatemanager.ProvidePluginsService(
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/Masterminds/semver/v3"
|
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/plugins/config"
|
"github.com/grafana/grafana/pkg/plugins/config"
|
||||||
"github.com/grafana/grafana/pkg/plugins/log"
|
"github.com/grafana/grafana/pkg/plugins/log"
|
||||||
@@ -19,16 +17,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/plugins/pluginscdn"
|
"github.com/grafana/grafana/pkg/plugins/pluginscdn"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
type Calculator struct {
|
||||||
CreatePluginVersionCfgKey = "create_plugin_version"
|
|
||||||
CreatePluginVersionScriptSupportEnabled = "4.15.0"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
scriptLoadingMinSupportedVersion = semver.MustParse(CreatePluginVersionScriptSupportEnabled)
|
|
||||||
)
|
|
||||||
|
|
||||||
type ModuleHashCalculator struct {
|
|
||||||
reg registry.Service
|
reg registry.Service
|
||||||
cfg *config.PluginManagementCfg
|
cfg *config.PluginManagementCfg
|
||||||
cdn *pluginscdn.Service
|
cdn *pluginscdn.Service
|
||||||
@@ -38,8 +27,8 @@ type ModuleHashCalculator struct {
|
|||||||
moduleHashCache sync.Map
|
moduleHashCache sync.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewModuleHashCalculator(cfg *config.PluginManagementCfg, reg registry.Service, cdn *pluginscdn.Service, signature *signature.Signature) *ModuleHashCalculator {
|
func NewCalculator(cfg *config.PluginManagementCfg, reg registry.Service, cdn *pluginscdn.Service, signature *signature.Signature) *Calculator {
|
||||||
return &ModuleHashCalculator{
|
return &Calculator{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
reg: reg,
|
reg: reg,
|
||||||
cdn: cdn,
|
cdn: cdn,
|
||||||
@@ -53,7 +42,7 @@ func NewModuleHashCalculator(cfg *config.PluginManagementCfg, reg registry.Servi
|
|||||||
// The plugin can also be a nested plugin.
|
// The plugin can also be a nested plugin.
|
||||||
// If the plugin is unsigned, an empty string is returned.
|
// If the plugin is unsigned, an empty string is returned.
|
||||||
// The results are cached to avoid repeated reads from the MANIFEST.txt file.
|
// The results are cached to avoid repeated reads from the MANIFEST.txt file.
|
||||||
func (c *ModuleHashCalculator) ModuleHash(ctx context.Context, pluginID, pluginVersion string) string {
|
func (c *Calculator) ModuleHash(ctx context.Context, pluginID, pluginVersion string) string {
|
||||||
p, ok := c.reg.Plugin(ctx, pluginID, pluginVersion)
|
p, ok := c.reg.Plugin(ctx, pluginID, pluginVersion)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.log.Error("Failed to calculate module hash as plugin is not registered", "pluginId", pluginID)
|
c.log.Error("Failed to calculate module hash as plugin is not registered", "pluginId", pluginID)
|
||||||
@@ -77,7 +66,7 @@ func (c *ModuleHashCalculator) ModuleHash(ctx context.Context, pluginID, pluginV
|
|||||||
// It will read the module hash from the MANIFEST.txt in the [[plugins.FS]] of the provided plugin.
|
// It will read the module hash from the MANIFEST.txt in the [[plugins.FS]] of the provided plugin.
|
||||||
// If childFSBase is provided, the function will try to get the hash from MANIFEST.txt for the provided children's
|
// If childFSBase is provided, the function will try to get the hash from MANIFEST.txt for the provided children's
|
||||||
// module.js file, rather than for the provided plugin.
|
// module.js file, rather than for the provided plugin.
|
||||||
func (c *ModuleHashCalculator) moduleHash(ctx context.Context, p *plugins.Plugin, childFSBase string) (r string, err error) {
|
func (c *Calculator) moduleHash(ctx context.Context, p *plugins.Plugin, childFSBase string) (r string, err error) {
|
||||||
if !c.cfg.Features.SriChecksEnabled {
|
if !c.cfg.Features.SriChecksEnabled {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
@@ -136,21 +125,7 @@ func (c *ModuleHashCalculator) moduleHash(ctx context.Context, p *plugins.Plugin
|
|||||||
return convertHashForSRI(moduleHash)
|
return convertHashForSRI(moduleHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ModuleHashCalculator) compatibleCreatePluginVersion(ps map[string]string) bool {
|
func (c *Calculator) cdnEnabled(pluginID string, fs plugins.FS) bool {
|
||||||
if cpv, ok := ps[CreatePluginVersionCfgKey]; ok {
|
|
||||||
createPluginVer, err := semver.NewVersion(cpv)
|
|
||||||
if err != nil {
|
|
||||||
c.log.Warn("Failed to parse create plugin version setting as semver", "version", cpv, "error", err)
|
|
||||||
} else {
|
|
||||||
if !createPluginVer.LessThan(scriptLoadingMinSupportedVersion) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *ModuleHashCalculator) cdnEnabled(pluginID string, fs plugins.FS) bool {
|
|
||||||
return c.cdn.PluginSupported(pluginID) || fs.Type().CDN()
|
return c.cdn.PluginSupported(pluginID) || fs.Type().CDN()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,6 +139,6 @@ func convertHashForSRI(h string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// moduleHashCacheKey returns a unique key for the module hash cache.
|
// moduleHashCacheKey returns a unique key for the module hash cache.
|
||||||
func (c *ModuleHashCalculator) moduleHashCacheKey(pluginId, pluginVersion string) string {
|
func (c *Calculator) moduleHashCacheKey(pluginId, pluginVersion string) string {
|
||||||
return pluginId + ":" + pluginVersion
|
return pluginId + ":" + pluginVersion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/plugins/pluginscdn"
|
"github.com/grafana/grafana/pkg/plugins/pluginscdn"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestService_ModuleHash(t *testing.T) {
|
func Test_ModuleHash(t *testing.T) {
|
||||||
const (
|
const (
|
||||||
pluginID = "grafana-test-datasource"
|
pluginID = "grafana-test-datasource"
|
||||||
parentPluginID = "grafana-test-app"
|
parentPluginID = "grafana-test-app"
|
||||||
@@ -247,7 +247,7 @@ func TestService_ModuleHash(t *testing.T) {
|
|||||||
Features: *features,
|
Features: *features,
|
||||||
}
|
}
|
||||||
|
|
||||||
svc := NewModuleHashCalculator(
|
svc := NewCalculator(
|
||||||
pCfg,
|
pCfg,
|
||||||
newPluginRegistry(t, tc.registry...),
|
newPluginRegistry(t, tc.registry...),
|
||||||
pluginscdn.ProvideService(pCfg),
|
pluginscdn.ProvideService(pCfg),
|
||||||
@@ -259,12 +259,12 @@ func TestService_ModuleHash(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestService_ModuleHash_Cache(t *testing.T) {
|
func Test_ModuleHash_Cache(t *testing.T) {
|
||||||
pCfg := &config.PluginManagementCfg{
|
pCfg := &config.PluginManagementCfg{
|
||||||
PluginSettings: config.PluginSettings{},
|
PluginSettings: config.PluginSettings{},
|
||||||
Features: config.Features{SriChecksEnabled: true},
|
Features: config.Features{SriChecksEnabled: true},
|
||||||
}
|
}
|
||||||
svc := NewModuleHashCalculator(
|
svc := NewCalculator(
|
||||||
pCfg,
|
pCfg,
|
||||||
newPluginRegistry(t),
|
newPluginRegistry(t),
|
||||||
pluginscdn.ProvideService(pCfg),
|
pluginscdn.ProvideService(pCfg),
|
||||||
@@ -305,7 +305,7 @@ func TestService_ModuleHash_Cache(t *testing.T) {
|
|||||||
Features: config.Features{SriChecksEnabled: true},
|
Features: config.Features{SriChecksEnabled: true},
|
||||||
}
|
}
|
||||||
reg := newPluginRegistry(t, pV1)
|
reg := newPluginRegistry(t, pV1)
|
||||||
svc = NewModuleHashCalculator(
|
svc = NewCalculator(
|
||||||
pCfg,
|
pCfg,
|
||||||
reg,
|
reg,
|
||||||
pluginscdn.ProvideService(pCfg),
|
pluginscdn.ProvideService(pCfg),
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func ProvideService(cfg *config.PluginManagementCfg, cdn *pluginscdn.Service,
|
func ProvideService(cfg *config.PluginManagementCfg, cdn *pluginscdn.Service,
|
||||||
calc *modulehash.ModuleHashCalculator) *Service {
|
calc *modulehash.Calculator) *Service {
|
||||||
return &Service{
|
return &Service{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
cdn: cdn,
|
cdn: cdn,
|
||||||
@@ -35,14 +35,14 @@ func ProvideService(cfg *config.PluginManagementCfg, cdn *pluginscdn.Service,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ProvideModuleHashCalculator(cfg *config.PluginManagementCfg, cdn *pluginscdn.Service,
|
func ProvideModuleHashCalculator(cfg *config.PluginManagementCfg, cdn *pluginscdn.Service,
|
||||||
signature *signature.Signature, reg registry.Service) *modulehash.ModuleHashCalculator {
|
signature *signature.Signature, reg registry.Service) *modulehash.Calculator {
|
||||||
return modulehash.NewModuleHashCalculator(cfg, reg, cdn, signature)
|
return modulehash.NewCalculator(cfg, reg, cdn, signature)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
cfg *config.PluginManagementCfg
|
cfg *config.PluginManagementCfg
|
||||||
cdn *pluginscdn.Service
|
cdn *pluginscdn.Service
|
||||||
calc *modulehash.ModuleHashCalculator
|
calc *modulehash.Calculator
|
||||||
|
|
||||||
log log.Logger
|
log log.Logger
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user