Skip to content

Commit

Permalink
Discover HTTPBIN_ env vars at process startup, not per request
Browse files Browse the repository at this point in the history
Signed-off-by: Mateusz Łoskot <[email protected]>
  • Loading branch information
mloskot committed Sep 21, 2024
1 parent 9ca4fa9 commit ed202f7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
10 changes: 10 additions & 0 deletions httpbin/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Check warning on line 83 in httpbin/cmd/cmd.go

View check run for this annotation

Codecov / codecov/patch

httpbin/cmd/cmd.go#L83

Added line #L83 was not covered by tests
}

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 != "" {
Expand Down
12 changes: 1 addition & 11 deletions httpbin/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"strings"
"time"
Expand All @@ -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,
})
}

Expand Down
4 changes: 4 additions & 0 deletions httpbin/httpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions httpbin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 53 in httpbin/options.go

View check run for this annotation

Codecov / codecov/patch

httpbin/options.go#L51-L53

Added lines #L51 - L53 were not covered by tests
}
}

// WithExcludeHeaders sets the headers to exclude in outgoing responses, to
// prevent possible information leakage.
func WithExcludeHeaders(excludeHeaders string) OptionFunc {
Expand Down

0 comments on commit ed202f7

Please sign in to comment.