Plugins: Plugin settings refactor (#46246)

* remove bus and direct use of sqlStore

* add decryption to interface

* return nil

* rename field

* re-order fields

* rename file
This commit is contained in:
Will Browne
2022-03-04 17:09:50 +01:00
committed by GitHub
parent b409c9782f
commit 6a8cbd8663
9 changed files with 43 additions and 31 deletions
@@ -7,8 +7,15 @@ import (
)
type Service interface {
// GetPluginSettings returns all Plugin Settings for the provided Org
GetPluginSettings(ctx context.Context, orgID int64) ([]*models.PluginSettingInfoDTO, error)
// GetPluginSettingById returns a Plugin Settings by Plugin ID
GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error
// UpdatePluginSetting updates a Plugin Setting
UpdatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error
// UpdatePluginSettingVersion updates a Plugin Setting's plugin version
UpdatePluginSettingVersion(ctx context.Context, cmd *models.UpdatePluginSettingVersionCmd) error
// DecryptedValues decrypts the encrypted secureJSONData of the provided plugin setting and
// returns the decrypted values.
DecryptedValues(ps *models.PluginSetting) map[string]string
}
+13 -20
View File
@@ -5,38 +5,31 @@ import (
"sync"
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/secrets"
"github.com/grafana/grafana/pkg/services/sqlstore"
)
func ProvideService(bus bus.Bus, store *sqlstore.SQLStore, secretsService secrets.Service) *Service {
func ProvideService(store *sqlstore.SQLStore, secretsService secrets.Service) *Service {
s := &Service{
bus: bus,
sqlStore: store,
secretsService: secretsService,
logger: log.New("pluginsettings"),
pluginSettingDecryptionCache: secureJSONDecryptionCache{
sqlStore: store,
decryptionCache: secureJSONDecryptionCache{
cache: make(map[int64]cachedDecryptedJSON),
},
secretsService: secretsService,
logger: log.New("pluginsettings"),
}
s.bus.AddHandler(s.GetPluginSettingById)
s.bus.AddHandler(s.UpdatePluginSetting)
s.bus.AddHandler(s.UpdatePluginSettingVersion)
return s
}
type Service struct {
bus bus.Bus
sqlStore *sqlstore.SQLStore
secretsService secrets.Service
sqlStore *sqlstore.SQLStore
decryptionCache secureJSONDecryptionCache
secretsService secrets.Service
logger log.Logger
pluginSettingDecryptionCache secureJSONDecryptionCache
logger log.Logger
}
type cachedDecryptedJSON struct {
@@ -72,10 +65,10 @@ func (s *Service) UpdatePluginSettingVersion(ctx context.Context, cmd *models.Up
}
func (s *Service) DecryptedValues(ps *models.PluginSetting) map[string]string {
s.pluginSettingDecryptionCache.Lock()
defer s.pluginSettingDecryptionCache.Unlock()
s.decryptionCache.Lock()
defer s.decryptionCache.Unlock()
if item, present := s.pluginSettingDecryptionCache.cache[ps.Id]; present && ps.Updated.Equal(item.updated) {
if item, present := s.decryptionCache.cache[ps.Id]; present && ps.Updated.Equal(item.updated) {
return item.json
}
@@ -85,7 +78,7 @@ func (s *Service) DecryptedValues(ps *models.PluginSetting) map[string]string {
return map[string]string{}
}
s.pluginSettingDecryptionCache.cache[ps.Id] = cachedDecryptedJSON{
s.decryptionCache.cache[ps.Id] = cachedDecryptedJSON{
updated: ps.Updated,
json: json,
}
@@ -5,7 +5,6 @@ import (
"testing"
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/secrets"
"github.com/grafana/grafana/pkg/services/secrets/fakes"
@@ -19,7 +18,7 @@ func TestService_DecryptedValuesCache(t *testing.T) {
ctx := context.Background()
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())
psService := ProvideService(bus.New(), nil, secretsService)
psService := ProvideService(nil, secretsService)
encryptedJsonData, err := secretsService.EncryptJsonData(
ctx,
@@ -57,7 +56,7 @@ func TestService_DecryptedValuesCache(t *testing.T) {
ctx := context.Background()
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())
psService := ProvideService(bus.New(), nil, secretsService)
psService := ProvideService(nil, secretsService)
encryptedJsonData, err := secretsService.EncryptJsonData(
ctx,