Skip to content

Commit

Permalink
feat: adds tinygo support. (#254)
Browse files Browse the repository at this point in the history
* feat: adds tinygo support.

* fix: fixes checkout.

* chore: attempts to compile test before running it.

* chore: mocks RBL for tinygo.

* tests: attempts to run single package test.

* tests: uses simpler package.

* tests: adds more packages to test.

* chore: adds support for json unmarshaler in tinygo.

* chore: back to original test command.

* chore: runs tests with tinygo test.

* chore: fixes loggers for tinygo.

* chore: fixes inspect_file as it uses timer.

* chore: aims to v3 instead.

* wip

* Exclude http package for tinygo and use tinyjson to generate marshalers for operator tests

* Fix testing on tinygo

* Finish

* Minor cleanup

* go mod tidy

* Format

* Remove top level transaction request

* Flag files only used by tinygo

* go mod tidy

* Fix

* License

* Fix

* Reduce recursion limit

* Use gjson for tests

* Remove auditlog tinyjson

* Remove tinyjson

* Cleanup

* Cleanup

* Disable tinygo 1.19 test

* gofmt

* Workflow

* Lint

* Fix

* Restore support for string array test data

Co-authored-by: Anuraag Agrawal <[email protected]>
  • Loading branch information
jcchavezs and anuraaga authored Aug 16, 2022
1 parent 828dd45 commit a867605
Show file tree
Hide file tree
Showing 47 changed files with 581 additions and 286 deletions.
34 changes: 20 additions & 14 deletions .github/workflows/tinygo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,33 @@ on:
- "**/*.md"
- "LICENSE"
pull_request:
branches:
- v3/dev
- v3/main
paths-ignore:
- "**/*.md"
- "LICENSE"

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 ./...
2 changes: 1 addition & 1 deletion collection/collection_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// 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
// 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,
Expand Down
2 changes: 1 addition & 1 deletion collection/collection_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// 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
// 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,
Expand Down
2 changes: 1 addition & 1 deletion collection/collection_size_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// 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
// 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,
Expand Down
2 changes: 1 addition & 1 deletion collection/collection_translation_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// 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
// 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,
Expand Down
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
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
36 changes: 19 additions & 17 deletions loggers/formats.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
/*
Package loggers implements a set of log formatters and writers
for audit logging.
//go:build !tinygo
// +build !tinygo

The following log formats are supported:
- JSON
- Coraza
- Native
The following log writers are supported:
- Serial
- Concurrent
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");
Expand All @@ -29,6 +14,23 @@ RegisterFormatter functions.
// 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.
//
// The following log formats are supported:
//
// - JSON
// - Coraza
// - Native
//
// The following log writers are supported:
//
// - Serial
// - Concurrent
//
// More writers and formatters can be registered using the RegisterWriter and
// RegisterFormatter functions.
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
Loading

0 comments on commit a867605

Please sign in to comment.