Skip to content

Commit

Permalink
refactor: set go 1.21 as minimal supported version (#104)
Browse files Browse the repository at this point in the history
- [x] Upgrade go mod to 1.21
- [x] Remove go 1.20 from CI
- [x] use `errors.Join` when necessary
- [x] use builtin `max` when necessary

---------

Signed-off-by: Eliott Bouhana <[email protected]>
  • Loading branch information
eliottness authored Jul 4, 2024
1 parent 266e3a0 commit 48f7206
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/_test_bare_metal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
fail-fast: false
matrix:
runs-on: [ macos-14, macos-13, macos-12, ubuntu-22.04, ubuntu-20.04, windows-latest, arm-4core-linux ]
go-version: [ '1.22', '1.21', '1.20' ]
go-version: [ '1.22', '1.21' ]
include:
# Test with DD_APPSEC_WAF_LOG_LEVEL (only latest go version)
- go-version: '1.22'
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/_test_containerized.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- golang:{0}-buster
# RPM-based image
- amazonlinux:2 # pretty popular on AWS workloads
go-version: [ "1.23-rc", "1.22", "1.21", "1.20" ]
go-version: [ "1.23-rc", "1.22", "1.21" ]
include:
# Test with DD_APPSEC_WAF_LOG_LEVEL (only latest go, without any particular tag)
- go-version: '1.23-rc'
Expand All @@ -34,8 +34,6 @@ jobs:
# The amazonlinux:2 variant is only relevant for the default go version yum ships (currently 1.22)
- go-version: '1.21'
image: amazonlinux:2
- go-version: '1.20'
image: amazonlinux:2
name: ${{ matrix.arch }} ${{ format(matrix.image, matrix.go-version) }} go${{ matrix.go-version }}${{ matrix.waf-log-level && format(' (DD_APPSEC_WAF_LOG_LEVEL={0})', matrix.waf-log-level) || '' }}
# We use ARM runners when needed to avoid the performance hit of QEMU
runs-on: ${{ matrix.arch == 'amd64' && 'ubuntu-latest' || 'arm-4core-linux' }}
Expand Down
7 changes: 1 addition & 6 deletions .github/workflows/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ GOVERSION="$(go env GOVERSION)"
GOOS="$(go env GOOS)"
GOARCH="$(go env GOARCH)"

case $GOVERSION in
*1.20* ) CGOCHECK="GODEBUG=cgocheck=2";;
*) CGOCHECK="GOEXPERIMENT=cgocheck2";;
esac

contains() {
case $1 in
*$2*) echo true;;
Expand Down Expand Up @@ -47,7 +42,7 @@ run() {

if [ "$cgo" = "1" ]; then
echo "Running again with cgocheck enabled..."
env "$CGOCHECK" CGO_ENABLED=1 go test -shuffle=on -tags="$tags" -args -waf-build-tags="$test_tags" -waf-supported="$waf_enabled" ./...
env "GOEXPERIMENT=cgocheck2" CGO_ENABLED=1 go test -shuffle=on -tags="$tags" -args -waf-build-tags="$test_tags" -waf-supported="$waf_enabled" ./...
fi

# TODO: remove condition once we have native arm64 linux runners
Expand Down
8 changes: 0 additions & 8 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,6 @@ func depthOf(ctx context.Context, obj reflect.Value) (depth int, err error) {

obj, kind := resolvePointer(obj)

//TODO: Remove this once Go 1.21 is the minimum supported version (it adds `builtin.max`)
max := func(x, y int) int {
if x > y {
return x
}
return y
}

var itemDepth int
switch kind {
case reflect.Array, reflect.Slice:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/DataDog/go-libddwaf/v3

go 1.20
go 1.21

require (
github.com/ebitengine/purego v0.6.0-alpha.5
Expand Down
20 changes: 6 additions & 14 deletions internal/bindings/waf_dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package bindings

import (
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -43,20 +44,13 @@ type wafSymbols struct {
// The caller is responsible for calling wafDl.Close on the returned object once they
// are done with it so that associated resources can be released.
func NewWafDl() (dl *WafDl, err error) {
var file string
file, err = lib.DumpEmbeddedWAF()
file, err := lib.DumpEmbeddedWAF()
if err != nil {
return
}
defer func() {
rmErr := os.Remove(file)
if rmErr != nil {
if err == nil {
err = rmErr
} else {
// TODO: rely on errors.Join() once go1.20 is our min supported Go version
err = fmt.Errorf("%w; along with an error while removing %s: %v", err, file, rmErr)
}
if rmErr := os.Remove(file); rmErr != nil {
err = errors.Join(err, fmt.Errorf("error removing %s: %w", file, rmErr))
}
}()

Expand All @@ -68,8 +62,7 @@ func NewWafDl() (dl *WafDl, err error) {
var symbols wafSymbols
if symbols, err = resolveWafSymbols(handle); err != nil {
if closeErr := purego.Dlclose(handle); closeErr != nil {
// TODO: rely on errors.Join() once go1.20 is our min supported Go version
err = fmt.Errorf("%w; along with an error while releasing the shared libddwaf library: %v", err, closeErr)
err = errors.Join(err, fmt.Errorf("error released the shared libddwaf library: %w", closeErr))
}
return
}
Expand All @@ -83,8 +76,7 @@ func NewWafDl() (dl *WafDl, err error) {
})
if err != nil {
if closeErr := purego.Dlclose(handle); closeErr != nil {
// TODO: rely on errors.Join() once go1.20 is our min supported Go version
err = fmt.Errorf("%w; along with an error while releasing the shared libddwaf library: %v", err, closeErr)
err = errors.Join(err, fmt.Errorf("error released the shared libddwaf library: %w", closeErr))
}
return
}
Expand Down
11 changes: 3 additions & 8 deletions internal/lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package lib
import (
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"os"
Expand All @@ -29,17 +30,11 @@ func DumpEmbeddedWAF() (path string, err error) {

defer func() {
if closeErr := file.Close(); closeErr != nil {
if err != nil {
// TODO: rely on errors.Join() once go1.20 is our min supported Go version
err = fmt.Errorf("%w; along with an error while releasingclosing the temporary file: %v", err, closeErr)
} else {
err = fmt.Errorf("error closing file: %w", closeErr)
}
err = errors.Join(err, fmt.Errorf("error closing file: %w", closeErr))
}
if path != "" && err != nil {
if rmErr := os.Remove(path); rmErr != nil {
// TODO: rely on errors.Join() once go1.20 is our min supported Go version
err = fmt.Errorf("%w; along with an error while releasingclosing the temporary file: %v", err, rmErr)
err = errors.Join(err, fmt.Errorf("error removing file: %w", rmErr))
}
}
}()
Expand Down
2 changes: 1 addition & 1 deletion waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Diagnostics struct {
Version string
}

// TopLevelErrors returns the list of top-level errors reported by the WAF on any of the Diagnostics
// TopLevelError returns the list of top-level errors reported by the WAF on any of the Diagnostics
// entries, rolled up into a single error value. Returns nil if no top-level errors were reported.
// Individual, item-level errors might still exist.
func (d *Diagnostics) TopLevelError() error {
Expand Down

0 comments on commit 48f7206

Please sign in to comment.