github.com/valdemarpavesi/helm@v2.9.1+incompatible/pkg/getter/httpgetter.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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/urlutil"
    27  	"k8s.io/helm/pkg/version"
    28  )
    29  
    30  //HttpGetter is the efault HTTP(/S) backend handler
    31  // TODO: change the name to HTTPGetter in Helm 3
    32  type HttpGetter struct { //nolint
    33  	client   *http.Client
    34  	username string
    35  	password string
    36  }
    37  
    38  //SetCredentials sets the credentials for the getter
    39  func (g *HttpGetter) SetCredentials(username, password string) {
    40  	g.username = username
    41  	g.password = password
    42  }
    43  
    44  //Get performs a Get from repo.Getter and returns the body.
    45  func (g *HttpGetter) Get(href string) (*bytes.Buffer, error) {
    46  	return g.get(href)
    47  }
    48  
    49  func (g *HttpGetter) get(href string) (*bytes.Buffer, error) {
    50  	buf := bytes.NewBuffer(nil)
    51  
    52  	// Set a helm specific user agent so that a repo server and metrics can
    53  	// separate helm calls from other tools interacting with repos.
    54  	req, err := http.NewRequest("GET", href, nil)
    55  	if err != nil {
    56  		return buf, err
    57  	}
    58  	req.Header.Set("User-Agent", "Helm/"+strings.TrimPrefix(version.GetVersion(), "v"))
    59  
    60  	if g.username != "" && g.password != "" {
    61  		req.SetBasicAuth(g.username, g.password)
    62  	}
    63  
    64  	resp, err := g.client.Do(req)
    65  	if err != nil {
    66  		return buf, err
    67  	}
    68  	if resp.StatusCode != 200 {
    69  		return buf, fmt.Errorf("Failed to fetch %s : %s", href, resp.Status)
    70  	}
    71  
    72  	_, err = io.Copy(buf, resp.Body)
    73  	resp.Body.Close()
    74  	return buf, err
    75  }
    76  
    77  // newHTTPGetter constructs a valid http/https client as Getter
    78  func newHTTPGetter(URL, CertFile, KeyFile, CAFile string) (Getter, error) {
    79  	return NewHTTPGetter(URL, CertFile, KeyFile, CAFile)
    80  }
    81  
    82  // NewHTTPGetter constructs a valid http/https client as HttpGetter
    83  func NewHTTPGetter(URL, CertFile, KeyFile, CAFile string) (*HttpGetter, error) {
    84  	var client HttpGetter
    85  	if CertFile != "" && KeyFile != "" {
    86  		tlsConf, err := tlsutil.NewClientTLS(CertFile, KeyFile, CAFile)
    87  		if err != nil {
    88  			return &client, fmt.Errorf("can't create TLS config for client: %s", err.Error())
    89  		}
    90  		tlsConf.BuildNameToCertificate()
    91  
    92  		sni, err := urlutil.ExtractHostname(URL)
    93  		if err != nil {
    94  			return &client, err
    95  		}
    96  		tlsConf.ServerName = sni
    97  
    98  		client.client = &http.Client{
    99  			Transport: &http.Transport{
   100  				TLSClientConfig: tlsConf,
   101  				Proxy:           http.ProxyFromEnvironment,
   102  			},
   103  		}
   104  	} else {
   105  		client.client = http.DefaultClient
   106  	}
   107  	return &client, nil
   108  }