Skip to content

Commit

Permalink
Allow to sync and store charts with spaces in name (#3758)
Browse files Browse the repository at this point in the history
Signed-off-by: Rafa Castelblanque <rcastelblanq@vmware.com>
  • Loading branch information
Rafa Castelblanque committed Nov 30, 2021
1 parent a1f95a5 commit 8fe3a96
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
7 changes: 7 additions & 0 deletions cmd/asset-syncer/server/testdata/helm-index-spaces.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
entries:
chart-with-spaces:
- appVersion: v1
name: "chart\x20with\x20spaces"
chart-without-spaces:
- appVersion: v2
name: "chart-without-spaces"
23 changes: 22 additions & 1 deletion cmd/asset-syncer/server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,27 @@ func satisfy(chartInput map[string]interface{}, code *gojq.Code, vars []interfac
return satisfied, nil
}

// Make sure charts are treated without escaped data
func unescapeChartsData(charts []models.Chart) []models.Chart {
result := []models.Chart{}
for _, chart := range charts {
chart.Name = unescapeOrDefaultValue(chart.Name)
chart.ID = unescapeOrDefaultValue(chart.ID)
result = append(result, chart)
}
return result
}

// Unescape string or return value itself if error
func unescapeOrDefaultValue(value string) string {
unescapedValue, err := url.PathUnescape(value)
if err != nil {
return value
} else {
return unescapedValue
}
}

func filterCharts(charts []models.Chart, filterRule *apprepov1alpha1.FilterRuleSpec) ([]models.Chart, error) {
if filterRule == nil || filterRule.JQ == "" {
// No filter
Expand Down Expand Up @@ -248,7 +269,7 @@ func (r *HelmRepo) Charts(fetchLatestOnly bool) ([]models.Chart, error) {
return []models.Chart{}, fmt.Errorf("no charts in repository index")
}

return filterCharts(charts, r.filter)
return filterCharts(unescapeChartsData(charts), r.filter)
}

// FetchFiles retrieves the important files of a chart and version from the repo
Expand Down
95 changes: 95 additions & 0 deletions cmd/asset-syncer/server/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,101 @@ func Test_filterCharts(t *testing.T) {
}
}

func TestUnescapeCharsData(t *testing.T) {
tests := []struct {
description string
input []models.Chart
expected []models.Chart
}{
{
"chart with encoded spaces in id",
[]models.Chart{
{ID: "foo%20bar"},
},
[]models.Chart{
{ID: "foo bar"},
},
},
{
"chart with encoded spaces in name",
[]models.Chart{
{Name: "foo%20bar"},
},
[]models.Chart{
{Name: "foo bar"},
},
},
{
"chart with mixed encoding in name",
[]models.Chart{
{Name: "test/foo%20bar"},
},
[]models.Chart{
{Name: "test/foo bar"},
},
},
{
"chart with no encoding nor spaces",
[]models.Chart{
{Name: "test/foobar"},
},
[]models.Chart{
{Name: "test/foobar"},
},
},
{
"chart with unencoded spaces",
[]models.Chart{
{Name: "test/foo bar"},
},
[]models.Chart{
{Name: "test/foo bar"},
},
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
res := unescapeChartsData(tt.input)
if !cmp.Equal(res, tt.expected) {
t.Errorf("Unexpected result: %v", cmp.Diff(res, tt.expected))
}
})
}
}

func TestHelmRepoAppliesUnescape(t *testing.T) {
repo := &models.RepoInternal{Name: "test", Namespace: "repo-namespace", URL: "http://testrepo.com"}
expectedRepo := &models.Repo{Name: repo.Name, Namespace: repo.Namespace, URL: repo.URL}
repoIndexYAMLBytes, _ := ioutil.ReadFile("testdata/helm-index-spaces.yaml")
repoIndexYAML := string(repoIndexYAMLBytes)
expectedCharts := []models.Chart{
{
ID: "test/chart with spaces",
Name: "chart with spaces",
Repo: expectedRepo,
Maintainers: []chart.Maintainer{},
ChartVersions: []models.ChartVersion{{AppVersion: "v1"}},
},
{
ID: "test/chart-without-spaces",
Name: "chart-without-spaces",
Repo: expectedRepo,
Maintainers: []chart.Maintainer{},
ChartVersions: []models.ChartVersion{{AppVersion: "v2"}},
},
}
helmRepo := &HelmRepo{
content: []byte(repoIndexYAML),
RepoInternal: repo,
}
t.Run("Helm repo applies unescaping to chart data", func(t *testing.T) {
charts, _ := helmRepo.Charts(false)
if !cmp.Equal(charts, expectedCharts) {
t.Errorf("Unexpected result: %v", cmp.Diff(charts, expectedCharts))
}
})
}

func Test_isURLDomainEqual(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 8fe3a96

Please sign in to comment.