-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Joe Elliott <[email protected]>
- Loading branch information
1 parent
b6d7289
commit 534f927
Showing
9 changed files
with
118 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
modules/frontend/pipeline/async_strip_headers_middleware.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package pipeline | ||
|
||
import ( | ||
"github.com/grafana/tempo/modules/frontend/combiner" | ||
) | ||
|
||
type stripHeadersWare struct { | ||
allowed map[string]struct{} | ||
next AsyncRoundTripper[combiner.PipelineResponse] | ||
} | ||
|
||
func NewStripHeadersWare(allowList []string) AsyncMiddleware[combiner.PipelineResponse] { | ||
// build allowed map | ||
allowed := make(map[string]struct{}, len(allowList)) | ||
for _, header := range allowList { | ||
allowed[header] = struct{}{} | ||
} | ||
|
||
return AsyncMiddlewareFunc[combiner.PipelineResponse](func(next AsyncRoundTripper[combiner.PipelineResponse]) AsyncRoundTripper[combiner.PipelineResponse] { | ||
return &stripHeadersWare{ | ||
next: next, | ||
allowed: allowed, | ||
} | ||
}) | ||
} | ||
|
||
func (c stripHeadersWare) RoundTrip(req Request) (Responses[combiner.PipelineResponse], error) { | ||
httpReq := req.HTTPRequest() | ||
|
||
if len(c.allowed) == 0 { | ||
clear(httpReq.Header) | ||
} else { | ||
// clear out headers not in allow list | ||
for header := range httpReq.Header { | ||
if _, ok := c.allowed[header]; !ok { | ||
delete(httpReq.Header, header) | ||
} | ||
} | ||
} | ||
|
||
return c.next.RoundTrip(req.CloneFromHTTPRequest(httpReq)) | ||
} |
55 changes: 55 additions & 0 deletions
55
modules/frontend/pipeline/async_strip_headers_middleware_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package pipeline | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/grafana/tempo/modules/frontend/combiner" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStripHeaders(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
allow []string | ||
headers map[string][]string | ||
expected http.Header | ||
}{ | ||
{ | ||
name: "empty allow list", | ||
allow: []string{}, | ||
headers: map[string][]string{"header1": {"value1"}, "header2": {"value2"}}, | ||
expected: map[string][]string{}, | ||
}, | ||
{ | ||
name: "allow list with one header", | ||
allow: []string{"header1"}, | ||
headers: map[string][]string{"header1": {"value1"}, "header2": {"value2"}}, | ||
expected: map[string][]string{"header1": {"value1"}}, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
next := AsyncRoundTripperFunc[combiner.PipelineResponse](func(req Request) (Responses[combiner.PipelineResponse], error) { | ||
actualHeaders := req.HTTPRequest().Header | ||
require.Equal(t, tc.expected, actualHeaders) | ||
|
||
return NewHTTPToAsyncResponse(&http.Response{ | ||
StatusCode: 200, | ||
Body: io.NopCloser(bytes.NewReader([]byte{})), | ||
}), nil | ||
}) | ||
|
||
stripHeaders := NewStripHeadersWare(tc.allow).Wrap(next) | ||
|
||
req, _ := http.NewRequest(http.MethodGet, "http://localhost:8080", nil) | ||
req.Header = tc.headers | ||
|
||
_, err := stripHeaders.RoundTrip(NewHTTPRequest(req)) | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters