Skip to content

Commit dd7a927

Browse files
authored
feat: add tls-cipher-suites and tls-min-version flags (#129)
This commit introduces a new command-line flag `-tls-cipher-suites` to the webhook server. This allows users to specify a comma-separated list of allowed TLS cipher suites, enhancing security configurability. This commit introduces a new command-line flag --tls-min-version to the webhook server. This allows callers to specify the minimum supported TLS version (VersionTLS10, VersionTLS11, VersionTLS12, VersionTLS13), improving security flexibility by allowing the enforcement of stricter TLS protocols. Assisted-by: gemini-3-pro-preview
1 parent f23c89a commit dd7a927

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
k8s.io/api v0.35.0
1212
k8s.io/apimachinery v0.35.0
1313
k8s.io/client-go v0.35.0
14+
k8s.io/component-base v0.35.0
1415
k8s.io/utils v0.0.0-20260106112306-0fe9cd71b2f8
1516
sigs.k8s.io/controller-runtime v0.22.4
1617
)
@@ -44,6 +45,7 @@ require (
4445
github.com/google/gnostic-models v0.7.1 // indirect
4546
github.com/google/pprof v0.0.0-20250403155104-27863c87afa6 // indirect
4647
github.com/google/uuid v1.6.0 // indirect
48+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
4749
github.com/json-iterator/go v1.1.12 // indirect
4850
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4951
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
@@ -53,6 +55,7 @@ require (
5355
github.com/prometheus/client_model v0.6.2 // indirect
5456
github.com/prometheus/common v0.67.5 // indirect
5557
github.com/prometheus/procfs v0.19.2 // indirect
58+
github.com/spf13/cobra v1.10.0 // indirect
5659
github.com/spf13/pflag v1.0.10 // indirect
5760
github.com/x448/float16 v0.8.4 // indirect
5861
go.uber.org/multierr v1.11.0 // indirect

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
66
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
77
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
88
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
9+
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
910
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1011
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1112
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
@@ -81,6 +82,8 @@ github.com/google/pprof v0.0.0-20250403155104-27863c87afa6 h1:BHT72Gu3keYf3ZEu2J
8182
github.com/google/pprof v0.0.0-20250403155104-27863c87afa6/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
8283
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
8384
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
85+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
86+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
8487
github.com/joshdk/go-junit v1.0.0 h1:S86cUKIdwBHWwA6xCmFlf3RTLfVXYQfvanM5Uh+K6GE=
8588
github.com/joshdk/go-junit v1.0.0/go.mod h1:TiiV0PqkaNfFXjEiyjWM3XXrhVyCa1K4Zfga6W52ung=
8689
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@@ -123,6 +126,10 @@ github.com/prometheus/procfs v0.19.2 h1:zUMhqEW66Ex7OXIiDkll3tl9a1ZdilUOd/F6ZXw4
123126
github.com/prometheus/procfs v0.19.2/go.mod h1:M0aotyiemPhBCM0z5w87kL22CxfcH05ZpYlu+b4J7mw=
124127
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
125128
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
129+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
130+
github.com/spf13/cobra v1.10.0 h1:a5/WeUlSDCvV5a45ljW2ZFtV0bTDpkfSAj3uqB6Sc+0=
131+
github.com/spf13/cobra v1.10.0/go.mod h1:9dhySC7dnTtEiqzmqfkLj47BslqLCUPMXjG2lj/NgoE=
132+
github.com/spf13/pflag v1.0.8/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
126133
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
127134
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
128135
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -190,6 +197,8 @@ k8s.io/apimachinery v0.35.0 h1:Z2L3IHvPVv/MJ7xRxHEtk6GoJElaAqDCCU0S6ncYok8=
190197
k8s.io/apimachinery v0.35.0/go.mod h1:jQCgFZFR1F4Ik7hvr2g84RTJSZegBc8yHgFWKn//hns=
191198
k8s.io/client-go v0.35.0 h1:IAW0ifFbfQQwQmga0UdoH0yvdqrbwMdq9vIFEhRpxBE=
192199
k8s.io/client-go v0.35.0/go.mod h1:q2E5AAyqcbeLGPdoRB+Nxe3KYTfPce1Dnu1myQdqz9o=
200+
k8s.io/component-base v0.35.0 h1:+yBrOhzri2S1BVqyVSvcM3PtPyx5GUxCK2tinZz1G94=
201+
k8s.io/component-base v0.35.0/go.mod h1:85SCX4UCa6SCFt6p3IKAPej7jSnF3L8EbfSyMZayJR0=
193202
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
194203
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
195204
k8s.io/kube-openapi v0.0.0-20251125145642-4e65d59e963e h1:iW9ChlU0cU16w8MpVYjXk12dqQ4BPFBEgif+ap7/hqQ=

main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"crypto/tls"
2021
"encoding/json"
2122
"flag"
2223
"os"
24+
"strings"
2325

2426
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2527
// to ensure that exec-entrypoint and run can make use of them.
@@ -31,10 +33,12 @@ import (
3133
"k8s.io/apimachinery/pkg/runtime"
3234
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3335
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
36+
cliflag "k8s.io/component-base/cli/flag"
3437
ctrl "sigs.k8s.io/controller-runtime"
3538
"sigs.k8s.io/controller-runtime/pkg/healthz"
3639
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3740
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
41+
"sigs.k8s.io/controller-runtime/pkg/webhook"
3842
//+kubebuilder:scaffold:imports
3943
)
4044

@@ -61,6 +65,11 @@ func main() {
6165
gCloudImage := flag.String("gcloud-image", webhooks.GcloudImageDefault, "Container image for the init container setting up GCloud SDK")
6266
tokenDefaultMode := flag.Int("token-default-mode", webhooks.VolumeModeDefault, "DefaultMode for the token volume. CAUTION: if you allow reading from others (e.g. '0444'), the token can read from anyone who can log in to the node.")
6367
setupContainerResources := flag.String("setup-container-resources", webhooks.SetupContainerResources, `Resource spec in json for the init container setting up GCloud SDK, e.g. '{"requests":{"cpu":"100m"}}'`)
68+
tlsCipherSuiteValues := cliflag.PreferredTLSCipherNames()
69+
tlsCipherSuiteInsecureValues := cliflag.InsecureTLSCipherNames()
70+
tlsCipherSuites := flag.String("tls-cipher-suites", "", "Comma-separated list of TLS cipher suites to be used by the webhook server. \nValues: "+strings.Join(tlsCipherSuiteValues, ", ")+"\nInsecure Values: "+strings.Join(tlsCipherSuiteInsecureValues, ", "))
71+
tlsMinVersionValues := cliflag.TLSPossibleVersions()
72+
tlsMinVersion := flag.String("tls-min-version", "", "The minimum TLS version to be used by the webhook server. ("+strings.Join(tlsMinVersionValues, ", ")+")")
6473

6574
opts := zap.Options{
6675
Development: true,
@@ -79,12 +88,43 @@ func main() {
7988
}
8089
}
8190

91+
var tlsOpts []func(*tls.Config)
92+
93+
if *tlsCipherSuites != "" {
94+
cipherSuites, err := cliflag.TLSCipherSuites(strings.Split(*tlsCipherSuites, ","))
95+
if err != nil {
96+
setupLog.Error(err, "unable to parse the value of --tls-cipher-suites")
97+
os.Exit(1)
98+
}
99+
if cipherSuites != nil {
100+
tlsOpts = append(tlsOpts, func(c *tls.Config) {
101+
c.CipherSuites = cipherSuites
102+
})
103+
}
104+
}
105+
106+
if *tlsMinVersion != "" {
107+
version, err := cliflag.TLSVersion(*tlsMinVersion)
108+
if err != nil {
109+
setupLog.Error(err, "unable to parse the value of --tls-min-version")
110+
os.Exit(1)
111+
}
112+
tlsOpts = append(tlsOpts, func(c *tls.Config) {
113+
c.MinVersion = version
114+
})
115+
}
116+
117+
webhookOptions := webhook.Options{
118+
TLSOpts: tlsOpts,
119+
}
120+
82121
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
83122
Scheme: scheme,
84123
Metrics: metricsserver.Options{
85124
BindAddress: *metricsAddr,
86125
},
87126
HealthProbeBindAddress: *probeAddr,
127+
WebhookServer: webhook.NewServer(webhookOptions),
88128
})
89129
if err != nil {
90130
setupLog.Error(err, "unable to start manager")

0 commit comments

Comments
 (0)