Skip to content

Commit

Permalink
fix: Parse hostname correctly from repoURL to fetch correct CA cert (a…
Browse files Browse the repository at this point in the history
…rgoproj#19488)

Signed-off-by: Siddhesh Ghadi <sghadi1203@gmail.com>
Co-authored-by: Jann Fischer <jann@mistrust.net>
Signed-off-by: ChichiCaleb <ChichiCaleb@chichiuchenna@gmail.com>
Signed-off-by: ChichiCaleb <chichiuchenna@gmail.com>
  • Loading branch information
2 people authored and ChichiCaleb committed Aug 15, 2024
1 parent fc7861a commit 79229b3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
22 changes: 12 additions & 10 deletions pkg/apis/application/v1alpha1/repository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1alpha1
import (
"fmt"
"net/url"
"strings"

"github.com/argoproj/argo-cd/v2/util/cert"
"github.com/argoproj/argo-cd/v2/util/git"
Expand Down Expand Up @@ -227,21 +228,22 @@ func getCAPath(repoURL string) string {
}

hostname := ""
// url.Parse() will happily parse most things thrown at it. When the URL
// is either https or oci, we use the parsed hostname to retrieve the cert,
// otherwise we'll use the parsed path (OCI repos are often specified as
// hostname, without protocol).
parsedURL, err := url.Parse(repoURL)
var parsedURL *url.URL
var err error
// Without schema in url, url.Parse() treats the url as differently
// and may incorrectly parses the hostname if url contains a path or port.
// To ensure proper parsing, prepend a dummy schema.
if !strings.Contains(repoURL, "://") {
parsedURL, err = url.Parse("protocol://" + repoURL)
} else {
parsedURL, err = url.Parse(repoURL)
}
if err != nil {
log.Warnf("Could not parse repo URL '%s': %v", repoURL, err)
return ""
}
if parsedURL.Scheme == "https" || parsedURL.Scheme == "oci" {
hostname = parsedURL.Host
} else if parsedURL.Scheme == "" {
hostname = parsedURL.Path
}

hostname = parsedURL.Hostname()
if hostname == "" {
log.Warnf("Could not get hostname for repository '%s'", repoURL)
return ""
Expand Down
11 changes: 9 additions & 2 deletions pkg/apis/application/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3386,18 +3386,25 @@ func TestGetCAPath(t *testing.T) {
"https://foo.example.com",
"oci://foo.example.com",
"foo.example.com",
"foo.example.com/charts",
"https://foo.example.com:5000",
"foo.example.com:5000",
"foo.example.com:5000/charts",
"ssh://foo.example.com",
}
invalidpath := []string{
"https://bar.example.com",
"oci://bar.example.com",
"bar.example.com",
"ssh://foo.example.com",
"git@example.com:organization/reponame.git",
"ssh://bar.example.com",
"git@foo.example.com:organization/reponame.git",
"ssh://git@foo.example.com:organization/reponame.git",
"/some/invalid/thing",
"../another/invalid/thing",
"./also/invalid",
"$invalid/as/well",
"..",
"://invalid",
}

for _, str := range validcert {
Expand Down

0 comments on commit 79229b3

Please # to comment.