From ed202f73ddbaad81fefbc3aef53eba88e4488014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81oskot?= Date: Sat, 21 Sep 2024 20:52:46 +0200 Subject: [PATCH] Discover HTTPBIN_ env vars at process startup, not per request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mateusz Łoskot --- httpbin/cmd/cmd.go | 10 ++++++++++ httpbin/handlers.go | 12 +----------- httpbin/httpbin.go | 4 ++++ httpbin/options.go | 8 ++++++++ 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/httpbin/cmd/cmd.go b/httpbin/cmd/cmd.go index 08a98465..9fa3a10e 100644 --- a/httpbin/cmd/cmd.go +++ b/httpbin/cmd/cmd.go @@ -74,10 +74,20 @@ func mainImpl(args []string, getEnv func(string) string, getHostname func() (str logger = slog.New(handler) } + env := make(map[string]string) + for _, e := range os.Environ() { + v := strings.SplitN(e, "=", 2) + if !strings.HasPrefix(v[0], "HTTPBIN_") { + continue + } + env[v[0]] = v[1] + } + opts := []httpbin.OptionFunc{ httpbin.WithMaxBodySize(cfg.MaxBodySize), httpbin.WithMaxDuration(cfg.MaxDuration), httpbin.WithObserver(httpbin.StdLogObserver(logger)), + httpbin.WithEnv(env), httpbin.WithExcludeHeaders(cfg.ExcludeHeaders), } if cfg.Prefix != "" { diff --git a/httpbin/handlers.go b/httpbin/handlers.go index 8342f9df..fbd3d46b 100644 --- a/httpbin/handlers.go +++ b/httpbin/handlers.go @@ -11,7 +11,6 @@ import ( "net/http" "net/http/httputil" "net/url" - "os" "strconv" "strings" "time" @@ -38,17 +37,8 @@ func (h *HTTPBin) Index(w http.ResponseWriter, r *http.Request) { // Env - returns environment variables with HTTPBIN_ prefix, if any pre-configured by operator func (h *HTTPBin) Env(w http.ResponseWriter, _ *http.Request) { - variables := make(map[string]string) - for _, e := range os.Environ() { - v := strings.SplitN(e, "=", 2) - if !strings.HasPrefix(v[0], "HTTPBIN_") { - continue - } - variables[v[0]] = v[1] - } - writeJSON(http.StatusOK, w, &envResponse{ - Env: variables, + Env: h.env, }) } diff --git a/httpbin/httpbin.go b/httpbin/httpbin.go index 3dfc99f7..7c60924a 100644 --- a/httpbin/httpbin.go +++ b/httpbin/httpbin.go @@ -57,6 +57,10 @@ type HTTPBin struct { // Set of hosts to which the /redirect-to endpoint will allow redirects AllowedRedirectDomains map[string]struct{} + // The operator-controlled environment variables filtered from + // the process environment, based on named HTTPBIN_ prefix. + env map[string]string + // Pre-computed error message for the /redirect-to endpoint, based on // -allowed-redirect-domains/ALLOWED_REDIRECT_DOMAINS forbiddenRedirectError string diff --git a/httpbin/options.go b/httpbin/options.go index e697a1c1..15f60904 100644 --- a/httpbin/options.go +++ b/httpbin/options.go @@ -46,6 +46,14 @@ func WithObserver(o Observer) OptionFunc { } } +// WithEnv sets the HTTPBIN_-prefixed environment variables reported +// by the /env endpoint. +func WithEnv(env map[string]string) OptionFunc { + return func(h *HTTPBin) { + h.env = env + } +} + // WithExcludeHeaders sets the headers to exclude in outgoing responses, to // prevent possible information leakage. func WithExcludeHeaders(excludeHeaders string) OptionFunc {