Add plugin validity checks to isPlugin

Signed-off-by: Zethan <zetHannes@outlook.com>
pull/10728/head
Zethan 4 years ago
parent ef59b54307
commit 5df3c3e3f6

@ -17,6 +17,7 @@ package installer
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
@ -26,6 +27,7 @@ import (
"github.com/pkg/errors"
"helm.sh/helm/v3/pkg/plugin"
"sigs.k8s.io/yaml"
)
// ErrMissingMetadata indicates that plugin.yaml is missing.
@ -120,10 +122,18 @@ func isRemoteHTTPArchive(source string) bool {
return false
}
// isPlugin checks if the directory contains a plugin.yaml file.
// isPlugin checks if the directory contains a valid plugin.yaml file
func isPlugin(dirname string) bool {
_, err := os.Stat(filepath.Join(dirname, plugin.PluginFileName))
return err == nil
fpath := filepath.Join(dirname, plugin.PluginFileName)
if _, err := os.Stat(fpath); err != nil {
return false
}
plug := &plugin.Plugin{Dir: dirname}
data, _ := ioutil.ReadFile(fpath)
if err := yaml.UnmarshalStrict(data, &plug.Metadata); err != nil || plug.Metadata == nil {
return false
}
return true
}
var logger = log.New(os.Stderr, "[debug] ", log.Lshortfile)

@ -38,3 +38,15 @@ func TestIsRemoteHTTPArchive(t *testing.T) {
t.Error("Expected media type match to fail")
}
}
func TestIsPlugin(t *testing.T) {
if isPlugin("../testdata/plugdir/bad/empty-config") {
t.Errorf("Expected empty plugin file not to be considered a plugin")
}
if !isPlugin("../testdata/plugdir/good/echo") {
t.Errorf("Expected valid plugin file to be considered a plugin")
}
if isPlugin("../testdata/plugdir/bad/invalid") {
t.Errorf("Expected invalid plugin not to be considered a plugin")
}
}

@ -0,0 +1,3 @@
name: "invalid-test"
version: "1.2.3"
usa
Loading…
Cancel
Save