-
Notifications
You must be signed in to change notification settings - Fork 11.6k
/
dashboard_file_store.go
92 lines (74 loc) · 2.38 KB
/
dashboard_file_store.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
package manager
import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/util"
)
var openDashboardFile = func(name string) (fs.File, error) {
// Wrapping in filepath.Clean to properly handle
// gosec G304 Potential file inclusion via variable rule.
return os.Open(filepath.Clean(name))
}
func (m *PluginManager) ListPluginDashboardFiles(ctx context.Context, args *plugins.ListPluginDashboardFilesArgs) (*plugins.ListPluginDashboardFilesResult, error) {
if args == nil {
return nil, fmt.Errorf("args cannot be nil")
}
if len(strings.TrimSpace(args.PluginID)) == 0 {
return nil, fmt.Errorf("args.PluginID cannot be empty")
}
plugin, exists := m.Plugin(ctx, args.PluginID)
if !exists {
return nil, plugins.NotFoundError{PluginID: args.PluginID}
}
references := []string{}
for _, include := range plugin.DashboardIncludes() {
references = append(references, include.Path)
}
return &plugins.ListPluginDashboardFilesResult{
FileReferences: references,
}, nil
}
func (m *PluginManager) GetPluginDashboardFileContents(ctx context.Context, args *plugins.GetPluginDashboardFileContentsArgs) (*plugins.GetPluginDashboardFileContentsResult, error) {
if args == nil {
return nil, fmt.Errorf("args cannot be nil")
}
if len(strings.TrimSpace(args.PluginID)) == 0 {
return nil, fmt.Errorf("args.PluginID cannot be empty")
}
if len(strings.TrimSpace(args.FileReference)) == 0 {
return nil, fmt.Errorf("args.FileReference cannot be empty")
}
plugin, exists := m.Plugin(ctx, args.PluginID)
if !exists {
return nil, plugins.NotFoundError{PluginID: args.PluginID}
}
var includedFile *plugins.Includes
for _, include := range plugin.DashboardIncludes() {
if args.FileReference == include.Path {
includedFile = include
break
}
}
if includedFile == nil {
return nil, fmt.Errorf("plugin dashboard file not found")
}
cleanPath, err := util.CleanRelativePath(includedFile.Path)
if err != nil {
// CleanRelativePath should clean and make the path relative so this is not expected to fail
return nil, err
}
dashboardFilePath := filepath.Join(plugin.PluginDir, cleanPath)
file, err := openDashboardFile(dashboardFilePath)
if err != nil {
return nil, err
}
return &plugins.GetPluginDashboardFileContentsResult{
Content: file,
}, nil
}
var _ plugins.DashboardFileStore = &PluginManager{}