Skip to content

Commit 1643cb5

Browse files
committed
Expose pprof endpoint when debugging
This commit introduces a change that allows requests to the pprof endpoint to be made without a client certificate when OLM is not configured to use tls certificates and is ran with the --debug flag.
1 parent 4c3c8f1 commit 1643cb5

File tree

4 files changed

+44
-31
lines changed

4 files changed

+44
-31
lines changed

Diff for: cmd/catalog/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func main() {
109109
*catalogNamespace = catalogNamespaceEnvVarValue
110110
}
111111

112-
listenAndServe, err := server.GetListenAndServeFunc(logger, tlsCertPath, tlsKeyPath, clientCAPath)
112+
listenAndServe, err := server.GetListenAndServeFunc(logger, tlsCertPath, tlsKeyPath, clientCAPath, *debug)
113113
if err != nil {
114114
logger.Fatal("Error setting up health/metric/pprof service: %v", err)
115115
}

Diff for: cmd/olm/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func main() {
118118
}
119119
logger.Infof("log level %s", logger.Level)
120120

121-
listenAndServe, err := server.GetListenAndServeFunc(logger, tlsCertPath, tlsKeyPath, clientCAPath)
121+
listenAndServe, err := server.GetListenAndServeFunc(logger, tlsCertPath, tlsKeyPath, clientCAPath, *debug)
122122
if err != nil {
123123
logger.Fatal("Error setting up health/metric/pprof service: %v", err)
124124
}

Diff for: pkg/lib/profile/profile.go

+25-24
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,37 @@ import (
66
)
77

88
type profileConfig struct {
9-
pprof bool
10-
cmdline bool
11-
profile bool
12-
symbol bool
13-
trace bool
9+
pprof bool
10+
cmdline bool
11+
profile bool
12+
symbol bool
13+
trace bool
14+
enableTLS bool
1415
}
1516

1617
// Option applies a configuration option to the given config.
1718
type Option func(p *profileConfig)
1819

1920
func (p *profileConfig) apply(options []Option) {
20-
if len(options) == 0 {
21-
// If no options are given, default to all
22-
p.pprof = true
23-
p.cmdline = true
24-
p.profile = true
25-
p.symbol = true
26-
p.trace = true
27-
28-
return
29-
}
30-
3121
for _, o := range options {
3222
o(p)
3323
}
3424
}
3525

26+
func DisableTLS(p *profileConfig) {
27+
p.enableTLS = false
28+
}
29+
3630
func defaultProfileConfig() *profileConfig {
3731
// Initialize config
38-
return &profileConfig{}
32+
return &profileConfig{
33+
pprof: true,
34+
cmdline: true,
35+
profile: true,
36+
symbol: true,
37+
trace: true,
38+
enableTLS: true,
39+
}
3940
}
4041

4142
// RegisterHandlers registers profile Handlers with the given ServeMux.
@@ -47,25 +48,25 @@ func RegisterHandlers(mux *http.ServeMux, options ...Option) {
4748
config.apply(options)
4849

4950
if config.pprof {
50-
mux.Handle("/debug/pprof/", requireVerifiedClientCertificate(http.HandlerFunc(pprof.Index)))
51+
mux.Handle("/debug/pprof/", pprofHandlerFunc(http.HandlerFunc(pprof.Index), config.enableTLS))
5152
}
5253
if config.cmdline {
53-
mux.Handle("/debug/pprof/cmdline", requireVerifiedClientCertificate(http.HandlerFunc(pprof.Cmdline)))
54+
mux.Handle("/debug/pprof/cmdline", pprofHandlerFunc(http.HandlerFunc(pprof.Cmdline), config.enableTLS))
5455
}
5556
if config.profile {
56-
mux.Handle("/debug/pprof/profile", requireVerifiedClientCertificate(http.HandlerFunc(pprof.Profile)))
57+
mux.Handle("/debug/pprof/profile", pprofHandlerFunc(http.HandlerFunc(pprof.Profile), config.enableTLS))
5758
}
5859
if config.symbol {
59-
mux.Handle("/debug/pprof/symbol", requireVerifiedClientCertificate(http.HandlerFunc(pprof.Symbol)))
60+
mux.Handle("/debug/pprof/symbol", pprofHandlerFunc(http.HandlerFunc(pprof.Symbol), config.enableTLS))
6061
}
6162
if config.trace {
62-
mux.Handle("/debug/pprof/trace", requireVerifiedClientCertificate(http.HandlerFunc(pprof.Trace)))
63+
mux.Handle("/debug/pprof/trace", pprofHandlerFunc(http.HandlerFunc(pprof.Trace), config.enableTLS))
6364
}
6465
}
6566

66-
func requireVerifiedClientCertificate(h http.Handler) http.Handler {
67+
func pprofHandlerFunc(h http.Handler, enableTLS bool) http.Handler {
6768
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
68-
if r.TLS == nil || len(r.TLS.VerifiedChains) == 0 {
69+
if enableTLS && (r.TLS == nil || len(r.TLS.VerifiedChains) == 0) {
6970
w.WriteHeader(http.StatusForbidden)
7071
return
7172
}

Diff for: pkg/lib/server/server.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ import (
1313
"github.com/sirupsen/logrus"
1414
)
1515

16-
func GetListenAndServeFunc(logger *logrus.Logger, tlsCertPath, tlsKeyPath, clientCAPath *string) (func() error, error) {
16+
func GetListenAndServeFunc(logger *logrus.Logger, tlsCertPath, tlsKeyPath, clientCAPath *string, debug bool) (func() error, error) {
1717
mux := http.NewServeMux()
18-
profile.RegisterHandlers(mux)
1918
mux.Handle("/metrics", promhttp.Handler())
2019
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
2120
w.WriteHeader(http.StatusOK)
@@ -25,10 +24,11 @@ func GetListenAndServeFunc(logger *logrus.Logger, tlsCertPath, tlsKeyPath, clien
2524
Handler: mux,
2625
Addr: ":8080",
2726
}
28-
listenAndServe := s.ListenAndServe
27+
var listenAndServe func() error
2928

30-
if *tlsCertPath != "" && *tlsKeyPath != "" {
29+
if !debug && *tlsCertPath != "" && *tlsKeyPath != "" {
3130
logger.Info("TLS keys set, using https for metrics")
31+
profile.RegisterHandlers(mux)
3232

3333
certStore, err := filemonitor.NewCertStore(*tlsCertPath, *tlsKeyPath)
3434
if err != nil {
@@ -74,7 +74,19 @@ func GetListenAndServeFunc(logger *logrus.Logger, tlsCertPath, tlsKeyPath, clien
7474
} else if *tlsCertPath != "" || *tlsKeyPath != "" {
7575
return nil, fmt.Errorf("both --tls-key and --tls-crt must be provided for TLS to be enabled")
7676
} else {
77-
logger.Info("TLS keys not set, using non-https for metrics")
77+
options := []profile.Option{}
78+
if debug {
79+
logger.Info("TLS keys not set and debug mode enabled, requests to pprof endpoint no longer require certificates")
80+
options = append(options, profile.DisableTLS)
81+
}
82+
83+
profile.RegisterHandlers(mux, options...)
84+
logger.Info("TLS keys not set, using non-https for metrics and healthz endpoints")
85+
listenAndServe = s.ListenAndServe
86+
}
87+
88+
if listenAndServe == nil {
89+
return nil, fmt.Errorf("unable to configure healthz/metrics/pprof server")
7890
}
7991
return listenAndServe, nil
8092
}

0 commit comments

Comments
 (0)