sigs.k8s.io/cluster-api@v1.7.1/util/flags/tls.go (about)

     1  /*
     2  Copyright 2022 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 flags implements the webhook server TLS options utilities.
    18  package flags
    19  
    20  import (
    21  	"crypto/tls"
    22  	"fmt"
    23  	"strings"
    24  
    25  	"github.com/spf13/pflag"
    26  	cliflag "k8s.io/component-base/cli/flag"
    27  )
    28  
    29  // TLSOptions has the options to configure the TLS settings
    30  // for a webhook server.
    31  type TLSOptions struct {
    32  	TLSMinVersion   string
    33  	TLSCipherSuites []string
    34  }
    35  
    36  // AddTLSOptions adds the webhook server TLS configuration flags
    37  // to the flag set.
    38  func AddTLSOptions(fs *pflag.FlagSet, options *TLSOptions) {
    39  	fs.StringVar(&options.TLSMinVersion, "tls-min-version", "VersionTLS12",
    40  		"The minimum TLS version in use by the webhook server.\n"+
    41  			fmt.Sprintf("Possible values are %s.", strings.Join(cliflag.TLSPossibleVersions(), ", ")),
    42  	)
    43  
    44  	tlsCipherPreferredValues := cliflag.PreferredTLSCipherNames()
    45  	tlsCipherInsecureValues := cliflag.InsecureTLSCipherNames()
    46  	fs.StringSliceVar(&options.TLSCipherSuites, "tls-cipher-suites", []string{},
    47  		"Comma-separated list of cipher suites for the webhook server. "+
    48  			"If omitted, the default Go cipher suites will be used. \n"+
    49  			"Preferred values: "+strings.Join(tlsCipherPreferredValues, ", ")+". \n"+
    50  			"Insecure values: "+strings.Join(tlsCipherInsecureValues, ", ")+".")
    51  }
    52  
    53  // GetTLSOptionOverrideFuncs returns a list of TLS configuration overrides to be used
    54  // by the webhook server.
    55  func GetTLSOptionOverrideFuncs(options TLSOptions) ([]func(*tls.Config), error) {
    56  	var tlsOptions []func(config *tls.Config)
    57  	tlsVersion, err := cliflag.TLSVersion(options.TLSMinVersion)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	tlsOptions = append(tlsOptions, func(cfg *tls.Config) {
    62  		cfg.MinVersion = tlsVersion
    63  	})
    64  
    65  	if len(options.TLSCipherSuites) != 0 {
    66  		suites, err := cliflag.TLSCipherSuites(options.TLSCipherSuites)
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  		tlsOptions = append(tlsOptions, func(cfg *tls.Config) {
    71  			cfg.CipherSuites = suites
    72  		})
    73  	}
    74  	return tlsOptions, nil
    75  }