diff --git a/main.go b/main.go index 00f2fef..a0663b6 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,9 @@ import ( "os/signal" "path/filepath" "strings" + "sync/atomic" "syscall" + "time" "github.com/go-kit/log" "github.com/go-kit/log/level" @@ -151,8 +153,9 @@ func Main() error { return fmt.Errorf("failed to listen on %s: %v", *listen, err) } + srv := newHTTPServer(mux) g.Add(func() error { - if err := http.Serve(l, mux); err != nil && err != http.ErrServerClosed { + if err := srv.Serve(l); err != nil && err != http.ErrServerClosed { return fmt.Errorf("server exited unexpectedly: %v", err) } return nil @@ -221,6 +224,42 @@ func Main() error { return nil } +// Limits for the health/metrics HTTP server. Legitimate traffic is a Prometheus +// scrape every 30s plus optional health checks, so these leave ample headroom. +// The idle timeout exceeds the scrape interval so scrapers can reuse connections. +const ( + httpReadHeaderTimeout = 2 * time.Second + httpIdleTimeout = 60 * time.Second + httpMaxHeaderBytes = 8 << 10 + httpMaxConns = 64 +) + +// newHTTPServer returns the server for the health and metrics endpoints. +// The listener is reachable by any pod in the cluster, so per-connection cost is +// bounded to keep an unauthenticated client from exhausting the plugin's small +// memory limit (Slowloris): stalled request headers time out, header size is +// capped, and connections beyond httpMaxConns are closed right away without +// blocking the accept loop. +func newHTTPServer(handler http.Handler) *http.Server { + var conns atomic.Int64 + return &http.Server{ + Handler: handler, + ReadHeaderTimeout: httpReadHeaderTimeout, + IdleTimeout: httpIdleTimeout, + MaxHeaderBytes: httpMaxHeaderBytes, + ConnState: func(c net.Conn, state http.ConnState) { + switch state { + case http.StateNew: + if conns.Add(1) > httpMaxConns { + _ = c.Close() + } + case http.StateHijacked, http.StateClosed: + conns.Add(-1) + } + }, + } +} + func main() { if err := Main(); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..2f64cb2 --- /dev/null +++ b/main_test.go @@ -0,0 +1,94 @@ +// Copyright 2026 Google LLC + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// https://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "errors" + "io" + "net" + "net/http" + "testing" + "time" +) + +// TestHTTPServerBoundsSlowConnections guards against Slowloris: stalled clients +// must not be able to hold an unbounded number of connections. +func TestHTTPServerBoundsSlowConnections(t *testing.T) { + l, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + srv := newHTTPServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + })) + go func() { _ = srv.Serve(l) }() + t.Cleanup(func() { _ = srv.Close() }) + addr := l.Addr().String() + + // dialSlow opens a connection that sends a partial request header and stalls. + dialSlow := func() net.Conn { + c, err := net.Dial("tcp", addr) + if err != nil { + t.Fatal(err) + } + _, _ = c.Write([]byte("GET /health HTTP/1.1\r\nHost: x\r\n")) + t.Cleanup(func() { _ = c.Close() }) + return c + } + + // Occupy every connection slot with a stalled client. + slow := make([]net.Conn, httpMaxConns) + for i := range slow { + slow[i] = dialSlow() + } + + // A connection beyond the cap is closed right away instead of being held. + if !closedWithin(dialSlow(), time.Second) { + t.Error("connection beyond httpMaxConns was not closed") + } + + // Stalled clients are disconnected once the header timeout elapses. + if !closedWithin(slow[0], httpReadHeaderTimeout+2*time.Second) { + t.Error("stalled connection was not closed after httpReadHeaderTimeout") + } + + // Freed slots are available to legitimate requests again. + client := &http.Client{Timeout: time.Second, Transport: &http.Transport{DisableKeepAlives: true}} + var resp *http.Response + for i := 0; i < 20; i++ { + if resp, err = client.Get("http://" + addr + "/health"); err == nil { + break + } + time.Sleep(100 * time.Millisecond) + } + if err != nil { + t.Fatalf("GET /health after slots were freed: %v", err) + } + _ = resp.Body.Close() + if resp.StatusCode != http.StatusOK { + t.Errorf("GET /health = %d, want %d", resp.StatusCode, http.StatusOK) + } +} + +// closedWithin reports whether the server closes c within d. +func closedWithin(c net.Conn, d time.Duration) bool { + _ = c.SetReadDeadline(time.Now().Add(d)) + _, err := io.Copy(io.Discard, c) + var ne net.Error + if errors.As(err, &ne) && ne.Timeout() { + return false // Still open when the deadline hit. + } + return true // EOF or reset: the server closed it. +}