github.com/y-taka-23/helm@v2.8.0+incompatible/pkg/tlsutil/cfg.go (about)

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