Skip to content
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

Disable pprof debug endpoint #31006

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions experiment/ml/analyze/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ import (
)

func serveOnPort(ctx context.Context, storageClient *storage.Client, predictor *predictionClient, port int, timeout time.Duration) error {
mux := http.NewServeMux()
Copy link
Contributor

@listx listx Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record, it looks like using our own ServeMux avoids using the DefaultServeMux which is used otherwise. DefaultServeMux exposes the pprof endpoints, which we want to avoid. References: https://stackoverflow.com/a/36249058/437583 https://pkg.go.dev/net/http#hdr-Servers


http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
status, body, err := processRequest(ctx, storageClient, predictor, r)
Expand All @@ -53,12 +54,12 @@ func serveOnPort(ctx context.Context, storageClient *storage.Client, predictor *
}
})

http.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})

log.Println("Listening for connections on port", port)
return http.ListenAndServe(":"+strconv.Itoa(port), nil)
return http.ListenAndServe(":"+strconv.Itoa(port), mux)
}

func processRequest(ctx context.Context, storageClient *storage.Client, predictor *predictionClient, r *http.Request) (int, string, error) {
Expand Down
22 changes: 12 additions & 10 deletions gcsweb/cmd/gcsweb/gcsweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,19 @@ func main() {

logrus.Info("Starting GCSWeb")

mux := http.NewServeMux()

// Canonicalize allowed buckets.
for i := range o.allowedBuckets {
bucket := joinPath(gcsPath, o.allowedBuckets[i])
logrus.WithField("bucket", bucket).Info("allowing bucket")
http.HandleFunc(bucket+"/", s.gcsRequest)
http.HandleFunc(bucket, func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(bucket+"/", s.gcsRequest)
mux.HandleFunc(bucket, func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, bucket+"/", http.StatusPermanentRedirect)
})
}
// Handle unknown buckets.
http.HandleFunc("/gcs/", unknownBucketRequest)
mux.HandleFunc("/gcs/", unknownBucketRequest)

// Serve icons and styles.
longCacheServer := func(h http.Handler) http.HandlerFunc {
Expand All @@ -217,25 +219,25 @@ func main() {
}

if o.flIcons != "" { // If user specifies custom icons path then read it at runtime
http.Handle("/icons/", longCacheServer(http.StripPrefix("/icons/", http.FileServer(http.Dir(o.flIcons)))))
mux.Handle("/icons/", longCacheServer(http.StripPrefix("/icons/", http.FileServer(http.Dir(o.flIcons)))))
} else {
http.Handle("/icons/", longCacheServer(http.FileServer(http.FS(embededStatic))))
mux.Handle("/icons/", longCacheServer(http.FileServer(http.FS(embededStatic))))
}
if o.flStyles != "" { // If user specifies custom styles path then read it at runtime
http.Handle("/styles/", longCacheServer(http.StripPrefix("/styles/", http.FileServer(http.Dir(o.flStyles)))))
mux.Handle("/styles/", longCacheServer(http.StripPrefix("/styles/", http.FileServer(http.Dir(o.flStyles)))))
} else {
http.Handle("/styles/", longCacheServer(http.FileServer(http.FS(embededStatic))))
mux.Handle("/styles/", longCacheServer(http.FileServer(http.FS(embededStatic))))
}

// Serve HTTP.
http.HandleFunc("/robots.txt", robotsRequest)
http.HandleFunc("/", otherRequest)
mux.HandleFunc("/robots.txt", robotsRequest)
mux.HandleFunc("/", otherRequest)

health := pjutil.NewHealthOnPort(o.instrumentationOptions.HealthPort)
health.ServeReady()

logrus.Infof("serving on port %d", o.flPort)
if err := http.ListenAndServe(fmt.Sprintf(":%d", o.flPort), nil); err != nil {
if err := http.ListenAndServe(fmt.Sprintf(":%d", o.flPort), mux); err != nil {
logrus.WithError(err).Fatal("couldn't start the http server")
}
}
Expand Down