2022-08-30 10:30:43 -05:00
|
|
|
package dashboards
|
2022-03-10 11:38:04 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
var _ FileStore = (*FileStoreManager)(nil)
|
|
|
|
|
|
|
|
type FileStoreManager struct {
|
|
|
|
pluginStore plugins.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProvideFileStoreManager(pluginStore plugins.Store) *FileStoreManager {
|
|
|
|
return &FileStoreManager{
|
|
|
|
pluginStore: pluginStore,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 11:38:04 -06:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
func (m *FileStoreManager) ListPluginDashboardFiles(ctx context.Context, args *ListPluginDashboardFilesArgs) (*ListPluginDashboardFilesResult, error) {
|
2022-03-10 11:38:04 -06:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
plugin, exists := m.pluginStore.Plugin(ctx, args.PluginID)
|
2022-03-10 11:38:04 -06:00
|
|
|
if !exists {
|
|
|
|
return nil, plugins.NotFoundError{PluginID: args.PluginID}
|
|
|
|
}
|
|
|
|
|
|
|
|
references := []string{}
|
|
|
|
for _, include := range plugin.DashboardIncludes() {
|
|
|
|
references = append(references, include.Path)
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
return &ListPluginDashboardFilesResult{
|
2022-03-10 11:38:04 -06:00
|
|
|
FileReferences: references,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
func (m *FileStoreManager) GetPluginDashboardFileContents(ctx context.Context, args *GetPluginDashboardFileContentsArgs) (*GetPluginDashboardFileContentsResult, error) {
|
2022-03-10 11:38:04 -06:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
plugin, exists := m.pluginStore.Plugin(ctx, args.PluginID)
|
2022-03-10 11:38:04 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
return &GetPluginDashboardFileContentsResult{
|
2022-03-10 11:38:04 -06:00
|
|
|
Content: file,
|
|
|
|
}, nil
|
|
|
|
}
|