@@ -18,13 +18,16 @@ package main
1818
1919import (
2020 "context"
21+ "crypto/tls"
2122 "flag"
2223 "os"
2324 "time"
2425
2526 "k8s.io/apimachinery/pkg/runtime"
2627 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2728 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
29+ "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
30+
2831 // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2932 // to ensure that exec-entrypoint and run can make use of them.
3033 _ "k8s.io/client-go/plugin/pkg/client/auth"
@@ -38,7 +41,6 @@ import (
3841 licensev1 "github.com/labring/sealos/controllers/license/api/v1"
3942 userv1 "github.com/labring/sealos/controllers/user/api/v1"
4043 "github.com/labring/sealos/controllers/user/controllers"
41- configpkg "github.com/labring/sealos/controllers/user/controllers/helper/config"
4244 ratelimiter "github.com/labring/sealos/controllers/user/controllers/helper/ratelimiter"
4345 //+kubebuilder:scaffold:imports
4446 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
@@ -70,6 +72,9 @@ func main() {
7072 operationReqExpirationTime time.Duration
7173 restartPredicateDuration time.Duration
7274 operationReqRetentionTime time.Duration
75+ secureMetrics bool
76+ enableHTTP2 bool
77+ tlsOpts []func (* tls.Config )
7378 )
7479 flag .StringVar (
7580 & metricsAddr ,
@@ -128,6 +133,14 @@ func main() {
128133 "/config.yaml" ,
129134 "The path to the configuration file." ,
130135 )
136+ flag .BoolVar (
137+ & secureMetrics ,
138+ "metrics-secure" ,
139+ true ,
140+ "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead." ,
141+ )
142+ flag .BoolVar (& enableHTTP2 , "enable-http2" , false ,
143+ "If set, HTTP/2 will be enabled for the metrics and webhook servers" )
131144 rateLimiterOptions .BindFlags (flag .CommandLine )
132145 opts := zap.Options {
133146 Development : true ,
@@ -137,11 +150,45 @@ func main() {
137150
138151 ctrl .SetLogger (zap .New (zap .UseFlagOptions (& opts )))
139152
153+ // if the enable-http2 flag is false (the default), http/2 should be disabled
154+ // due to its vulnerabilities. More specifically, disabling http/2 will
155+ // prevent from being vulnerable to the HTTP/2 Stream Cancellation and
156+ // Rapid Reset CVEs. For more information see:
157+ // - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
158+ // - https://github.com/advisories/GHSA-4374-p667-p6c8
159+ disableHTTP2 := func (c * tls.Config ) {
160+ setupLog .Info ("disabling http/2" )
161+ c .NextProtos = []string {"http/1.1" }
162+ }
163+
164+ if ! enableHTTP2 {
165+ tlsOpts = append (tlsOpts , disableHTTP2 )
166+ }
167+ // Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
168+ // More info:
169+ // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.1/pkg/metrics/server
170+ // - https://book.kubebuilder.io/reference/metrics.html
171+ metricsServerOptions := metricsserver.Options {
172+ BindAddress : metricsAddr ,
173+ SecureServing : secureMetrics ,
174+ TLSOpts : tlsOpts ,
175+ }
176+
177+ if secureMetrics {
178+ // FilterProvider is used to protect the metrics endpoint with authn/authz.
179+ // These configurations ensure that only authorized users and service accounts
180+ // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
181+ // https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.1/pkg/metrics/filters#WithAuthenticationAndAuthorization
182+ metricsServerOptions .FilterProvider = filters .WithAuthenticationAndAuthorization
183+
184+ // TODO(user): If CertDir, CertName, and KeyName are not specified, controller-runtime will automatically
185+ // generate self-signed certificates for the metrics server. While convenient for development and testing,
186+ // this setup is not recommended for production.
187+ }
188+
140189 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
141- Scheme : scheme ,
142- Metrics : metricsserver.Options {
143- BindAddress : metricsAddr ,
144- },
190+ Scheme : scheme ,
191+ Metrics : metricsServerOptions ,
145192 // WebhookServer: webhook.NewServer(webhook.Options{
146193 // Port: 9443,
147194 // }),
@@ -171,19 +218,6 @@ func main() {
171218 os .Exit (1 )
172219 }
173220
174- // Load the configuration file
175- config := & configpkg.Config {}
176- if err := configpkg .LoadConfig (configFilePath , config ); err != nil {
177- setupLog .Error (err , "unable to load configuration file" )
178- os .Exit (1 )
179- }
180-
181- // Set the configuration
182- if err := setConfigToEnv (* config ); err != nil {
183- setupLog .Error (err , "unable to set configuration to environment variables" )
184- os .Exit (1 )
185- }
186-
187221 if err := controllers .SetupLicenseGate (mgr ); err != nil {
188222 setupLog .Error (err , "unable to set up license gate" )
189223 os .Exit (1 )
@@ -243,10 +277,3 @@ func main() {
243277 os .Exit (1 )
244278 }
245279}
246-
247- func setConfigToEnv (cfg configpkg.Config ) error {
248- if err := os .Setenv ("SEALOS_CLOUD_APISERVER_HOST" , cfg .CloudAPIServerDomain ); err != nil {
249- return err
250- }
251- return os .Setenv ("SEALOS_CLOUD_APISERVER_PORT" , cfg .CloudAPIServerPort )
252- }
0 commit comments