Skip to content

Commit

Permalink
validate plugin metadata before loading
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Fisher <matt.fisher@microsoft.com>
  • Loading branch information
Matthew Fisher committed Sep 17, 2020
1 parent f65808b commit b0296c0
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/plugin/installer/local_installer_test.go
Expand Up @@ -48,7 +48,7 @@ func TestLocalInstaller(t *testing.T) {
t.Fatal(err)
}

source := "../testdata/plugdir/echo"
source := "../testdata/plugdir/good/echo"
i, err := NewForSource(source, "", home)
if err != nil {
t.Errorf("unexpected error: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/installer/vcs_installer_test.go
Expand Up @@ -61,7 +61,7 @@ func TestVCSInstaller(t *testing.T) {
}

source := "https://github.com/adamreese/helm-env"
testRepoPath, _ := filepath.Abs("../testdata/plugdir/echo")
testRepoPath, _ := filepath.Abs("../testdata/plugdir/good/echo")
repo := &testRepo{
local: testRepoPath,
tags: []string{"0.1.0", "0.1.1"},
Expand Down
13 changes: 11 additions & 2 deletions pkg/plugin/plugin.go
Expand Up @@ -21,9 +21,10 @@ import (
"path/filepath"
"strings"

helm_env "k8s.io/helm/pkg/helm/environment"

"github.com/ghodss/yaml"
yaml2 "gopkg.in/yaml.v2"

helm_env "k8s.io/helm/pkg/helm/environment"
)

const pluginFileName = "plugin.yaml"
Expand Down Expand Up @@ -119,12 +120,20 @@ func LoadDir(dirname string) (*Plugin, error) {
}

plug := &Plugin{Dir: dirname}
if err := validateMeta(data); err != nil {
return nil, err
}
if err := yaml.Unmarshal(data, &plug.Metadata); err != nil {
return nil, err
}
return plug, nil
}

func validateMeta(data []byte) error {
// This is done ONLY for validation. We need to use ghodss/yaml for the actual parsing.
return yaml2.UnmarshalStrict(data, &Metadata{})
}

// LoadAll loads all plugins found beneath the base directory.
//
// This scans only one directory level.
Expand Down
13 changes: 10 additions & 3 deletions pkg/plugin/plugin_test.go
Expand Up @@ -64,7 +64,7 @@ func TestPrepareCommand(t *testing.T) {
}

func TestLoadDir(t *testing.T) {
dirname := "testdata/plugdir/hello"
dirname := "testdata/plugdir/good/hello"
plug, err := LoadDir(dirname)
if err != nil {
t.Fatalf("error loading Hello plugin: %s", err)
Expand Down Expand Up @@ -92,8 +92,15 @@ func TestLoadDir(t *testing.T) {
}
}

func TestLoadDirDuplicateEntries(t *testing.T) {
dirname := "testdata/plugdir/bad/duplicate-entries"
if _, err := LoadDir(dirname); err == nil {
t.Errorf("successfully loaded plugin with duplicate entries when it should've failed")
}
}

func TestDownloader(t *testing.T) {
dirname := "testdata/plugdir/downloader"
dirname := "testdata/plugdir/good/downloader"
plug, err := LoadDir(dirname)
if err != nil {
t.Fatalf("error loading Hello plugin: %s", err)
Expand Down Expand Up @@ -131,7 +138,7 @@ func TestLoadAll(t *testing.T) {
t.Fatalf("expected empty dir to have 0 plugins")
}

basedir := "testdata/plugdir"
basedir := "testdata/plugdir/good"
plugs, err := LoadAll(basedir)
if err != nil {
t.Fatalf("Could not load %q: %s", basedir, err)
Expand Down
12 changes: 12 additions & 0 deletions pkg/plugin/testdata/plugdir/bad/duplicate-entries/plugin.yaml
@@ -0,0 +1,12 @@
name: "duplicate-entries"
version: "0.1.0"
usage: "usage"
description: |-
description
command: "echo hello"
useTunnel: true
ignoreFlags: true
hooks:
install: "echo installing..."
hooks:
install: "echo installing something different"
File renamed without changes.
Expand Up @@ -6,6 +6,5 @@ description: |-
command: "$HELM_PLUGIN_SELF/hello.sh"
useTunnel: true
ignoreFlags: true
install: "echo installing..."
hooks:
install: "echo installing..."

0 comments on commit b0296c0

Please sign in to comment.