Skip to content

Commit

Permalink
Merge pull request #103 from slintes/disable-http2-0.14
Browse files Browse the repository at this point in the history
[release-0.14] Disable HTTP/s on webhook server
  • Loading branch information
slintes authored Oct 20, 2023
2 parents 408d8fe + 12db76e commit 87c1f89
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"

v1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
Expand Down Expand Up @@ -48,12 +46,6 @@ const (
LabelNameRoleControlPlane = "node-role.kubernetes.io/control-plane"
)

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 @@ -17,9 +17,11 @@ limitations under the License.
package main

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

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand All @@ -39,6 +41,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 @@ -55,11 +63,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 @@ -71,7 +82,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 @@ -83,6 +96,8 @@ func main() {
os.Exit(1)
}

configureWebhookServer(mgr, enableHTTP2)

if err = (&controllers.NodeMaintenanceReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Expand Down Expand Up @@ -123,3 +138,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 87c1f89

Please sign in to comment.