mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Plugins: Refactor plugin settings service (#45967)
* tests passing * rename and rejig * move interface to package and rename to Store * new package * add import alias
This commit is contained in:
parent
6dea7275a6
commit
b54b438a24
@ -48,7 +48,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/services/login"
|
"github.com/grafana/grafana/pkg/services/login"
|
||||||
"github.com/grafana/grafana/pkg/services/ngalert"
|
"github.com/grafana/grafana/pkg/services/ngalert"
|
||||||
"github.com/grafana/grafana/pkg/services/notifications"
|
"github.com/grafana/grafana/pkg/services/notifications"
|
||||||
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
pluginSettings "github.com/grafana/grafana/pkg/services/pluginsettings/service"
|
||||||
"github.com/grafana/grafana/pkg/services/provisioning"
|
"github.com/grafana/grafana/pkg/services/provisioning"
|
||||||
"github.com/grafana/grafana/pkg/services/query"
|
"github.com/grafana/grafana/pkg/services/query"
|
||||||
"github.com/grafana/grafana/pkg/services/queryhistory"
|
"github.com/grafana/grafana/pkg/services/queryhistory"
|
||||||
@ -140,7 +140,7 @@ type HTTPServer struct {
|
|||||||
commentsService *comments.Service
|
commentsService *comments.Service
|
||||||
AlertNotificationService *alerting.AlertNotificationService
|
AlertNotificationService *alerting.AlertNotificationService
|
||||||
DashboardsnapshotsService *dashboardsnapshots.Service
|
DashboardsnapshotsService *dashboardsnapshots.Service
|
||||||
PluginSettings *pluginsettings.ServiceImpl
|
PluginSettings *pluginSettings.Service
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerOptions struct {
|
type ServerOptions struct {
|
||||||
@ -171,7 +171,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
|
|||||||
notificationService *notifications.NotificationService, dashboardService dashboards.DashboardService,
|
notificationService *notifications.NotificationService, dashboardService dashboards.DashboardService,
|
||||||
dashboardProvisioningService dashboards.DashboardProvisioningService, folderService dashboards.FolderService,
|
dashboardProvisioningService dashboards.DashboardProvisioningService, folderService dashboards.FolderService,
|
||||||
datasourcePermissionsService permissions.DatasourcePermissionsService, alertNotificationService *alerting.AlertNotificationService,
|
datasourcePermissionsService permissions.DatasourcePermissionsService, alertNotificationService *alerting.AlertNotificationService,
|
||||||
dashboardsnapshotsService *dashboardsnapshots.Service, commentsService *comments.Service, pluginSettings *pluginsettings.ServiceImpl,
|
dashboardsnapshotsService *dashboardsnapshots.Service, commentsService *comments.Service, pluginSettings *pluginSettings.Service,
|
||||||
) (*HTTPServer, error) {
|
) (*HTTPServer, error) {
|
||||||
web.Env = cfg.Env
|
web.Env = cfg.Env
|
||||||
m := web.New()
|
m := web.New()
|
||||||
|
@ -22,10 +22,10 @@ type templateData struct {
|
|||||||
|
|
||||||
// NewApiPluginProxy create a plugin proxy
|
// NewApiPluginProxy create a plugin proxy
|
||||||
func NewApiPluginProxy(ctx *models.ReqContext, proxyPath string, route *plugins.Route,
|
func NewApiPluginProxy(ctx *models.ReqContext, proxyPath string, route *plugins.Route,
|
||||||
appID string, cfg *setting.Cfg, store pluginsettings.Service, secretsService secrets.Service) *httputil.ReverseProxy {
|
appID string, cfg *setting.Cfg, pluginSettingsService pluginsettings.Service, secretsService secrets.Service) *httputil.ReverseProxy {
|
||||||
director := func(req *http.Request) {
|
director := func(req *http.Request) {
|
||||||
query := models.GetPluginSettingByIdQuery{OrgId: ctx.OrgId, PluginId: appID}
|
query := models.GetPluginSettingByIdQuery{OrgId: ctx.OrgId, PluginId: appID}
|
||||||
if err := store.GetPluginSettingById(ctx.Req.Context(), &query); err != nil {
|
if err := pluginSettingsService.GetPluginSettingById(ctx.Req.Context(), &query); err != nil {
|
||||||
ctx.JsonApiErr(500, "Failed to fetch plugin settings", err)
|
ctx.JsonApiErr(500, "Failed to fetch plugin settings", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -267,11 +267,12 @@ func TestPluginProxy(t *testing.T) {
|
|||||||
Resp: responseWriter,
|
Resp: responseWriter,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
store := &mockPluginsSettingsService{}
|
pluginSettingsService := &mockPluginsSettingsService{
|
||||||
store.pluginSetting = &models.PluginSetting{
|
pluginSetting: &models.PluginSetting{
|
||||||
SecureJsonData: map[string][]byte{},
|
SecureJsonData: map[string][]byte{},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
proxy := NewApiPluginProxy(ctx, "", route, "", &setting.Cfg{}, store, secretsService)
|
proxy := NewApiPluginProxy(ctx, "", route, "", &setting.Cfg{}, pluginSettingsService, secretsService)
|
||||||
proxy.ServeHTTP(ctx.Resp, ctx.Req)
|
proxy.ServeHTTP(ctx.Resp, ctx.Req)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@ -285,7 +286,7 @@ func TestPluginProxy(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getPluginProxiedRequest is a helper for easier setup of tests based on global config and ReqContext.
|
// getPluginProxiedRequest is a helper for easier setup of tests based on global config and ReqContext.
|
||||||
func getPluginProxiedRequest(t *testing.T, secretsService secrets.Service, ctx *models.ReqContext, cfg *setting.Cfg, route *plugins.Route, store pluginsettings.Service) *http.Request {
|
func getPluginProxiedRequest(t *testing.T, secretsService secrets.Service, ctx *models.ReqContext, cfg *setting.Cfg, route *plugins.Route, pluginSettingsService pluginsettings.Service) *http.Request {
|
||||||
// insert dummy route if none is specified
|
// insert dummy route if none is specified
|
||||||
if route == nil {
|
if route == nil {
|
||||||
route = &plugins.Route{
|
route = &plugins.Route{
|
||||||
@ -294,7 +295,7 @@ func getPluginProxiedRequest(t *testing.T, secretsService secrets.Service, ctx *
|
|||||||
ReqRole: models.ROLE_EDITOR,
|
ReqRole: models.ROLE_EDITOR,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
proxy := NewApiPluginProxy(ctx, "", route, "", cfg, store, secretsService)
|
proxy := NewApiPluginProxy(ctx, "", route, "", cfg, pluginSettingsService, secretsService)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, "/api/plugin-proxy/grafana-simple-app/api/v4/alerts", nil)
|
req, err := http.NewRequest(http.MethodGet, "/api/plugin-proxy/grafana-simple-app/api/v4/alerts", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@ -307,15 +308,19 @@ type mockPluginsSettingsService struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *mockPluginsSettingsService) GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error {
|
func (s *mockPluginsSettingsService) GetPluginSettings(_ context.Context, _ int64) ([]*models.PluginSettingInfoDTO, error) {
|
||||||
|
return nil, s.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *mockPluginsSettingsService) GetPluginSettingById(_ context.Context, query *models.GetPluginSettingByIdQuery) error {
|
||||||
query.Result = s.pluginSetting
|
query.Result = s.pluginSetting
|
||||||
return s.err
|
return s.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *mockPluginsSettingsService) UpdatePluginSettingVersion(ctx context.Context, cmd *models.UpdatePluginSettingVersionCmd) error {
|
func (s *mockPluginsSettingsService) UpdatePluginSettingVersion(_ context.Context, _ *models.UpdatePluginSettingVersionCmd) error {
|
||||||
return s.err
|
return s.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *mockPluginsSettingsService) UpdatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error {
|
func (s *mockPluginsSettingsService) UpdatePluginSetting(_ context.Context, _ *models.UpdatePluginSettingCmd) error {
|
||||||
return s.err
|
return s.err
|
||||||
}
|
}
|
||||||
|
@ -15,32 +15,32 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/plugins/adapters"
|
"github.com/grafana/grafana/pkg/plugins/adapters"
|
||||||
"github.com/grafana/grafana/pkg/services/datasources"
|
"github.com/grafana/grafana/pkg/services/datasources"
|
||||||
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
pluginSettings "github.com/grafana/grafana/pkg/services/pluginsettings/service"
|
||||||
"github.com/grafana/grafana/pkg/services/secrets"
|
"github.com/grafana/grafana/pkg/services/secrets"
|
||||||
"github.com/grafana/grafana/pkg/util/errutil"
|
"github.com/grafana/grafana/pkg/util/errutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ProvideService(bus bus.Bus, cacheService *localcache.CacheService, pluginStore plugins.Store,
|
func ProvideService(bus bus.Bus, cacheService *localcache.CacheService, pluginStore plugins.Store,
|
||||||
dataSourceCache datasources.CacheService, secretsService secrets.Service,
|
dataSourceCache datasources.CacheService, secretsService secrets.Service,
|
||||||
pluginSettingsService *pluginsettings.ServiceImpl) *Provider {
|
pluginSettingsService *pluginSettings.Service) *Provider {
|
||||||
return &Provider{
|
return &Provider{
|
||||||
Bus: bus,
|
bus: bus,
|
||||||
CacheService: cacheService,
|
cacheService: cacheService,
|
||||||
pluginStore: pluginStore,
|
pluginStore: pluginStore,
|
||||||
DataSourceCache: dataSourceCache,
|
dataSourceCache: dataSourceCache,
|
||||||
SecretsService: secretsService,
|
secretsService: secretsService,
|
||||||
PluginSettingsService: pluginSettingsService,
|
pluginSettingsService: pluginSettingsService,
|
||||||
logger: log.New("plugincontext"),
|
logger: log.New("plugincontext"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Provider struct {
|
type Provider struct {
|
||||||
Bus bus.Bus
|
bus bus.Bus
|
||||||
CacheService *localcache.CacheService
|
cacheService *localcache.CacheService
|
||||||
pluginStore plugins.Store
|
pluginStore plugins.Store
|
||||||
DataSourceCache datasources.CacheService
|
dataSourceCache datasources.CacheService
|
||||||
SecretsService secrets.Service
|
secretsService secrets.Service
|
||||||
PluginSettingsService *pluginsettings.ServiceImpl
|
pluginSettingsService *pluginSettings.Service
|
||||||
logger log.Logger
|
logger log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +70,7 @@ func (p *Provider) Get(ctx context.Context, pluginID string, datasourceUID strin
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return pc, false, errutil.Wrap("Failed to unmarshal plugin json data", err)
|
return pc, false, errutil.Wrap("Failed to unmarshal plugin json data", err)
|
||||||
}
|
}
|
||||||
decryptedSecureJSONData = p.PluginSettingsService.DecryptedValues(ps)
|
decryptedSecureJSONData = p.pluginSettingsService.DecryptedValues(ps)
|
||||||
updated = ps.Updated
|
updated = ps.Updated
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +86,7 @@ func (p *Provider) Get(ctx context.Context, pluginID string, datasourceUID strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
if datasourceUID != "" {
|
if datasourceUID != "" {
|
||||||
ds, err := p.DataSourceCache.GetDatasourceByUID(ctx, datasourceUID, user, skipCache)
|
ds, err := p.dataSourceCache.GetDatasourceByUID(ctx, datasourceUID, user, skipCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return pc, false, errutil.Wrap("Failed to get datasource", err)
|
return pc, false, errutil.Wrap("Failed to get datasource", err)
|
||||||
}
|
}
|
||||||
@ -106,7 +106,7 @@ const pluginSettingsCachePrefix = "plugin-setting-"
|
|||||||
func (p *Provider) getCachedPluginSettings(ctx context.Context, pluginID string, user *models.SignedInUser) (*models.PluginSetting, error) {
|
func (p *Provider) getCachedPluginSettings(ctx context.Context, pluginID string, user *models.SignedInUser) (*models.PluginSetting, error) {
|
||||||
cacheKey := pluginSettingsCachePrefix + pluginID
|
cacheKey := pluginSettingsCachePrefix + pluginID
|
||||||
|
|
||||||
if cached, found := p.CacheService.Get(cacheKey); found {
|
if cached, found := p.cacheService.Get(cacheKey); found {
|
||||||
ps := cached.(*models.PluginSetting)
|
ps := cached.(*models.PluginSetting)
|
||||||
if ps.OrgId == user.OrgId {
|
if ps.OrgId == user.OrgId {
|
||||||
return ps, nil
|
return ps, nil
|
||||||
@ -114,17 +114,17 @@ func (p *Provider) getCachedPluginSettings(ctx context.Context, pluginID string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
query := models.GetPluginSettingByIdQuery{PluginId: pluginID, OrgId: user.OrgId}
|
query := models.GetPluginSettingByIdQuery{PluginId: pluginID, OrgId: user.OrgId}
|
||||||
if err := p.PluginSettingsService.GetPluginSettingById(ctx, &query); err != nil {
|
if err := p.pluginSettingsService.GetPluginSettingById(ctx, &query); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
p.CacheService.Set(cacheKey, query.Result, pluginSettingsCacheTTL)
|
p.cacheService.Set(cacheKey, query.Result, pluginSettingsCacheTTL)
|
||||||
return query.Result, nil
|
return query.Result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provider) decryptSecureJsonDataFn() func(map[string][]byte) map[string]string {
|
func (p *Provider) decryptSecureJsonDataFn() func(map[string][]byte) map[string]string {
|
||||||
return func(m map[string][]byte) map[string]string {
|
return func(m map[string][]byte) map[string]string {
|
||||||
decryptedJsonData, err := p.SecretsService.DecryptJsonData(context.Background(), m)
|
decryptedJsonData, err := p.secretsService.DecryptJsonData(context.Background(), m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.logger.Error("Failed to decrypt secure json data", "error", err)
|
p.logger.Error("Failed to decrypt secure json data", "error", err)
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,6 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/services/ngalert"
|
"github.com/grafana/grafana/pkg/services/ngalert"
|
||||||
"github.com/grafana/grafana/pkg/services/notifications"
|
"github.com/grafana/grafana/pkg/services/notifications"
|
||||||
"github.com/grafana/grafana/pkg/services/plugindashboards"
|
"github.com/grafana/grafana/pkg/services/plugindashboards"
|
||||||
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
|
||||||
"github.com/grafana/grafana/pkg/services/provisioning"
|
"github.com/grafana/grafana/pkg/services/provisioning"
|
||||||
"github.com/grafana/grafana/pkg/services/rendering"
|
"github.com/grafana/grafana/pkg/services/rendering"
|
||||||
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
|
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
|
||||||
@ -35,7 +34,7 @@ func ProvideBackgroundServiceRegistry(
|
|||||||
metrics *metrics.InternalMetricsService, secretsService *secretsManager.SecretsService,
|
metrics *metrics.InternalMetricsService, secretsService *secretsManager.SecretsService,
|
||||||
remoteCache *remotecache.RemoteCache, thumbnailsService thumbs.Service,
|
remoteCache *remotecache.RemoteCache, thumbnailsService thumbs.Service,
|
||||||
// Need to make sure these are initialized, is there a better place to put them?
|
// Need to make sure these are initialized, is there a better place to put them?
|
||||||
_ *plugindashboards.Service, _ *dashboardsnapshots.Service, _ *pluginsettings.ServiceImpl,
|
_ *plugindashboards.Service, _ *dashboardsnapshots.Service,
|
||||||
_ *alerting.AlertNotificationService, _ serviceaccounts.Service,
|
_ *alerting.AlertNotificationService, _ serviceaccounts.Service,
|
||||||
) *BackgroundServiceRegistry {
|
) *BackgroundServiceRegistry {
|
||||||
return NewBackgroundServiceRegistry(
|
return NewBackgroundServiceRegistry(
|
||||||
|
@ -58,6 +58,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/services/oauthtoken"
|
"github.com/grafana/grafana/pkg/services/oauthtoken"
|
||||||
"github.com/grafana/grafana/pkg/services/plugindashboards"
|
"github.com/grafana/grafana/pkg/services/plugindashboards"
|
||||||
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
||||||
|
pluginSettings "github.com/grafana/grafana/pkg/services/pluginsettings/service"
|
||||||
"github.com/grafana/grafana/pkg/services/query"
|
"github.com/grafana/grafana/pkg/services/query"
|
||||||
"github.com/grafana/grafana/pkg/services/queryhistory"
|
"github.com/grafana/grafana/pkg/services/queryhistory"
|
||||||
"github.com/grafana/grafana/pkg/services/quota"
|
"github.com/grafana/grafana/pkg/services/quota"
|
||||||
@ -193,8 +194,8 @@ var wireBasicSet = wire.NewSet(
|
|||||||
dashboardsnapshots.ProvideService,
|
dashboardsnapshots.ProvideService,
|
||||||
datasourceservice.ProvideService,
|
datasourceservice.ProvideService,
|
||||||
wire.Bind(new(datasources.DataSourceService), new(*datasourceservice.Service)),
|
wire.Bind(new(datasources.DataSourceService), new(*datasourceservice.Service)),
|
||||||
pluginsettings.ProvideService,
|
pluginSettings.ProvideService,
|
||||||
wire.Bind(new(pluginsettings.Service), new(*pluginsettings.ServiceImpl)),
|
wire.Bind(new(pluginsettings.Service), new(*pluginSettings.Service)),
|
||||||
alerting.ProvideService,
|
alerting.ProvideService,
|
||||||
serviceaccountsmanager.ProvideServiceAccountsService,
|
serviceaccountsmanager.ProvideServiceAccountsService,
|
||||||
wire.Bind(new(serviceaccounts.Service), new(*serviceaccountsmanager.ServiceAccountsService)),
|
wire.Bind(new(serviceaccounts.Service), new(*serviceaccountsmanager.ServiceAccountsService)),
|
||||||
|
@ -10,32 +10,24 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
||||||
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
||||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type pluginSettingsStore interface {
|
func ProvideService(bus bus.Bus, pluginStore plugins.Store, pluginDashboardManager plugins.PluginDashboardManager,
|
||||||
GetPluginSettings(ctx context.Context, orgID int64) ([]*models.PluginSettingInfoDTO, error)
|
dashboardImportService dashboardimport.Service, pluginSettingsService pluginsettings.Service) *Service {
|
||||||
}
|
s := newService(bus, pluginStore, pluginDashboardManager, dashboardImportService, pluginSettingsService)
|
||||||
|
|
||||||
func ProvideService(sqlStore *sqlstore.SQLStore, bus bus.Bus, pluginStore plugins.Store,
|
|
||||||
pluginDashboardManager plugins.PluginDashboardManager, dashboardImportService dashboardimport.Service,
|
|
||||||
pluginSettingsStore *pluginsettings.ServiceImpl) *Service {
|
|
||||||
s := new(sqlStore, bus, pluginStore, pluginDashboardManager, dashboardImportService, pluginSettingsStore)
|
|
||||||
s.updateAppDashboards()
|
s.updateAppDashboards()
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func new(pluginSettingsStore pluginSettingsStore, bus bus.Bus, pluginStore plugins.Store,
|
func newService(bus bus.Bus, pluginStore plugins.Store, pluginDashboardManager plugins.PluginDashboardManager,
|
||||||
pluginDashboardManager plugins.PluginDashboardManager, dashboardImportService dashboardimport.Service,
|
dashboardImportService dashboardimport.Service, pluginSettingsService pluginsettings.Service) *Service {
|
||||||
pluginSettings pluginsettings.Service) *Service {
|
|
||||||
s := &Service{
|
s := &Service{
|
||||||
pluginSettingsStore: pluginSettingsStore,
|
|
||||||
bus: bus,
|
bus: bus,
|
||||||
pluginStore: pluginStore,
|
pluginStore: pluginStore,
|
||||||
pluginDashboardManager: pluginDashboardManager,
|
pluginDashboardManager: pluginDashboardManager,
|
||||||
dashboardImportService: dashboardImportService,
|
dashboardImportService: dashboardImportService,
|
||||||
|
pluginSettingsService: pluginSettingsService,
|
||||||
logger: log.New("plugindashboards"),
|
logger: log.New("plugindashboards"),
|
||||||
pluginSettings: pluginSettings,
|
|
||||||
}
|
}
|
||||||
bus.AddEventListener(s.handlePluginStateChanged)
|
bus.AddEventListener(s.handlePluginStateChanged)
|
||||||
|
|
||||||
@ -43,19 +35,18 @@ func new(pluginSettingsStore pluginSettingsStore, bus bus.Bus, pluginStore plugi
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
pluginSettingsStore pluginSettingsStore
|
|
||||||
bus bus.Bus
|
bus bus.Bus
|
||||||
pluginStore plugins.Store
|
pluginStore plugins.Store
|
||||||
pluginDashboardManager plugins.PluginDashboardManager
|
pluginDashboardManager plugins.PluginDashboardManager
|
||||||
dashboardImportService dashboardimport.Service
|
dashboardImportService dashboardimport.Service
|
||||||
|
pluginSettingsService pluginsettings.Service
|
||||||
logger log.Logger
|
logger log.Logger
|
||||||
pluginSettings pluginsettings.Service
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) updateAppDashboards() {
|
func (s *Service) updateAppDashboards() {
|
||||||
s.logger.Debug("Looking for app dashboard updates")
|
s.logger.Debug("Looking for app dashboard updates")
|
||||||
|
|
||||||
pluginSettings, err := s.pluginSettingsStore.GetPluginSettings(context.Background(), 0)
|
pluginSettings, err := s.pluginSettingsService.GetPluginSettings(context.Background(), 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Error("Failed to get all plugin settings", "error", err)
|
s.logger.Error("Failed to get all plugin settings", "error", err)
|
||||||
return
|
return
|
||||||
@ -111,7 +102,7 @@ func (s *Service) syncPluginDashboards(ctx context.Context, plugin plugins.Plugi
|
|||||||
|
|
||||||
// update version in plugin_setting table to mark that we have processed the update
|
// update version in plugin_setting table to mark that we have processed the update
|
||||||
query := models.GetPluginSettingByIdQuery{PluginId: plugin.ID, OrgId: orgID}
|
query := models.GetPluginSettingByIdQuery{PluginId: plugin.ID, OrgId: orgID}
|
||||||
if err := s.pluginSettings.GetPluginSettingById(ctx, &query); err != nil {
|
if err := s.pluginSettingsService.GetPluginSettingById(ctx, &query); err != nil {
|
||||||
s.logger.Error("Failed to read plugin setting by ID", "error", err)
|
s.logger.Error("Failed to read plugin setting by ID", "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -123,7 +114,7 @@ func (s *Service) syncPluginDashboards(ctx context.Context, plugin plugins.Plugi
|
|||||||
PluginVersion: plugin.Info.Version,
|
PluginVersion: plugin.Info.Version,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.pluginSettings.UpdatePluginSettingVersion(ctx, &cmd); err != nil {
|
if err := s.pluginSettingsService.UpdatePluginSettingVersion(ctx, &cmd); err != nil {
|
||||||
s.logger.Error("Failed to update plugin setting version", "error", err)
|
s.logger.Error("Failed to update plugin setting version", "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
"github.com/grafana/grafana/pkg/services/dashboardimport"
|
||||||
|
"github.com/grafana/grafana/pkg/services/pluginsettings/service"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,8 +19,8 @@ func TestService(t *testing.T) {
|
|||||||
scenarioInput{}, func(ctx *scenarioContext) {
|
scenarioInput{}, func(ctx *scenarioContext) {
|
||||||
ctx.s.updateAppDashboards()
|
ctx.s.updateAppDashboards()
|
||||||
|
|
||||||
require.Len(t, ctx.getPluginSettingsArgs, 1)
|
require.Len(t, ctx.pluginSettingsService.getPluginSettingsArgs, 1)
|
||||||
require.Equal(t, int64(0), ctx.getPluginSettingsArgs[0])
|
require.Equal(t, int64(0), ctx.pluginSettingsService.getPluginSettingsArgs[0])
|
||||||
require.Empty(t, ctx.deleteDashboardArgs)
|
require.Empty(t, ctx.deleteDashboardArgs)
|
||||||
require.Empty(t, ctx.importDashboardArgs)
|
require.Empty(t, ctx.importDashboardArgs)
|
||||||
})
|
})
|
||||||
@ -41,7 +42,7 @@ func TestService(t *testing.T) {
|
|||||||
}, func(ctx *scenarioContext) {
|
}, func(ctx *scenarioContext) {
|
||||||
ctx.s.updateAppDashboards()
|
ctx.s.updateAppDashboards()
|
||||||
|
|
||||||
require.NotEmpty(t, ctx.getPluginSettingsArgs)
|
require.NotEmpty(t, ctx.pluginSettingsService.getPluginSettingsArgs)
|
||||||
require.Empty(t, ctx.deleteDashboardArgs)
|
require.Empty(t, ctx.deleteDashboardArgs)
|
||||||
require.Empty(t, ctx.importDashboardArgs)
|
require.Empty(t, ctx.importDashboardArgs)
|
||||||
})
|
})
|
||||||
@ -63,7 +64,7 @@ func TestService(t *testing.T) {
|
|||||||
}, func(ctx *scenarioContext) {
|
}, func(ctx *scenarioContext) {
|
||||||
ctx.s.updateAppDashboards()
|
ctx.s.updateAppDashboards()
|
||||||
|
|
||||||
require.NotEmpty(t, ctx.getPluginSettingsArgs)
|
require.NotEmpty(t, ctx.pluginSettingsService.getPluginSettingsArgs)
|
||||||
require.Empty(t, ctx.deleteDashboardArgs)
|
require.Empty(t, ctx.deleteDashboardArgs)
|
||||||
require.Empty(t, ctx.importDashboardArgs)
|
require.Empty(t, ctx.importDashboardArgs)
|
||||||
})
|
})
|
||||||
@ -95,7 +96,7 @@ func TestService(t *testing.T) {
|
|||||||
}, func(ctx *scenarioContext) {
|
}, func(ctx *scenarioContext) {
|
||||||
ctx.s.updateAppDashboards()
|
ctx.s.updateAppDashboards()
|
||||||
|
|
||||||
require.NotEmpty(t, ctx.getPluginSettingsArgs)
|
require.NotEmpty(t, ctx.pluginSettingsService.getPluginSettingsArgs)
|
||||||
require.Empty(t, ctx.deleteDashboardArgs)
|
require.Empty(t, ctx.deleteDashboardArgs)
|
||||||
require.Empty(t, ctx.importDashboardArgs)
|
require.Empty(t, ctx.importDashboardArgs)
|
||||||
})
|
})
|
||||||
@ -130,7 +131,7 @@ func TestService(t *testing.T) {
|
|||||||
}, func(ctx *scenarioContext) {
|
}, func(ctx *scenarioContext) {
|
||||||
ctx.s.updateAppDashboards()
|
ctx.s.updateAppDashboards()
|
||||||
|
|
||||||
require.NotEmpty(t, ctx.getPluginSettingsArgs)
|
require.NotEmpty(t, ctx.pluginSettingsService.getPluginSettingsArgs)
|
||||||
require.Empty(t, ctx.deleteDashboardArgs)
|
require.Empty(t, ctx.deleteDashboardArgs)
|
||||||
require.Empty(t, ctx.importDashboardArgs)
|
require.Empty(t, ctx.importDashboardArgs)
|
||||||
})
|
})
|
||||||
@ -178,7 +179,7 @@ func TestService(t *testing.T) {
|
|||||||
}, func(ctx *scenarioContext) {
|
}, func(ctx *scenarioContext) {
|
||||||
ctx.s.updateAppDashboards()
|
ctx.s.updateAppDashboards()
|
||||||
|
|
||||||
require.NotEmpty(t, ctx.getPluginSettingsArgs)
|
require.NotEmpty(t, ctx.pluginSettingsService.getPluginSettingsArgs)
|
||||||
require.Len(t, ctx.deleteDashboardArgs, 1)
|
require.Len(t, ctx.deleteDashboardArgs, 1)
|
||||||
require.Equal(t, int64(2), ctx.deleteDashboardArgs[0].OrgId)
|
require.Equal(t, int64(2), ctx.deleteDashboardArgs[0].OrgId)
|
||||||
require.Equal(t, int64(3), ctx.deleteDashboardArgs[0].Id)
|
require.Equal(t, int64(3), ctx.deleteDashboardArgs[0].Id)
|
||||||
@ -334,18 +335,6 @@ func TestService(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type pluginSettingsStoreMock struct {
|
|
||||||
getPluginSettingsFunc func(ctx context.Context, orgID int64) ([]*models.PluginSettingInfoDTO, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *pluginSettingsStoreMock) GetPluginSettings(ctx context.Context, orgID int64) ([]*models.PluginSettingInfoDTO, error) {
|
|
||||||
if m.getPluginSettingsFunc != nil {
|
|
||||||
return m.getPluginSettingsFunc(ctx, orgID)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type pluginStoreMock struct {
|
type pluginStoreMock struct {
|
||||||
plugins.Store
|
plugins.Store
|
||||||
pluginFunc func(ctx context.Context, pluginID string) (plugins.PluginDTO, bool)
|
pluginFunc func(ctx context.Context, pluginID string) (plugins.PluginDTO, bool)
|
||||||
@ -394,6 +383,41 @@ func (m *importDashboardServiceMock) ImportDashboard(ctx context.Context, req *d
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type pluginsSettingsServiceMock struct {
|
||||||
|
service.Service
|
||||||
|
|
||||||
|
storedPluginSettings []*models.PluginSettingInfoDTO
|
||||||
|
getPluginSettingsArgs []int64
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *pluginsSettingsServiceMock) GetPluginSettings(_ context.Context, orgID int64) ([]*models.PluginSettingInfoDTO, error) {
|
||||||
|
s.getPluginSettingsArgs = append(s.getPluginSettingsArgs, orgID)
|
||||||
|
return s.storedPluginSettings, s.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *pluginsSettingsServiceMock) GetPluginSettingById(_ context.Context, query *models.GetPluginSettingByIdQuery) error {
|
||||||
|
for _, setting := range s.storedPluginSettings {
|
||||||
|
if setting.PluginId == query.PluginId {
|
||||||
|
query.Result = &models.PluginSetting{
|
||||||
|
PluginId: query.PluginId,
|
||||||
|
OrgId: query.OrgId,
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *pluginsSettingsServiceMock) UpdatePluginSettingVersion(_ context.Context, _ *models.UpdatePluginSettingVersionCmd) error {
|
||||||
|
return s.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *pluginsSettingsServiceMock) UpdatePluginSetting(_ context.Context, _ *models.UpdatePluginSettingCmd) error {
|
||||||
|
return s.err
|
||||||
|
}
|
||||||
|
|
||||||
type scenarioInput struct {
|
type scenarioInput struct {
|
||||||
storedPluginSettings []*models.PluginSettingInfoDTO
|
storedPluginSettings []*models.PluginSettingInfoDTO
|
||||||
installedPlugins []plugins.PluginDTO
|
installedPlugins []plugins.PluginDTO
|
||||||
@ -403,8 +427,7 @@ type scenarioInput struct {
|
|||||||
type scenarioContext struct {
|
type scenarioContext struct {
|
||||||
t *testing.T
|
t *testing.T
|
||||||
bus bus.Bus
|
bus bus.Bus
|
||||||
pluginSettingsStore pluginSettingsStore
|
pluginSettingsService *pluginsSettingsServiceMock
|
||||||
getPluginSettingsArgs []int64
|
|
||||||
pluginStore plugins.Store
|
pluginStore plugins.Store
|
||||||
pluginDashboardManager plugins.PluginDashboardManager
|
pluginDashboardManager plugins.PluginDashboardManager
|
||||||
importDashboardService dashboardimport.Service
|
importDashboardService dashboardimport.Service
|
||||||
@ -422,7 +445,6 @@ func scenario(t *testing.T, desc string, input scenarioInput, f func(ctx *scenar
|
|||||||
sCtx := &scenarioContext{
|
sCtx := &scenarioContext{
|
||||||
t: t,
|
t: t,
|
||||||
bus: bus.New(),
|
bus: bus.New(),
|
||||||
getPluginSettingsArgs: []int64{},
|
|
||||||
importDashboardArgs: []*dashboardimport.ImportDashboardRequest{},
|
importDashboardArgs: []*dashboardimport.ImportDashboardRequest{},
|
||||||
deleteDashboardArgs: []*models.DeleteDashboardCommand{},
|
deleteDashboardArgs: []*models.DeleteDashboardCommand{},
|
||||||
getPluginSettingsByIdArgs: []*models.GetPluginSettingByIdQuery{},
|
getPluginSettingsByIdArgs: []*models.GetPluginSettingByIdQuery{},
|
||||||
@ -430,15 +452,6 @@ func scenario(t *testing.T, desc string, input scenarioInput, f func(ctx *scenar
|
|||||||
getDashboardsByPluginIdQueryArgs: []*models.GetDashboardsByPluginIdQuery{},
|
getDashboardsByPluginIdQueryArgs: []*models.GetDashboardsByPluginIdQuery{},
|
||||||
}
|
}
|
||||||
|
|
||||||
getPluginSettings := func(_ context.Context, orgID int64) ([]*models.PluginSettingInfoDTO, error) {
|
|
||||||
sCtx.getPluginSettingsArgs = append(sCtx.getPluginSettingsArgs, orgID)
|
|
||||||
return input.storedPluginSettings, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
sCtx.pluginSettingsStore = &pluginSettingsStoreMock{
|
|
||||||
getPluginSettingsFunc: getPluginSettings,
|
|
||||||
}
|
|
||||||
|
|
||||||
getPlugin := func(ctx context.Context, pluginID string) (plugins.PluginDTO, bool) {
|
getPlugin := func(ctx context.Context, pluginID string) (plugins.PluginDTO, bool) {
|
||||||
for _, p := range input.installedPlugins {
|
for _, p := range input.installedPlugins {
|
||||||
if p.ID == pluginID {
|
if p.ID == pluginID {
|
||||||
@ -449,6 +462,10 @@ func scenario(t *testing.T, desc string, input scenarioInput, f func(ctx *scenar
|
|||||||
return plugins.PluginDTO{}, false
|
return plugins.PluginDTO{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sCtx.pluginSettingsService = &pluginsSettingsServiceMock{
|
||||||
|
storedPluginSettings: input.storedPluginSettings,
|
||||||
|
}
|
||||||
|
|
||||||
sCtx.pluginStore = &pluginStoreMock{
|
sCtx.pluginStore = &pluginStoreMock{
|
||||||
pluginFunc: getPlugin,
|
pluginFunc: getPlugin,
|
||||||
}
|
}
|
||||||
@ -498,14 +515,6 @@ func scenario(t *testing.T, desc string, input scenarioInput, f func(ctx *scenar
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
mock := &mockPluginsSettingsService{}
|
|
||||||
for _, p := range input.storedPluginSettings {
|
|
||||||
mock.pluginSetting = &models.PluginSetting{
|
|
||||||
PluginId: p.PluginId,
|
|
||||||
OrgId: p.OrgId,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sCtx.bus.AddHandler(func(ctx context.Context, query *models.GetDashboardsByPluginIdQuery) error {
|
sCtx.bus.AddHandler(func(ctx context.Context, query *models.GetDashboardsByPluginIdQuery) error {
|
||||||
sCtx.getDashboardsByPluginIdQueryArgs = append(sCtx.getDashboardsByPluginIdQueryArgs, query)
|
sCtx.getDashboardsByPluginIdQueryArgs = append(sCtx.getDashboardsByPluginIdQueryArgs, query)
|
||||||
dashboards := []*models.Dashboard{}
|
dashboards := []*models.Dashboard{}
|
||||||
@ -536,7 +545,7 @@ func scenario(t *testing.T, desc string, input scenarioInput, f func(ctx *scenar
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
sCtx.s = new(sCtx.pluginSettingsStore, sCtx.bus, sCtx.pluginStore, sCtx.pluginDashboardManager, sCtx.importDashboardService, mock)
|
sCtx.s = newService(sCtx.bus, sCtx.pluginStore, sCtx.pluginDashboardManager, sCtx.importDashboardService, sCtx.pluginSettingsService)
|
||||||
|
|
||||||
t.Cleanup(bus.ClearBusHandlers)
|
t.Cleanup(bus.ClearBusHandlers)
|
||||||
|
|
||||||
@ -544,21 +553,3 @@ func scenario(t *testing.T, desc string, input scenarioInput, f func(ctx *scenar
|
|||||||
f(sCtx)
|
f(sCtx)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type mockPluginsSettingsService struct {
|
|
||||||
pluginSetting *models.PluginSetting
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *mockPluginsSettingsService) GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error {
|
|
||||||
query.Result = s.pluginSetting
|
|
||||||
return s.err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *mockPluginsSettingsService) UpdatePluginSettingVersion(ctx context.Context, cmd *models.UpdatePluginSettingVersionCmd) error {
|
|
||||||
return s.err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *mockPluginsSettingsService) UpdatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error {
|
|
||||||
return s.err
|
|
||||||
}
|
|
||||||
|
14
pkg/services/pluginsettings/ifaces.go
Normal file
14
pkg/services/pluginsettings/ifaces.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package pluginsettings
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service interface {
|
||||||
|
GetPluginSettings(ctx context.Context, orgID int64) ([]*models.PluginSettingInfoDTO, error)
|
||||||
|
GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error
|
||||||
|
UpdatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error
|
||||||
|
UpdatePluginSettingVersion(ctx context.Context, cmd *models.UpdatePluginSettingVersionCmd) error
|
||||||
|
}
|
@ -1,96 +0,0 @@
|
|||||||
package pluginsettings
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"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"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ServiceImpl struct {
|
|
||||||
Bus bus.Bus
|
|
||||||
SQLStore *sqlstore.SQLStore
|
|
||||||
SecretsService secrets.Service
|
|
||||||
|
|
||||||
logger log.Logger
|
|
||||||
pluginSettingDecryptionCache secureJSONDecryptionCache
|
|
||||||
}
|
|
||||||
|
|
||||||
type Service interface {
|
|
||||||
GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error
|
|
||||||
UpdatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error
|
|
||||||
UpdatePluginSettingVersion(ctx context.Context, cmd *models.UpdatePluginSettingVersionCmd) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type cachedDecryptedJSON struct {
|
|
||||||
updated time.Time
|
|
||||||
json map[string]string
|
|
||||||
}
|
|
||||||
|
|
||||||
type secureJSONDecryptionCache struct {
|
|
||||||
cache map[int64]cachedDecryptedJSON
|
|
||||||
sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func ProvideService(bus bus.Bus, store *sqlstore.SQLStore, secretsService secrets.Service) *ServiceImpl {
|
|
||||||
s := &ServiceImpl{
|
|
||||||
Bus: bus,
|
|
||||||
SQLStore: store,
|
|
||||||
SecretsService: secretsService,
|
|
||||||
logger: log.New("pluginsettings"),
|
|
||||||
pluginSettingDecryptionCache: secureJSONDecryptionCache{
|
|
||||||
cache: make(map[int64]cachedDecryptedJSON),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
s.Bus.AddHandler(s.GetPluginSettingById)
|
|
||||||
s.Bus.AddHandler(s.UpdatePluginSetting)
|
|
||||||
s.Bus.AddHandler(s.UpdatePluginSettingVersion)
|
|
||||||
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *ServiceImpl) GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error {
|
|
||||||
return s.SQLStore.GetPluginSettingById(ctx, query)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *ServiceImpl) UpdatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error {
|
|
||||||
var err error
|
|
||||||
cmd.EncryptedSecureJsonData, err = s.SecretsService.EncryptJsonData(ctx, cmd.SecureJsonData, secrets.WithoutScope())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.SQLStore.UpdatePluginSetting(ctx, cmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *ServiceImpl) UpdatePluginSettingVersion(ctx context.Context, cmd *models.UpdatePluginSettingVersionCmd) error {
|
|
||||||
return s.SQLStore.UpdatePluginSettingVersion(ctx, cmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *ServiceImpl) DecryptedValues(ps *models.PluginSetting) map[string]string {
|
|
||||||
s.pluginSettingDecryptionCache.Lock()
|
|
||||||
defer s.pluginSettingDecryptionCache.Unlock()
|
|
||||||
|
|
||||||
if item, present := s.pluginSettingDecryptionCache.cache[ps.Id]; present && ps.Updated.Equal(item.updated) {
|
|
||||||
return item.json
|
|
||||||
}
|
|
||||||
|
|
||||||
json, err := s.SecretsService.DecryptJsonData(context.Background(), ps.SecureJsonData)
|
|
||||||
if err != nil {
|
|
||||||
s.logger.Error("Failed to decrypt secure json data", "error", err)
|
|
||||||
return map[string]string{}
|
|
||||||
}
|
|
||||||
|
|
||||||
s.pluginSettingDecryptionCache.cache[ps.Id] = cachedDecryptedJSON{
|
|
||||||
updated: ps.Updated,
|
|
||||||
json: json,
|
|
||||||
}
|
|
||||||
|
|
||||||
return json
|
|
||||||
}
|
|
94
pkg/services/pluginsettings/service/service.go
Normal file
94
pkg/services/pluginsettings/service/service.go
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"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 {
|
||||||
|
s := &Service{
|
||||||
|
bus: bus,
|
||||||
|
sqlStore: store,
|
||||||
|
secretsService: secretsService,
|
||||||
|
logger: log.New("pluginsettings"),
|
||||||
|
pluginSettingDecryptionCache: secureJSONDecryptionCache{
|
||||||
|
cache: make(map[int64]cachedDecryptedJSON),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
logger log.Logger
|
||||||
|
pluginSettingDecryptionCache secureJSONDecryptionCache
|
||||||
|
}
|
||||||
|
|
||||||
|
type cachedDecryptedJSON struct {
|
||||||
|
updated time.Time
|
||||||
|
json map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
type secureJSONDecryptionCache struct {
|
||||||
|
cache map[int64]cachedDecryptedJSON
|
||||||
|
sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetPluginSettings(ctx context.Context, orgID int64) ([]*models.PluginSettingInfoDTO, error) {
|
||||||
|
return s.sqlStore.GetPluginSettings(ctx, orgID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error {
|
||||||
|
return s.sqlStore.GetPluginSettingById(ctx, query)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) UpdatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error {
|
||||||
|
var err error
|
||||||
|
cmd.EncryptedSecureJsonData, err = s.secretsService.EncryptJsonData(ctx, cmd.SecureJsonData, secrets.WithoutScope())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.sqlStore.UpdatePluginSetting(ctx, cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) UpdatePluginSettingVersion(ctx context.Context, cmd *models.UpdatePluginSettingVersionCmd) error {
|
||||||
|
return s.sqlStore.UpdatePluginSettingVersion(ctx, cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) DecryptedValues(ps *models.PluginSetting) map[string]string {
|
||||||
|
s.pluginSettingDecryptionCache.Lock()
|
||||||
|
defer s.pluginSettingDecryptionCache.Unlock()
|
||||||
|
|
||||||
|
if item, present := s.pluginSettingDecryptionCache.cache[ps.Id]; present && ps.Updated.Equal(item.updated) {
|
||||||
|
return item.json
|
||||||
|
}
|
||||||
|
|
||||||
|
json, err := s.secretsService.DecryptJsonData(context.Background(), ps.SecureJsonData)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("Failed to decrypt secure json data", "error", err)
|
||||||
|
return map[string]string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
s.pluginSettingDecryptionCache.cache[ps.Id] = cachedDecryptedJSON{
|
||||||
|
updated: ps.Updated,
|
||||||
|
json: json,
|
||||||
|
}
|
||||||
|
|
||||||
|
return json
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package pluginsettings
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
Loading…
Reference in New Issue
Block a user