mirror of
https://github.com/grafana/grafana.git
synced 2025-07-31 20:42:10 +08:00

* add core plugin flow * add instrumentation * move func * remove cruft * support external backend plugins * refactor + clean up * remove comments * refactor loader * simplify core plugin path arg * cleanup loggers * move signature validator to plugins package * fix sig packaging * cleanup plugin model * remove unnecessary plugin field * add start+stop for pm * fix failures * add decommissioned state * export fields just to get things flowing * fix comments * set static routes * make image loading idempotent * merge with backend plugin manager * re-use funcs * reorder imports + remove unnecessary interface * add some TODOs + remove unused func * remove unused instrumentation func * simplify client usage * remove import alias * re-use backendplugin.Plugin interface * re order funcs * improve var name * fix log statements * refactor data model * add logic for dupe check during loading * cleanup state setting * refactor loader * cleanup manager interface * add rendering flow * refactor loading + init * add renderer support * fix renderer plugin * reformat imports * track errors * fix plugin signature inheritance * name param in interface * update func comment * fix func arg name * introduce class concept * remove func * fix external plugin check * apply changes from pm-experiment * fix core plugins * fix imports * rename interface * comment API interface * add support for testdata plugin * enable alerting + use correct core plugin contracts * slim manager API * fix param name * fix filter * support static routes * fix rendering * tidy rendering * get tests compiling * fix install+uninstall * start finder test * add finder test coverage * start loader tests * add test for core plugins * load core + bundled test * add test for nested plugin loading * add test files * clean interface + fix registering some core plugins * refactoring * reformat and create sub packages * simplify core plugin init * fix ctx cancel scenario * migrate initializer * remove Init() funcs * add test starter * new logger * flesh out initializer tests * refactoring * remove unused svc * refactor rendering flow * fixup loader tests * add enabled helper func * fix logger name * fix data fetchers * fix case where plugin dir doesn't exist * improve coverage + move dupe checking to loader * remove noisy debug logs * register core plugins automagically * add support for renderer in catalog * make private func + fix req validation * use interface * re-add check for renderer in catalog * tidy up from moving to auto reg core plugins * core plugin registrar * guards * copy over core plugins for test infra * all tests green * renames * propagate new interfaces * kill old manager * get compiling * tidy up * update naming * refactor manager test + cleanup * add more cases to finder test * migrate validator to field * more coverage * refactor dupe checking * add test for plugin class * add coverage for initializer * split out rendering * move * fixup tests * fix uss test * fix frontend settings * fix grafanads test * add check when checking sig errors * fix enabled map * fixup * allow manual setup of CM * rename to cloud-monitoring * remove TODO * add installer interface for testing * loader interface returns * tests passing * refactor + add more coverage * support 'stackdriver' * fix frontend settings loading * improve naming based on package name * small tidy * refactor test * fix renderer start * make cloud-monitoring plugin ID clearer * add plugin update test * add integration tests * don't break all if sig can't be calculated * add root URL check test * add more signature verification tests * update DTO name * update enabled plugins comment * update comments * fix linter * revert fe naming change * fix errors endpoint * reset error code field name * re-order test to help verify * assert -> require * pm check * add missing entry + re-order * re-check * dump icon log * verify manager contents first * reformat * apply PR feedback * apply style changes * fix one vs all loading err * improve log output * only start when no signature error * move log * rework plugin update check * fix test * fix multi loading from cfg.PluginSettings * improve log output #2 * add error abstraction to capture errors without registering a plugin * add debug log * add unsigned warning * e2e test attempt * fix logger * set home path * prevent panic * alternate * ugh.. fix home path * return renderer even if not started * make renderer plugin managed * add fallback renderer icon, update renderer badge + prevent changes when renderer is installed * fix icon loading * rollback renderer changes * use correct field * remove unneccessary block * remove newline * remove unused func * fix bundled plugins base + module fields * remove unused field since refactor * add authorizer abstraction * loader only returns plugins expected to run * fix multi log output
274 lines
7.3 KiB
Go
274 lines
7.3 KiB
Go
package initializer
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/grafana/grafana-aws-sdk/pkg/awsds"
|
|
|
|
"github.com/gosimple/slug"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/grpcplugin"
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
"github.com/grafana/grafana/pkg/util"
|
|
)
|
|
|
|
var logger = log.New("plugin.initializer")
|
|
|
|
type Initializer struct {
|
|
cfg *setting.Cfg
|
|
license models.Licensing
|
|
}
|
|
|
|
func New(cfg *setting.Cfg, license models.Licensing) Initializer {
|
|
return Initializer{
|
|
cfg: cfg,
|
|
license: license,
|
|
}
|
|
}
|
|
|
|
func (i *Initializer) Initialize(p *plugins.Plugin) error {
|
|
if len(p.Dependencies.Plugins) == 0 {
|
|
p.Dependencies.Plugins = []plugins.Dependency{}
|
|
}
|
|
|
|
if p.Dependencies.GrafanaVersion == "" {
|
|
p.Dependencies.GrafanaVersion = "*"
|
|
}
|
|
|
|
for _, include := range p.Includes {
|
|
if include.Role == "" {
|
|
include.Role = models.ROLE_VIEWER
|
|
}
|
|
}
|
|
|
|
i.handleModuleDefaults(p)
|
|
|
|
p.Info.Logos.Small = pluginLogoURL(p.Type, p.Info.Logos.Small, p.BaseURL)
|
|
p.Info.Logos.Large = pluginLogoURL(p.Type, p.Info.Logos.Large, p.BaseURL)
|
|
|
|
for i := 0; i < len(p.Info.Screenshots); i++ {
|
|
p.Info.Screenshots[i].Path = evalRelativePluginURLPath(p.Info.Screenshots[i].Path, p.BaseURL, p.Type)
|
|
}
|
|
|
|
if p.IsApp() {
|
|
for _, child := range p.Children {
|
|
i.setPathsBasedOnApp(p, child)
|
|
}
|
|
|
|
// slugify pages
|
|
for _, include := range p.Includes {
|
|
if include.Slug == "" {
|
|
include.Slug = slug.Make(include.Name)
|
|
}
|
|
if include.Type == "page" && include.DefaultNav {
|
|
p.DefaultNavURL = i.cfg.AppSubURL + "/plugins/" + p.ID + "/page/" + include.Slug
|
|
}
|
|
if include.Type == "dashboard" && include.DefaultNav {
|
|
p.DefaultNavURL = i.cfg.AppSubURL + "/dashboard/db/" + include.Slug
|
|
}
|
|
}
|
|
}
|
|
|
|
pluginLog := logger.New("pluginID", p.ID)
|
|
p.SetLogger(pluginLog)
|
|
|
|
if p.Backend {
|
|
var backendFactory backendplugin.PluginFactoryFunc
|
|
if p.IsRenderer() {
|
|
cmd := plugins.ComposeRendererStartCommand()
|
|
backendFactory = grpcplugin.NewRendererPlugin(p.ID, filepath.Join(p.PluginDir, cmd),
|
|
func(pluginID string, renderer pluginextensionv2.RendererPlugin, logger log.Logger) error {
|
|
p.Renderer = renderer
|
|
return nil
|
|
},
|
|
)
|
|
} else {
|
|
cmd := plugins.ComposePluginStartCommand(p.Executable)
|
|
backendFactory = grpcplugin.NewBackendPlugin(p.ID, filepath.Join(p.PluginDir, cmd))
|
|
}
|
|
|
|
if backendClient, err := backendFactory(p.ID, pluginLog, i.envVars(p)); err != nil {
|
|
return err
|
|
} else {
|
|
p.RegisterClient(backendClient)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (i *Initializer) InitializeWithFactory(p *plugins.Plugin, factory backendplugin.PluginFactoryFunc) error {
|
|
err := i.Initialize(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if factory != nil {
|
|
var err error
|
|
|
|
f, err := factory(p.ID, log.New("pluginID", p.ID), []string{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p.RegisterClient(f)
|
|
} else {
|
|
logger.Warn("Could not initialize core plugin process", "pluginID", p.ID)
|
|
return fmt.Errorf("could not initialize plugin %s", p.ID)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (i *Initializer) handleModuleDefaults(p *plugins.Plugin) {
|
|
if p.IsCorePlugin() {
|
|
// Previously there was an assumption that the Core plugins directory
|
|
// should be public/app/plugins/<plugin type>/<plugin id>
|
|
// However this can be an issue if the Core plugins directory is renamed
|
|
baseDir := filepath.Base(p.PluginDir)
|
|
|
|
// use path package for the following statements because these are not file paths
|
|
p.Module = path.Join("app/plugins", string(p.Type), baseDir, "module")
|
|
p.BaseURL = path.Join("public/app/plugins", string(p.Type), baseDir)
|
|
return
|
|
}
|
|
|
|
metrics.SetPluginBuildInformation(p.ID, string(p.Type), p.Info.Version, string(p.Signature))
|
|
|
|
p.Module = path.Join("plugins", p.ID, "module")
|
|
p.BaseURL = path.Join("public/plugins", p.ID)
|
|
}
|
|
|
|
func (i *Initializer) setPathsBasedOnApp(parent *plugins.Plugin, child *plugins.Plugin) {
|
|
appSubPath := strings.ReplaceAll(strings.Replace(child.PluginDir, parent.PluginDir, "", 1), "\\", "/")
|
|
child.IncludedInAppID = parent.ID
|
|
child.BaseURL = parent.BaseURL
|
|
|
|
if parent.IsCorePlugin() {
|
|
child.Module = util.JoinURLFragments("app/plugins/app/"+parent.ID, appSubPath) + "/module"
|
|
} else {
|
|
child.Module = util.JoinURLFragments("plugins/"+parent.ID, appSubPath) + "/module"
|
|
}
|
|
}
|
|
|
|
func pluginLogoURL(pluginType plugins.Type, path, baseURL string) string {
|
|
if path == "" {
|
|
return defaultLogoPath(pluginType)
|
|
}
|
|
|
|
return evalRelativePluginURLPath(path, baseURL, pluginType)
|
|
}
|
|
|
|
func defaultLogoPath(pluginType plugins.Type) string {
|
|
return "public/img/icn-" + string(pluginType) + ".svg"
|
|
}
|
|
|
|
func evalRelativePluginURLPath(pathStr, baseURL string, pluginType plugins.Type) string {
|
|
if pathStr == "" {
|
|
return ""
|
|
}
|
|
|
|
u, _ := url.Parse(pathStr)
|
|
if u.IsAbs() {
|
|
return pathStr
|
|
}
|
|
|
|
// is set as default or has already been prefixed with base path
|
|
if pathStr == defaultLogoPath(pluginType) || strings.HasPrefix(pathStr, baseURL) {
|
|
return pathStr
|
|
}
|
|
|
|
return path.Join(baseURL, pathStr)
|
|
}
|
|
|
|
func (i *Initializer) envVars(plugin *plugins.Plugin) []string {
|
|
hostEnv := []string{
|
|
fmt.Sprintf("GF_VERSION=%s", i.cfg.BuildVersion),
|
|
}
|
|
|
|
if i.license != nil && i.license.HasLicense() {
|
|
hostEnv = append(
|
|
hostEnv,
|
|
fmt.Sprintf("GF_EDITION=%s", i.license.Edition()),
|
|
fmt.Sprintf("GF_ENTERPRISE_license_PATH=%s", i.cfg.EnterpriseLicensePath),
|
|
)
|
|
|
|
if envProvider, ok := i.license.(models.LicenseEnvironment); ok {
|
|
for k, v := range envProvider.Environment() {
|
|
hostEnv = append(hostEnv, fmt.Sprintf("%s=%s", k, v))
|
|
}
|
|
}
|
|
}
|
|
|
|
hostEnv = append(hostEnv, i.awsEnvVars()...)
|
|
hostEnv = append(hostEnv, i.azureEnvVars()...)
|
|
return getPluginSettings(plugin.ID, i.cfg).asEnvVar("GF_PLUGIN", hostEnv)
|
|
}
|
|
|
|
func (i *Initializer) awsEnvVars() []string {
|
|
var variables []string
|
|
if i.cfg.AWSAssumeRoleEnabled {
|
|
variables = append(variables, awsds.AssumeRoleEnabledEnvVarKeyName+"=true")
|
|
}
|
|
if len(i.cfg.AWSAllowedAuthProviders) > 0 {
|
|
variables = append(variables, awsds.AllowedAuthProvidersEnvVarKeyName+"="+strings.Join(i.cfg.AWSAllowedAuthProviders, ","))
|
|
}
|
|
|
|
return variables
|
|
}
|
|
|
|
func (i *Initializer) azureEnvVars() []string {
|
|
var variables []string
|
|
if i.cfg.Azure.Cloud != "" {
|
|
variables = append(variables, "AZURE_CLOUD="+i.cfg.Azure.Cloud)
|
|
}
|
|
if i.cfg.Azure.ManagedIdentityClientId != "" {
|
|
variables = append(variables, "AZURE_MANAGED_IDENTITY_CLIENT_ID="+i.cfg.Azure.ManagedIdentityClientId)
|
|
}
|
|
if i.cfg.Azure.ManagedIdentityEnabled {
|
|
variables = append(variables, "AZURE_MANAGED_IDENTITY_ENABLED=true")
|
|
}
|
|
|
|
return variables
|
|
}
|
|
|
|
type pluginSettings map[string]string
|
|
|
|
func (ps pluginSettings) asEnvVar(prefix string, hostEnv []string) []string {
|
|
var env []string
|
|
for k, v := range ps {
|
|
key := fmt.Sprintf("%s_%s", prefix, strings.ToUpper(k))
|
|
if value := os.Getenv(key); value != "" {
|
|
v = value
|
|
}
|
|
|
|
env = append(env, fmt.Sprintf("%s=%s", key, v))
|
|
}
|
|
|
|
env = append(env, hostEnv...)
|
|
|
|
return env
|
|
}
|
|
|
|
func getPluginSettings(pluginID string, cfg *setting.Cfg) pluginSettings {
|
|
ps := pluginSettings{}
|
|
for k, v := range cfg.PluginSettings[pluginID] {
|
|
if k == "path" || strings.ToLower(k) == "id" {
|
|
continue
|
|
}
|
|
ps[k] = v
|
|
}
|
|
|
|
return ps
|
|
}
|