@@ -14,10 +14,10 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- // Package main initializes a KubescapeValidator controller.
18
17
package main
19
18
20
19
import (
20
+ "crypto/tls"
21
21
"flag"
22
22
"os"
23
23
@@ -31,11 +31,14 @@ import (
31
31
ctrl "sigs.k8s.io/controller-runtime"
32
32
"sigs.k8s.io/controller-runtime/pkg/healthz"
33
33
"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"
34
37
35
- validationv1 "github.com/validator-labs/validator-plugin-kubescape/api/v1alpha1"
38
+ validationv1alpha1 "github.com/validator-labs/validator-plugin-kubescape/api/v1alpha1"
36
39
"github.com/validator-labs/validator-plugin-kubescape/internal/controller"
37
40
validatorv1alpha1 "github.com/validator-labs/validator/api/v1alpha1"
38
- //+kubebuilder:scaffold:imports
41
+ // +kubebuilder:scaffold:imports
39
42
)
40
43
41
44
var (
@@ -45,23 +48,28 @@ var (
45
48
46
49
func init () {
47
50
utilruntime .Must (clientgoscheme .AddToScheme (scheme ))
48
-
49
- utilruntime .Must (validationv1 .AddToScheme (scheme ))
50
-
51
51
utilruntime .Must (validatorv1alpha1 .AddToScheme (scheme ))
52
-
53
- //+kubebuilder:scaffold:scheme
52
+ utilruntime . Must ( validationv1alpha1 . AddToScheme ( scheme ))
53
+ // +kubebuilder:scaffold:scheme
54
54
}
55
55
56
56
func main () {
57
57
var metricsAddr string
58
58
var enableLeaderElection bool
59
59
var probeAddr string
60
- flag .StringVar (& metricsAddr , "metrics-bind-address" , ":8080" , "The address the metric endpoint binds to." )
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." )
61
65
flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
62
66
flag .BoolVar (& enableLeaderElection , "leader-elect" , false ,
63
67
"Enable leader election for controller manager. " +
64
68
"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" )
65
73
opts := zap.Options {
66
74
Development : true ,
67
75
}
@@ -70,11 +78,61 @@ func main() {
70
78
71
79
ctrl .SetLogger (zap .New (zap .UseFlagOptions (& opts )))
72
80
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/[email protected] /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/[email protected] /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
+
73
129
mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
74
130
Scheme : scheme ,
131
+ Metrics : metricsServerOptions ,
132
+ WebhookServer : webhookServer ,
75
133
HealthProbeBindAddress : probeAddr ,
76
134
LeaderElection : enableLeaderElection ,
77
- LeaderElectionID : "21f802fb .spectrocloud.labs" ,
135
+ LeaderElectionID : "cf0b361a .spectrocloud.labs" ,
78
136
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
79
137
// when the Manager ends. This requires the binary to immediately end when the
80
138
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
@@ -100,7 +158,7 @@ func main() {
100
158
setupLog .Error (err , "unable to create controller" , "controller" , "KubescapeValidator" )
101
159
os .Exit (1 )
102
160
}
103
- //+kubebuilder:scaffold:builder
161
+ // +kubebuilder:scaffold:builder
104
162
105
163
if err := mgr .AddHealthzCheck ("healthz" , healthz .Ping ); err != nil {
106
164
setupLog .Error (err , "unable to set up health check" )
0 commit comments