Skip to content

Commit

Permalink
resolve gosec suggestion, use crypo package for randomness (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Sirianni authored Jul 8, 2021
1 parent 2113838 commit 7d4488e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
23 changes: 19 additions & 4 deletions operator/builtin/transformer/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package filter

import (
"context"
"crypto/rand"
"fmt"
"math/rand"
"math/big"

"github.com/antonmedv/expr"
"github.com/antonmedv/expr/vm"
Expand All @@ -17,6 +18,10 @@ func init() {
operator.Register("filter", func() operator.Builder { return NewFilterOperatorConfig("") })
}

var upperBound = big.NewInt(1000)

var randInt = rand.Int // allow override for testing

// NewFilterOperatorConfig creates a filter operator config with default values
func NewFilterOperatorConfig(operatorID string) *FilterOperatorConfig {
return &FilterOperatorConfig{
Expand Down Expand Up @@ -51,7 +56,7 @@ func (c FilterOperatorConfig) Build(context operator.BuildContext) ([]operator.O
filterOperator := &FilterOperator{
TransformerOperator: transformer,
expression: compiledExpression,
dropRatio: c.DropRatio,
dropCutoff: big.NewInt(int64(c.DropRatio * 1000)),
}

return []operator.Operator{filterOperator}, nil
Expand All @@ -61,7 +66,7 @@ func (c FilterOperatorConfig) Build(context operator.BuildContext) ([]operator.O
type FilterOperator struct {
helper.TransformerOperator
expression *vm.Program
dropRatio float64
dropCutoff *big.Int // [0..1000)
}

// Process will drop incoming entries that match the filter expression
Expand All @@ -81,7 +86,17 @@ func (f *FilterOperator) Process(ctx context.Context, entry *entry.Entry) error
return nil
}

if !filtered || rand.Float64() > f.dropRatio {
if !filtered {
f.Write(ctx, entry)
return nil
}

i, err := randInt(rand.Reader, upperBound)
if err != nil {
return err
}

if i.Cmp(f.dropCutoff) >= 0 {
f.Write(ctx, entry)
}

Expand Down
14 changes: 11 additions & 3 deletions operator/builtin/transformer/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package filter

import (
"context"
"math/rand"
"io"
"math/big"
"os"
"testing"

Expand Down Expand Up @@ -141,14 +142,21 @@ func TestFilterDropRatio(t *testing.T) {
},
}

nextIndex := 0
randos := []int64{250, 750}
randInt = func(io.Reader, *big.Int) (*big.Int, error) {
defer func() {
nextIndex = (nextIndex + 1) % len(randos)
}()
return big.NewInt(randos[nextIndex]), nil
}

for i := 1; i < 11; i++ {
rand.Seed(1)
err = filterOperator.Process(context.Background(), testEntry)
require.NoError(t, err)
}

for i := 1; i < 11; i++ {
rand.Seed(2)
err = filterOperator.Process(context.Background(), testEntry)
require.NoError(t, err)
}
Expand Down

0 comments on commit 7d4488e

Please sign in to comment.