2021-03-23 20:24:08 +03:00
|
|
|
package plugincontext
|
|
|
|
|
|
|
|
|
|
import (
|
2021-10-07 16:33:50 +02:00
|
|
|
"context"
|
2021-03-23 20:24:08 +03:00
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
2022-06-03 03:24:24 -04:00
|
|
|
"fmt"
|
2021-03-23 20:24:08 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2021-11-01 09:53:33 +00:00
|
|
|
|
2021-03-23 20:24:08 +03:00
|
|
|
"github.com/grafana/grafana/pkg/infra/localcache"
|
|
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2023-02-28 15:10:27 +00:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/log"
|
2021-03-23 20:24:08 +03:00
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
2023-03-07 16:22:30 +00:00
|
|
|
"github.com/grafana/grafana/pkg/services/pluginsintegration/adapters"
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginsettings"
|
2022-08-10 11:56:48 +02:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2021-03-23 20:24:08 +03:00
|
|
|
)
|
|
|
|
|
|
2022-04-13 15:24:13 +02:00
|
|
|
func ProvideService(cacheService *localcache.CacheService, pluginStore plugins.Store,
|
2022-04-25 13:57:45 -03:00
|
|
|
dataSourceCache datasources.CacheService, dataSourceService datasources.DataSourceService,
|
2022-03-04 17:09:50 +01:00
|
|
|
pluginSettingsService pluginsettings.Service) *Provider {
|
2021-08-25 15:11:22 +02:00
|
|
|
return &Provider{
|
2022-03-03 11:39:15 +01:00
|
|
|
cacheService: cacheService,
|
2021-11-01 09:53:33 +00:00
|
|
|
pluginStore: pluginStore,
|
2022-03-03 11:39:15 +01:00
|
|
|
dataSourceCache: dataSourceCache,
|
2022-04-25 13:57:45 -03:00
|
|
|
dataSourceService: dataSourceService,
|
2022-03-03 11:39:15 +01:00
|
|
|
pluginSettingsService: pluginSettingsService,
|
2021-10-07 16:33:50 +02:00
|
|
|
logger: log.New("plugincontext"),
|
2021-08-25 15:11:22 +02:00
|
|
|
}
|
2021-03-23 20:24:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Provider struct {
|
2022-03-03 11:39:15 +01:00
|
|
|
cacheService *localcache.CacheService
|
2021-11-01 09:53:33 +00:00
|
|
|
pluginStore plugins.Store
|
2022-03-03 11:39:15 +01:00
|
|
|
dataSourceCache datasources.CacheService
|
2022-04-25 13:57:45 -03:00
|
|
|
dataSourceService datasources.DataSourceService
|
2022-03-04 17:09:50 +01:00
|
|
|
pluginSettingsService pluginsettings.Service
|
2021-10-07 16:33:50 +02:00
|
|
|
logger log.Logger
|
2021-03-23 20:24:08 +03:00
|
|
|
}
|
|
|
|
|
|
2021-08-25 15:11:22 +02: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.
|
2022-08-10 11:56:48 +02:00
|
|
|
func (p *Provider) Get(ctx context.Context, pluginID string, user *user.SignedInUser) (backend.PluginContext, bool, error) {
|
2022-05-06 10:58:02 +02:00
|
|
|
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.
|
2022-08-10 11:56:48 +02:00
|
|
|
func (p *Provider) GetWithDataSource(ctx context.Context, pluginID string, user *user.SignedInUser, ds *datasources.DataSource) (backend.PluginContext, bool, error) {
|
2022-05-06 10:58:02 +02:00
|
|
|
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 {
|
2022-06-03 03:24:24 -04:00
|
|
|
return pCtx, exists, fmt.Errorf("%v: %w", "Failed to convert datasource", err)
|
2022-05-06 10:58:02 +02:00
|
|
|
}
|
|
|
|
|
pCtx.DataSourceInstanceSettings = datasourceSettings
|
|
|
|
|
|
|
|
|
|
return pCtx, true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pluginSettingsCacheTTL = 5 * time.Second
|
|
|
|
|
const pluginSettingsCachePrefix = "plugin-setting-"
|
|
|
|
|
|
2022-08-10 11:56:48 +02:00
|
|
|
func (p *Provider) pluginContext(ctx context.Context, pluginID string, user *user.SignedInUser) (backend.PluginContext, bool, error) {
|
2021-11-17 11:04:22 +00:00
|
|
|
plugin, exists := p.pluginStore.Plugin(ctx, pluginID)
|
|
|
|
|
if !exists {
|
2022-05-06 10:58:02 +02:00
|
|
|
return backend.PluginContext{}, false, nil
|
2021-03-23 20:24:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonData := json.RawMessage{}
|
|
|
|
|
decryptedSecureJSONData := map[string]string{}
|
|
|
|
|
var updated time.Time
|
|
|
|
|
|
2021-11-02 13:42:55 +01:00
|
|
|
ps, err := p.getCachedPluginSettings(ctx, pluginID, user)
|
2021-03-23 20:24:08 +03:00
|
|
|
if err != nil {
|
2023-01-23 13:56:20 -05:00
|
|
|
// pluginsettings.ErrPluginSettingNotFound is expected if there's no row found for plugin setting in database (if non-app plugin).
|
2021-03-23 20:24:08 +03:00
|
|
|
// If it's not this expected error something is wrong with cache or database and we return the error to the client.
|
2023-01-23 13:56:20 -05:00
|
|
|
if !errors.Is(err, pluginsettings.ErrPluginSettingNotFound) {
|
2022-06-03 03:24:24 -04:00
|
|
|
return backend.PluginContext{}, false, fmt.Errorf("%v: %w", "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 {
|
2022-06-03 03:24:24 -04:00
|
|
|
return backend.PluginContext{}, false, fmt.Errorf("%v: %w", "Failed to unmarshal plugin json data", err)
|
2021-03-23 20:24:08 +03:00
|
|
|
}
|
2022-03-03 11:39:15 +01:00
|
|
|
decryptedSecureJSONData = p.pluginSettingsService.DecryptedValues(ps)
|
2021-03-23 20:24:08 +03:00
|
|
|
updated = ps.Updated
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 10:58:02 +02:00
|
|
|
return backend.PluginContext{
|
2022-08-11 13:28:55 +02:00
|
|
|
OrgID: user.OrgID,
|
2021-11-01 09:53:33 +00:00
|
|
|
PluginID: plugin.ID,
|
2021-03-23 20:24:08 +03:00
|
|
|
User: adapters.BackendUserFromSignedInUser(user),
|
|
|
|
|
AppInstanceSettings: &backend.AppInstanceSettings{
|
|
|
|
|
JSONData: jsonData,
|
|
|
|
|
DecryptedSecureJSONData: decryptedSecureJSONData,
|
|
|
|
|
Updated: updated,
|
|
|
|
|
},
|
2022-05-06 10:58:02 +02:00
|
|
|
}, true, nil
|
2021-03-23 20:24:08 +03:00
|
|
|
}
|
|
|
|
|
|
2022-08-10 11:56:48 +02:00
|
|
|
func (p *Provider) getCachedPluginSettings(ctx context.Context, pluginID string, user *user.SignedInUser) (*pluginsettings.DTO, error) {
|
2023-02-24 11:13:22 +00:00
|
|
|
cacheKey := getCacheKey(pluginID)
|
2021-03-23 20:24:08 +03:00
|
|
|
|
2022-03-03 11:39:15 +01:00
|
|
|
if cached, found := p.cacheService.Get(cacheKey); found {
|
2022-03-18 20:49:13 +01:00
|
|
|
ps := cached.(*pluginsettings.DTO)
|
2022-08-11 13:28:55 +02:00
|
|
|
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,
|
2022-08-11 13:28:55 +02:00
|
|
|
OrgID: user.OrgID,
|
2022-03-18 20:49:13 +01:00
|
|
|
})
|
|
|
|
|
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
|
|
|
}
|
2021-10-07 16:33:50 +02:00
|
|
|
|
2023-02-24 11:13:22 +00:00
|
|
|
func (p *Provider) InvalidateSettingsCache(_ context.Context, pluginID string) {
|
|
|
|
|
p.cacheService.Delete(getCacheKey(pluginID))
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-13 09:27:03 -04:00
|
|
|
func (p *Provider) decryptSecureJsonDataFn(ctx context.Context) func(ds *datasources.DataSource) (map[string]string, error) {
|
|
|
|
|
return func(ds *datasources.DataSource) (map[string]string, error) {
|
|
|
|
|
return p.dataSourceService.DecryptedValues(ctx, ds)
|
2021-10-07 16:33:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-24 11:13:22 +00:00
|
|
|
|
|
|
|
|
func getCacheKey(pluginID string) string {
|
|
|
|
|
return pluginSettingsCachePrefix + pluginID
|
|
|
|
|
}
|