Plugins: Set grafana config, plugin version and user agent on plugin requests (#75171)

* first pass

* fixup

* remove test line

* fix tests

* use new fields

* fix imports + formatting

* fix tests

* rollback changes

* undo whitespace

* apply pr feedback
This commit is contained in:
Will Browne
2023-09-21 11:33:31 +02:00
committed by GitHub
parent dd8f88b194
commit 7fca1bde54
37 changed files with 248 additions and 102 deletions

View File

@ -5,35 +5,54 @@ import (
"encoding/json"
"errors"
"fmt"
"runtime"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/useragent"
"github.com/grafana/grafana/pkg/infra/localcache"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/config"
"github.com/grafana/grafana/pkg/plugins/envvars"
"github.com/grafana/grafana/pkg/services/auth/identity"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/pluginsintegration/adapters"
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginsettings"
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
"github.com/grafana/grafana/pkg/setting"
)
const (
pluginSettingsCacheTTL = 5 * time.Second
pluginSettingsCachePrefix = "plugin-setting-"
)
var ErrPluginNotFound = errors.New("plugin not found")
func ProvideService(cacheService *localcache.CacheService, pluginStore pluginstore.Store,
dataSourceService datasources.DataSourceService, pluginSettingsService pluginsettings.Service) *Provider {
func ProvideService(cfg *setting.Cfg, cacheService *localcache.CacheService, pluginStore pluginstore.Store,
dataSourceService datasources.DataSourceService, pluginSettingsService pluginsettings.Service,
licensing plugins.Licensing, pCfg *config.Cfg) *Provider {
return &Provider{
cfg: cfg,
cacheService: cacheService,
pluginStore: pluginStore,
dataSourceService: dataSourceService,
pluginSettingsService: pluginSettingsService,
pluginEnvVars: envvars.NewProvider(pCfg, licensing),
logger: log.New("plugin.context"),
}
}
type Provider struct {
cfg *setting.Cfg
pluginEnvVars *envvars.Service
cacheService *localcache.CacheService
pluginStore pluginstore.Store
dataSourceService datasources.DataSourceService
pluginSettingsService pluginsettings.Service
logger log.Logger
}
// Get allows getting plugin context by its ID. If datasourceUID is not empty string
@ -47,7 +66,8 @@ func (p *Provider) Get(ctx context.Context, pluginID string, user identity.Reque
}
pCtx := backend.PluginContext{
PluginID: pluginID,
PluginID: pluginID,
PluginVersion: plugin.Info.Version,
}
if user != nil && !user.IsNil() {
pCtx.OrgID = user.GetOrgID()
@ -62,6 +82,12 @@ func (p *Provider) Get(ctx context.Context, pluginID string, user identity.Reque
pCtx.AppInstanceSettings = appSettings
}
ua, err := useragent.New(p.cfg.BuildVersion, runtime.GOOS, runtime.GOARCH)
if err != nil {
p.logger.Warn("Could not create user agent", "error", err)
}
pCtx.UserAgent = ua
return pCtx, nil
}
@ -69,13 +95,14 @@ func (p *Provider) Get(ctx context.Context, pluginID string, user identity.Reque
// resolved and appended to the returned context.
// Note: *user.SignedInUser can be nil.
func (p *Provider) GetWithDataSource(ctx context.Context, pluginID string, user identity.Requester, ds *datasources.DataSource) (backend.PluginContext, error) {
_, exists := p.pluginStore.Plugin(ctx, pluginID)
plugin, exists := p.pluginStore.Plugin(ctx, pluginID)
if !exists {
return backend.PluginContext{}, ErrPluginNotFound
}
pCtx := backend.PluginContext{
PluginID: pluginID,
PluginID: pluginID,
PluginVersion: plugin.Info.Version,
}
if user != nil && !user.IsNil() {
pCtx.OrgID = user.GetOrgID()
@ -88,12 +115,18 @@ func (p *Provider) GetWithDataSource(ctx context.Context, pluginID string, user
}
pCtx.DataSourceInstanceSettings = datasourceSettings
settings := p.pluginEnvVars.GetConfigMap(ctx, pluginID, plugin.ExternalService)
pCtx.GrafanaConfig = backend.NewGrafanaCfg(settings)
ua, err := useragent.New(p.cfg.BuildVersion, runtime.GOOS, runtime.GOARCH)
if err != nil {
p.logger.Warn("Could not create user agent", "error", err)
}
pCtx.UserAgent = ua
return pCtx, nil
}
const pluginSettingsCacheTTL = 5 * time.Second
const pluginSettingsCachePrefix = "plugin-setting-"
func (p *Provider) appInstanceSettings(ctx context.Context, pluginID string, orgID int64) (*backend.AppInstanceSettings, error) {
jsonData := json.RawMessage{}
decryptedSecureJSONData := map[string]string{}