forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_deny_list_middleware.go
More file actions
52 lines (44 loc) · 1.38 KB
/
async_deny_list_middleware.go
File metadata and controls
52 lines (44 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
}