mirror of
https://github.com/grafana/grafana.git
synced 2025-07-29 12:42:15 +08:00
Plugins: Fs: Add option to access unallowed files in dev mode (#66492)
* Plugins: Fs: Add option to access unallowed files in dev mode * Plugins: Fs: allow accessing unallowed files only when in dev mode * Plugins: Fs: Add ProvideLocalFinder * Plugins: FS: Pass whole config in NewLocalFinder() * Plugins: FS: Add AllowListLocalFS * Plugins: FS: Fix some tests * Plugins: FS: Update tests * Plugins: FS: Removed dead code * Plugins: FS: Add tests for AllowListFS * Plugins: FS: Update comments * Plugins: FS: Use variadic arguments for allow list rather than map * Plugins: FS: Remove unnecessary log * Plugins: FS: Do not escape plugin root dir * Fix merge conflict * Plugins: FS: Update comments * Plugins: FS: PR review changes * Fix merge conflict * Fix tests * Cleanup * Fix flaky test * Changes from PR review * Lint * Add comment to LocalFS.Remove * Fix Windows * Renamed devMode to production
This commit is contained in:
82
pkg/plugins/test_utils.go
Normal file
82
pkg/plugins/test_utils.go
Normal file
@ -0,0 +1,82 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/fs"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
_ FS = &inMemoryFS{}
|
||||
_ fs.File = &inMemoryFile{}
|
||||
_ fs.FileInfo = &inMemoryFileInfo{}
|
||||
)
|
||||
|
||||
// inMemoryFS is an FS that stores files in-memory.
|
||||
type inMemoryFS struct {
|
||||
files map[string][]byte
|
||||
}
|
||||
|
||||
// NewInMemoryFS returns a new FS with the specified files and content.
|
||||
// The provided value is a map from file name (keys) to file content (values).
|
||||
func NewInMemoryFS(files map[string][]byte) FS {
|
||||
return &inMemoryFS{files: files}
|
||||
}
|
||||
|
||||
func (f inMemoryFS) Base() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (f inMemoryFS) Files() ([]string, error) {
|
||||
fps := make([]string, 0, len(f.files))
|
||||
for fn := range f.files {
|
||||
fps = append(fps, fn)
|
||||
}
|
||||
return fps, nil
|
||||
}
|
||||
|
||||
func (f inMemoryFS) Open(fn string) (fs.File, error) {
|
||||
if _, ok := f.files[fn]; !ok {
|
||||
return nil, ErrFileNotExist
|
||||
}
|
||||
return &inMemoryFile{path: fn, reader: bytes.NewReader(f.files[fn])}, nil
|
||||
}
|
||||
|
||||
// NewFakeFS returns a new FS that always returns ErrFileNotExist when trying to Open() and empty Files().
|
||||
func NewFakeFS() FS {
|
||||
return NewInMemoryFS(nil)
|
||||
}
|
||||
|
||||
// inMemoryFile is a fs.File whose content is stored in memory.
|
||||
type inMemoryFile struct {
|
||||
path string
|
||||
reader *bytes.Reader
|
||||
}
|
||||
|
||||
func (f *inMemoryFile) Stat() (fs.FileInfo, error) {
|
||||
return inMemoryFileInfo{
|
||||
name: f.path,
|
||||
size: f.reader.Size(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (f *inMemoryFile) Read(b []byte) (int, error) {
|
||||
return f.reader.Read(b)
|
||||
}
|
||||
|
||||
func (f *inMemoryFile) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type inMemoryFileInfo struct {
|
||||
name string
|
||||
size int64
|
||||
}
|
||||
|
||||
func (f inMemoryFileInfo) Name() string { return f.name }
|
||||
func (f inMemoryFileInfo) Size() int64 { return f.size }
|
||||
func (f inMemoryFileInfo) Mode() os.FileMode { return 0444 } // Read for all
|
||||
func (f inMemoryFileInfo) ModTime() time.Time { return time.Time{} }
|
||||
func (f inMemoryFileInfo) IsDir() bool { return false }
|
||||
func (f inMemoryFileInfo) Sys() interface{} { return nil }
|
Reference in New Issue
Block a user