Plugins: Unexport PluginDir field from PluginDTO (#59190)

* unexport pluginDir from dto

* more err checks

* tidy

* fix tests

* fix dboard file tests

* fix import

* fix tests

* apply PR feedback

* combine interfaces

* fix logs and clean up test

* filepath clean

* use fs.File

* rm explicit type
This commit is contained in:
Will Browne
2022-12-02 13:46:55 +01:00
committed by GitHub
parent 6d1bcd9f40
commit 76233f9997
15 changed files with 223 additions and 286 deletions
+3 -8
View File
@@ -4,8 +4,6 @@ import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/grafana/grafana/pkg/plugins"
@@ -24,10 +22,8 @@ func ProvideFileStoreManager(pluginStore plugins.Store) *FileStoreManager {
}
}
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))
var openDashboardFile = func(p plugins.PluginDTO, name string) (fs.File, error) {
return p.File(name)
}
func (m *FileStoreManager) ListPluginDashboardFiles(ctx context.Context, args *ListPluginDashboardFilesArgs) (*ListPluginDashboardFilesResult, error) {
@@ -90,8 +86,7 @@ func (m *FileStoreManager) GetPluginDashboardFileContents(ctx context.Context, a
return nil, err
}
dashboardFilePath := filepath.Join(plugin.PluginDir, cleanPath)
file, err := openDashboardFile(dashboardFilePath)
file, err := openDashboardFile(plugin, cleanPath)
if err != nil {
return nil, err
}
@@ -3,6 +3,7 @@ package dashboards
import (
"context"
"io"
"io/fs"
"testing"
"testing/fstest"
@@ -117,20 +118,24 @@ func TestDashboardFileStore(t *testing.T) {
t.Run("With filesystem", func(t *testing.T) {
origOpenDashboardFile := openDashboardFile
mapFs := fstest.MapFS{
"plugins/plugin-id/dashboards/dash1.json": {
"dashboards/dash1.json": {
Data: []byte("dash1"),
},
"plugins/plugin-id/dashboards/dash2.json": {
"dashboards/dash2.json": {
Data: []byte("dash2"),
},
"plugins/plugin-id/dashboards/dash3.json": {
"dashboards/dash3.json": {
Data: []byte("dash3"),
},
"plugins/plugin-id/dash2.json": {
"dash2.json": {
Data: []byte("dash2"),
},
}
openDashboardFile = mapFs.Open
openDashboardFile = func(p plugins.PluginDTO, name string) (fs.File, error) {
f, err := mapFs.Open(name)
require.NoError(t, err)
return f, nil
}
t.Cleanup(func() {
openDashboardFile = origOpenDashboardFile
})
@@ -156,7 +161,6 @@ func TestDashboardFileStore(t *testing.T) {
b, err := io.ReadAll(res.Content)
require.NoError(t, err)
require.Equal(t, "dash1", string(b))
require.NoError(t, res.Content.Close())
})
t.Run("Should return file content for dashboards/dash2.json", func(t *testing.T) {
@@ -170,7 +174,6 @@ func TestDashboardFileStore(t *testing.T) {
b, err := io.ReadAll(res.Content)
require.NoError(t, err)
require.Equal(t, "dash2", string(b))
require.NoError(t, res.Content.Close())
})
t.Run("Should return error when trying to read relative file", func(t *testing.T) {
@@ -189,39 +192,39 @@ func TestDashboardFileStore(t *testing.T) {
func setupPluginDashboardsForTest(t *testing.T) *FileStoreManager {
t.Helper()
return &FileStoreManager{
pluginStore: &plugins.FakePluginStore{
PluginList: []plugins.PluginDTO{
p1 := &plugins.Plugin{
JSONData: plugins.JSONData{
ID: "pluginWithoutDashboards",
Includes: []*plugins.Includes{
{
JSONData: plugins.JSONData{
ID: "pluginWithoutDashboards",
Includes: []*plugins.Includes{
{
Type: "page",
},
},
},
},
{
PluginDir: "plugins/plugin-id",
JSONData: plugins.JSONData{
ID: "pluginWithDashboards",
Includes: []*plugins.Includes{
{
Type: "page",
},
{
Type: "dashboard",
Path: "dashboards/dash1.json",
},
{
Type: "dashboard",
Path: "dashboards/dash2.json",
},
},
},
Type: "page",
},
},
},
}
p2 := &plugins.Plugin{
JSONData: plugins.JSONData{
ID: "pluginWithDashboards",
Includes: []*plugins.Includes{
{
Type: "page",
},
{
Type: "dashboard",
Path: "dashboards/dash1.json",
},
{
Type: "dashboard",
Path: "dashboards/dash2.json",
},
},
},
}
return &FileStoreManager{
pluginStore: &plugins.FakePluginStore{
PluginList: []plugins.PluginDTO{p1.ToDTO(), p2.ToDTO()},
},
}
}