github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/profile/profile.go (about) 1 package profile 2 3 import ( 4 "net/http" 5 "net/http/pprof" 6 ) 7 8 type profileConfig struct { 9 pprof bool 10 cmdline bool 11 profile bool 12 symbol bool 13 trace bool 14 enableTLS bool 15 } 16 17 // Option applies a configuration option to the given config. 18 type Option func(p *profileConfig) 19 20 func (p *profileConfig) apply(options []Option) { 21 for _, o := range options { 22 o(p) 23 } 24 } 25 26 func WithTLS(enabled bool) Option { 27 return func(p *profileConfig) { 28 p.enableTLS = enabled 29 } 30 } 31 32 func defaultProfileConfig() *profileConfig { 33 // Initialize config 34 return &profileConfig{ 35 pprof: true, 36 cmdline: true, 37 profile: true, 38 symbol: true, 39 trace: true, 40 enableTLS: true, 41 } 42 } 43 44 // RegisterHandlers registers profile Handlers with the given ServeMux. 45 // 46 // The Handlers registered are determined by the given options. 47 // If no options are given, all available handlers are registered by default. 48 func RegisterHandlers(mux *http.ServeMux, options ...Option) { 49 config := defaultProfileConfig() 50 config.apply(options) 51 52 if config.pprof { 53 mux.Handle("/debug/pprof/", pprofHandlerFunc(http.HandlerFunc(pprof.Index), config.enableTLS)) 54 } 55 if config.cmdline { 56 mux.Handle("/debug/pprof/cmdline", pprofHandlerFunc(http.HandlerFunc(pprof.Cmdline), config.enableTLS)) 57 } 58 if config.profile { 59 mux.Handle("/debug/pprof/profile", pprofHandlerFunc(http.HandlerFunc(pprof.Profile), config.enableTLS)) 60 } 61 if config.symbol { 62 mux.Handle("/debug/pprof/symbol", pprofHandlerFunc(http.HandlerFunc(pprof.Symbol), config.enableTLS)) 63 } 64 if config.trace { 65 mux.Handle("/debug/pprof/trace", pprofHandlerFunc(http.HandlerFunc(pprof.Trace), config.enableTLS)) 66 } 67 } 68 69 func pprofHandlerFunc(h http.Handler, enableTLS bool) http.Handler { 70 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 71 if enableTLS && (r.TLS == nil || len(r.TLS.VerifiedChains) == 0) { 72 w.WriteHeader(http.StatusForbidden) 73 return 74 } 75 h.ServeHTTP(w, r) 76 }) 77 }