@@ -18,6 +18,7 @@ limitations under the License.
18
18
package main
19
19
20
20
import (
21
+ "crypto/tls"
21
22
"flag"
22
23
"os"
23
24
@@ -31,6 +32,8 @@ import (
31
32
ctrl "sigs.k8s.io/controller-runtime"
32
33
"sigs.k8s.io/controller-runtime/pkg/healthz"
33
34
"sigs.k8s.io/controller-runtime/pkg/log/zap"
35
+ "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
36
+ metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
34
37
35
38
validationv1alpha1 "github.com/validator-labs/validator-plugin-maas/api/v1alpha1"
36
39
"github.com/validator-labs/validator-plugin-maas/internal/controller"
@@ -51,12 +54,23 @@ func init() {
51
54
}
52
55
53
56
func main () {
57
+ var metricsAddr string
54
58
var enableLeaderElection bool
55
59
var probeAddr string
60
+ var secureMetrics bool
61
+ var enableHTTP2 bool
62
+ var tlsOpts []func (* tls.Config )
56
63
flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
57
64
flag .BoolVar (& enableLeaderElection , "leader-elect" , false ,
58
65
"Enable leader election for controller manager. " +
59
66
"Enabling this will ensure there is only one active controller manager." )
67
+ flag .StringVar (& metricsAddr , "metrics-bind-address" , "0" , "The address the metrics endpoint binds to. " +
68
+ "Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service." )
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" )
73
+
60
74
opts := zap.Options {
61
75
Development : true ,
62
76
}
@@ -65,8 +79,52 @@ func main() {
65
79
66
80
ctrl .SetLogger (zap .New (zap .UseFlagOptions (& opts )))
67
81
82
+ // if the enable-http2 flag is false (the default), http/2 should be disabled
83
+ // due to its vulnerabilities. More specifically, disabling http/2 will
84
+ // prevent from being vulnerable to the HTTP/2 Stream Cancellation and
85
+ // Rapid Reset CVEs. For more information see:
86
+ // - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
87
+ // - https://github.com/advisories/GHSA-4374-p667-p6c8
88
+ disableHTTP2 := func (c * tls.Config ) {
89
+ setupLog .Info ("disabling http/2" )
90
+ c .NextProtos = []string {"http/1.1" }
91
+ }
92
+
93
+ if ! enableHTTP2 {
94
+ tlsOpts = append (tlsOpts , disableHTTP2 )
95
+ }
96
+ // Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
97
+ // More info:
98
+ // - https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/metrics/server
99
+ // - https://book.kubebuilder.io/reference/metrics.html
100
+ metricsServerOptions := metricsserver.Options {
101
+ BindAddress : metricsAddr ,
102
+ SecureServing : secureMetrics ,
103
+ TLSOpts : tlsOpts ,
104
+ }
105
+
106
+ if secureMetrics {
107
+ // FilterProvider is used to protect the metrics endpoint with authn/authz.
108
+ // These configurations ensure that only authorized users and service accounts
109
+ // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
110
+ // https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/metrics/filters#WithAuthenticationAndAuthorization
111
+ metricsServerOptions .FilterProvider = filters .WithAuthenticationAndAuthorization
112
+
113
+ // TODO(user): If CertDir, CertName, and KeyName are not specified, controller-runtime will automatically
114
+ // generate self-signed certificates for the metrics server. While convenient for development and testing,
115
+ // this setup is not recommended for production.
116
+
117
+ // TODO(user): If cert-manager is enabled in config/default/kustomization.yaml,
118
+ // you can uncomment the following lines to use the certificate managed by cert-manager.
119
+ // metricsServerOptions.CertDir = "/tmp/k8s-metrics-server/metrics-certs"
120
+ // metricsServerOptions.CertName = "tls.crt"
121
+ // metricsServerOptions.KeyName = "tls.key"
122
+
123
+ }
124
+
68
125
mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
69
126
Scheme : scheme ,
127
+ Metrics : metricsServerOptions ,
70
128
HealthProbeBindAddress : probeAddr ,
71
129
LeaderElection : enableLeaderElection ,
72
130
LeaderElectionID : "ecaf1259.spectrocloud.labs" ,
0 commit comments