Skip to content

Commit

Permalink
fix: registry argument to be only the host instead full URL
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Aguilar <pablo.aguilar@outlook.com.br>
  • Loading branch information
thepabloaguilar committed Mar 8, 2024
1 parent 98a888e commit b9d82d8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
11 changes: 9 additions & 2 deletions util/helm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/json"
"errors"
"fmt"
executil "github.com/argoproj/argo-cd/v2/util/exec"
"io"
"net/http"
"net/url"
Expand All @@ -19,6 +18,8 @@ import (
"strings"
"time"

executil "github.com/argoproj/argo-cd/v2/util/exec"

"github.com/argoproj/pkg/sync"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -428,10 +429,16 @@ func (c *nativeHelmChart) GetTags(chart string, noCache bool) (*TagsList, error)
TLSClientConfig: tlsConf,
DisableKeepAlives: true,
}}

repoURL, err := url.Parse(c.repoURL)
if err != nil {
return nil, fmt.Errorf("error parsing repo url: %w", err)
}

repo.Client = &auth.Client{
Client: client,
Cache: nil,
Credential: auth.StaticCredential(c.repoURL, auth.Credential{
Credential: auth.StaticCredential(repoURL.Host, auth.Credential{
Username: c.creds.Username,
Password: c.creds.Password,
}),
Expand Down
68 changes: 68 additions & 0 deletions util/helm/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,71 @@ func TestGetTagsFromUrl(t *testing.T) {
"2.8.0-prerelease.1+build.1234",
})
}

func TestGetTagsFromURLPrivateRepoAuthentication(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Logf("called %s", r.URL.Path)

authorization := r.Header.Get("Authorization")
if authorization == "" {
w.Header().Set("WWW-Authenticate", `Basic realm="helm repo to get tags"`)
w.WriteHeader(http.StatusUnauthorized)
return
}

t.Logf("authorization received %s", authorization)

responseTags := TagsList{
Tags: []string{
"2.8.0",
"2.8.0-prerelease",
"2.8.0_build",
"2.8.0-prerelease_build",
"2.8.0-prerelease.1_build.1234",
},
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(responseTags)
if err != nil {
t.Fatal(err)
}
}))
t.Cleanup(server.Close)

testCases := []struct {
name string
repoURL string
}{
{
name: "should login correctly when the repo path is in the server root",
repoURL: server.URL,
},
{
name: "should login correctly when the repo path is not in the server root",
repoURL: fmt.Sprintf("%s/my-repo", server.URL),
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
client := NewClient(testCase.repoURL, Creds{
InsecureSkipVerify: true,
Username: "my-username",
Password: "my-password",
}, true, "")

tags, err := client.GetTags("mychart", true)

assert.NoError(t, err)
assert.ElementsMatch(t, tags.Tags, []string{
"2.8.0",
"2.8.0-prerelease",
"2.8.0+build",
"2.8.0-prerelease+build",
"2.8.0-prerelease.1+build.1234",
})
})
}
}

0 comments on commit b9d82d8

Please sign in to comment.