-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into broken-links
- Loading branch information
Showing
38 changed files
with
314 additions
and
107 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package pipeline | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/grafana/tempo/modules/frontend/combiner" | ||
) | ||
|
||
type urlDenylistWare struct { | ||
denyList []*regexp.Regexp | ||
next AsyncRoundTripper[combiner.PipelineResponse] | ||
} | ||
|
||
func NewURLDenyListWare(denyList []string) AsyncMiddleware[combiner.PipelineResponse] { | ||
compiledDenylist := make([]*regexp.Regexp, 0) | ||
for _, v := range denyList { | ||
r, err := regexp.Compile(v) | ||
if err == nil { | ||
compiledDenylist = append(compiledDenylist, r) | ||
} else { | ||
panic(fmt.Sprintf("error compiling query frontend deny list regex: %s", err)) | ||
} | ||
} | ||
|
||
return AsyncMiddlewareFunc[combiner.PipelineResponse](func(next AsyncRoundTripper[combiner.PipelineResponse]) AsyncRoundTripper[combiner.PipelineResponse] { | ||
return &urlDenylistWare{ | ||
next: next, | ||
denyList: compiledDenylist, | ||
} | ||
}) | ||
} | ||
|
||
func (c urlDenylistWare) RoundTrip(req Request) (Responses[combiner.PipelineResponse], error) { | ||
if len(c.denyList) != 0 { | ||
err := c.validateRequest(req.HTTPRequest().URL.String()) | ||
if err != nil { | ||
return NewBadRequest(err), nil | ||
} | ||
} | ||
|
||
return c.next.RoundTrip(req) | ||
} | ||
|
||
func (c urlDenylistWare) validateRequest(url string) error { | ||
for _, v := range c.denyList { | ||
if v.MatchString(url) { | ||
return fmt.Errorf("Invalid request %s. This query has been identified as one that destabilizes our system. Contact your system administrator for more information", url) | ||
} | ||
} | ||
return nil | ||
} |
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" | ||
"context" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/grafana/tempo/modules/frontend/combiner" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var next = AsyncRoundTripperFunc[combiner.PipelineResponse](func(_ Request) (Responses[combiner.PipelineResponse], error) { | ||
return NewHTTPToAsyncResponse(&http.Response{ | ||
StatusCode: 200, | ||
Body: io.NopCloser(bytes.NewReader([]byte{})), | ||
}), nil | ||
}) | ||
|
||
func TestURLBlackListMiddlewareForEmptyBlackList(t *testing.T) { | ||
regexes := []string{} | ||
roundTrip := NewURLDenyListWare(regexes).Wrap(next) | ||
statusCode := DoRequest(t, "http://localhost:8080/api/v2/traces/123345", roundTrip) | ||
assert.Equal(t, 200, statusCode) | ||
} | ||
|
||
func TestURLBlackListMiddlewarePanicsOnSyntacticallyIncorrectRegex(t *testing.T) { | ||
regexes := []string{"qr/^(.*\\.traces\\/[a-f0-9]{32}$/"} | ||
assert.Panics(t, func() { | ||
NewURLDenyListWare(regexes).Wrap(next) | ||
}) | ||
} | ||
|
||
func TestURLBlackListMiddleware(t *testing.T) { | ||
regexes := []string{ | ||
"^.*v2.*", | ||
} | ||
roundTrip := NewURLDenyListWare(regexes).Wrap(next) | ||
statusCode := DoRequest(t, "http://localhost:9000?param1=a¶m2=b", roundTrip) | ||
assert.Equal(t, 200, statusCode) | ||
|
||
// Blacklisted url | ||
statusCode = DoRequest(t, "http://localhost:8080/api/v2/traces/123345", roundTrip) | ||
assert.Equal(t, 400, statusCode) | ||
} | ||
|
||
func DoRequest(t *testing.T, url string, rt AsyncRoundTripper[combiner.PipelineResponse]) int { | ||
req, _ := http.NewRequest(http.MethodGet, url, nil) | ||
resp, _ := rt.RoundTrip(NewHTTPRequest(req)) | ||
httpResponse, _, err := resp.Next(context.Background()) | ||
require.NoError(t, err) | ||
return httpResponse.HTTPResponse().StatusCode | ||
} |
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
Oops, something went wrong.