Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helm Charts: Prevent vendor panic by adding validation #718

Merged
merged 4 commits into from Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,8 @@

- **helm**: Fix `vendor --prune` deleting charts with a custom directory
**[#717](https://github.com/grafana/tanka/pull/717)**
- **helm**: Add validation at vendoring time for invalid chart names
**[#718](https://github.com/grafana/tanka/pull/718)**

## 0.22.0 (2022-06-03)

Expand Down
7 changes: 5 additions & 2 deletions pkg/helm/charts.go
Expand Up @@ -98,7 +98,7 @@ func (c Charts) Vendor(prune bool) error {
}

// Check that there are no output conflicts before vendoring
if err := c.Manifest.Requires.CheckForOutputConflicts(); err != nil {
if err := c.Manifest.Requires.Validate(); err != nil {
return err
}

Expand Down Expand Up @@ -207,7 +207,7 @@ func (c *Charts) Add(reqs []string) error {
log.Println(" OK:", s)
}

if err := requirements.CheckForOutputConflicts(); err != nil {
if err := requirements.Validate(); err != nil {
return err
}

Expand Down Expand Up @@ -323,6 +323,9 @@ func parseReqRepo(s string) string {
// parseReqName parses a name from a string of the format `repo/name`
func parseReqName(s string) string {
elems := strings.SplitN(s, "/", 2)
if len(elems) == 1 {
return ""
}
name := elems[1]
return name
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/helm/charts_test.go
Expand Up @@ -103,7 +103,7 @@ func TestAdd(t *testing.T) {

// Adding a chart with a different version to the same path, causes a conflict
err = c.Add([]string{"stable/prometheus@11.12.0"})
assert.EqualError(t, err, `Output directory conflicts found:
assert.EqualError(t, err, `Validation errors:
- output directory "prometheus" is used twice, by charts "stable/prometheus@11.12.1" and "stable/prometheus@11.12.0"`)

// Add a chart with a specific extract directory
Expand Down Expand Up @@ -167,3 +167,18 @@ func TestPrune(t *testing.T) {
})
}
}

func TestInvalidChartName(t *testing.T) {
tempDir := t.TempDir()
c, err := InitChartfile(filepath.Join(tempDir, Filename))
require.NoError(t, err)

c.Manifest.Requires = append(c.Manifest.Requires, Requirement{
Chart: "noslash",
Version: *semver.MustParse("1.0.0"),
})

err = c.Vendor(false)
assert.EqualError(t, err, `Validation errors:
- Chart name "noslash" is not valid. Expecting a repo/name format.`)
}
9 changes: 7 additions & 2 deletions pkg/helm/spec.go
Expand Up @@ -100,11 +100,16 @@ func (r Requirements) Has(req Requirement) bool {
return false
}

func (r Requirements) CheckForOutputConflicts() error {
func (r Requirements) Validate() error {
outputDirs := make(map[string]Requirement)
errs := make([]string, 0)

for _, req := range r {
if !strings.Contains(req.Chart, "/") {
errs = append(errs, fmt.Sprintf("Chart name %q is not valid. Expecting a repo/name format.", req.Chart))
continue
}

dir := req.Directory
if dir == "" {
dir = parseReqName(req.Chart)
Expand All @@ -116,7 +121,7 @@ func (r Requirements) CheckForOutputConflicts() error {
}

if len(errs) > 0 {
return fmt.Errorf("Output directory conflicts found:\n - " + strings.Join(errs, "\n - "))
return fmt.Errorf("Validation errors:\n - " + strings.Join(errs, "\n - "))
}

return nil
Expand Down