-
Notifications
You must be signed in to change notification settings - Fork 516
CNTRLPLANE-3973: Make sure unencrypted metrics bind to localhost only, and remove dead code #6350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,8 @@ package common | |
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "fmt" | ||
| "net" | ||
| "net/http" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
|
|
@@ -12,8 +12,8 @@ import ( | |
| ) | ||
|
|
||
| const ( | ||
| // DefaultBindAddress is the port for the metrics listener | ||
| DefaultBindAddress = ":8797" | ||
| // DefaultMetricsBindAddress is the port for the metrics listener | ||
| DefaultMetricsBindAddress = "127.0.0.1:8797" | ||
| ) | ||
|
|
||
| // MCC Metrics | ||
|
|
@@ -135,10 +135,22 @@ func RegisterMetrics(metrics []prometheus.Collector) error { | |
| return nil | ||
| } | ||
|
|
||
| // StartMetricsListener is metrics listener via http on localhost | ||
| func StartMetricsListener(addr string, stopCh <-chan struct{}, registerFunc func() error, tlsMinVersion string, tlsCipherSuites []string) { | ||
| // StartMetricsListener starts the prometheus metrics listener | ||
| // | ||
| // It is unencrypted and should bind to localhost, with kube-rbac-proxy exposing a TLS endpoint outside localhost | ||
| func StartMetricsListener(addr string, stopCh <-chan struct{}, registerFunc func() error) { | ||
| if addr == "" { | ||
| addr = DefaultBindAddress | ||
| addr = DefaultMetricsBindAddress | ||
| } | ||
|
|
||
| host, _, err := net.SplitHostPort(addr) | ||
| if err != nil { | ||
| klog.Errorf("invalid metrics listen address %q: %v", addr, err) | ||
| return | ||
| } | ||
| if ip := net.ParseIP(host); ip == nil || !ip.IsLoopback() { | ||
| klog.Errorf("metrics is listening on %q, it should listen on localhost and be exposed via a TLS proxy", addr) | ||
| return | ||
| } | ||
|
|
||
| klog.Info("Registering Prometheus metrics") | ||
|
|
@@ -148,17 +160,10 @@ func StartMetricsListener(addr string, stopCh <-chan struct{}, registerFunc func | |
| return | ||
| } | ||
|
|
||
| // Get TLS config from provided settings, or use defaults | ||
| tlsConfig := GetGoTLSConfig(tlsMinVersion, tlsCipherSuites) | ||
|
|
||
| klog.Infof("Starting metrics listener on %s with TLS min version: %s", addr, tlsMinVersion) | ||
| klog.Infof("Starting metrics listener on %s", addr) | ||
| mux := http.NewServeMux() | ||
| mux.Handle("/metrics", promhttp.Handler()) | ||
| s := http.Server{ | ||
| TLSConfig: tlsConfig, | ||
| TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)), | ||
| Addr: addr, | ||
| Handler: mux} | ||
| s := http.Server{Addr: addr, Handler: mux} | ||
|
Comment on lines
+163
to
+166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win Bound request and shutdown time. Without As per path instructions, “context.Context for cancellation and timeouts.” 🧰 Tools🪛 ast-grep (0.45.0)[warning] 165-165: This (http-server-missing-read-timeout-go) 🤖 Prompt for AI AgentsSources: Path instructions, Linters/SAST tools |
||
|
|
||
| go func() { | ||
| if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win
Avoid logging raw rejected listener addresses.
addris operator-configurable; invalid values may contain internal hostnames and are written directly to logs. Redact the address or log only the validation failure.As per coding guidelines, “Flag logging that may expose … internal hostnames.”
🤖 Prompt for AI Agents
Source: Coding guidelines