@@ -17,6 +17,7 @@ limitations under the License.
1717package main
1818
1919import (
20+ "crypto/tls"
2021 "flag"
2122 "os"
2223
@@ -37,6 +38,7 @@ import (
3738 ctrl "sigs.k8s.io/controller-runtime"
3839 "sigs.k8s.io/controller-runtime/pkg/healthz"
3940 "sigs.k8s.io/controller-runtime/pkg/log/zap"
41+ "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
4042 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
4143 // +kubebuilder:scaffold:imports
4244)
@@ -63,11 +65,19 @@ func main() {
6365 var metricsAddr string
6466 var enableLeaderElection bool
6567 var probeAddr string
66- flag .StringVar (& metricsAddr , "metrics-bind-address" , ":8080" , "The address the metric endpoint binds to." )
68+ var secureMetrics bool
69+ var enableHTTP2 bool
70+ var tlsOpts []func (* tls.Config )
71+ flag .StringVar (& metricsAddr , "metrics-bind-address" , "0" , "The address the metrics endpoint binds to. " +
72+ "Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service." )
6773 flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
6874 flag .BoolVar (& enableLeaderElection , "leader-elect" , false ,
6975 "Enable leader election for controller manager. " +
7076 "Enabling this will ensure there is only one active controller manager." )
77+ flag .BoolVar (& secureMetrics , "metrics-secure" , true ,
78+ "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead." )
79+ flag .BoolVar (& enableHTTP2 , "enable-http2" , false ,
80+ "If set, HTTP/2 will be enabled for the metrics and webhook servers" )
7181 opts := zap.Options {
7282 Development : true ,
7383 }
@@ -76,12 +86,45 @@ func main() {
7686
7787 ctrl .SetLogger (zap .New (zap .UseFlagOptions (& opts )))
7888
89+ // if the enable-http2 flag is false (the default), http/2 should be disabled
90+ // due to its vulnerabilities. More specifically, disabling http/2 will
91+ // prevent from being vulnerable to the HTTP/2 Stream Cancellation and
92+ // Rapid Reset CVEs. For more information see:
93+ // - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
94+ // - https://github.com/advisories/GHSA-4374-p667-p6c8
95+ disableHTTP2 := func (c * tls.Config ) {
96+ setupLog .Info ("disabling http/2" )
97+ c .NextProtos = []string {"http/1.1" }
98+ }
99+
100+ if ! enableHTTP2 {
101+ tlsOpts = append (tlsOpts , disableHTTP2 )
102+ }
103+
104+ // Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
105+ // More info:
106+ // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.0/pkg/metrics/server
107+ // - https://book.kubebuilder.io/reference/metrics.html
108+ metricsServerOptions := metricsserver.Options {
109+ BindAddress : metricsAddr ,
110+ SecureServing : secureMetrics ,
111+ TLSOpts : tlsOpts ,
112+ }
113+
114+ if secureMetrics {
115+ // FilterProvider is used to protect the metrics endpoint with authn/authz.
116+ // These configurations ensure that only authorized users and service accounts
117+ // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
118+ // https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.0/pkg/metrics/filters#WithAuthenticationAndAuthorization
119+ metricsServerOptions .FilterProvider = filters .WithAuthenticationAndAuthorization
120+ }
121+
79122 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
80123 Scheme : scheme ,
81124 HealthProbeBindAddress : probeAddr ,
82125 LeaderElection : enableLeaderElection ,
83126 LeaderElectionID : "ef59679f.redhat.com" ,
84- Metrics : metricsserver. Options { BindAddress : metricsAddr } ,
127+ Metrics : metricsServerOptions ,
85128 })
86129 if err != nil {
87130 setupLog .Error (err , "unable to start manager" )
0 commit comments