Skip to content

Commit ef70872

Browse files
fix: correct filename for provenance file (#1492) (#1495)
According to the helm [spec](https://github.com/helm/community/blob/05eab5bba990d4f5556f90bfc001662d1a76f9a5/hips/hip-0006.md#2-support-for-provenance-files) > assuming the presence of a file suffixed with .prov sitting next to a chart .tgz in a repository. This is why, we initially downloaded the provenance file into a file with only the `.prov` extension. However, according to the [helm documentation](https://github.com/helm/helm-www/blob/cf3e4f875101fa3f72ff6194499b5365723f0ec1/content/en/docs/topics/provenance.md) > For example, if the base URL for a package is https://example.com/charts/mychart-1.2.3.tgz, the provenance file, if it exists, MUST be accessible at https://example.com/charts/mychart-1.2.3.tgz.prov. Accordingly, we changed the filename for the provenance content to include the extensions of the original helm-chart (cherry picked from commit ab3c2d9) Co-authored-by: Frederic Wilhelm <frederic.wilhelm@sap.com>
1 parent adc65c4 commit ef70872

3 files changed

Lines changed: 7 additions & 37 deletions

File tree

api/ocm/extensions/download/handlers/helm/download_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ var _ = Describe("upload", func() {
8080
})
8181
It("register helm download handler by name for special artifact type", func() {
8282
MustBeSuccessful(download.RegisterHandlerByName(env, helm.PATH, nil, download.ForArtifactType(ArtifactType)))
83-
8483
repo := Must(ctf.Open(env.OCMContext(), accessobj.ACC_READONLY, CTFPath, 0o777, env))
8584
cv := Must(repo.LookupComponentVersion(Component, Version))
8685
path := Must(download.DownloadResource(env.OCMContext(), Must(cv.SelectResources(selectors.Identity(v1.Identity{"name": SpecialOCIResource})))[0], "/resource", download.WithFileSystem(env.FileSystem())))
@@ -94,6 +93,7 @@ var _ = Describe("upload", func() {
9493
cv := Must(repo.LookupComponentVersion(Component, Version))
9594
path := Must(download.DownloadResource(env.OCMContext(), Must(cv.SelectResources(selectors.Identity(v1.Identity{"name": UnusualOCIResource})))[0], "/resource", download.WithFileSystem(env.FileSystem())))
9695
MustBeSuccessful(tarutils.ExtractArchiveToFs(env.FileSystem(), path, env.FileSystem()))
96+
Expect(Must(vfs.FileExists(env.FileSystem(), path+".prov"))).To(BeTrue())
9797
Expect(Must(vfs.FileExists(env.FileSystem(), "/postgresql/Chart.yaml"))).To(BeTrue())
9898
})
9999

@@ -103,6 +103,7 @@ var _ = Describe("upload", func() {
103103
cv := Must(repo.LookupComponentVersion(Component, Version))
104104
path := Must(download.DownloadResource(env.OCMContext(), Must(cv.SelectResources(selectors.Identity(v1.Identity{"name": UnusualOCIResource})))[0], "/path.tgz", download.WithFileSystem(env.FileSystem())))
105105
MustBeSuccessful(tarutils.ExtractArchiveToFs(env.FileSystem(), path, env.FileSystem()))
106+
Expect(Must(vfs.FileExists(env.FileSystem(), path+".prov"))).To(BeTrue())
106107
Expect(Must(vfs.FileExists(env.FileSystem(), "/postgresql/Chart.yaml"))).To(BeTrue())
107108
})
108109

@@ -112,6 +113,7 @@ var _ = Describe("upload", func() {
112113
cv := Must(repo.LookupComponentVersion(Component, Version))
113114
path := Must(download.DownloadResource(env.OCMContext(), Must(cv.SelectResources(selectors.Identity(v1.Identity{"name": UnusualOCIResource})))[0], "/path.tar.gz", download.WithFileSystem(env.FileSystem())))
114115
MustBeSuccessful(tarutils.ExtractArchiveToFs(env.FileSystem(), path, env.FileSystem()))
116+
Expect(Must(vfs.FileExists(env.FileSystem(), path+".prov"))).To(BeTrue())
115117
Expect(Must(vfs.FileExists(env.FileSystem(), "/postgresql/Chart.yaml"))).To(BeTrue())
116118
})
117119

api/ocm/extensions/download/handlers/helm/handler.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"io"
7-
"path/filepath"
87
"strings"
98

109
"github.com/mandelsoft/goutils/finalizer"
@@ -156,7 +155,10 @@ func download(p common.Printer, art oci.ArtifactAccess, path string, fs vfs.File
156155
return "", "", err
157156
}
158157
finalize.Close(provBlob)
159-
prov = trimExtN(chart, 2) + ".prov"
158+
159+
// Path to provenance file is the same as chart, but with .prov suffix. See
160+
// https://github.com/helm/helm-www/blob/cf3e4f875101fa3f72ff6194499b5365723f0ec1/content/en/docs/topics/provenance.md
161+
prov = chart + ".prov"
160162
if err := write(p, provBlob, prov, fs); err != nil {
161163
return "", "", err
162164
}
@@ -167,19 +169,6 @@ func download(p common.Printer, art oci.ArtifactAccess, path string, fs vfs.File
167169
return chart, prov, nil
168170
}
169171

170-
// trimExtN trims the file extension from the path, at most n times.
171-
// This is useful for removing the ".tgz" or ".tar.gz" suffix from a file name.
172-
func trimExtN(path string, n int) string {
173-
for i := 0; i < n; i++ {
174-
trimmed := strings.TrimSuffix(path, filepath.Ext(path))
175-
if trimmed == path {
176-
break
177-
}
178-
path = trimmed
179-
}
180-
return path
181-
}
182-
183172
func findLayer(layers []ocispec.Descriptor, mediaType string) (*ocispec.Descriptor, error) {
184173
var candidates []*ocispec.Descriptor
185174

api/ocm/extensions/download/handlers/helm/handler_test.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,3 @@ func TestAssureArchiveSuffix(t *testing.T) {
2222
}
2323
}
2424
}
25-
26-
func TestTrimExtN(t *testing.T) {
27-
tests := []struct {
28-
input string
29-
n int
30-
expected string
31-
}{
32-
{"chart.tgz", 1, "chart"},
33-
{"chart.tar.gz", 1, "chart.tar"},
34-
{"chart.tar.gz", 2, "chart"},
35-
{"archive.zip", 1, "archive"},
36-
{"archive", 1, "archive"},
37-
}
38-
39-
for _, tt := range tests {
40-
result := trimExtN(tt.input, tt.n)
41-
if result != tt.expected {
42-
t.Errorf("trimExtN(%q, %d) = %q; want %q", tt.input, tt.n, result, tt.expected)
43-
}
44-
}
45-
}

0 commit comments

Comments
 (0)