Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds tinygo support. #254

Merged
merged 42 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
4fa885f
feat: adds tinygo support.
jcchavezs May 27, 2022
6b01598
fix: fixes checkout.
jcchavezs May 30, 2022
cc8655a
chore: attempts to compile test before running it.
jcchavezs May 30, 2022
8b84690
chore: mocks RBL for tinygo.
jcchavezs May 30, 2022
d2d2665
tests: attempts to run single package test.
jcchavezs May 30, 2022
4dd7e56
tests: uses simpler package.
jcchavezs May 30, 2022
b0858ed
tests: adds more packages to test.
jcchavezs May 30, 2022
0b81a06
chore: adds support for json unmarshaler in tinygo.
jcchavezs May 30, 2022
dc3a922
chore: back to original test command.
jcchavezs May 30, 2022
12844fe
chore: runs tests with tinygo test.
jcchavezs Jun 22, 2022
c93a620
chore: fixes loggers for tinygo.
jcchavezs Jun 22, 2022
d244844
chore: fixes inspect_file as it uses timer.
jcchavezs Jun 23, 2022
bdaa2a0
chore: aims to v3 instead.
jcchavezs Jul 8, 2022
77ed679
wip
jcchavezs Jul 13, 2022
0fb8381
Exclude http package for tinygo and use tinyjson to generate marshale…
anuraaga Aug 1, 2022
9057514
Fix testing on tinygo
anuraaga Aug 2, 2022
cc74461
Finish
anuraaga Aug 2, 2022
802128a
Minor cleanup
anuraaga Aug 2, 2022
84d5d9d
go mod tidy
anuraaga Aug 2, 2022
bb1ab53
Format
anuraaga Aug 2, 2022
c714838
Remove top level transaction request
anuraaga Aug 2, 2022
709d6f1
Flag files only used by tinygo
anuraaga Aug 2, 2022
867e7d2
Merge branch 'v3/dev' of github.com:corazawaf/coraza into tinygo_supp…
anuraaga Aug 4, 2022
aebf7fc
go mod tidy
anuraaga Aug 4, 2022
5d9350f
Merge pull request #1 from anuraaga/tinygo_support_rag
anuraaga Aug 4, 2022
c26d562
Fix
anuraaga Aug 4, 2022
d0e2644
License
anuraaga Aug 4, 2022
f6f346b
Fix
anuraaga Aug 4, 2022
c1cdc71
Reduce recursion limit
anuraaga Aug 4, 2022
a045056
Merge branch 'v3/dev' of github.com:corazawaf/coraza into tinygo_support
anuraaga Aug 11, 2022
8de7164
Use gjson for tests
anuraaga Aug 11, 2022
25bb292
Remove auditlog tinyjson
anuraaga Aug 11, 2022
5c570a6
Remove tinyjson
anuraaga Aug 11, 2022
0caa057
Cleanup
anuraaga Aug 11, 2022
85588e8
Cleanup
anuraaga Aug 11, 2022
7c4c423
Disable tinygo 1.19 test
anuraaga Aug 11, 2022
768febf
Merge branch 'v3/dev' of github.com:corazawaf/coraza into tinygo_support
anuraaga Aug 16, 2022
cf85b48
gofmt
anuraaga Aug 16, 2022
00471ff
Workflow
anuraaga Aug 16, 2022
8693b61
Lint
anuraaga Aug 16, 2022
2893e9b
Fix
anuraaga Aug 16, 2022
3c11e77
Restore support for string array test data
anuraaga Aug 16, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions .github/workflows/tinygo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@ name: Tinygo tests

on:
push:
branches:
- master
paths-ignore:
- "**/*.md"
- "LICENSE"
pull_request:

