github.com/wangchanggan/helm@v0.0.0-20211020154240-11b1b7d5406d/pkg/getter/httpgetter.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package getter
    17  
    18  import (
    19  	"bytes"
    20  	"fmt"
    21  	"io"
    22  	"net/http"
    23  	"strings"
    24  
    25  	"k8s.io/helm/pkg/tlsutil"
    26  	"k8s.io/helm/pkg/version"
    27  )
    28  
    29  //HttpGetter is the default HTTP(/S) backend handler
    30  // TODO: change the name to HTTPGetter in Helm 3
    31  type HttpGetter struct { //nolint
    32  	client   *http.Client
    33  	username string
    34  	password string
    35  }
    36  
    37  //SetCredentials sets the credentials for the getter
    38  func (g *HttpGetter) SetCredentials(username, password string) {
    39  	g.username = username
    40  	g.password = password
    41  }
    42  
    43  //Get performs a Get from repo.Getter and returns the body.
    44  func (g *HttpGetter) Get(href string) (*bytes.Buffer, error) {
    45  	return g.get(href)
    46  }
    47  
    48  func (g *HttpGetter) get(href string) (*bytes.Buffer, error) {
    49  	buf := bytes.NewBuffer(nil)
    50  
    51  	// Set a helm specific user agent so that a repo server and metrics can
    52  	// separate helm calls from other tools interacting with repos.
    53  	req, err := http.NewRequest("GET", href, nil)
    54  	if err != nil {
    55  		return buf, err
    56  	}
    57  	req.Header.Set("User-Agent", "Helm/"+strings.TrimPrefix(version.GetVersion(), "v"))
    58  
    59  	if g.username != "" && g.password != "" {
    60  		req.SetBasicAuth(g.username, g.password)
    61  	}
    62  
    63  	resp, err := g.client.Do(req)
    64  	if err != nil {
    65  		return buf, err
    66  	}
    67  	if resp.StatusCode != 200 {
    68  		return buf, fmt.Errorf("Failed to fetch %s : %s", href, resp.Status)
    69  	}
    70  
    71  	_, err = io.Copy(buf, resp.Body)
    72  	resp.Body.Close()
    73  	return buf, err
    74  }
    75  
    76  // newHTTPGetter constructs a valid http/https client as Getter
    77  func newHTTPGetter(URL, CertFile, KeyFile, CAFile string) (Getter, error) {
    78  	return NewHTTPGetter(URL, CertFile, KeyFile, CAFile)
    79  }
    80  
    81  // NewHTTPGetter constructs a valid http/https client as HttpGetter
    82  func NewHTTPGetter(URL, CertFile, KeyFile, CAFile string) (*HttpGetter, error) {
    83  	var client HttpGetter
    84  	tr := &http.Transport{
    85  		DisableCompression: true,
    86  		Proxy:              http.ProxyFromEnvironment,
    87  	}
    88  	if (CertFile != "" && KeyFile != "") || CAFile != "" {
    89  		tlsConf, err := tlsutil.NewTLSConfig(URL, CertFile, KeyFile, CAFile)
    90  		if err != nil {
    91  			return &client, fmt.Errorf("can't create TLS config: %s", err.Error())
    92  		}
    93  		tr.TLSClientConfig = tlsConf
    94  	}
    95  	client.client = &http.Client{Transport: tr}
    96  	return &client, nil
    97  }