From 5a011b89abc5f9d6d7b5333121659c2767263fe0 Mon Sep 17 00:00:00 2001 From: pg9182 <96569817+pg9182@users.noreply.github.com> Date: Sat, 22 Oct 2022 18:24:35 -0400 Subject: cmd/atlas: Add support for running an insecure debug server --- cmd/atlas/main.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/cmd/atlas/main.go b/cmd/atlas/main.go index 65a801b..e019b4e 100644 --- a/cmd/atlas/main.go +++ b/cmd/atlas/main.go @@ -5,10 +5,14 @@ import ( "context" "errors" "fmt" + "net/http" "os" "os/signal" + "strings" "syscall" + "net/http/pprof" + "github.com/hashicorp/go-envparse" _ "github.com/mattn/go-sqlite3" "github.com/r2northstar/atlas/pkg/atlas" @@ -49,6 +53,15 @@ func main() { } } + if dbgAddr, _ := getEnvList("INSECURE_DEBUG_SERVER_ADDR", e, os.Environ()); dbgAddr != "" { + go func() { + fmt.Fprintf(os.Stderr, "warning: running insecure debug server on %q\n", dbgAddr) + if err := runDebugServer(dbgAddr); err != nil { + fmt.Fprintf(os.Stderr, "warning: failed to start debug server: %v\n", err) + } + }() + } + var c atlas.Config if err := c.UnmarshalEnv(e, false); err != nil { fmt.Fprintf(os.Stderr, "error: parse config: %v\n", err) @@ -80,6 +93,27 @@ func main() { } } +func runDebugServer(addr string) error { + m := http.NewServeMux() + m.HandleFunc("/debug/pprof/", pprof.Index) + m.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) + m.HandleFunc("/debug/pprof/profile", pprof.Profile) + m.HandleFunc("/debug/pprof/symbol", pprof.Symbol) + m.HandleFunc("/debug/pprof/trace", pprof.Trace) + return http.ListenAndServe(addr, m) +} + +func getEnvList(k string, e ...[]string) (string, bool) { + for _, l := range e { + for _, x := range l { + if xk, xv, ok := strings.Cut(x, "="); ok && xk == k { + return xv, true + } + } + } + return "", false +} + func readEnv(name string) ([]string, error) { f, err := os.Open(name) if err != nil { -- cgit v1.2.3