Unverified Commit 21aa21bf authored by bryanl's avatar bryanl
Browse files

Don't install helm charts with hooks



If a helm hook is encountered in a YAML file in a helm chart's `templates`
directory, skip it.

Fixes #700
Fixes #703
Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent d6701325
......@@ -16,15 +16,18 @@
package registry
import (
"bytes"
"io/ioutil"
"net/url"
"path"
"path/filepath"
"strings"
"github.com/ksonnet/ksonnet/pkg/app"
"github.com/ksonnet/ksonnet/pkg/helm"
"github.com/ksonnet/ksonnet/pkg/parts"
"github.com/ksonnet/ksonnet/pkg/util/archive"
ksstrings "github.com/ksonnet/ksonnet/pkg/util/strings"
"github.com/pkg/errors"
)
......@@ -161,6 +164,11 @@ func (h *Helm) ResolveLibrary(partName string, partAlias string, version string,
name := path.Join(chart.Name, "helm", chart.Version, f.Name)
if containsHelmHook(name, b) {
// skip this file because it has a helm hook in it
return nil
}
return onFile(name, b)
}
......@@ -242,3 +250,15 @@ func (h *Helm) SetURI(uri string) error {
h.spec.URI = uri
return nil
}
// containsHelmHook checks file contents for helm hooks. Helm hooks are denoted by
// `helm.sh/hook`.
func containsHelmHook(name string, b []byte) bool {
dir := filepath.Dir(name)
ext := filepath.Ext(name)
if strings.Contains(dir, "templates") && ksstrings.InSlice(ext, []string{".yaml", ".yml"}) {
return bytes.Contains(b, []byte("helm.sh/hook"))
}
return false
}
......@@ -333,6 +333,54 @@ func TestHelm_ValidateURI_invalid(t *testing.T) {
})
}
func Test_containsHelmHook(t *testing.T) {
cases := []struct {
name string
path string
b []byte
expected bool
}{
{
name: "contains helm hook",
path: "templates/file.yaml",
b: []byte(`"helm.sh/hook": test-success`),
expected: true,
},
{
name: "doesn't contain helm hook",
path: "templates/file.yaml",
b: []byte(`"other": test-success`),
expected: false,
},
{
name: "contains helm hook in directory under templates",
path: "templates/nested/file.yaml",
b: []byte(`"helm.sh/hook": test-success`),
expected: true,
},
{
name: "contains helm hook in directory",
path: "nested/file.yaml",
b: []byte(`"helm.sh/hook": test-success`),
expected: false,
},
{
name: "contains helm hook in directory under templates and not YAML",
path: "templates/file.txt",
b: []byte(`"helm.sh/hook": test-success`),
expected: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := containsHelmHook(tc.path, tc.b)
assert.Equal(t, tc.expected, got)
})
}
}
type fakeHelmRepositoryClient struct {
entries *helm.Repository
entriesErr error
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment