Plugins: Refactor plugin config into separate env var and request scoped services (#83261)

* seperate services for env + req

* merge with main

* fix tests

* undo changes to golden file

* fix linter

* remove unused fields

* split out new config struct

* provide config

* undo go mod changes

* more renaming

* fix tests

* undo bra.toml changes

* update go.work.sum

* undo changes

* trigger

* apply PR feedback
This commit is contained in:
Will Browne
2024-02-27 12:38:02 +01:00
committed by GitHub
parent d04c579fc2
commit 5edd96ae77
62 changed files with 1933 additions and 1207 deletions
+3 -3
View File
@@ -5,18 +5,18 @@ import (
"github.com/grafana/grafana/pkg/plugins/config"
)
func ProvideOSSAuthorizer(cfg *config.Cfg) *UnsignedPluginAuthorizer {
func ProvideOSSAuthorizer(cfg *config.PluginManagementCfg) *UnsignedPluginAuthorizer {
return NewUnsignedAuthorizer(cfg)
}
func NewUnsignedAuthorizer(cfg *config.Cfg) *UnsignedPluginAuthorizer {
func NewUnsignedAuthorizer(cfg *config.PluginManagementCfg) *UnsignedPluginAuthorizer {
return &UnsignedPluginAuthorizer{
cfg: cfg,
}
}
type UnsignedPluginAuthorizer struct {
cfg *config.Cfg
cfg *config.PluginManagementCfg
}
func (u *UnsignedPluginAuthorizer) CanLoadPlugin(p *plugins.Plugin) bool {
+4 -4
View File
@@ -59,17 +59,17 @@ func (m *PluginManifest) isV2() bool {
type Signature struct {
kr plugins.KeyRetriever
cfg *config.Cfg
cfg *config.PluginManagementCfg
log log.Logger
}
var _ plugins.SignatureCalculator = &Signature{}
func ProvideService(cfg *config.Cfg, kr plugins.KeyRetriever) *Signature {
func ProvideService(cfg *config.PluginManagementCfg, kr plugins.KeyRetriever) *Signature {
return NewCalculator(cfg, kr)
}
func NewCalculator(cfg *config.Cfg, kr plugins.KeyRetriever) *Signature {
func NewCalculator(cfg *config.PluginManagementCfg, kr plugins.KeyRetriever) *Signature {
return &Signature{
kr: kr,
cfg: cfg,
@@ -77,7 +77,7 @@ func NewCalculator(cfg *config.Cfg, kr plugins.KeyRetriever) *Signature {
}
}
func DefaultCalculator(cfg *config.Cfg) *Signature {
func DefaultCalculator(cfg *config.PluginManagementCfg) *Signature {
return &Signature{
kr: statickey.New(),
cfg: cfg,
@@ -52,7 +52,7 @@ NR7DnB0CCQHO+4FlSPtXFTzNepoc+CytQyDAeOLMLmf2Tqhk2YShk+G/YlVX
-----END PGP SIGNATURE-----`
t.Run("valid manifest", func(t *testing.T) {
s := ProvideService(&config.Cfg{}, statickey.New())
s := ProvideService(&config.PluginManagementCfg{}, statickey.New())
manifest, err := s.readPluginManifest(context.Background(), []byte(txt))
require.NoError(t, err)
@@ -69,7 +69,7 @@ NR7DnB0CCQHO+4FlSPtXFTzNepoc+CytQyDAeOLMLmf2Tqhk2YShk+G/YlVX
t.Run("invalid manifest", func(t *testing.T) {
modified := strings.ReplaceAll(txt, "README.md", "xxxxxxxxxx")
s := ProvideService(&config.Cfg{}, statickey.New())
s := ProvideService(&config.PluginManagementCfg{}, statickey.New())
_, err := s.readPluginManifest(context.Background(), []byte(modified))
require.Error(t, err)
})
@@ -107,7 +107,7 @@ khdr/tZ1PDgRxMqB/u+Vtbpl0xSxgblnrDOYMSI=
-----END PGP SIGNATURE-----`
t.Run("valid manifest", func(t *testing.T) {
s := ProvideService(&config.Cfg{}, statickey.New())
s := ProvideService(&config.PluginManagementCfg{}, statickey.New())
manifest, err := s.readPluginManifest(context.Background(), []byte(txt))
require.NoError(t, err)
@@ -155,7 +155,7 @@ func TestCalculate(t *testing.T) {
for _, tc := range tcs {
basePath := filepath.Join(parentDir, "testdata/non-pvt-with-root-url/plugin")
s := ProvideService(&config.Cfg{GrafanaAppURL: tc.appURL}, statickey.New())
s := ProvideService(&config.PluginManagementCfg{GrafanaAppURL: tc.appURL}, statickey.New())
sig, err := s.Calculate(context.Background(), &fakes.FakePluginSource{
PluginClassFunc: func(ctx context.Context) plugins.Class {
return plugins.ClassExternal
@@ -183,7 +183,7 @@ func TestCalculate(t *testing.T) {
basePath := "../testdata/renderer-added-file/plugin"
runningWindows = true
s := ProvideService(&config.Cfg{}, statickey.New())
s := ProvideService(&config.PluginManagementCfg{}, statickey.New())
sig, err := s.Calculate(context.Background(), &fakes.FakePluginSource{
PluginClassFunc: func(ctx context.Context) plugins.Class {
return plugins.ClassExternal
@@ -247,7 +247,7 @@ func TestCalculate(t *testing.T) {
toSlash = tc.platform.toSlashFunc()
fromSlash = tc.platform.fromSlashFunc()
s := ProvideService(&config.Cfg{}, statickey.New())
s := ProvideService(&config.PluginManagementCfg{}, statickey.New())
pfs, err := tc.fsFactory()
require.NoError(t, err)
pfs, err = newPathSeparatorOverrideFS(string(tc.platform.separator), pfs)
@@ -715,7 +715,7 @@ func Test_validateManifest(t *testing.T) {
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
s := ProvideService(&config.Cfg{}, statickey.New())
s := ProvideService(&config.PluginManagementCfg{}, statickey.New())
err := s.validateManifest(context.Background(), *tc.manifest, nil)
require.Errorf(t, err, tc.expectedErr)
})
@@ -811,7 +811,7 @@ pHo=
}
func Test_VerifyRevokedKey(t *testing.T) {
s := ProvideService(&config.Cfg{}, &revokedKeyProvider{})
s := ProvideService(&config.PluginManagementCfg{}, &revokedKeyProvider{})
m := createV2Manifest(t)
txt := `-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512