github.com/koderover/helm@v2.17.0+incompatible/pkg/tlsutil/tls.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  	"fmt"
    23  	"io/ioutil"
    24  	"k8s.io/helm/pkg/urlutil"
    25  )
    26  
    27  func newTLSConfigCommon(certFile, keyFile, caFile string) (*tls.Config, error) {
    28  	config := tls.Config{}
    29  
    30  	if certFile != "" && keyFile != "" {
    31  		cert, err := CertFromFilePair(certFile, keyFile)
    32  		if err != nil {
    33  			return nil, err
    34  		}
    35  		config.Certificates = []tls.Certificate{*cert}
    36  	}
    37  
    38  	if caFile != "" {
    39  		cp, err := CertPoolFromFile(caFile)
    40  		if err != nil {
    41  			return nil, err
    42  		}
    43  		config.RootCAs = cp
    44  	}
    45  
    46  	return &config, nil
    47  }
    48  
    49  // NewClientTLS returns tls.Config appropriate for client auth.
    50  func NewClientTLS(certFile, keyFile, caFile string) (*tls.Config, error) {
    51  	return newTLSConfigCommon(certFile, keyFile, caFile)
    52  }
    53  
    54  // NewTLSConfig returns tls.Config appropriate for client and/or server auth.
    55  func NewTLSConfig(url, certFile, keyFile, caFile string) (*tls.Config, error) {
    56  	config, err := newTLSConfigCommon(certFile, keyFile, caFile)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	config.BuildNameToCertificate()
    61  
    62  	serverName, err := urlutil.ExtractHostname(url)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	config.ServerName = serverName
    67  
    68  	return config, nil
    69  }
    70  
    71  // CertPoolFromFile returns an x509.CertPool containing the certificates
    72  // in the given PEM-encoded file.
    73  // Returns an error if the file could not be read, a certificate could not
    74  // be parsed, or if the file does not contain any certificates
    75  func CertPoolFromFile(filename string) (*x509.CertPool, error) {
    76  	b, err := ioutil.ReadFile(filename)
    77  	if err != nil {
    78  		return nil, fmt.Errorf("can't read CA file: %v", filename)
    79  	}
    80  	cp := x509.NewCertPool()
    81  	if !cp.AppendCertsFromPEM(b) {
    82  		return nil, fmt.Errorf("failed to append certificates from file: %s", filename)
    83  	}
    84  	return cp, nil
    85  }
    86  
    87  // CertFromFilePair returns an tls.Certificate containing the
    88  // certificates public/private key pair from a pair of given PEM-encoded files.
    89  // Returns an error if the file could not be read, a certificate could not
    90  // be parsed, or if the file does not contain any certificates
    91  func CertFromFilePair(certFile, keyFile string) (*tls.Certificate, error) {
    92  	cert, err := tls.LoadX509KeyPair(certFile, keyFile)
    93  	if err != nil {
    94  		return nil, fmt.Errorf("can't load key pair from cert %s and key %s: %s", certFile, keyFile, err)
    95  	}
    96  	return &cert, err
    97  }