diff --git a/.gitignore b/.gitignore index 7ea0164aa..384f10fbd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,19 +1,21 @@ -*.exe -*.exe~ -*.dll -*.so -*.o -*.dylib -.DS_Store -.idea -# Test binary, built with `go test -c` -*.test - -# Output of the go coverage tool, specifically when used with LiteIDE -*.out -test/crs -/main -vendor/ -coraza-waf -__debug_bin -seclang/crs_test.go \ No newline at end of file +*.exe +*.exe~ +*.dll +*.so +*.o +*.dylib +.DS_Store +.idea +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out +test/crs +/main +vendor/ +coraza-waf +__debug_bin +seclang/crs_test.go + +build/ \ No newline at end of file diff --git a/.golangci.yml b/.golangci.yml index f9e494413..d268d06ca 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -18,3 +18,8 @@ linters: - goimports - gofmt - gocritic +issues: + exclude-rules: + - path: magefile\.go + linters: + - deadcode diff --git a/README.md b/README.md index ff95ca513..a0a2ee3c2 100644 --- a/README.md +++ b/README.md @@ -114,9 +114,25 @@ func main() { * [Coraza Playground](https://playground.coraza.io/): sandbox rule testing web interface * [OWASP Core Ruleset](https://github.com/coreruleset/coreruleset/): Awesome rule set, compatible with Coraza +## Development + +Coraza only requires Go for development. You can run `mage.go` to issue development commands. + +See the list of commands + +```shell +go run mage.go -l +``` + +For example, to format your code before submission, run + +```shell +go run mage.go format +``` + ## Contribute -Contributions are welcome! Please refer to [CONTRIBUTING.md](https://github.com/corazawaf/coraza/blob/v2/master/CONTRIBUTING.md) for guidance. +Contributions are welcome! Please refer to [CONTRIBUTING.md](./CONTRIBUTING.md) for guidance. ## Thanks diff --git a/go.mod b/go.mod index 403badf5a..f84c41b5e 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/anuraaga/go-modsecurity v0.0.0-20220816070944-f36055ce7d5d github.com/corazawaf/libinjection-go v0.0.0-20220207031228-44e9c4250eb5 github.com/foxcpp/go-mockdns v1.0.0 + github.com/magefile/mage v1.13.0 github.com/miekg/dns v1.1.50 // indirect github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9 github.com/tidwall/gjson v1.14.2 diff --git a/go.sum b/go.sum index 8e1e3d545..a9ce39cda 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/corazawaf/libinjection-go v0.0.0-20220207031228-44e9c4250eb5 h1:Sukhx github.com/corazawaf/libinjection-go v0.0.0-20220207031228-44e9c4250eb5/go.mod h1:OP4TM7xdJ2skyXqNX1AN1wN5nNZEmJNuWbNPOItn7aw= 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/magefile/mage v1.13.0 h1:XtLJl8bcCM7EFoO8FyH8XK3t7G5hQAeK+i4tq+veT9M= +github.com/magefile/mage v1.13.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= 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= diff --git a/mage.go b/mage.go new file mode 100644 index 000000000..ab1e54e73 --- /dev/null +++ b/mage.go @@ -0,0 +1,16 @@ +//go:build ignore +// +build ignore + +// Entrypoint to mage for running without needing to install the command. +// https://magefile.org/zeroinstall/ +package main + +import ( + "os" + + "github.com/magefile/mage/mage" +) + +func main() { + os.Exit(mage.Main()) +} diff --git a/magefile.go b/magefile.go new file mode 100644 index 000000000..31fee3e6d --- /dev/null +++ b/magefile.go @@ -0,0 +1,78 @@ +//go:build mage +// +build mage + +// 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 main + +import ( + "errors" + "fmt" + "os" + + "github.com/magefile/mage/mg" + "github.com/magefile/mage/sh" +) + +var golangCILintVer = "v1.48.0" // https://github.com/golangci/golangci-lint/releases +var gosImportsVer = "v0.1.5" // https://github.com/rinchsan/gosimports/releases/tag/v0.1.5 + +var errCommitFormatting = errors.New("files not formatted, please commit formatting changes") + +// Format formats code in this repository. +func Format() error { + if err := sh.RunV("go", "mod", "tidy"); err != nil { + return err + } + return sh.RunV("go", "run", fmt.Sprintf("github.com/rinchsan/gosimports/cmd/gosimports@%s", gosImportsVer), "-w", ".") +} + +// Lint verifies code quality. +func Lint() error { + mg.SerialDeps(Format) + + if sh.Run("git", "diff", "--exit-code") != nil { + return errCommitFormatting + } + + return sh.RunV("go", "run", fmt.Sprintf("github.com/golangci/golangci-lint/cmd/golangci-lint@%s", golangCILintVer), "run") +} + +// Test runs all tests. +func Test() error { + return sh.RunV("go", "test", "./...") +} + +// Coverage runs tests with coverage and race detector enabled. +func Coverage() error { + if err := os.MkdirAll("build", 0755); err != nil { + return err + } + if err := sh.RunV("go", "test", "-race", "-coverprofile=build/coverage.txt", "-covermode=atomic", "-coverpkg=./...", "./..."); err != nil { + return err + } + + return sh.RunV("go", "tool", "cover", "-html=build/coverage.txt", "-o", "build/coverage.html") +} + +// Doc runs godoc, access at http://localhost:6060 +func Doc() error { + return sh.RunV("go", "run", "golang.org/x/tools/cmd/godoc@latest", "-http=:6060") +} + +// Check runs tests and lint. +func Check() { + mg.SerialDeps(Test, Lint) +}