jobs:
test:
strategy:
matrix:
go-version: [1.18.x, 1.19.x]
# TODO(anuraaga): Add 1.19.x after TinyGo adds support for it.
go-version: [1.18.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
container:
image: tinygo/tinygo:0.24.0
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
with:
lfs: true
fetch-depth: 0 #for better blame info
- name: Download vendored dependencies
run: go mod vendor
- name: Tests and coverage
run: go test -v --tags=tinygo ./...
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

- name: Tests
run: tinygo test ./...
5 changes: 4 additions & 1 deletion examples/http-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ require github.com/corazawaf/coraza/v3 v3.0.0-00010101000000-000000000000
require (
github.com/cloudflare/ahocorasick v0.0.0-20210425175752-730270c3e184 // indirect
github.com/corazawaf/libinjection-go v0.0.0-20220207031228-44e9c4250eb5 // indirect
github.com/tidwall/gjson v1.14.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
)

replace github.com/corazawaf/coraza/v3 => ../..
replace github.com/corazawaf/coraza/v3 => ../..
6 changes: 6 additions & 0 deletions examples/http-server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/miekg/dns v1.1.25 h1:dFwPR6SfLtrSwgDcIq2bcU/gVutB4sNApq2HBdqcakg=
github.com/miekg/dns v1.1.25/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
Expand Down
3 changes: 2 additions & 1 deletion examples/http-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"

"github.com/corazawaf/coraza/v3"
txhttp "github.com/corazawaf/coraza/v3/http"
"github.com/corazawaf/coraza/v3/seclang"
"github.com/corazawaf/coraza/v3/types"
)
Expand Down Expand Up @@ -62,7 +63,7 @@ func corazaRequestHandler(h http.Handler) http.Handler {
ProcessRequestHeaders and ProcessRequestBody.
It fails if any of these functions returns an error and it stops on interruption.
*/
if it, err := tx.ProcessRequest(r); err != nil {
if it, err := txhttp.ProcessRequest(tx, r); err != nil {
showCorazaError(w, 500, err.Error())
return
} else if it != nil {
Expand Down
9 changes: 5 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ github.com/corazawaf/libinjection-go v0.0.0-20220207031228-44e9c4250eb5/go.mod h
github.com/foxcpp/go-mockdns v1.0.0 h1:7jBqxd3WDWwi/6WhDvacvH1XsN3rOLXyHM1uhvIx6FI=
github.com/foxcpp/go-mockdns v1.0.0/go.mod h1:lgRN6+KxQBawyIghpnl5CezHFGS9VLzvtVlwxvzXTQ4=
github.com/miekg/dns v1.1.25/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/miekg/dns v1.1.25/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA=
github.com/miekg/dns v1.1.50/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9 h1:lL+y4Xv20pVlCGyLzNHRC0I0rIHhIL1lTvHizoS/dU8=
github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9/go.mod h1:EHPiTAKtiFmrMldLUNswFwfZ2eJIYBHktdaUTZxYWRw=
github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA=
github.com/miekg/dns v1.1.50/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9 h1:lL+y4Xv20pVlCGyLzNHRC0I0rIHhIL1lTvHizoS/dU8=
github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9/go.mod h1:EHPiTAKtiFmrMldLUNswFwfZ2eJIYBHktdaUTZxYWRw=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
16 changes: 16 additions & 0 deletions http/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package http allows populating a coraza transaction with information from an HTTP Request.
package http
4 changes: 4 additions & 0 deletions http/http.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// tinygo does not support net.http so this package is not needed for it
//go:build !tinygo
// +build !tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
4 changes: 4 additions & 0 deletions http/http_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// tinygo does not support net.http so this package is not needed for it
//go:build !tinygo
// +build !tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
3 changes: 3 additions & 0 deletions loggers/concurrent_writer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !tinygo
// +build !tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
3 changes: 3 additions & 0 deletions loggers/concurrent_writer_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !tinygo
// +build !tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
31 changes: 18 additions & 13 deletions loggers/formats.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
//go:build !tinygo
// +build !tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*
Package loggers implements a set of log formatters and writers
for audit logging.
Expand All @@ -16,19 +33,7 @@ The following log writers are supported:
More writers and formatters can be registered using the RegisterWriter and
RegisterFormatter functions.
*/
// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package loggers

import (
Expand Down
3 changes: 3 additions & 0 deletions loggers/formats_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !tinygo
// +build !tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
31 changes: 31 additions & 0 deletions loggers/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//go:build !tinygo
// +build !tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package loggers

func init() {
RegisterLogWriter("concurrent", func() LogWriter {
return &concurrentWriter{}
})
RegisterLogWriter("serial", func() LogWriter {
return &serialWriter{}
})

RegisterLogFormatter("json", jsonFormatter)
RegisterLogFormatter("jsonlegacy", legacyJSONFormatter)
RegisterLogFormatter("native", nativeFormatter)
}
31 changes: 31 additions & 0 deletions loggers/init_tinygo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//go:build tinygo
// +build tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package loggers

func init() {
RegisterLogWriter("concurrent", func() LogWriter {
return noopWriter{}
})
RegisterLogWriter("serial", func() LogWriter {
return noopWriter{}
})

RegisterLogFormatter("json", noopFormater)
RegisterLogFormatter("jsonlegacy", noopFormater)
RegisterLogFormatter("native", noopFormater)
}
13 changes: 0 additions & 13 deletions loggers/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,3 @@ func GetLogFormatter(name string) (LogFormatter, error) {
}
return formatter, nil
}

func init() {
RegisterLogWriter("concurrent", func() LogWriter {
return &concurrentWriter{}
})
RegisterLogWriter("serial", func() LogWriter {
return &serialWriter{}
})

RegisterLogFormatter("json", jsonFormatter)
RegisterLogFormatter("jsonlegacy", legacyJSONFormatter)
RegisterLogFormatter("native", nativeFormatter)
}
3 changes: 0 additions & 3 deletions loggers/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,3 @@ func TestDefaultWriters(t *testing.T) {
}
}
}
func TestWriterPlugins(t *testing.T) {

}
23 changes: 23 additions & 0 deletions loggers/noop_formater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Currently only used with TinyGo
//go:build tinygo
// +build tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package loggers

func noopFormater(al *AuditLog) ([]byte, error) {
return nil, nil
}
32 changes: 32 additions & 0 deletions loggers/noop_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Currently only used with TinyGo
//go:build tinygo
// +build tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package loggers

import (
"github.com/corazawaf/coraza/v3/types"
)

// noopWriter is used to store logs in a single file
type noopWriter struct{}

func (noopWriter) Init(types.Config) error { return nil }
func (noopWriter) Write(*AuditLog) error { return nil }
func (noopWriter) Close() error { return nil }

var _ LogWriter = (*noopWriter)(nil)
3 changes: 3 additions & 0 deletions loggers/serial_writer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !tinygo
// +build !tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
3 changes: 3 additions & 0 deletions loggers/serial_writer_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !tinygo
// +build !tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
3 changes: 1 addition & 2 deletions operators/detect_sqli.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import (
"github.com/corazawaf/libinjection-go"
)

type detectSQLi struct {
}
type detectSQLi struct{}

func (o *detectSQLi) Init(options coraza.RuleOperatorOptions) error {
return nil
Expand Down
7 changes: 4 additions & 3 deletions operators/inspect_file.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !tinygo
// +build !tinygo

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -27,9 +30,7 @@ type inspectFile struct {
}

func (o *inspectFile) Init(options coraza.RuleOperatorOptions) error {
data := options.Arguments

o.path = data
o.path = options.Arguments
return nil
}

Expand Down
Loading