...

Source file src/helm.sh/helm/v3/pkg/getter/getter.go

Documentation: helm.sh/helm/v3/pkg/getter

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package getter
    18  
    19  import (
    20  	"bytes"
    21  	"net/http"
    22  	"time"
    23  
    24  	"github.com/pkg/errors"
    25  
    26  	"helm.sh/helm/v3/pkg/cli"
    27  	"helm.sh/helm/v3/pkg/registry"
    28  )
    29  
    30  // options are generic parameters to be provided to the getter during instantiation.
    31  //
    32  // Getters may or may not ignore these parameters as they are passed in.
    33  type options struct {
    34  	url                   string
    35  	certFile              string
    36  	keyFile               string
    37  	caFile                string
    38  	unTar                 bool
    39  	insecureSkipVerifyTLS bool
    40  	plainHTTP             bool
    41  	username              string
    42  	password              string
    43  	passCredentialsAll    bool
    44  	userAgent             string
    45  	version               string
    46  	registryClient        *registry.Client
    47  	timeout               time.Duration
    48  	transport             *http.Transport
    49  }
    50  
    51  // Option allows specifying various settings configurable by the user for overriding the defaults
    52  // used when performing Get operations with the Getter.
    53  type Option func(*options)
    54  
    55  // WithURL informs the getter the server name that will be used when fetching objects. Used in conjunction with
    56  // WithTLSClientConfig to set the TLSClientConfig's server name.
    57  func WithURL(url string) Option {
    58  	return func(opts *options) {
    59  		opts.url = url
    60  	}
    61  }
    62  
    63  // WithBasicAuth sets the request's Authorization header to use the provided credentials
    64  func WithBasicAuth(username, password string) Option {
    65  	return func(opts *options) {
    66  		opts.username = username
    67  		opts.password = password
    68  	}
    69  }
    70  
    71  func WithPassCredentialsAll(pass bool) Option {
    72  	return func(opts *options) {
    73  		opts.passCredentialsAll = pass
    74  	}
    75  }
    76  
    77  // WithUserAgent sets the request's User-Agent header to use the provided agent name.
    78  func WithUserAgent(userAgent string) Option {
    79  	return func(opts *options) {
    80  		opts.userAgent = userAgent
    81  	}
    82  }
    83  
    84  // WithInsecureSkipVerifyTLS determines if a TLS Certificate will be checked
    85  func WithInsecureSkipVerifyTLS(insecureSkipVerifyTLS bool) Option {
    86  	return func(opts *options) {
    87  		opts.insecureSkipVerifyTLS = insecureSkipVerifyTLS
    88  	}
    89  }
    90  
    91  // WithTLSClientConfig sets the client auth with the provided credentials.
    92  func WithTLSClientConfig(certFile, keyFile, caFile string) Option {
    93  	return func(opts *options) {
    94  		opts.certFile = certFile
    95  		opts.keyFile = keyFile
    96  		opts.caFile = caFile
    97  	}
    98  }
    99  
   100  func WithPlainHTTP(plainHTTP bool) Option {
   101  	return func(opts *options) {
   102  		opts.plainHTTP = plainHTTP
   103  	}
   104  }
   105  
   106  // WithTimeout sets the timeout for requests
   107  func WithTimeout(timeout time.Duration) Option {
   108  	return func(opts *options) {
   109  		opts.timeout = timeout
   110  	}
   111  }
   112  
   113  func WithTagName(tagname string) Option {
   114  	return func(opts *options) {
   115  		opts.version = tagname
   116  	}
   117  }
   118  
   119  func WithRegistryClient(client *registry.Client) Option {
   120  	return func(opts *options) {
   121  		opts.registryClient = client
   122  	}
   123  }
   124  
   125  func WithUntar() Option {
   126  	return func(opts *options) {
   127  		opts.unTar = true
   128  	}
   129  }
   130  
   131  // WithTransport sets the http.Transport to allow overwriting the HTTPGetter default.
   132  func WithTransport(transport *http.Transport) Option {
   133  	return func(opts *options) {
   134  		opts.transport = transport
   135  	}
   136  }
   137  
   138  // Getter is an interface to support GET to the specified URL.
   139  type Getter interface {
   140  	// Get file content by url string
   141  	Get(url string, options ...Option) (*bytes.Buffer, error)
   142  }
   143  
   144  // Constructor is the function for every getter which creates a specific instance
   145  // according to the configuration
   146  type Constructor func(options ...Option) (Getter, error)
   147  
   148  // Provider represents any getter and the schemes that it supports.
   149  //
   150  // For example, an HTTP provider may provide one getter that handles both
   151  // 'http' and 'https' schemes.
   152  type Provider struct {
   153  	Schemes []string
   154  	New     Constructor
   155  }
   156  
   157  // Provides returns true if the given scheme is supported by this Provider.
   158  func (p Provider) Provides(scheme string) bool {
   159  	for _, i := range p.Schemes {
   160  		if i == scheme {
   161  			return true
   162  		}
   163  	}
   164  	return false
   165  }
   166  
   167  // Providers is a collection of Provider objects.
   168  type Providers []Provider
   169  
   170  // ByScheme returns a Provider that handles the given scheme.
   171  //
   172  // If no provider handles this scheme, this will return an error.
   173  func (p Providers) ByScheme(scheme string) (Getter, error) {
   174  	for _, pp := range p {
   175  		if pp.Provides(scheme) {
   176  			return pp.New()
   177  		}
   178  	}
   179  	return nil, errors.Errorf("scheme %q not supported", scheme)
   180  }
   181  
   182  const (
   183  	// The cost timeout references curl's default connection timeout.
   184  	// https://github.com/curl/curl/blob/master/lib/connect.h#L40C21-L40C21
   185  	// The helm commands are usually executed manually. Considering the acceptable waiting time, we reduced the entire request time to 120s.
   186  	DefaultHTTPTimeout = 120
   187  )
   188  
   189  var defaultOptions = []Option{WithTimeout(time.Second * DefaultHTTPTimeout)}
   190  
   191  var httpProvider = Provider{
   192  	Schemes: []string{"http", "https"},
   193  	New: func(options ...Option) (Getter, error) {
   194  		options = append(options, defaultOptions...)
   195  		return NewHTTPGetter(options...)
   196  	},
   197  }
   198  
   199  var ociProvider = Provider{
   200  	Schemes: []string{registry.OCIScheme},
   201  	New:     NewOCIGetter,
   202  }
   203  
   204  // All finds all of the registered getters as a list of Provider instances.
   205  // Currently, the built-in getters and the discovered plugins with downloader
   206  // notations are collected.
   207  func All(settings *cli.EnvSettings) Providers {
   208  	result := Providers{httpProvider, ociProvider}
   209  	pluginDownloaders, _ := collectPlugins(settings)
   210  	result = append(result, pluginDownloaders...)
   211  	return result
   212  }
   213  

View as plain text