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
|
|
|
|
2024-06-13 07:11:35 +03:00
|
|
|
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
2021-03-23 20:24:08 +03:00
|
|
|
"github.com/grafana/grafana/pkg/infra/localcache"
|
2023-09-21 11:33:31 +02:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
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"
|
2024-02-27 12:38:02 +01:00
|
|
|
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginconfig"
|
2023-03-07 16:22:30 +00:00
|
|
|
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginsettings"
|
2023-09-11 13:59:24 +02:00
|
|
|
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
|
2023-09-21 11:33:31 +02:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
pluginSettingsCacheTTL = 5 * time.Second
|
|
|
|
|
pluginSettingsCachePrefix = "plugin-setting-"
|
2021-03-23 20:24:08 +03:00
|
|
|
)
|
|
|
|
|
|
2023-09-21 11:33:31 +02:00
|
|
|
func ProvideService(cfg *setting.Cfg, cacheService *localcache.CacheService, pluginStore pluginstore.Store,
|
2024-01-19 15:56:52 +01:00
|
|
|
dataSourceCache datasources.CacheService, dataSourceService datasources.DataSourceService,
|
2024-02-27 12:38:02 +01:00
|
|
|
pluginSettingsService pluginsettings.Service, pluginRequestConfigProvider pluginconfig.PluginRequestConfigProvider) *Provider {
|
2021-08-25 15:11:22 +02:00
|
|
|
return &Provider{
|
2024-05-28 15:59:06 +02:00
|
|
|
BaseProvider: newBaseProvider(cfg, pluginRequestConfigProvider),
|
|
|
|
|
cacheService: cacheService,
|
|
|
|
|
pluginStore: pluginStore,
|
|
|
|
|
dataSourceCache: dataSourceCache,
|
|
|
|
|
dataSourceService: dataSourceService,
|
|
|
|
|
pluginSettingsService: pluginSettingsService,
|
|
|
|
|
logger: log.New("plugin.context"),
|
2021-08-25 15:11:22 +02:00
|
|
|
}
|
2021-03-23 20:24:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Provider struct {
|
2024-05-28 15:59:06 +02:00
|
|
|
*BaseProvider
|
|
|
|
|
cacheService *localcache.CacheService
|
|
|
|
|
pluginStore pluginstore.Store
|
|
|
|
|
dataSourceCache datasources.CacheService
|
|
|
|
|
dataSourceService datasources.DataSourceService
|
|
|
|
|
pluginSettingsService pluginsettings.Service
|
|
|
|
|
logger log.Logger
|
2021-03-23 20:24:08 +03:00
|
|
|
}
|
|
|
|
|
|
2024-01-19 16:22:17 +01:00
|
|
|
// Get will retrieve plugin context by the provided pluginID and orgID.
|
|
|
|
|
// This is intended to be used for app plugin requests.
|
|
|
|
|
// PluginContext.AppInstanceSettings will be resolved and appended to the returned context.
|
2023-08-30 16:51:18 +02:00
|
|
|
// Note: identity.Requester can be nil.
|
|
|
|
|
func (p *Provider) Get(ctx context.Context, pluginID string, user identity.Requester, orgID int64) (backend.PluginContext, error) {
|
2023-06-08 13:59:51 +02:00
|
|
|
plugin, exists := p.pluginStore.Plugin(ctx, pluginID)
|
|
|
|
|
if !exists {
|
2023-09-25 12:10:47 +02:00
|
|
|
return backend.PluginContext{}, plugins.ErrPluginNotRegistered
|
2023-06-08 13:59:51 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-28 15:59:06 +02:00
|
|
|
pCtx := p.GetBasePluginContext(ctx, plugin, user)
|
2023-06-08 13:59:51 +02:00
|
|
|
if plugin.IsApp() {
|
2023-06-08 18:36:41 +02:00
|
|
|
appSettings, err := p.appInstanceSettings(ctx, pluginID, orgID)
|
2023-06-08 13:59:51 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return backend.PluginContext{}, err
|
|
|
|
|
}
|
|
|
|
|
pCtx.AppInstanceSettings = appSettings
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pCtx, nil
|
2022-05-06 10:58:02 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-19 16:22:17 +01:00
|
|
|
// GetWithDataSource will retrieve plugin context by the provided pluginID and datasource.
|
|
|
|
|
// This is intended to be used for datasource plugin requests.
|
|
|
|
|
// PluginContext.DataSourceInstanceSettings will be resolved and appended to the returned context.
|
|
|
|
|
// Note: identity.Requester can be nil.
|
2023-08-30 16:51:18 +02:00
|
|
|
func (p *Provider) GetWithDataSource(ctx context.Context, pluginID string, user identity.Requester, ds *datasources.DataSource) (backend.PluginContext, error) {
|
2023-09-21 11:33:31 +02:00
|
|
|
plugin, exists := p.pluginStore.Plugin(ctx, pluginID)
|
2023-06-08 18:36:41 +02:00
|
|
|
if !exists {
|
2023-09-25 12:10:47 +02:00
|
|
|
return backend.PluginContext{}, plugins.ErrPluginNotRegistered
|
2023-06-08 18:36:41 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-28 15:59:06 +02:00
|
|
|
pCtx := p.GetBasePluginContext(ctx, plugin, user)
|
2022-05-06 10:58:02 +02:00
|
|
|
|
|
|
|
|
datasourceSettings, err := adapters.ModelToInstanceSettings(ds, p.decryptSecureJsonDataFn(ctx))
|
|
|
|
|
if err != nil {
|
2023-06-08 13:59:51 +02:00
|
|
|
return pCtx, err
|
2022-05-06 10:58:02 +02:00
|
|
|
}
|
|
|
|
|
pCtx.DataSourceInstanceSettings = datasourceSettings
|
|
|
|
|
|
2023-06-08 13:59:51 +02:00
|
|
|
return pCtx, nil
|
2022-05-06 10:58:02 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-22 11:32:25 -08:00
|
|
|
func (p *Provider) GetDataSourceInstanceSettings(ctx context.Context, uid string) (*backend.DataSourceInstanceSettings, error) {
|
2024-06-20 17:53:07 +03:00
|
|
|
user, err := identity.GetRequester(ctx)
|
2024-01-22 11:32:25 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
ds, err := p.dataSourceCache.GetDatasourceByUID(ctx, uid, user, false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return adapters.ModelToInstanceSettings(ds, p.decryptSecureJsonDataFn(ctx))
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-19 16:22:17 +01:00
|
|
|
// PluginContextForDataSource will retrieve plugin context by the provided pluginID and datasource UID / K8s name.
|
|
|
|
|
// This is intended to be used for datasource API server plugin requests.
|
2024-01-22 11:32:25 -08:00
|
|
|
func (p *Provider) PluginContextForDataSource(ctx context.Context, datasourceSettings *backend.DataSourceInstanceSettings) (backend.PluginContext, error) {
|
|
|
|
|
pluginID := datasourceSettings.Type
|
2024-01-19 15:56:52 +01:00
|
|
|
plugin, exists := p.pluginStore.Plugin(ctx, pluginID)
|
|
|
|
|
if !exists {
|
|
|
|
|
return backend.PluginContext{}, plugins.ErrPluginNotRegistered
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 17:53:07 +03:00
|
|
|
user, err := identity.GetRequester(ctx)
|
2024-01-19 15:56:52 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return backend.PluginContext{}, err
|
|
|
|
|
}
|
2024-05-28 15:59:06 +02:00
|
|
|
pCtx := p.GetBasePluginContext(ctx, plugin, user)
|
2024-01-19 15:56:52 +01:00
|
|
|
pCtx.DataSourceInstanceSettings = datasourceSettings
|
|
|
|
|
|
|
|
|
|
return pCtx, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-08 18:36:41 +02:00
|
|
|
func (p *Provider) appInstanceSettings(ctx context.Context, pluginID string, orgID int64) (*backend.AppInstanceSettings, error) {
|
2021-03-23 20:24:08 +03:00
|
|
|
jsonData := json.RawMessage{}
|
|
|
|
|
decryptedSecureJSONData := map[string]string{}
|
|
|
|
|
var updated time.Time
|
|
|
|
|
|
2023-06-08 18:36:41 +02:00
|
|
|
ps, err := p.getCachedPluginSettings(ctx, pluginID, orgID)
|
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).
|
2023-06-08 13:59:51 +02:00
|
|
|
// Otherwise, 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) {
|
2023-06-08 13:59:51 +02:00
|
|
|
return nil, 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 {
|
2023-06-08 13:59:51 +02:00
|
|
|
return nil, 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
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-08 13:59:51 +02:00
|
|
|
return &backend.AppInstanceSettings{
|
|
|
|
|
JSONData: jsonData,
|
|
|
|
|
DecryptedSecureJSONData: decryptedSecureJSONData,
|
|
|
|
|
Updated: updated,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *Provider) InvalidateSettingsCache(_ context.Context, pluginID string) {
|
|
|
|
|
p.cacheService.Delete(getCacheKey(pluginID))
|
2021-03-23 20:24:08 +03:00
|
|
|
}
|
|
|
|
|
|
2023-06-08 18:36:41 +02:00
|
|
|
func (p *Provider) getCachedPluginSettings(ctx context.Context, pluginID string, orgID int64) (*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)
|
2023-06-08 18:36:41 +02:00
|
|
|
if ps.OrgID == 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,
|
2023-06-08 18:36:41 +02:00
|
|
|
OrgID: 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
|
|
|
|
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
|
|
|
|
|
}
|