-
Notifications
You must be signed in to change notification settings - Fork 80
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
Add headerbp integration #669
Changes from 7 commits
1bc1961
c52cdf1
496bc9b
75e5b96
7bdf051
6342673
a752b34
94c324c
49741ba
4200301
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -15,6 +15,7 @@ import ( | |||||||||||||||||||||||||||
"github.com/prometheus/client_golang/prometheus" | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
"github.com/reddit/baseplate.go/breakerbp" | ||||||||||||||||||||||||||||
"github.com/reddit/baseplate.go/internal/headerbp" | ||||||||||||||||||||||||||||
//lint:ignore SA1019 This library is internal only, not actually deprecated | ||||||||||||||||||||||||||||
"github.com/reddit/baseplate.go/internalv2compat" | ||||||||||||||||||||||||||||
"github.com/reddit/baseplate.go/retrybp" | ||||||||||||||||||||||||||||
|
@@ -88,6 +89,11 @@ func NewClient(config ClientConfig, middleware ...ClientMiddleware) (*http.Clien | |||||||||||||||||||||||||||
if config.CircuitBreaker != nil { | ||||||||||||||||||||||||||||
defaults = append([]ClientMiddleware{CircuitBreaker(*config.CircuitBreaker)}, defaults...) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
// only add the middleware to forward baseplate headers if the client is for internal calls | ||||||||||||||||||||||||||||
if config.InternalOnly { | ||||||||||||||||||||||||||||
defaults = append(defaults, ClientHeaderBPMiddleware(config.Slug)) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
middleware = append(middleware, defaults...) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
return &http.Client{ | ||||||||||||||||||||||||||||
|
@@ -349,3 +355,26 @@ func PrometheusClientMetrics(serverSlug string) ClientMiddleware { | |||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
// ClientHeaderBPMiddleware is a middleware that forwards baseplate headers from the context to the outgoing request. | ||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||
// If it detects any new baseplate headers set on the request, it will reject the request and return an error. | ||||||||||||||||||||||||||||
func ClientHeaderBPMiddleware(client string) ClientMiddleware { | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
🔕 maybe best spelled out since we have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||||
return func(next http.RoundTripper) http.RoundTripper { | ||||||||||||||||||||||||||||
return roundTripperFunc(func(req *http.Request) (*http.Response, error) { | ||||||||||||||||||||||||||||
for k := range req.Header { | ||||||||||||||||||||||||||||
if err := headerbp.CheckClientHeader(k, | ||||||||||||||||||||||||||||
headerbp.WithHTTPClient("", client, ""), | ||||||||||||||||||||||||||||
); err != nil { | ||||||||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+365
to
+370
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
🔕 I find this slightly more readable 🙂 |
||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
headerbp.SetOutgoingHeaders( | ||||||||||||||||||||||||||||
req.Context(), | ||||||||||||||||||||||||||||
headerbp.WithHTTPClient("", client, ""), | ||||||||||||||||||||||||||||
headerbp.WithHeaderSetter(req.Header.Set), | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
return next.RoundTrip(req) | ||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ import ( | |
|
||
"github.com/reddit/baseplate.go/ecinterface" | ||
"github.com/reddit/baseplate.go/errorsbp" | ||
"github.com/reddit/baseplate.go/internal/headerbp" | ||
//lint:ignore SA1019 This library is internal only, not actually deprecated | ||
"github.com/reddit/baseplate.go/internalv2compat" | ||
"github.com/reddit/baseplate.go/log" | ||
|
@@ -517,3 +518,48 @@ func (rr *responseRecorder) WriteHeader(code int) { | |
rr.ResponseWriter.WriteHeader(code) | ||
rr.responseCode = code | ||
} | ||
|
||
type untrustedHeadersKey struct{} | ||
|
||
func setUntrustedHeaders(ctx context.Context, h map[string]string) context.Context { | ||
return context.WithValue(ctx, untrustedHeadersKey{}, h) | ||
} | ||
|
||
func GetUntrustedBaseplateHeaders(ctx context.Context) (map[string]string, bool) { | ||
h, ok := ctx.Value(untrustedHeadersKey{}).(map[string]string) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a big fan of always checking if the assertion was successful, though I got the impression that if we tightly control it, it's okay to not check to make the API more user-friendly 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that really does anything here though |
||
return h, ok | ||
} | ||
|
||
// ServerHeaderBPMiddleware is a middleware that extracts baseplate headers from the incoming request and adds them to the context. | ||
// | ||
// If the request is flagged as untrusted, it will remove the baseplate headers from the request and add them to the | ||
// context. These can be retrieved using GetUntrustedBaseplateHeaders. | ||
func ServerHeaderBPMiddleware(service string) Middleware { | ||
return func(name string, next HandlerFunc) HandlerFunc { | ||
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) (err error) { | ||
if r.Header.Get(headerbp.IsUntrustedRequestHeaderCanonicalHTTP) != "" { | ||
untrusted := make(map[string]string) | ||
for k, v := range r.Header { | ||
if headerbp.IsBaseplateHeader(k) { | ||
if len(v) > 0 { | ||
untrusted[strings.ToLower(k)] = v[0] | ||
} | ||
r.Header.Del(k) | ||
} | ||
} | ||
ctx = setUntrustedHeaders(ctx, untrusted) | ||
return next(ctx, w, r) | ||
} | ||
headers := headerbp.NewIncomingHeaders( | ||
headerbp.WithHTTPService(service, name), | ||
) | ||
for k, v := range r.Header { | ||
if len(v) > 0 { | ||
headers.RecordHeader(k, v[0]) | ||
} | ||
} | ||
ctx = headers.SetOnContext(ctx) | ||
return next(ctx, w, r.WithContext(ctx)) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we don't have a distinct "internal vs non-internal" client type, I was thinking it would probably be good to have some sort of flag that you have to manually set to have HTTP clients automatically propagate headers?