Skip to content

Commit

Permalink
Disable HTTP/2
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Sluiter <[email protected]>
  • Loading branch information
slintes committed Oct 20, 2023
1 parent e0b3a2f commit 74f8136
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
25 changes: 0 additions & 25 deletions api/v1alpha1/nodehealthcheck_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package v1alpha1

import (
"fmt"
"os"
"path/filepath"
"reflect"
"time"

Expand All @@ -33,10 +31,6 @@ import (
)

const (
WebhookCertDir = "/apiserver.local.config/certificates"
WebhookCertName = "apiserver.crt"
WebhookKeyName = "apiserver.key"

OngoingRemediationError = "prohibited due to running remediation"
minHealthyError = "MinHealthy must not be negative"
invalidSelectorError = "Invalid selector"
Expand All @@ -52,25 +46,6 @@ const (
var nodehealthchecklog = logf.Log.WithName("nodehealthcheck-resource")

func (nhc *NodeHealthCheck) SetupWebhookWithManager(mgr ctrl.Manager) error {

// check if OLM injected certs
certs := []string{filepath.Join(WebhookCertDir, WebhookCertName), filepath.Join(WebhookCertDir, WebhookKeyName)}
certsInjected := true
for _, fname := range certs {
if _, err := os.Stat(fname); err != nil {
certsInjected = false
break
}
}
if certsInjected {
server := mgr.GetWebhookServer()
server.CertDir = WebhookCertDir
server.CertName = WebhookCertName
server.KeyName = WebhookKeyName
} else {
nodehealthchecklog.Info("OLM injected certs for webhooks not found")
}

return ctrl.NewWebhookManagedBy(mgr).
For(nhc).
Complete()
Expand Down
51 changes: 50 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
package main

import (
"crypto/tls"
"flag"
"fmt"
"os"
"path/filepath"
"runtime"

// +kubebuilder:scaffold:imports
Expand Down Expand Up @@ -51,6 +53,12 @@ import (
"github.com/medik8s/node-healthcheck-operator/version"
)

const (
WebhookCertDir = "/apiserver.local.config/certificates"
WebhookCertName = "apiserver.crt"
WebhookKeyName = "apiserver.key"
)

var (
scheme = pkgruntime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
Expand All @@ -72,11 +80,13 @@ func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var enableHTTP2 bool
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", true,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&enableHTTP2, "enable-http2", false, "If HTTP/2 should be enabled for the metrics and webhook servers.")

opts := zap.Options{
Development: true,
Expand All @@ -90,7 +100,9 @@ func main() {
printVersion()

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Scheme: scheme,
// HEADS UP: once controller runtime is updated and this changes to metrics.Options{},
// and in case you configure TLS / SecureServing, disable HTTP/2 in it for mitigating related CVEs!
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
Expand All @@ -102,6 +114,8 @@ func main() {
os.Exit(1)
}

configureWebhookServer(mgr, enableHTTP2)

upgradeChecker, err := cluster.NewClusterUpgradeStatusChecker(mgr)
if err != nil {
setupLog.Error(err, "unable initialize cluster upgrade checker")
Expand Down Expand Up @@ -194,3 +208,38 @@ func printVersion() {
setupLog.Info(fmt.Sprintf("Git Commit: %s", version.GitCommit))
setupLog.Info(fmt.Sprintf("Build Date: %s", version.BuildDate))
}

func configureWebhookServer(mgr ctrl.Manager, enableHTTP2 bool) {

server := mgr.GetWebhookServer()

// check for OLM injected certs
certs := []string{filepath.Join(WebhookCertDir, WebhookCertName), filepath.Join(WebhookCertDir, WebhookKeyName)}
certsInjected := true
for _, fname := range certs {
if _, err := os.Stat(fname); err != nil {
certsInjected = false
break
}
}
if certsInjected {
server.CertDir = WebhookCertDir
server.CertName = WebhookCertName
server.KeyName = WebhookKeyName
} else {
setupLog.Info("OLM injected certs for webhooks not found")
}

// disable http/2 for mitigating relevant CVEs
if !enableHTTP2 {
server.TLSOpts = append(server.TLSOpts,
func(c *tls.Config) {
c.NextProtos = []string{"http/1.1"}
},
)
setupLog.Info("HTTP/2 for webhooks disabled")
} else {
setupLog.Info("HTTP/2 for webhooks enabled")
}

}

0 comments on commit 74f8136

Please sign in to comment.