Skip to content

Commit 1607a62

Browse files
committed
fix: use-plain-http not working on some envs
Signed-off-by: Tomas Pizarro Moreno <tomas.pizarro@broadcom.com>
1 parent e0b5630 commit 1607a62

File tree

2 files changed

+9
-82
lines changed

2 files changed

+9
-82
lines changed

pkg/client/repo/oci/oci.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ func (r *Repo) getTagManifest(chartName, version string) (*ocispec.Manifest, err
102102
u := *r.url
103103
u.Path = path.Join(u.Path, "/", chartName)
104104

105-
// helm replaces plus(+) characters with underscores(_) in the tag (version)
106-
ref, err := name.ParseReference(u.Host + u.Path + ":" + strings.ReplaceAll(version, "+", "_"))
105+
ref, err := r.parseReference(u, version)
107106
if err != nil {
108107
return nil, errors.Errorf("failed parsing OCI reference: %s", err)
109108
}
@@ -213,8 +212,7 @@ func (r *Repo) Fetch(chartName string, version string) (string, error) {
213212
u := *r.url
214213
u.Path = path.Join(u.Path, "/", chartName)
215214

216-
// helm replaces plus(+) characters with underscores(_) in the tag (version)
217-
ref, err := name.ParseReference(u.Host + u.Path + ":" + strings.ReplaceAll(version, "+", "_"))
215+
ref, err := r.parseReference(u, version)
218216
if err != nil {
219217
return "", errors.Errorf("failed parsing OCI reference: %s", err)
220218
}
@@ -285,9 +283,7 @@ func (r *Repo) Has(chartName string, version string) (bool, error) {
285283
u := *r.url
286284
u.Path = path.Join(u.Path, "/", chartName)
287285

288-
// helm replaces plus(+) characters with underscores(_) in the tag (version)
289-
ref, err := name.ParseReference(u.Host + u.Path + ":" + strings.ReplaceAll(version, "+", "_"))
290-
286+
ref, err := r.parseReference(u, version)
291287
if err != nil {
292288
return false, errors.Errorf("failed parsing OCI reference: %s", err)
293289
}
@@ -353,24 +349,13 @@ func isHelmChartContentLayerMediaType(t string) bool {
353349
return false
354350
}
355351

356-
// ociReferenceExists checks if a given oci reference exists in the repository
357-
func ociReferenceExists(ociRef, username, password string) (bool, error) {
358-
ociReference, err := name.ParseReference(ociRef)
359-
if err != nil {
360-
return false, errors.Trace(err)
361-
}
362-
authOptions := authn.Basic{Username: username, Password: password}
363-
opt := []remote.Option{
364-
remote.WithAuth(&authOptions),
365-
}
366-
_, err = remote.Head(ociReference, opt...)
367-
if err != nil {
368-
if strings.Contains(err.Error(), "404 Not Found") {
369-
return false, nil
370-
}
371-
return false, errors.Trace(err)
352+
func (r *Repo) parseReference(u url.URL, version string) (name.Reference, error) {
353+
// helm replaces plus(+) characters with underscores(_) in the tag (version)
354+
ref := u.Host + u.Path + ":" + strings.ReplaceAll(version, "+", "_")
355+
if r.usePlainHTTP {
356+
return name.ParseReference(ref, name.Insecure)
372357
}
373-
return true, nil
358+
return name.ParseReference(ref)
374359
}
375360

376361
// populateEntries populates the entries map with the info from the charts index

pkg/client/repo/oci/oci_internal_test.go

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,13 @@
11
package oci
22

33
import (
4-
"context"
5-
"fmt"
6-
"net/url"
74
"reflect"
85
"sort"
96
"testing"
107

11-
apiv1 "github.com/bitnami/charts-syncer/gen/proto/v1"
128
_ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
139
)
1410

15-
var (
16-
ociRepo = &apiv1.Repo{
17-
Kind: apiv1.Kind_OCI,
18-
Auth: &apiv1.Auth{
19-
Username: "user",
20-
Password: "password",
21-
},
22-
}
23-
)
24-
25-
func TestOciReferenceExists(t *testing.T) {
26-
tests := []struct {
27-
desc string
28-
ociPartialRef string // to be added to repo url returned by PrepareOCIServer
29-
pushTestAsset bool
30-
want bool
31-
}{
32-
{
33-
desc: "Artifact should exist",
34-
ociPartialRef: "index:latest",
35-
pushTestAsset: true,
36-
want: true,
37-
},
38-
{
39-
desc: "Artifact should not exist",
40-
ociPartialRef: "non-existing-index:latest",
41-
pushTestAsset: false,
42-
want: false,
43-
},
44-
}
45-
for _, tc := range tests {
46-
t.Run(tc.desc, func(t *testing.T) {
47-
ctx, cancel := context.WithCancel(context.Background())
48-
defer cancel()
49-
PrepareOCIServer(ctx, t, ociRepo)
50-
u, err := url.Parse(ociRepo.Url)
51-
if err != nil {
52-
t.Fatal(err)
53-
}
54-
ociRef := fmt.Sprintf("%s%s/%s", u.Host, u.Path, tc.ociPartialRef)
55-
if tc.pushTestAsset {
56-
PushFileToOCI(t, "../../../../testdata/oci/index.json", ociRef)
57-
}
58-
got, err := ociReferenceExists(ociRef, ociRepo.GetAuth().GetUsername(), ociRepo.GetAuth().GetPassword())
59-
if err != nil {
60-
t.Fatal(err)
61-
}
62-
if got != tc.want {
63-
t.Errorf("wrong result from OCI reference existence check. got: %v, want: %v", got, tc.want)
64-
}
65-
})
66-
}
67-
}
68-
6911
func TestListWithEntries(t *testing.T) {
7012
entries := map[string][]string{
7113
"chartA": {"1.0.1", "1.0.2"},

0 commit comments

Comments
 (0)