support proxy for helm repo validation (#10358)

pull/10401/head
Matt Hook 1 year ago committed by GitHub
parent ada6b31f69
commit 9440aa733d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,18 +1,15 @@
package libhelm
import (
"errors"
"fmt"
"net/http"
"net/url"
"path"
"strings"
"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 {
if repoUrl == "" {
return errors.New("URL is required")
@ -20,30 +17,32 @@ func ValidateHelmRepositoryURL(repoUrl string, client *http.Client) error {
url, err := url.ParseRequestURI(repoUrl)
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") {
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")
if client == nil {
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())
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
if !statusOK {
return errors.Errorf(invalidChartRepo, repoUrl)
return fmt.Errorf(invalidChartRepo, repoUrl, err)
}
return nil

Loading…
Cancel
Save