Files
grafana/pkg/plugins/plugincontext/plugincontext.go
T

139 lines
4.8 KiB
Go
Raw Normal View History

2021-03-23 20:24:08 +03:00
package plugincontext
import (
"context"
2021-03-23 20:24:08 +03:00
"encoding/json"
"errors"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
2021-03-23 20:24:08 +03:00
"github.com/grafana/grafana/pkg/infra/localcache"
"github.com/grafana/grafana/pkg/infra/log"
2021-03-23 20:24:08 +03:00
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/adapters"
"github.com/grafana/grafana/pkg/services/datasources"
2022-03-04 17:09:50 +01:00
"github.com/grafana/grafana/pkg/services/pluginsettings"
2021-03-23 20:24:08 +03:00
"github.com/grafana/grafana/pkg/util/errutil"
)
2022-04-13 15:24:13 +02:00
func ProvideService(cacheService *localcache.CacheService, pluginStore plugins.Store,
dataSourceCache datasources.CacheService, dataSourceService datasources.DataSourceService,
2022-03-04 17:09:50 +01:00
pluginSettingsService pluginsettings.Service) *Provider {
return &Provider{
cacheService: cacheService,
pluginStore: pluginStore,
dataSourceCache: dataSourceCache,
dataSourceService: dataSourceService,
pluginSettingsService: pluginSettingsService,
logger: log.New("plugincontext"),
}
2021-03-23 20:24:08 +03:00
}
type Provider struct {
cacheService *localcache.CacheService
pluginStore plugins.Store
dataSourceCache datasources.CacheService
dataSourceService datasources.DataSourceService
2022-03-04 17:09:50 +01:00
pluginSettingsService pluginsettings.Service
logger log.Logger
2021-03-23 20:24:08 +03:00
}
// Get allows getting plugin context by its ID. If datasourceUID is not empty string
2021-03-23 20:24:08 +03:00
// then PluginContext.DataSourceInstanceSettings will be resolved and appended to
// returned context.
func (p *Provider) Get(ctx context.Context, pluginID string, user *models.SignedInUser) (backend.PluginContext, bool, error) {
return p.pluginContext(ctx, pluginID, user)
}
// GetWithDataSource allows getting plugin context by its ID and PluginContext.DataSourceInstanceSettings will be
// resolved and appended to the returned context.
func (p *Provider) GetWithDataSource(ctx context.Context, pluginID string, user *models.SignedInUser, ds *models.DataSource) (backend.PluginContext, bool, error) {
pCtx, exists, err := p.pluginContext(ctx, pluginID, user)
if err != nil {
return pCtx, exists, err
}
datasourceSettings, err := adapters.ModelToInstanceSettings(ds, p.decryptSecureJsonDataFn(ctx))
if err != nil {
return pCtx, exists, errutil.Wrap("Failed to convert datasource", err)
}
pCtx.DataSourceInstanceSettings = datasourceSettings
return pCtx, true, nil
}
const pluginSettingsCacheTTL = 5 * time.Second
const pluginSettingsCachePrefix = "plugin-setting-"
func (p *Provider) pluginContext(ctx context.Context, pluginID string, user *models.SignedInUser) (backend.PluginContext, bool, error) {
plugin, exists := p.pluginStore.Plugin(ctx, pluginID)
if !exists {
return backend.PluginContext{}, false, nil
2021-03-23 20:24:08 +03:00
}
jsonData := json.RawMessage{}
decryptedSecureJSONData := map[string]string{}
var updated time.Time
ps, err := p.getCachedPluginSettings(ctx, pluginID, user)
2021-03-23 20:24:08 +03:00
if err != nil {
// models.ErrPluginSettingNotFound is expected if there's no row found for plugin setting in database (if non-app plugin).
// If it's not this expected error something is wrong with cache or database and we return the error to the client.
if !errors.Is(err, models.ErrPluginSettingNotFound) {
return backend.PluginContext{}, false, errutil.Wrap("Failed to get plugin settings", err)
2021-03-23 20:24:08 +03:00
}
} else {
2022-03-18 20:49:13 +01:00
jsonData, err = json.Marshal(ps.JSONData)
2021-03-23 20:24:08 +03:00
if err != nil {
return backend.PluginContext{}, false, errutil.Wrap("Failed to unmarshal plugin json data", err)
2021-03-23 20:24:08 +03:00
}
decryptedSecureJSONData = p.pluginSettingsService.DecryptedValues(ps)
2021-03-23 20:24:08 +03:00
updated = ps.Updated
}
return backend.PluginContext{
2021-03-23 20:24:08 +03:00
OrgID: user.OrgId,
PluginID: plugin.ID,
2021-03-23 20:24:08 +03:00
User: adapters.BackendUserFromSignedInUser(user),
AppInstanceSettings: &backend.AppInstanceSettings{
JSONData: jsonData,
DecryptedSecureJSONData: decryptedSecureJSONData,
Updated: updated,
},
}, true, nil
2021-03-23 20:24:08 +03:00
}
2022-03-18 20:49:13 +01:00
func (p *Provider) getCachedPluginSettings(ctx context.Context, pluginID string, user *models.SignedInUser) (*pluginsettings.DTO, error) {
2021-03-23 20:24:08 +03:00
cacheKey := pluginSettingsCachePrefix + pluginID
if cached, found := p.cacheService.Get(cacheKey); found {
2022-03-18 20:49:13 +01:00
ps := cached.(*pluginsettings.DTO)
if ps.OrgID == user.OrgId {
2021-03-23 20:24:08 +03:00
return ps, nil
}
}
2022-03-18 20:49:13 +01:00
ps, err := p.pluginSettingsService.GetPluginSettingByPluginID(ctx, &pluginsettings.GetByPluginIDArgs{
PluginID: pluginID,
OrgID: user.OrgId,
})
if err != nil {
2021-03-23 20:24:08 +03:00
return nil, err
}
2022-03-18 20:49:13 +01:00
p.cacheService.Set(cacheKey, ps, pluginSettingsCacheTTL)
return ps, nil
2021-03-23 20:24:08 +03:00
}
func (p *Provider) decryptSecureJsonDataFn(ctx context.Context) func(ds *models.DataSource) map[string]string {
return func(ds *models.DataSource) map[string]string {
decryptedJsonData, err := p.dataSourceService.DecryptedValues(ctx, ds)
if err != nil {
p.logger.Error("Failed to decrypt secure json data", "error", err)
}
return decryptedJsonData
}
}