Skip to content

Commit 97a685a

Browse files
authored
Merge pull request #7483 from srm09/manager/add-tls-configs
⚠️ Adds TLS options to managers
2 parents 69576fc + 3ad56fa commit 97a685a

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

bootstrap/kubeadm/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050
"sigs.k8s.io/cluster-api/controllers/remote"
5151
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
5252
"sigs.k8s.io/cluster-api/feature"
53+
"sigs.k8s.io/cluster-api/util/flags"
5354
"sigs.k8s.io/cluster-api/version"
5455
)
5556

@@ -85,6 +86,7 @@ var (
8586
webhookCertDir string
8687
healthAddr string
8788
tokenTTL time.Duration
89+
tlsOptions = flags.TLSOptions{}
8890
logOptions = logs.NewOptions()
8991
)
9092

@@ -135,6 +137,8 @@ func InitFlags(fs *pflag.FlagSet) {
135137
fs.StringVar(&healthAddr, "health-addr", ":9440",
136138
"The address the health endpoint binds to.")
137139

140+
flags.AddTLSOptions(fs, &tlsOptions)
141+
138142
feature.MutableGates.AddFlag(fs)
139143
}
140144

@@ -165,6 +169,13 @@ func main() {
165169

166170
restConfig := ctrl.GetConfigOrDie()
167171
restConfig.UserAgent = remote.DefaultClusterAPIUserAgent("cluster-api-kubeadm-bootstrap-manager")
172+
173+
tlsOptionOverrides, err := flags.GetTLSOptionOverrideFuncs(tlsOptions)
174+
if err != nil {
175+
setupLog.Error(err, "unable to add TLS settings to the webhook server")
176+
os.Exit(1)
177+
}
178+
168179
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
169180
Scheme: scheme,
170181
MetricsBindAddress: metricsBindAddr,
@@ -183,6 +194,7 @@ func main() {
183194
Port: webhookPort,
184195
HealthProbeBindAddress: healthAddr,
185196
CertDir: webhookCertDir,
197+
TLSOpts: tlsOptionOverrides,
186198
})
187199
if err != nil {
188200
setupLog.Error(err, "unable to start manager")

controlplane/kubeadm/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import (
5353
kubeadmcontrolplanecontrollers "sigs.k8s.io/cluster-api/controlplane/kubeadm/controllers"
5454
kcpwebhooks "sigs.k8s.io/cluster-api/controlplane/kubeadm/webhooks"
5555
"sigs.k8s.io/cluster-api/feature"
56+
"sigs.k8s.io/cluster-api/util/flags"
5657
"sigs.k8s.io/cluster-api/version"
5758
)
5859

@@ -89,6 +90,7 @@ var (
8990
webhookCertDir string
9091
healthAddr string
9192
etcdDialTimeout time.Duration
93+
tlsOptions = flags.TLSOptions{}
9294
logOptions = logs.NewOptions()
9395
)
9496

@@ -139,6 +141,8 @@ func InitFlags(fs *pflag.FlagSet) {
139141
fs.DurationVar(&etcdDialTimeout, "etcd-dial-timeout-duration", 10*time.Second,
140142
"Duration that the etcd client waits at most to establish a connection with etcd")
141143

144+
flags.AddTLSOptions(fs, &tlsOptions)
145+
142146
feature.MutableGates.AddFlag(fs)
143147
}
144148
func main() {
@@ -169,6 +173,13 @@ func main() {
169173

170174
restConfig := ctrl.GetConfigOrDie()
171175
restConfig.UserAgent = remote.DefaultClusterAPIUserAgent("cluster-api-kubeadm-control-plane-manager")
176+
177+
tlsOptionOverrides, err := flags.GetTLSOptionOverrideFuncs(tlsOptions)
178+
if err != nil {
179+
setupLog.Error(err, "unable to add TLS settings to the webhook server")
180+
os.Exit(1)
181+
}
182+
172183
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
173184
Scheme: scheme,
174185
MetricsBindAddress: metricsBindAddr,
@@ -187,6 +198,7 @@ func main() {
187198
Port: webhookPort,
188199
HealthProbeBindAddress: healthAddr,
189200
CertDir: webhookCertDir,
201+
TLSOpts: tlsOptionOverrides,
190202
})
191203
if err != nil {
192204
setupLog.Error(err, "unable to start manager")

main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import (
6767
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
6868
runtimeregistry "sigs.k8s.io/cluster-api/internal/runtime/registry"
6969
runtimewebhooks "sigs.k8s.io/cluster-api/internal/webhooks/runtime"
70+
"sigs.k8s.io/cluster-api/util/flags"
7071
"sigs.k8s.io/cluster-api/version"
7172
"sigs.k8s.io/cluster-api/webhooks"
7273
)
@@ -99,6 +100,7 @@ var (
99100
webhookPort int
100101
webhookCertDir string
101102
healthAddr string
103+
tlsOptions = flags.TLSOptions{}
102104
logOptions = logs.NewOptions()
103105
)
104106

@@ -198,6 +200,8 @@ func InitFlags(fs *pflag.FlagSet) {
198200
fs.StringVar(&healthAddr, "health-addr", ":9440",
199201
"The address the health endpoint binds to.")
200202

203+
flags.AddTLSOptions(fs, &tlsOptions)
204+
201205
feature.MutableGates.AddFlag(fs)
202206
}
203207

@@ -240,6 +244,12 @@ func main() {
240244
os.Exit(1)
241245
}
242246

247+
tlsOptionOverrides, err := flags.GetTLSOptionOverrideFuncs(tlsOptions)
248+
if err != nil {
249+
setupLog.Error(err, "unable to add TLS settings to the webhook server")
250+
os.Exit(1)
251+
}
252+
243253
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
244254
Scheme: scheme,
245255
MetricsBindAddress: metricsBindAddr,
@@ -258,6 +268,7 @@ func main() {
258268
Port: webhookPort,
259269
CertDir: webhookCertDir,
260270
HealthProbeBindAddress: healthAddr,
271+
TLSOpts: tlsOptionOverrides,
261272
})
262273
if err != nil {
263274
setupLog.Error(err, "unable to start manager")

util/flags/tls.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}

0 commit comments

Comments
 (0)