Skip to content

Commit

Permalink
Addresses review comments
Browse files Browse the repository at this point in the history
This patch tries two approaches:
1. Using a common util function generate the TLS Options
2. Using a local method to generate the TLS Options.

Signed-off-by: Sagar Muchhal <muchhals@vmware.com>
  • Loading branch information
srm09 committed Nov 4, 2022
1 parent 48844c7 commit dcb1cb1
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 60 deletions.
32 changes: 18 additions & 14 deletions bootstrap/kubeadm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ var (
healthAddr string
tokenTTL time.Duration
tlsMinVersion string
tlsCipherSuites string
tlsCipherSuites []string
logOptions = logs.NewOptions()
)

Expand Down Expand Up @@ -139,15 +139,18 @@ func InitFlags(fs *pflag.FlagSet) {
fs.StringVar(&healthAddr, "health-addr", ":9440",
"The address the health endpoint binds to.")

flag.StringVar(&tlsMinVersion, "tls-min-version", "VersionTLS12",
flag.StringVar(&tlsMinVersion, "tls-min-version", "",
"The minimum TLS version in use by the webhook server.\n"+
fmt.Sprintf("Possible values are %s.", strings.Join(cliflag.TLSPossibleVersions(), ", ")),
)

flag.StringVar(&tlsCipherSuites, "tls-cipher-suites", "",
"Comma-separated list of cipher suites for the server. If omitted, the default Go cipher suites will be used.\n"+
fmt.Sprintf("Possible values are %s.", strings.Join(cliflag.TLSCipherPossibleValues(), ", ")),
)
tlsCipherPreferredValues := cliflag.PreferredTLSCipherNames()
tlsCipherInsecureValues := cliflag.InsecureTLSCipherNames()
fs.StringSliceVar(&tlsCipherSuites, "tls-cipher-suites", []string{},
"Comma-separated list of cipher suites for the webhook server. "+
"If omitted, the default Go cipher suites will be used. \n"+
"Preferred values: "+strings.Join(tlsCipherPreferredValues, ", ")+". \n"+
"Insecure values: "+strings.Join(tlsCipherInsecureValues, ", ")+".")

feature.MutableGates.AddFlag(fs)
}
Expand Down Expand Up @@ -179,6 +182,7 @@ func main() {

restConfig := ctrl.GetConfigOrDie()
restConfig.UserAgent = remote.DefaultClusterAPIUserAgent("cluster-api-kubeadm-bootstrap-manager")
tlsOptions := calculateTLSOpts(tlsMinVersion, tlsCipherSuites)
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsBindAddr,
Expand All @@ -197,6 +201,7 @@ func main() {
Port: webhookPort,
HealthProbeBindAddress: healthAddr,
CertDir: webhookCertDir,
TLSOpts: tlsOptions,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand All @@ -207,7 +212,6 @@ func main() {
ctx := ctrl.SetupSignalHandler()

setupChecks(mgr)
setupWebhookTLSConfigs(mgr, tlsMinVersion, tlsCipherSuites)
setupWebhooks(mgr)
setupReconcilers(ctx, mgr)

Expand All @@ -231,28 +235,28 @@ func setupChecks(mgr ctrl.Manager) {
}
}

func setupWebhookTLSConfigs(mgr ctrl.Manager, tlsMinVersion, tlsCipherSuites string) {
func calculateTLSOpts(tlsMinVersion string, tlsCipherSuites []string) []func(*tls.Config) {
var tlsOptions []func(config *tls.Config)
tlsVersion, err := cliflag.TLSVersion(tlsMinVersion)
if err != nil {
setupLog.Error(err, "unable to set TLS min version")
os.Exit(1)
}
mgr.GetWebhookServer().TLSOpts = append(mgr.GetWebhookServer().TLSOpts, func(cfg *tls.Config) {
tlsOptions = append(tlsOptions, func(cfg *tls.Config) {
cfg.MinVersion = tlsVersion
})

if tlsCipherSuites != "" {
cipherSuites := strings.Split(tlsCipherSuites, ",")
suites, err := cliflag.TLSCipherSuites(cipherSuites)
if len(tlsCipherSuites) != 0 {
suites, err := cliflag.TLSCipherSuites(tlsCipherSuites)
if err != nil {
setupLog.Error(err, "unable to set TLS Cipher suites")
os.Exit(1)
}

mgr.GetWebhookServer().TLSOpts = append(mgr.GetWebhookServer().TLSOpts, func(cfg *tls.Config) {
tlsOptions = append(tlsOptions, func(cfg *tls.Config) {
cfg.CipherSuites = suites
})
}
return tlsOptions
}

func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
Expand Down
32 changes: 18 additions & 14 deletions controlplane/kubeadm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ var (
healthAddr string
etcdDialTimeout time.Duration
tlsMinVersion string
tlsCipherSuites string
tlsCipherSuites []string
logOptions = logs.NewOptions()
)

Expand Down Expand Up @@ -143,15 +143,18 @@ func InitFlags(fs *pflag.FlagSet) {
fs.DurationVar(&etcdDialTimeout, "etcd-dial-timeout-duration", 10*time.Second,
"Duration that the etcd client waits at most to establish a connection with etcd")

flag.StringVar(&tlsMinVersion, "tls-min-version", "VersionTLS12",
flag.StringVar(&tlsMinVersion, "tls-min-version", "",
"The minimum TLS version in use by the webhook server.\n"+
fmt.Sprintf("Possible values are %s.", strings.Join(cliflag.TLSPossibleVersions(), ", ")),
)

flag.StringVar(&tlsCipherSuites, "tls-cipher-suites", "",
"Comma-separated list of cipher suites for the server. If omitted, the default Go cipher suites will be used.\n"+
fmt.Sprintf("Possible values are %s.", strings.Join(cliflag.TLSCipherPossibleValues(), ", ")),
)
tlsCipherPreferredValues := cliflag.PreferredTLSCipherNames()
tlsCipherInsecureValues := cliflag.InsecureTLSCipherNames()
fs.StringSliceVar(&tlsCipherSuites, "tls-cipher-suites", []string{},
"Comma-separated list of cipher suites for the webhook server. "+
"If omitted, the default Go cipher suites will be used. \n"+
"Preferred values: "+strings.Join(tlsCipherPreferredValues, ", ")+". \n"+
"Insecure values: "+strings.Join(tlsCipherInsecureValues, ", ")+".")

feature.MutableGates.AddFlag(fs)
}
Expand Down Expand Up @@ -183,6 +186,7 @@ func main() {

restConfig := ctrl.GetConfigOrDie()
restConfig.UserAgent = remote.DefaultClusterAPIUserAgent("cluster-api-kubeadm-control-plane-manager")
tlsOptions := calculateTLSOpts(tlsMinVersion, tlsCipherSuites)
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsBindAddr,
Expand All @@ -201,6 +205,7 @@ func main() {
Port: webhookPort,
HealthProbeBindAddress: healthAddr,
CertDir: webhookCertDir,
TLSOpts: tlsOptions,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand All @@ -211,7 +216,6 @@ func main() {
ctx := ctrl.SetupSignalHandler()

setupChecks(mgr)
setupWebhookTLSConfigs(mgr, tlsMinVersion, tlsCipherSuites)
setupReconcilers(ctx, mgr)
setupWebhooks(mgr)

Expand All @@ -235,28 +239,28 @@ func setupChecks(mgr ctrl.Manager) {
}
}

func setupWebhookTLSConfigs(mgr ctrl.Manager, tlsMinVersion, tlsCipherSuites string) {
func calculateTLSOpts(tlsMinVersion string, tlsCipherSuites []string) []func(*tls.Config) {
var tlsOptions []func(config *tls.Config)
tlsVersion, err := cliflag.TLSVersion(tlsMinVersion)
if err != nil {
setupLog.Error(err, "unable to set TLS min version")
os.Exit(1)
}
mgr.GetWebhookServer().TLSOpts = append(mgr.GetWebhookServer().TLSOpts, func(cfg *tls.Config) {
tlsOptions = append(tlsOptions, func(cfg *tls.Config) {
cfg.MinVersion = tlsVersion
})

if tlsCipherSuites != "" {
cipherSuites := strings.Split(tlsCipherSuites, ",")
suites, err := cliflag.TLSCipherSuites(cipherSuites)
if len(tlsCipherSuites) != 0 {
suites, err := cliflag.TLSCipherSuites(tlsCipherSuites)
if err != nil {
setupLog.Error(err, "unable to set TLS Cipher suites")
os.Exit(1)
}

mgr.GetWebhookServer().TLSOpts = append(mgr.GetWebhookServer().TLSOpts, func(cfg *tls.Config) {
tlsOptions = append(tlsOptions, func(cfg *tls.Config) {
cfg.CipherSuites = suites
})
}
return tlsOptions
}

func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
Expand Down
49 changes: 17 additions & 32 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main

import (
"context"
"crypto/tls"
"flag"
"fmt"
"math/rand"
Expand Down Expand Up @@ -69,6 +68,7 @@ import (
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
runtimeregistry "sigs.k8s.io/cluster-api/internal/runtime/registry"
runtimewebhooks "sigs.k8s.io/cluster-api/internal/webhooks/runtime"
"sigs.k8s.io/cluster-api/util/flags"
"sigs.k8s.io/cluster-api/version"
"sigs.k8s.io/cluster-api/webhooks"
)
Expand Down Expand Up @@ -102,7 +102,7 @@ var (
webhookCertDir string
healthAddr string
tlsMinVersion string
tlsCipherSuites string
tlsCipherSuites []string
logOptions = logs.NewOptions()
)

Expand Down Expand Up @@ -202,15 +202,18 @@ func InitFlags(fs *pflag.FlagSet) {
fs.StringVar(&healthAddr, "health-addr", ":9440",
"The address the health endpoint binds to.")

flag.StringVar(&tlsMinVersion, "tls-min-version", "VersionTLS12",
flag.StringVar(&tlsMinVersion, "tls-min-version", "",
"The minimum TLS version in use by the webhook server.\n"+
fmt.Sprintf("Possible values are %s.", strings.Join(cliflag.TLSPossibleVersions(), ", ")),
)

flag.StringVar(&tlsCipherSuites, "tls-cipher-suites", "",
"Comma-separated list of cipher suites for the server. If omitted, the default Go cipher suites will be used.\n"+
fmt.Sprintf("Possible values are %s.", strings.Join(cliflag.TLSCipherPossibleValues(), ", ")),
)
tlsCipherPreferredValues := cliflag.PreferredTLSCipherNames()
tlsCipherInsecureValues := cliflag.InsecureTLSCipherNames()
fs.StringSliceVar(&tlsCipherSuites, "tls-cipher-suites", []string{},
"Comma-separated list of cipher suites for the webhook server. "+
"If omitted, the default Go cipher suites will be used. \n"+
"Preferred values: "+strings.Join(tlsCipherPreferredValues, ", ")+". \n"+
"Insecure values: "+strings.Join(tlsCipherInsecureValues, ", ")+".")

feature.MutableGates.AddFlag(fs)
}
Expand Down Expand Up @@ -254,6 +257,12 @@ func main() {
os.Exit(1)
}

tlsOptions, err := flags.CalculateTLSOpts(tlsMinVersion, tlsCipherSuites)
if err != nil {
setupLog.Error(err, "unable to add TLS settings to the webhook server")
os.Exit(1)
}

mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsBindAddr,
Expand All @@ -272,6 +281,7 @@ func main() {
Port: webhookPort,
CertDir: webhookCertDir,
HealthProbeBindAddress: healthAddr,
TLSOpts: tlsOptions,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand All @@ -283,7 +293,6 @@ func main() {

setupChecks(mgr)
setupIndexes(ctx, mgr)
setupWebhookTLSConfigs(mgr, tlsMinVersion, tlsCipherSuites)
setupReconcilers(ctx, mgr)
setupWebhooks(mgr)

Expand Down Expand Up @@ -314,30 +323,6 @@ func setupIndexes(ctx context.Context, mgr ctrl.Manager) {
}
}

func setupWebhookTLSConfigs(mgr ctrl.Manager, tlsMinVersion, tlsCipherSuites string) {
tlsVersion, err := cliflag.TLSVersion(tlsMinVersion)
if err != nil {
setupLog.Error(err, "unable to set TLS min version")
os.Exit(1)
}
mgr.GetWebhookServer().TLSOpts = append(mgr.GetWebhookServer().TLSOpts, func(cfg *tls.Config) {
cfg.MinVersion = tlsVersion
})

if tlsCipherSuites != "" {
cipherSuites := strings.Split(tlsCipherSuites, ",")
suites, err := cliflag.TLSCipherSuites(cipherSuites)
if err != nil {
setupLog.Error(err, "unable to set TLS Cipher suites")
os.Exit(1)
}

mgr.GetWebhookServer().TLSOpts = append(mgr.GetWebhookServer().TLSOpts, func(cfg *tls.Config) {
cfg.CipherSuites = suites
})
}
}

func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
// Set up a ClusterCacheTracker and ClusterCacheReconciler to provide to controllers
// requiring a connection to a remote cluster
Expand Down
48 changes: 48 additions & 0 deletions util/flags/tls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package flags implements the webhook server TLS options utilities.
package flags

import (
"crypto/tls"

cliflag "k8s.io/component-base/cli/flag"
)

// CalculateTLSOpts returns a list of TLS configuration overrides
// to be used when setting the webhook server.
func CalculateTLSOpts(tlsMinVersion string, tlsCipherSuites []string) ([]func(*tls.Config), error) {
var tlsOptions []func(config *tls.Config)
tlsVersion, err := cliflag.TLSVersion(tlsMinVersion)
if err != nil {
return tlsOptions, err
}
tlsOptions = append(tlsOptions, func(cfg *tls.Config) {
cfg.MinVersion = tlsVersion
})

if len(tlsCipherSuites) != 0 {
suites, err := cliflag.TLSCipherSuites(tlsCipherSuites)
if err != nil {
return tlsOptions, err
}
tlsOptions = append(tlsOptions, func(cfg *tls.Config) {
cfg.CipherSuites = suites
})
}
return tlsOptions, nil
}

0 comments on commit dcb1cb1

Please sign in to comment.