sigs.k8s.io/external-dns@v0.14.1/pkg/tlsutils/tlsconfig.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes 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 tlsutils
    18  
    19  import (
    20  	"crypto/tls"
    21  	"crypto/x509"
    22  	"errors"
    23  	"fmt"
    24  	"os"
    25  	"strings"
    26  )
    27  
    28  const defaultMinVersion = 0
    29  
    30  // CreateTLSConfig creates tls.Config instance from TLS parameters passed in environment variables with the given prefix
    31  func CreateTLSConfig(prefix string) (*tls.Config, error) {
    32  	caFile := os.Getenv(fmt.Sprintf("%s_CA_FILE", prefix))
    33  	certFile := os.Getenv(fmt.Sprintf("%s_CERT_FILE", prefix))
    34  	keyFile := os.Getenv(fmt.Sprintf("%s_KEY_FILE", prefix))
    35  	serverName := os.Getenv(fmt.Sprintf("%s_TLS_SERVER_NAME", prefix))
    36  	isInsecureStr := strings.ToLower(os.Getenv(fmt.Sprintf("%s_TLS_INSECURE", prefix)))
    37  	isInsecure := isInsecureStr == "true" || isInsecureStr == "yes" || isInsecureStr == "1"
    38  	tlsConfig, err := NewTLSConfig(certFile, keyFile, caFile, serverName, isInsecure, defaultMinVersion)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	return tlsConfig, nil
    43  }
    44  
    45  // NewTLSConfig creates a tls.Config instance from directly-passed parameters, loading the ca, cert, and key from disk
    46  func NewTLSConfig(certPath, keyPath, caPath, serverName string, insecure bool, minVersion uint16) (*tls.Config, error) {
    47  	if certPath != "" && keyPath == "" || certPath == "" && keyPath != "" {
    48  		return nil, errors.New("either both cert and key or none must be provided")
    49  	}
    50  	var certificates []tls.Certificate
    51  	if certPath != "" {
    52  		cert, err := tls.LoadX509KeyPair(certPath, keyPath)
    53  		if err != nil {
    54  			return nil, fmt.Errorf("could not load TLS cert: %w", err)
    55  		}
    56  		certificates = append(certificates, cert)
    57  	}
    58  	roots, err := loadRoots(caPath)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	return &tls.Config{
    64  		MinVersion:         minVersion,
    65  		Certificates:       certificates,
    66  		RootCAs:            roots,
    67  		InsecureSkipVerify: insecure,
    68  		ServerName:         serverName,
    69  	}, nil
    70  }
    71  
    72  // loads CA cert
    73  func loadRoots(caPath string) (*x509.CertPool, error) {
    74  	if caPath == "" {
    75  		return nil, nil
    76  	}
    77  
    78  	roots := x509.NewCertPool()
    79  	pem, err := os.ReadFile(caPath)
    80  	if err != nil {
    81  		return nil, fmt.Errorf("error reading %s: %w", caPath, err)
    82  	}
    83  	ok := roots.AppendCertsFromPEM(pem)
    84  	if !ok {
    85  		return nil, fmt.Errorf("could not read root certs: %w", err)
    86  	}
    87  	return roots, nil
    88  }