11/*
2- Copyright 2023 .
2+ Copyright 2024 .
33
44Licensed under the Apache License, Version 2.0 (the "License");
55you may not use this file except in compliance with the License.
@@ -14,10 +14,10 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- // Package main initializes an AWSValidator controller.
1817package main
1918
2019import (
20+ "crypto/tls"
2121 "flag"
2222 "os"
2323
@@ -31,11 +31,14 @@ import (
3131 ctrl "sigs.k8s.io/controller-runtime"
3232 "sigs.k8s.io/controller-runtime/pkg/healthz"
3333 "sigs.k8s.io/controller-runtime/pkg/log/zap"
34+ "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
35+ metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
36+ "sigs.k8s.io/controller-runtime/pkg/webhook"
3437
3538 validationv1alpha1 "github.com/validator-labs/validator-plugin-aws/api/v1alpha1"
3639 "github.com/validator-labs/validator-plugin-aws/internal/controller"
3740 validatorv1alpha1 "github.com/validator-labs/validator/api/v1alpha1"
38- //+kubebuilder:scaffold:imports
41+ // +kubebuilder:scaffold:imports
3942)
4043
4144var (
@@ -47,16 +50,26 @@ func init() {
4750 utilruntime .Must (clientgoscheme .AddToScheme (scheme ))
4851 utilruntime .Must (validatorv1alpha1 .AddToScheme (scheme ))
4952 utilruntime .Must (validationv1alpha1 .AddToScheme (scheme ))
50- //+kubebuilder:scaffold:scheme
53+ // +kubebuilder:scaffold:scheme
5154}
5255
5356func main () {
57+ var metricsAddr string
5458 var enableLeaderElection bool
5559 var probeAddr string
60+ var secureMetrics bool
61+ var enableHTTP2 bool
62+ var tlsOpts []func (* tls.Config )
63+ flag .StringVar (& metricsAddr , "metrics-bind-address" , "0" , "The address the metrics endpoint binds to. " +
64+ "Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service." )
5665 flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
5766 flag .BoolVar (& enableLeaderElection , "leader-elect" , false ,
5867 "Enable leader election for controller manager. " +
5968 "Enabling this will ensure there is only one active controller manager." )
69+ flag .BoolVar (& secureMetrics , "metrics-secure" , true ,
70+ "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead." )
71+ flag .BoolVar (& enableHTTP2 , "enable-http2" , false ,
72+ "If set, HTTP/2 will be enabled for the metrics and webhook servers" )
6073 opts := zap.Options {
6174 Development : true ,
6275 }
@@ -65,11 +78,61 @@ func main() {
6578
6679 ctrl .SetLogger (zap .New (zap .UseFlagOptions (& opts )))
6780
81+ // if the enable-http2 flag is false (the default), http/2 should be disabled
82+ // due to its vulnerabilities. More specifically, disabling http/2 will
83+ // prevent from being vulnerable to the HTTP/2 Stream Cancellation and
84+ // Rapid Reset CVEs. For more information see:
85+ // - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
86+ // - https://github.com/advisories/GHSA-4374-p667-p6c8
87+ disableHTTP2 := func (c * tls.Config ) {
88+ setupLog .Info ("disabling http/2" )
89+ c .NextProtos = []string {"http/1.1" }
90+ }
91+
92+ if ! enableHTTP2 {
93+ tlsOpts = append (tlsOpts , disableHTTP2 )
94+ }
95+
96+ webhookServer := webhook .NewServer (webhook.Options {
97+ TLSOpts : tlsOpts ,
98+ })
99+
100+ // Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
101+ // More info:
102+ // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.1/pkg/metrics/server
103+ // - https://book.kubebuilder.io/reference/metrics.html
104+ metricsServerOptions := metricsserver.Options {
105+ BindAddress : metricsAddr ,
106+ SecureServing : secureMetrics ,
107+ TLSOpts : tlsOpts ,
108+ }
109+
110+ if secureMetrics {
111+ // FilterProvider is used to protect the metrics endpoint with authn/authz.
112+ // These configurations ensure that only authorized users and service accounts
113+ // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
114+ // https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.1/pkg/metrics/filters#WithAuthenticationAndAuthorization
115+ metricsServerOptions .FilterProvider = filters .WithAuthenticationAndAuthorization
116+
117+ // TODO(user): If CertDir, CertName, and KeyName are not specified, controller-runtime will automatically
118+ // generate self-signed certificates for the metrics server. While convenient for development and testing,
119+ // this setup is not recommended for production.
120+
121+ // TODO(user): If cert-manager is enabled in config/default/kustomization.yaml,
122+ // you can uncomment the following lines to use the certificate managed by cert-manager.
123+
124+ // metricsServerOptions.CertDir = "/tmp/k8s-metrics-server/metrics-certs"
125+ // metricsServerOptions.CertName = "tls.crt"
126+ // metricsServerOptions.KeyName = "tls.key"
127+ }
128+
68129 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
69130 Scheme : scheme ,
131+ Metrics : metricsServerOptions ,
132+ WebhookServer : webhookServer ,
70133 HealthProbeBindAddress : probeAddr ,
71134 LeaderElection : enableLeaderElection ,
72- LeaderElectionID : "1f172fb1 .spectrocloud.labs" ,
135+ LeaderElectionID : "52db6862 .spectrocloud.labs" ,
73136 // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
74137 // when the Manager ends. This requires the binary to immediately end when the
75138 // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
@@ -95,7 +158,7 @@ func main() {
95158 setupLog .Error (err , "unable to create controller" , "controller" , "AwsValidator" )
96159 os .Exit (1 )
97160 }
98- //+kubebuilder:scaffold:builder
161+ // +kubebuilder:scaffold:builder
99162
100163 if err := mgr .AddHealthzCheck ("healthz" , healthz .Ping ); err != nil {
101164 setupLog .Error (err , "unable to set up health check" )
0 commit comments