Skip to content

Commit

Permalink
Merge pull request #101 from openshift-cherrypick-robot/cherry-pick-1…
Browse files Browse the repository at this point in the history
…00-to-release-0.16

[release-0.16] Disable HTTP/s on webhook server
  • Loading branch information
slintes authored Oct 20, 2023
2 parents 4a73234 + 4d0afb1 commit ebb38a1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
26 changes: 0 additions & 26 deletions api/v1beta1/nodemaintenance_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package v1beta1
import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/medik8s/common/pkg/nodes"

Expand Down Expand Up @@ -48,12 +46,6 @@ const (
EtcdQuorumPDBNamespace = "openshift-etcd"
)

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

// log is for logging in this package.
var nodemaintenancelog = logf.Log.WithName("nodemaintenance-resource")

Expand All @@ -73,24 +65,6 @@ func (r *NodeMaintenance) SetupWebhookWithManager(mgr ctrl.Manager) error {
client: mgr.GetClient(),
}

// 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 {
nodemaintenancelog.Info("OLM injected certs for webhooks not found")
}

return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
Expand Down
52 changes: 51 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package main

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

"github.com/medik8s/common/pkg/lease"
Expand All @@ -43,6 +45,12 @@ import (
//+kubebuilder:scaffold:imports
)

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

var (
scheme = k8sruntime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
Expand All @@ -59,11 +67,14 @@ 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", false,
"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 @@ -75,7 +86,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 @@ -87,6 +100,8 @@ func main() {
os.Exit(1)
}

configureWebhookServer(mgr, enableHTTP2)

cl := mgr.GetClient()
leaseManagerInitializer := &leaseManagerInitializer{cl: cl}
if err := mgr.Add(leaseManagerInitializer); err != nil {
Expand Down Expand Up @@ -142,3 +157,38 @@ func (ls *leaseManagerInitializer) Start(context.Context) error {
ls.Manager, err = lease.NewManager(ls.cl, controllers.LeaseHolderIdentity)
return err
}

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 ebb38a1

Please sign in to comment.