Skip to content

Commit

Permalink
#12 add mockery, add more code to the builder and compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
thegodenage committed Mar 16, 2024
1 parent 81e97d2 commit 3d8f5af
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 7 deletions.
11 changes: 11 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
quiet: False
keeptree: True
disable-version-string: True
with-expecter: True
mockname: "{{.InterfaceName}}"
filename: "{{.MockName}}.go"
outpkg: mocks
packages:
waffle/internal/rule:
interfaces:
Builder:
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ we are missing a real modular and open source **Web Application Firewall** that
+ golangci-lint
+ make (if windows, try using chocolatey)
+ openssl (if windows, try using git bash)
+ GRPC tools
+ [mockery](https://vektra.github.io/mockery/latest/installation/)

1. Create certificates and FS embed go file provider `make certs_windows`
2. Execute `docker compose up -d` to create needed infrastructure
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ require (
github.com/goccy/go-yaml v1.11.3
github.com/google/uuid v1.6.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/stretchr/testify v1.9.0
nhooyr.io/websocket v1.8.10
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/Netflix/go-env v0.0.0-20220526054621-78278af1949d h1:wvStE9wLpws31NiW
github.com/Netflix/go-env v0.0.0-20220526054621-78278af1949d/go.mod h1:9XMFaCeRyW7fC9XJOWQ+NdAv8VLG7ys7l3x4ozEGLUQ=
github.com/corazawaf/libinjection-go v0.1.3 h1:PUplAYho1BBl0tIVbhDsNRuVGIeUYSiCEc9oQpb2rJU=
github.com/corazawaf/libinjection-go v0.1.3/go.mod h1:OP4TM7xdJ2skyXqNX1AN1wN5nNZEmJNuWbNPOItn7aw=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
Expand All @@ -27,6 +29,12 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -35,5 +43,9 @@ golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q=
nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
27 changes: 21 additions & 6 deletions internal/rule/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,39 @@ type Compiler interface {
}

type CustomCompiler struct {
builder Builder
}

var _ Compiler = (*CustomCompiler)(nil)

func (c *CustomCompiler) Compile(name, value string) (*Predicate, error) {
if len(name) == 0 {
return nil, ErrEmptyPredicateName
}

if len(value) == 0 {
return nil, ErrEmptyPredicateValue
if err := validateInput(name, value); err != nil {
return nil, fmt.Errorf("validate name and value: %w", err)
}

variable, expression, err := getVariableAndLogicalExpression(strings.TrimSpace(value))
if err != nil {
return nil, fmt.Errorf("get variable and logical expression for predicate: %w", err)
}

pred, err := c.builder.Build(name, variable, expression)
if err != nil {
return nil, fmt.Errorf("build predicate: %w", err)
}

return pred, nil
}

func validateInput(name string, value string) error {
if len(name) == 0 {
return ErrEmptyPredicateName
}

if len(value) == 0 {
return ErrEmptyPredicateValue
}

return nil
}

func getVariableAndLogicalExpression(value string) (string, string, error) {
Expand Down
55 changes: 55 additions & 0 deletions internal/rule/compiler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package rule

import (
"reflect"
"testing"
)

func TestCustomCompiler_Compile(t *testing.T) {
type fields struct {
builder Builder
}
type args struct {
name string
value string
}
tests := []struct {
name string
fields fields
args args
want *Predicate
wantErr bool
}{
{
name: "empty name, error returned",
args: args{
name: "",
value: "p => p",
},
wantErr: true,
},
{
name: "empty value, error returned",
args: args{
name: "name",
value: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &CustomCompiler{
builder: tt.fields.builder,
}
got, err := c.Compile(tt.args.name, tt.args.value)
if (err != nil) != tt.wantErr {
t.Errorf("Compile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Compile() got = %v, want %v", got, tt.want)
}
})
}
}
18 changes: 18 additions & 0 deletions internal/rule/predicate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
package rule

type PredicateLogicalExpression func(payload []byte) (bool, error)

type Predicate struct {
Name string
LogicalExpression PredicateLogicalExpression
}

type Builder interface {
Build(name, variable, expression string) (*Predicate, error)
}

type PredicateBuilder struct {
}

var _ Builder = (*PredicateBuilder)(nil)

func (p *PredicateBuilder) Build(name, variable, expression string) (*Predicate, error) {
//TODO implement me
panic("implement me")
}
3 changes: 3 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ certs:
openssl req -new -sha256 -key ./cmd/proxy/.cert/server.key -out ./cmd/proxy/.cert/server.csr
openssl x509 -req -in ./cmd/proxy/.cert/server.csr -CA ./cmd/proxy/.cert/ca.crt -CAkey ./cmd/proxy/.cert/ca.key -CAcreateserial -out ./cmd/proxy/.cert/server.crt -days 3650 -sha256
openssl x509 -in ./cmd/proxy/.cert/server.crt -text -noout

mocks:
mockery
96 changes: 96 additions & 0 deletions mocks/waffle/internal/rule/Builder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3d8f5af

Please sign in to comment.