Skip to content

Commit c233cbe

Browse files
authored
Merge pull request #1657 from srm09/automated-cherry-pick-of-#1655-release-1.4
✨ Automated cherry pick of #1655: Updates flags for webhook server TLS config
2 parents 60246be + 8743618 commit c233cbe

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

main.go

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"crypto/tls"
2021
"flag"
2122
"fmt"
2223
"math/rand"
2324
"net/http"
2425
"net/http/pprof"
2526
"os"
2627
"reflect"
28+
"strings"
2729
"time"
2830

2931
"github.com/spf13/pflag"
@@ -55,6 +57,7 @@ var (
5557
syncPeriod time.Duration
5658
profilerAddress string
5759
tlsMinVersion string
60+
tlsCipherSuites string
5861

5962
defaultProfilerAddr = os.Getenv("PROFILER_ADDR")
6063
defaultSyncPeriod = manager.DefaultSyncPeriod
@@ -63,7 +66,6 @@ var (
6366
defaultWebhookPort = manager.DefaultWebhookServiceContainerPort
6467
defaultEnableKeepAlive = constants.DefaultEnableKeepAlive
6568
defaultKeepAliveDuration = constants.DefaultKeepAliveDuration
66-
defaultTLSMinVersion = "1.2"
6769
)
6870

6971
// InitFlags initializes the flags.
@@ -148,8 +150,16 @@ func InitFlags(fs *pflag.FlagSet) {
148150
flag.StringVar(
149151
&tlsMinVersion,
150152
"tls-min-version",
151-
defaultTLSMinVersion,
152-
"minimum TLS version in use by the webhook server. Possible values are \"\", \"1.0\", \"1.1\", \"1.2\" and \"1.3\".",
153+
"",
154+
"Minimum TLS version in use by the webhook server.\n"+
155+
fmt.Sprintf("Possible values are %s.", strings.Join(cliflag.TLSPossibleVersions(), ", ")),
156+
)
157+
flag.StringVar(
158+
&tlsCipherSuites,
159+
"tls-cipher-suites",
160+
"",
161+
"Comma-separated list of cipher suites for the server. If omitted, the default Go cipher suites will be used.\n"+
162+
fmt.Sprintf("Possible values are %s.", strings.Join(cliflag.TLSCipherPossibleValues(), ", ")),
153163
)
154164

155165
feature.MutableGates.AddFlag(fs)
@@ -234,7 +244,22 @@ func main() {
234244
os.Exit(1)
235245
}
236246

237-
mgr.GetWebhookServer().TLSMinVersion = tlsMinVersion
247+
minTLSVersionSetFunc, err := setMinTLSVersionFunc(tlsMinVersion)
248+
if err != nil {
249+
setupLog.Error(err, "unable to set TLS min version")
250+
os.Exit(1)
251+
}
252+
mgr.GetWebhookServer().TLSOpts = append(mgr.GetWebhookServer().TLSOpts, minTLSVersionSetFunc)
253+
254+
if tlsCipherSuites != "" {
255+
cipherSuitesSetFunc, err := setCipherSuiteFunc(tlsCipherSuites)
256+
if err != nil {
257+
setupLog.Error(err, "unable to set TLS Cipher suites")
258+
os.Exit(1)
259+
}
260+
mgr.GetWebhookServer().TLSOpts = append(mgr.GetWebhookServer().TLSOpts, cipherSuitesSetFunc)
261+
}
262+
238263
setupChecks(mgr)
239264

240265
sigHandler := ctrlsig.SetupSignalHandler()
@@ -339,6 +364,27 @@ func setupSupervisorControllers(ctx *context.ControllerManagerContext, mgr ctrlm
339364
return nil
340365
}
341366

367+
func setCipherSuiteFunc(cipherSuiteString string) (func(cfg *tls.Config), error) {
368+
cipherSuites := strings.Split(cipherSuiteString, ",")
369+
suites, err := cliflag.TLSCipherSuites(cipherSuites)
370+
if err != nil {
371+
return nil, err
372+
}
373+
return func(cfg *tls.Config) {
374+
cfg.CipherSuites = suites
375+
}, nil
376+
}
377+
378+
func setMinTLSVersionFunc(versionName string) (func(cfg *tls.Config), error) {
379+
tlsVersion, err := cliflag.TLSVersion(versionName)
380+
if err != nil {
381+
return nil, err
382+
}
383+
return func(cfg *tls.Config) {
384+
cfg.MinVersion = tlsVersion
385+
}, nil
386+
}
387+
342388
func setupChecks(mgr ctrlmgr.Manager) {
343389
if err := mgr.AddReadyzCheck("webhook", mgr.GetWebhookServer().StartedChecker()); err != nil {
344390
setupLog.Error(err, "unable to create ready check")

0 commit comments

Comments
 (0)