github.com/keleustes/helm@v3.0.0-beta.3+incompatible/internal/tlsutil/cfg.go (about)

     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 tlsutil
    18  
    19  import (
    20  	"crypto/tls"
    21  	"crypto/x509"
    22  	"os"
    23  
    24  	"github.com/pkg/errors"
    25  )
    26  
    27  // Options represents configurable options used to create client and server TLS configurations.
    28  type Options struct {
    29  	CaCertFile string
    30  	// If either the KeyFile or CertFile is empty, ClientConfig() will not load them,
    31  	// preventing Helm from authenticating to Tiller. They are required to be non-empty
    32  	// when calling ServerConfig, otherwise an error is returned.
    33  	KeyFile  string
    34  	CertFile string
    35  	// Client-only options
    36  	InsecureSkipVerify bool
    37  	// Server-only options
    38  	ClientAuth tls.ClientAuthType
    39  }
    40  
    41  // ClientConfig retusn a TLS configuration for use by a Helm client.
    42  func ClientConfig(opts Options) (cfg *tls.Config, err error) {
    43  	var cert *tls.Certificate
    44  	var pool *x509.CertPool
    45  
    46  	if opts.CertFile != "" || opts.KeyFile != "" {
    47  		if cert, err = CertFromFilePair(opts.CertFile, opts.KeyFile); err != nil {
    48  			if os.IsNotExist(err) {
    49  				return nil, errors.Wrapf(err, "could not load x509 key pair (cert: %q, key: %q)", opts.CertFile, opts.KeyFile)
    50  			}
    51  			return nil, errors.Wrapf(err, "could not read x509 key pair (cert: %q, key: %q)", opts.CertFile, opts.KeyFile)
    52  		}
    53  	}
    54  	if !opts.InsecureSkipVerify && opts.CaCertFile != "" {
    55  		if pool, err = CertPoolFromFile(opts.CaCertFile); err != nil {
    56  			return nil, err
    57  		}
    58  	}
    59  
    60  	cfg = &tls.Config{InsecureSkipVerify: opts.InsecureSkipVerify, Certificates: []tls.Certificate{*cert}, RootCAs: pool}
    61  	return cfg, nil
    62  }
    63  
    64  // ServerConfig returns a TLS configuration for use by the Tiller server.
    65  func ServerConfig(opts Options) (cfg *tls.Config, err error) {
    66  	var cert *tls.Certificate
    67  	var pool *x509.CertPool
    68  
    69  	if cert, err = CertFromFilePair(opts.CertFile, opts.KeyFile); err != nil {
    70  		if os.IsNotExist(err) {
    71  			return nil, errors.Wrapf(err, "could not load x509 key pair (cert: %q, key: %q)", opts.CertFile, opts.KeyFile)
    72  		}
    73  		return nil, errors.Wrapf(err, "could not read x509 key pair (cert: %q, key: %q)", opts.CertFile, opts.KeyFile)
    74  	}
    75  	if opts.ClientAuth >= tls.VerifyClientCertIfGiven && opts.CaCertFile != "" {
    76  		if pool, err = CertPoolFromFile(opts.CaCertFile); err != nil {
    77  			return nil, err
    78  		}
    79  	}
    80  
    81  	cfg = &tls.Config{MinVersion: tls.VersionTLS12, ClientAuth: opts.ClientAuth, Certificates: []tls.Certificate{*cert}, ClientCAs: pool}
    82  	return cfg, nil
    83  }