From 3b17121d44eedf493850d1df49eb5fb785976787 Mon Sep 17 00:00:00 2001 From: Tadayuki Onishi Date: Tue, 16 Aug 2022 01:18:34 +0900 Subject: [PATCH] fix: Suppressed ssh scheme url warn log (#9836) * Fixed ssh scheme warn log degrade by #8508 Signed-off-by: kenchan0130 * Expanded repository type getCAPath testing Signed-off-by: kenchan0130 --- .../application/v1alpha1/repository_types.go | 40 ++++++++++++------- pkg/apis/application/v1alpha1/types_test.go | 1 + 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/pkg/apis/application/v1alpha1/repository_types.go b/pkg/apis/application/v1alpha1/repository_types.go index 21e43286292df..e5f6971648949 100644 --- a/pkg/apis/application/v1alpha1/repository_types.go +++ b/pkg/apis/application/v1alpha1/repository_types.go @@ -195,30 +195,40 @@ func (repo *Repository) GetHelmCreds() helm.Creds { } func getCAPath(repoURL string) string { - hostname := "" + // For git ssh protocol url without ssh://, url.Parse() will fail to parse. + // However, no warn log is output since ssh scheme url is a possible format. + if ok, _ := git.IsSSHURL(repoURL); ok { + return "" + } + 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 receive the cert, // otherwise we'll use the parsed path (OCI repos are often specified as // hostname, without protocol). - if parsedURL, err := url.Parse(repoURL); err == nil { - if parsedURL.Scheme == "https" || parsedURL.Scheme == "oci" { - hostname = parsedURL.Host - } else if parsedURL.Scheme == "" { - hostname = parsedURL.Path - } - } 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 } - if hostname != "" { - if caPath, err := cert.GetCertBundlePathForRepository(hostname); err == nil { - return caPath - } else { - log.Warnf("Could not get cert bundle path for repository '%s': %v", repoURL, err) - } + if hostname == "" { + log.Warnf("Could not get hostname for repository '%s'", repoURL) + return "" + } + + caPath, err := cert.GetCertBundlePathForRepository(hostname) + if err != nil { + log.Warnf("Could not get cert bundle path for repository '%s': %v", repoURL, err) + return "" } - return "" + + return caPath } // CopySettingsFrom copies all repository settings from source to receiver diff --git a/pkg/apis/application/v1alpha1/types_test.go b/pkg/apis/application/v1alpha1/types_test.go index a06facf59debf..a68fc019366c1 100644 --- a/pkg/apis/application/v1alpha1/types_test.go +++ b/pkg/apis/application/v1alpha1/types_test.go @@ -2628,6 +2628,7 @@ func TestGetCAPath(t *testing.T) { "oci://bar.example.com", "bar.example.com", "ssh://foo.example.com", + "git@example.com:organization/reponame.git", "/some/invalid/thing", "../another/invalid/thing", "./also/invalid",