Files
grafana/pkg/services/provisioning/plugins/config_reader.go
Arve Knudsen 87c3a2b790 PluginManager: Make Plugins, Renderer and DataSources non-global (#31866)
* PluginManager: Make Plugins and DataSources non-global

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix integration tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Replace outdated command

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* DashboardService: Ensure it gets constructed with necessary parameters

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix build

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* DashboardService: Ensure it gets constructed with necessary parameters

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Remove dead code

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix test

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix test

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Remove FocusConvey

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix test

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Remove dead code

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Undo interface changes

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Backend: Move tsdbifaces.RequestHandler to plugins.DataRequestHandler

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Rename to DataSourceCount

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Consolidate dashboard interfaces into one

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix test

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix dashboard integration tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
2021-03-17 16:06:10 +01:00

137 lines
2.9 KiB
Go

package plugins
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/plugins/manager"
"gopkg.in/yaml.v2"
)
type configReader interface {
readConfig(path string) ([]*pluginsAsConfig, error)
}
type configReaderImpl struct {
log log.Logger
}
func newConfigReader(logger log.Logger) configReader {
return &configReaderImpl{log: logger}
}
func (cr *configReaderImpl) readConfig(path string) ([]*pluginsAsConfig, error) {
var apps []*pluginsAsConfig
cr.log.Debug("Looking for plugin provisioning files", "path", path)
files, err := ioutil.ReadDir(path)
if err != nil {
cr.log.Error("Failed to read plugin provisioning files from directory", "path", path, "error", err)
return apps, nil
}
for _, file := range files {
if strings.HasSuffix(file.Name(), ".yaml") || strings.HasSuffix(file.Name(), ".yml") {
cr.log.Debug("Parsing plugin provisioning file", "path", path, "file.Name", file.Name())
app, err := cr.parsePluginConfig(path, file)
if err != nil {
return nil, err
}
if app != nil {
apps = append(apps, app)
}
}
}
cr.log.Debug("Validating plugins")
if err := validateRequiredField(apps); err != nil {
return nil, err
}
checkOrgIDAndOrgName(apps)
err = cr.validatePluginsConfig(apps)
if err != nil {
return nil, err
}
return apps, nil
}
func (cr *configReaderImpl) parsePluginConfig(path string, file os.FileInfo) (*pluginsAsConfig, error) {
filename, err := filepath.Abs(filepath.Join(path, file.Name()))
if err != nil {
return nil, err
}
// nolint:gosec
// We can ignore the gosec G304 warning on this one because `filename` comes from ps.Cfg.ProvisioningPath
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var cfg *pluginsAsConfigV0
err = yaml.Unmarshal(yamlFile, &cfg)
if err != nil {
return nil, err
}
return cfg.mapToPluginsFromConfig(), nil
}
func validateRequiredField(apps []*pluginsAsConfig) error {
for i := range apps {
var errStrings []string
for index, app := range apps[i].Apps {
if app.PluginID == "" {
errStrings = append(
errStrings,
fmt.Sprintf("app item %d in configuration doesn't contain required field type", index+1),
)
}
}
if len(errStrings) != 0 {
return fmt.Errorf(strings.Join(errStrings, "\n"))
}
}
return nil
}
func (cr *configReaderImpl) validatePluginsConfig(apps []*pluginsAsConfig) error {
for i := range apps {
if apps[i].Apps == nil {
continue
}
for _, app := range apps[i].Apps {
if !manager.IsAppInstalled(app.PluginID) {
return fmt.Errorf("app plugin not installed: %s", app.PluginID)
}
}
}
return nil
}
func checkOrgIDAndOrgName(apps []*pluginsAsConfig) {
for i := range apps {
for _, app := range apps[i].Apps {
if app.OrgID < 1 {
if app.OrgName == "" {
app.OrgID = 1
} else {
app.OrgID = 0
}
}
}
}
}