@@ -17,6 +17,7 @@ limitations under the License.
1717package main
1818
1919import (
20+ "crypto/tls"
2021 "flag"
2122 "os"
2223
@@ -32,6 +33,7 @@ import (
3233 ctrl "sigs.k8s.io/controller-runtime"
3334 "sigs.k8s.io/controller-runtime/pkg/healthz"
3435 "sigs.k8s.io/controller-runtime/pkg/log/zap"
36+ "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
3537 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3638
3739 spinv1alpha1 "github.com/spinkube/spin-operator/api/v1alpha1"
@@ -58,12 +60,19 @@ func main() {
5860 var enableLeaderElection bool
5961 var probeAddr string
6062 var enableWebhooks bool
63+ var secureMetrics bool
64+ var enableHTTP2 bool
65+ var tlsOpts []func (* tls.Config )
6166 flag .StringVar (& metricsAddr , "metrics-bind-address" , ":8080" , "The address the metric endpoint binds to." )
6267 flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8082" , "The address the probe endpoint binds to." )
6368 flag .BoolVar (& enableLeaderElection , "leader-elect" , false ,
6469 "Enable leader election for controller manager. " +
6570 "Enabling this will ensure there is only one active controller manager." )
6671 flag .BoolVar (& enableWebhooks , "enable-webhooks" , false , "Enable admission webhooks" )
72+ flag .BoolVar (& secureMetrics , "metrics-secure" , true ,
73+ "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead." )
74+ flag .BoolVar (& enableHTTP2 , "enable-http2" , false ,
75+ "If set, HTTP/2 will be enabled for the metrics server" )
6776 opts := zap.Options {
6877 Development : true ,
6978 }
@@ -78,9 +87,46 @@ func main() {
7887 "enableLeaderElection" , enableLeaderElection ,
7988 "enableWebhooks" , enableWebhooks )
8089
90+ // if the enable-http2 flag is false (the default), http/2 should be disabled
91+ // due to its vulnerabilities. More specifically, disabling http/2 will
92+ // prevent from being vulnerable to the HTTP/2 Stream Cancellation and
93+ // Rapid Reset CVEs. For more information see:
94+ // - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
95+ // - https://github.com/advisories/GHSA-4374-p667-p6c8
96+ disableHTTP2 := func (c * tls.Config ) {
97+ setupLog .Info ("disabling http/2" )
98+ c .NextProtos = []string {"http/1.1" }
99+ }
100+
101+ if ! enableHTTP2 {
102+ tlsOpts = append (tlsOpts , disableHTTP2 )
103+ }
104+
105+ // Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
106+ // More info:
107+ // - https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/metrics/server 108+ // - https://book.kubebuilder.io/reference/metrics.html
109+ metricsServerOptions := metricsserver.Options {
110+ BindAddress : metricsAddr ,
111+ SecureServing : secureMetrics ,
112+ TLSOpts : tlsOpts ,
113+ }
114+
115+ if secureMetrics {
116+ // FilterProvider is used to protect the metrics endpoint with authn/authz.
117+ // These configurations ensure that only authorized users and service accounts
118+ // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
119+ // https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/metrics/filters#WithAuthenticationAndAuthorization 120+ metricsServerOptions .FilterProvider = filters .WithAuthenticationAndAuthorization
121+
122+ // TODO(user): If CertDir, CertName, and KeyName are not specified, controller-runtime will automatically
123+ // generate self-signed certificates for the metrics server. While convenient for development and testing,
124+ // this setup is not recommended for production.
125+ }
126+
81127 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
82128 Scheme : scheme ,
83- Metrics : metricsserver. Options { BindAddress : metricsAddr } ,
129+ Metrics : metricsServerOptions ,
84130 HealthProbeBindAddress : probeAddr ,
85131 LeaderElection : enableLeaderElection ,
86132 LeaderElectionID : "90ba2d18.spinkube.dev" ,
0 commit comments