github.com/appscode/helm@v3.0.0-alpha.1+incompatible/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  	"io"
    21  	"net/http"
    22  
    23  	"github.com/pkg/errors"
    24  
    25  	"helm.sh/helm/pkg/tlsutil"
    26  	"helm.sh/helm/pkg/urlutil"
    27  )
    28  
    29  // HTTPGetter is the efault HTTP(/S) backend handler
    30  type HTTPGetter struct {
    31  	client    *http.Client
    32  	username  string
    33  	password  string
    34  	userAgent 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  // SetUserAgent sets the HTTP User-Agent for the getter
    44  func (g *HTTPGetter) SetUserAgent(userAgent string) {
    45  	g.userAgent = userAgent
    46  }
    47  
    48  //Get performs a Get from repo.Getter and returns the body.
    49  func (g *HTTPGetter) Get(href string) (*bytes.Buffer, error) {
    50  	return g.get(href)
    51  }
    52  
    53  func (g *HTTPGetter) get(href string) (*bytes.Buffer, error) {
    54  	buf := bytes.NewBuffer(nil)
    55  
    56  	// Set a helm specific user agent so that a repo server and metrics can
    57  	// separate helm calls from other tools interacting with repos.
    58  	req, err := http.NewRequest("GET", href, nil)
    59  	if err != nil {
    60  		return buf, err
    61  	}
    62  	// req.Header.Set("User-Agent", "Helm/"+strings.TrimPrefix(version.GetVersion(), "v"))
    63  	if g.userAgent != "" {
    64  		req.Header.Set("User-Agent", g.userAgent)
    65  	}
    66  
    67  	if g.username != "" && g.password != "" {
    68  		req.SetBasicAuth(g.username, g.password)
    69  	}
    70  
    71  	resp, err := g.client.Do(req)
    72  	if err != nil {
    73  		return buf, err
    74  	}
    75  	if resp.StatusCode != 200 {
    76  		return buf, errors.Errorf("failed to fetch %s : %s", href, resp.Status)
    77  	}
    78  
    79  	_, err = io.Copy(buf, resp.Body)
    80  	resp.Body.Close()
    81  	return buf, err
    82  }
    83  
    84  // newHTTPGetter constructs a valid http/https client as Getter
    85  func newHTTPGetter(URL, CertFile, KeyFile, CAFile string) (Getter, error) {
    86  	return NewHTTPGetter(URL, CertFile, KeyFile, CAFile)
    87  }
    88  
    89  // NewHTTPGetter constructs a valid http/https client as HTTPGetter
    90  func NewHTTPGetter(URL, CertFile, KeyFile, CAFile string) (*HTTPGetter, error) {
    91  	var client HTTPGetter
    92  	if CertFile != "" && KeyFile != "" {
    93  		tlsConf, err := tlsutil.NewClientTLS(CertFile, KeyFile, CAFile)
    94  		if err != nil {
    95  			return &client, errors.Wrap(err, "can't create TLS config for client")
    96  		}
    97  		tlsConf.BuildNameToCertificate()
    98  
    99  		sni, err := urlutil.ExtractHostname(URL)
   100  		if err != nil {
   101  			return &client, err
   102  		}
   103  		tlsConf.ServerName = sni
   104  
   105  		client.client = &http.Client{
   106  			Transport: &http.Transport{
   107  				TLSClientConfig: tlsConf,
   108  				Proxy:           http.ProxyFromEnvironment,
   109  			},
   110  		}
   111  	} else {
   112  		client.client = http.DefaultClient
   113  	}
   114  	return &client, nil
   115  }