support proxy for helm repo validation (#10359)

pull/10413/head
Matt Hook 2023-09-29 11:37:30 +13:00 committed by GitHub
parent 6163aaa577
commit 157393c965
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 10 deletions

View File

@ -1,18 +1,15 @@
package libhelm package libhelm
import ( import (
"errors"
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"path" "path"
"strings" "strings"
"time" "time"
"github.com/pkg/errors"
) )
const invalidChartRepo = "%q is not a valid chart repository or cannot be reached"
func ValidateHelmRepositoryURL(repoUrl string, client *http.Client) error { func ValidateHelmRepositoryURL(repoUrl string, client *http.Client) error {
if repoUrl == "" { if repoUrl == "" {
return errors.New("URL is required") return errors.New("URL is required")
@ -20,11 +17,11 @@ func ValidateHelmRepositoryURL(repoUrl string, client *http.Client) error {
url, err := url.ParseRequestURI(repoUrl) url, err := url.ParseRequestURI(repoUrl)
if err != nil { if err != nil {
return errors.Wrap(err, fmt.Sprintf("invalid helm chart URL: %s", repoUrl)) return fmt.Errorf("invalid helm repository URL '%s': %w", repoUrl, err)
} }
if !strings.EqualFold(url.Scheme, "http") && !strings.EqualFold(url.Scheme, "https") { if !strings.EqualFold(url.Scheme, "http") && !strings.EqualFold(url.Scheme, "https") {
return errors.New(fmt.Sprintf("invalid helm chart URL: %s", repoUrl)) return fmt.Errorf("invalid helm repository URL '%s'", repoUrl)
} }
url.Path = path.Join(url.Path, "index.yaml") url.Path = path.Join(url.Path, "index.yaml")
@ -32,18 +29,20 @@ func ValidateHelmRepositoryURL(repoUrl string, client *http.Client) error {
if client == nil { if client == nil {
client = &http.Client{ client = &http.Client{
Timeout: time.Second * 10, Timeout: time.Second * 10,
Transport: http.DefaultTransport,
} }
} }
const invalidChartRepo = "%s is not a valid chart repository or cannot be reached: %w"
response, err := client.Head(url.String()) response, err := client.Head(url.String())
if err != nil { if err != nil {
return errors.Wrapf(err, invalidChartRepo, repoUrl) return fmt.Errorf(invalidChartRepo, repoUrl, err)
} }
// Success is indicated with 2xx status codes // Success is indicated with 2xx status codes. 3xx status codes indicate a redirect.
statusOK := response.StatusCode >= 200 && response.StatusCode < 300 statusOK := response.StatusCode >= 200 && response.StatusCode < 300
if !statusOK { if !statusOK {
return errors.Errorf(invalidChartRepo, repoUrl) return fmt.Errorf(invalidChartRepo, repoUrl, err)
} }
return nil return nil