Skip to content

Commit

Permalink
updating filter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Varkeychan Jacob authored and Varkeychan Jacob committed Nov 16, 2023
1 parent ce4d775 commit cdc0ea1
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 94 deletions.
8 changes: 1 addition & 7 deletions sdk/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import (

// Filter evaluates whether request should be blocked, `true` blocks the request and `false` continues it.
type Filter interface {
// EvaluateURLAndHeaders can be used to evaluate both URL and headers
EvaluateURLAndHeaders(span sdk.Span, url string, headers map[string][]string) result.FilterResult

// EvaluateBody can be used to evaluate the body content
EvaluateBody(span sdk.Span, body []byte, headers map[string][]string) result.FilterResult

// Evaluate can be used to evaluate URL, headers and body content in one call
Evaluate(span sdk.Span, url string, body []byte, headers map[string][]string) result.FilterResult
Evaluate(span sdk.Span) result.FilterResult
}
26 changes: 2 additions & 24 deletions sdk/filter/multifilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,10 @@ func NewMultiFilter(filter ...Filter) *MultiFilter {
return &MultiFilter{filters: filter}
}

// EvaluateURLAndHeaders runs URL and headers evaluation for each filter until one returns true
func (m *MultiFilter) EvaluateURLAndHeaders(span sdk.Span, url string, headers map[string][]string) result.FilterResult {
for _, f := range (*m).filters {
filterResult := f.EvaluateURLAndHeaders(span, url, headers)
if filterResult.Block {
return filterResult
}
}
return result.FilterResult{}
}

// EvaluateBody runs body evaluators for each filter until one returns true
func (m *MultiFilter) EvaluateBody(span sdk.Span, body []byte, headers map[string][]string) result.FilterResult {
for _, f := range (*m).filters {
filterResult := f.EvaluateBody(span, body, headers)
if filterResult.Block {
return filterResult
}
}
return result.FilterResult{}
}

// Evaluate runs body evaluators for each filter until one returns true
func (m *MultiFilter) Evaluate(span sdk.Span, url string, body []byte, headers map[string][]string) result.FilterResult {
func (m *MultiFilter) Evaluate(span sdk.Span) result.FilterResult {
for _, f := range (*m).filters {
filterResult := f.Evaluate(span, url, body, headers)
filterResult := f.Evaluate(span)
if filterResult.Block {
return filterResult
}
Expand Down
4 changes: 2 additions & 2 deletions sdk/filter/multifilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestMultiFilterEmpty(t *testing.T) {
assert.False(t, res.Block)
res = f.EvaluateBody(nil, nil, nil)
assert.False(t, res.Block)
res = f.Evaluate(nil, "", nil, nil)
res = f.Evaluate(nil)
assert.False(t, res.Block)
}

Expand Down Expand Up @@ -99,7 +99,7 @@ func TestMultiFilterStopsAfterTrue(t *testing.T) {
assert.Equal(t, tCase.expectedURLAndHeadersFilterResult, res.Block)
res = tCase.multiFilter.EvaluateBody(nil, nil, nil)
assert.Equal(t, tCase.expectedBodyFilterResult, res.Block)
res = tCase.multiFilter.Evaluate(nil, "", nil, nil)
res = tCase.multiFilter.Evaluate(nil)
assert.Equal(t, tCase.expectedFilterResult, res.Block)
})
}
Expand Down
12 changes: 1 addition & 11 deletions sdk/filter/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,7 @@ type NoopFilter struct{}

var _ Filter = NoopFilter{}

// EvaluateURLAndHeaders that always returns false
func (NoopFilter) EvaluateURLAndHeaders(span sdk.Span, url string, headers map[string][]string) result.FilterResult {
return result.FilterResult{}
}

// EvaluateBody that always returns false
func (NoopFilter) EvaluateBody(span sdk.Span, body []byte, headers map[string][]string) result.FilterResult {
return result.FilterResult{}
}

// Evaluate that always returns false
func (NoopFilter) Evaluate(span sdk.Span, url string, body []byte, headers map[string][]string) result.FilterResult {
func (NoopFilter) Evaluate(span sdk.Span) result.FilterResult {
return result.FilterResult{}
}
2 changes: 1 addition & 1 deletion sdk/filter/noop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func TestNoopFilter(t *testing.T) {
assert.False(t, res.Block)
res = f.EvaluateBody(nil, nil, nil)
assert.False(t, res.Block)
res = f.Evaluate(nil, "", nil, nil)
res = f.Evaluate(nil)
assert.False(t, res.Block)
}
23 changes: 7 additions & 16 deletions sdk/instrumentation/google.golang.org/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
internalconfig "github.com/hypertrace/goagent/sdk/internal/config"
"github.com/hypertrace/goagent/sdk/internal/container"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/stats"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -99,27 +98,19 @@ func wrapHandler(
len(reqBody) > 0 && err == nil {
setTruncatedBodyAttribute("request", reqBody, int(dataCaptureConfig.BodyMaxSizeBytes.Value), span)

if md, ok := metadata.FromIncomingContext(ctx); ok {
processingBody := reqBody
if int(dataCaptureConfig.BodyMaxProcessingSizeBytes.Value) < len(reqBody) {
processingBody = reqBody[:dataCaptureConfig.BodyMaxProcessingSizeBytes.Value]
}
filterResult := filter.EvaluateBody(span, processingBody, md)
if filterResult.Block {
return nil, status.Error(StatusCode(int(filterResult.ResponseStatusCode)), StatusText(int(filterResult.ResponseStatusCode)))
}
filterResult := filter.Evaluate(span)
if filterResult.Block {
return nil, status.Error(StatusCode(int(filterResult.ResponseStatusCode)), StatusText(int(filterResult.ResponseStatusCode)))
}
}

if dataCaptureConfig.RpcMetadata.Request.Value {
setAttributesFromRequestIncomingMetadata(ctx, span)

if md, ok := metadata.FromIncomingContext(ctx); ok {
// TODO: decide what should be passed as URL in GRPC
filterResult := filter.EvaluateURLAndHeaders(span, "", md)
if filterResult.Block {
return nil, status.Error(StatusCode(int(filterResult.ResponseStatusCode)), StatusText(int(filterResult.ResponseStatusCode)))
}
// TODO: decide what should be passed as URL in GRPC
filterResult := filter.Evaluate(span)
if filterResult.Block {
return nil, status.Error(StatusCode(int(filterResult.ResponseStatusCode)), StatusText(int(filterResult.ResponseStatusCode)))
}
}

Expand Down
17 changes: 2 additions & 15 deletions sdk/instrumentation/net/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package http // import "github.com/hypertrace/goagent/sdk/instrumentation/net/ht

import (
"bytes"
"encoding/base64"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -72,14 +71,13 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
host := r.Host
span.SetAttribute("http.request.header.host", host)

headers := r.Header
// Sets an attribute per each request header.
if h.dataCaptureConfig.HttpHeaders.Request.Value {
SetAttributesFromHeaders("request", NewHeaderMapAccessor(r.Header), span)
}

// run filters on headers
filterResult := h.filter.EvaluateURLAndHeaders(span, url, headers)
filterResult := h.filter.Evaluate(span)
if filterResult.Block {
w.WriteHeader(int(filterResult.ResponseStatusCode))
return
Expand All @@ -103,19 +101,8 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
isMultipartFormDataBody)
}

processingBody := body
if int(h.dataCaptureConfig.BodyMaxProcessingSizeBytes.Value) < len(body) {
processingBody = body[:h.dataCaptureConfig.BodyMaxProcessingSizeBytes.Value]
}
// if body is multipart/form-data, base64 encode it before passing it on to the filter
if isMultipartFormDataBody {
origProcessingBody := processingBody
processingBody = make([]byte, base64.RawStdEncoding.EncodedLen(len(origProcessingBody)))
base64.RawStdEncoding.Encode(processingBody, origProcessingBody)
}

// run body filters
filterResult := h.filter.EvaluateBody(span, processingBody, headers)
filterResult := h.filter.Evaluate(span)
if filterResult.Block {
w.WriteHeader(int(filterResult.ResponseStatusCode))
return
Expand Down
20 changes: 2 additions & 18 deletions sdk/internal/mock/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,10 @@ import (
)

type Filter struct {
URLAndHeadersEvaluator func(span sdk.Span, url string, headers map[string][]string) result.FilterResult
BodyEvaluator func(span sdk.Span, body []byte, headers map[string][]string) result.FilterResult
Evaluator func(span sdk.Span, url string, body []byte, headers map[string][]string) result.FilterResult
Evaluator func(span sdk.Span, url string, body []byte, headers map[string][]string) result.FilterResult
}

func (f Filter) EvaluateURLAndHeaders(span sdk.Span, url string, headers map[string][]string) result.FilterResult {
if f.URLAndHeadersEvaluator == nil {
return result.FilterResult{}
}
return f.URLAndHeadersEvaluator(span, url, headers)
}

func (f Filter) EvaluateBody(span sdk.Span, body []byte, headers map[string][]string) result.FilterResult {
if f.BodyEvaluator == nil {
return result.FilterResult{}
}
return f.BodyEvaluator(span, body, headers)
}

func (f Filter) Evaluate(span sdk.Span, url string, body []byte, headers map[string][]string) result.FilterResult {
func (f Filter) Evaluate(span sdk.Span) result.FilterResult {
if f.Evaluator == nil {
return result.FilterResult{}
}
Expand Down

0 comments on commit cdc0ea1

Please sign in to comment.