forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_reader.go
138 lines (113 loc) · 3.05 KB
/
config_reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package plugins
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
)
type configReader interface {
readConfig(ctx context.Context, path string) ([]*pluginsAsConfig, error)
}
type configReaderImpl struct {
log log.Logger
pluginStore pluginstore.Store
}
func newConfigReader(logger log.Logger, pluginStore pluginstore.Store) configReader {
return &configReaderImpl{log: logger, pluginStore: pluginStore}
}
func (cr *configReaderImpl) readConfig(ctx context.Context, path string) ([]*pluginsAsConfig, error) {
var apps []*pluginsAsConfig
cr.log.Debug("Looking for plugin provisioning files", "path", path)
files, err := os.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(ctx, apps)
if err != nil {
return nil, err
}
return apps, nil
}
func (cr *configReaderImpl) parsePluginConfig(path string, file fs.DirEntry) (*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 := os.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 {
errs := []error{}
for index, app := range apps[i].Apps {
if app.PluginID == "" {
err := fmt.Errorf("app item %d in configuration doesn't contain required field type", index+1)
errs = append(errs, err)
}
}
if len(errs) != 0 {
return errors.Join(errs...)
}
}
return nil
}
func (cr *configReaderImpl) validatePluginsConfig(ctx context.Context, apps []*pluginsAsConfig) error {
for i := range apps {
if apps[i].Apps == nil {
continue
}
for _, app := range apps[i].Apps {
if _, exists := cr.pluginStore.Plugin(ctx, app.PluginID); !exists {
return fmt.Errorf("plugin not installed: %q", 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
}
}
}
}
}