[v9.4.x] Plugins: Markdown fetch retry with lowercase (#65388)

cherry pick
This commit is contained in:
Will Browne
2023-03-28 12:19:20 +02:00
committed by GitHub
parent d14faf3cc0
commit 0d7efc076c
2 changed files with 162 additions and 6 deletions
+25 -6
View File
@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"path"
"path/filepath"
@@ -43,6 +44,8 @@ var pluginsCDNFallbackRedirectRequests = promauto.NewCounterVec(prometheus.Count
Help: "Number of requests to the plugins CDN backend redirect fallback handler.",
}, []string{"plugin_id", "plugin_version"})
var ErrUnexpectedFileExtension = errors.New("unexpected file extension")
func (hs *HTTPServer) GetPluginList(c *contextmodel.ReqContext) response.Response {
typeFilter := c.Query("type")
enabledFilter := c.Query("enabled")
@@ -538,12 +541,17 @@ func translatePluginRequestErrorToAPIError(err error) response.Response {
func (hs *HTTPServer) pluginMarkdown(ctx context.Context, pluginId string, name string) ([]byte, error) {
plugin, exists := hs.pluginStore.Plugin(ctx, pluginId)
if !exists {
return nil, plugins.NotFoundError{PluginID: pluginId}
return make([]byte, 0), plugins.NotFoundError{PluginID: pluginId}
}
md, err := plugin.File(mdFilepath(strings.ToUpper(name)))
file, err := mdFilepath(strings.ToUpper(name))
if err != nil {
md, err = plugin.File(mdFilepath(strings.ToUpper(name)))
return make([]byte, 0), err
}
md, err := readPluginFile(plugin, file)
if err != nil {
md, err = readPluginFile(plugin, strings.ToLower(file))
if err != nil {
return make([]byte, 0), nil
}
@@ -553,7 +561,6 @@ func (hs *HTTPServer) pluginMarkdown(ctx context.Context, pluginId string, name
hs.log.Error("Failed to close plugin markdown file", "err", err)
}
}()
d, err := io.ReadAll(md)
if err != nil {
return make([]byte, 0), nil
@@ -561,6 +568,18 @@ func (hs *HTTPServer) pluginMarkdown(ctx context.Context, pluginId string, name
return d, nil
}
func mdFilepath(mdFilename string) string {
return filepath.Clean(filepath.Join("/", fmt.Sprintf("%s.md", mdFilename)))
var readPluginFile = func(p plugins.PluginDTO, name string) (fs.File, error) {
return p.File(name)
}
func mdFilepath(mdFilename string) (string, error) {
fileExt := filepath.Ext(mdFilename)
switch fileExt {
case "md":
return util.CleanRelativePath(mdFilename)
case "":
return util.CleanRelativePath(fmt.Sprintf("%s.md", mdFilename))
default:
return "", ErrUnexpectedFileExtension
}
}