Skip to content

Commit 2a0301d

Browse files
Julio-GuerraeliottnessHellzy
authored
Public API: Load() (#17)
- `func Load() (ok bool, err error)` to ease the integration in dd-trace-go so that we can distinguish the different possible error cases: - Panic errors that must be used to gracefully abort the caller and prevent it from continuing. Such errors are represented by the new `PanicError` error type. - Unsupported target OS and/or architecture, which must be handled as an informative error, is worth logging to the user, and prevents the caller from continuing. This error is represented by the new `UnsupportedTarget` error type. - Every other unexpected error that can possible happen is returned as-is, but `ok` is `false`, so that the caller knows it cannot use the package. - Informative errors, about internal non-critical failures, can also happen (eg. if os.Remove fails removing the temporary files), and are returned as-is, but with `ok` being `true` so that the caller can log the error and continue its startup. - The dd-trace-go integration of this PR can be found here DataDog/dd-trace-go#2090 - Note that `Health()` has been kept for the sake of simplicity health-checking cases such as in the tests, where it returns the `Load()` error only if `ok` is false, meaning that go-libddwaf is not healthy. - Simplified build tags, removed everywhere it was possible, down to purego which isn't properly stubbed for unsupported targets and which we stub ourselves at our level in the new `waf_dl_unsupported.go` file. --------- Co-authored-by: Eliott Bouhana <[email protected]> Co-authored-by: François Mazeau <[email protected]>
1 parent 412ddc3 commit 2a0301d

22 files changed

+381
-219
lines changed

cgo_ref_pool.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// This product includes software developed at Datadog (https://www.datadoghq.com/).
44
// Copyright 2016-present Datadog, Inc.
55

6-
//go:build (linux || darwin) && (amd64 || arm64)
7-
86
package waf
97

108
import (

context.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// This product includes software developed at Datadog (https://www.datadoghq.com/).
44
// Copyright 2016-present Datadog, Inc.
55

6-
//go:build (linux || darwin) && (amd64 || arm64)
7-
86
package waf
97

108
import (
@@ -56,7 +54,6 @@ func NewContext(handle *Handle) *Context {
5654
return &Context{handle: handle, cContext: cContext}
5755
}
5856

59-
6057
// Run encodes the given addressesToData values and runs them against the WAF rules within the given
6158
// timeout value. It returns the matches as a JSON string (usually opaquely used) along with the corresponding
6259
// actions in any. In case of an error, matches and actions can still be returned, for instance in the case of a

ctypes.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
// This product includes software developed at Datadog (https://www.datadoghq.com/).
44
// Copyright 2016-present Datadog, Inc.
55

6-
// Purego only works on linux/macOS with amd64 and arm64 for now
7-
//go:build (linux || darwin) && (amd64 || arm64)
8-
96
package waf
107

118
import (
@@ -162,7 +159,7 @@ func stringToUintptr(arg string) uintptr {
162159
// keepAlive() globals
163160
var (
164161
alwaysFalse bool
165-
escapeSink any
162+
escapeSink any
166163
)
167164

168165
// keepAlive is a copy of runtime.KeepAlive

ctypes_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ func getSymbol(t *testing.T, lib uintptr, symbol string) uintptr {
3939
}
4040

4141
func TestWafObject(t *testing.T) {
42-
4342
lib := getLibddwaf(t)
4443

4544
t.Run("invalid", func(t *testing.T) {

decoder.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
// This product includes software developed at Datadog (https://www.datadoghq.com/).
44
// Copyright 2016-present Datadog, Inc.
55

6-
//go:build (linux || darwin) && (amd64 || arm64)
7-
86
package waf
97

10-
// decodeErrors tranforms the wafObject received by the wafRulesetInfo after the call to wafDl.wafInit to a map where
8+
// decodeErrors transforms the wafObject received by the wafRulesetInfo after the call to wafDl.wafInit to a map where
119
// keys are the error message and the value is a array of all the rule ids which triggered this specific error
1210
func decodeErrors(obj *wafObject) (map[string][]string, error) {
1311
if obj._type != wafMapType {

encoder.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// This product includes software developed at Datadog (https://www.datadoghq.com/).
44
// Copyright 2016-present Datadog, Inc.
55

6-
//go:build (linux || darwin) && (amd64 || arm64)
7-
86
package waf
97

108
import (

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.18
44

55
require (
66
github.com/ebitengine/purego v0.4.0-alpha.4.0.20230519103000-ee8dcecc618f
7+
github.com/pkg/errors v0.9.1
78
github.com/stretchr/testify v1.8.1
89
go.uber.org/atomic v1.10.0
910
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1010
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
1111
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1212
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
13+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
14+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1315
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1416
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1517
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

handle.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
// This product includes software developed at Datadog (https://www.datadoghq.com/).
44
// Copyright 2016-present Datadog, Inc.
55

6-
//go:build (linux || darwin) && (amd64 || arm64)
7-
86
package waf
97

108
import (
119
"errors"
1210
"fmt"
11+
"sync"
12+
1313
"github.com/DataDog/go-libddwaf/internal/noopfree"
1414
"go.uber.org/atomic"
15-
"sync"
1615
)
1716

1817
// Handle represents an instance of the WAF for a given ruleset.
@@ -42,16 +41,20 @@ type Handle struct {
4241

4342
// NewHandle creates and returns a new instance of the WAF with the given security rules and configuration
4443
// of the sensitive data obfuscator. The returned handle is nil in case of an error.
45-
// Rules-related metrics, including errors, are accessible with the `RulesetInfo()` method.
44+
// Rules-related metrics, including errors, are accessible with the `RulesetInfo()` method.
4645
func NewHandle(rules any, keyObfuscatorRegex string, valueObfuscatorRegex string) (*Handle, error) {
47-
// The order of action is the following:
48-
// - Open the ddwaf C library
49-
// - Encode the security rules as a ddwaf_object
50-
// - Create a ddwaf_config object and fill the values
51-
// - Run ddwaf_init to create a new handle based on the given rules and config
52-
// - Check for errors and streamline the ddwaf_ruleset_info returned
53-
if err := InitWaf(); err != nil {
46+
// The order of action is the following:
47+
// - Open the ddwaf C library
48+
// - Encode the security rules as a ddwaf_object
49+
// - Create a ddwaf_config object and fill the values
50+
// - Run ddwaf_init to create a new handle based on the given rules and config
51+
// - Check for errors and streamline the ddwaf_ruleset_info returned
52+
53+
if ok, err := Load(); !ok {
5454
return nil, err
55+
// The case where ok == true && err != nil is ignored on purpose, as
56+
// this is out of the scope of NewHandle which only requires a properly
57+
// loaded libddwaf in order to use it
5558
}
5659

5760
encoder := newMaxEncoder()
@@ -66,7 +69,7 @@ func NewHandle(rules any, keyObfuscatorRegex string, valueObfuscatorRegex string
6669
cHandle := wafLib.wafInit(obj, config, cRulesetInfo)
6770
keepAlive(encoder.cgoRefs)
6871
// Note that the encoded obj was copied by libddwaf, so we don't need to keep them alive
69-
// for the lifetime of the handle (ddwaf API guarantee).
72+
// for the lifetime of the handle (ddwaf API guarantee).
7073
if cHandle == 0 {
7174
return nil, errors.New("could not instantiate the WAF")
7275
}
@@ -111,7 +114,7 @@ func (handle *Handle) closeContext(context *Context) {
111114
// Close puts the handle in termination state, when all the contexts are closed the handle will be destroyed
112115
func (handle *Handle) Close() {
113116
if handle.addRefCounter(-1) > 0 {
114-
// There are still Contexts that are not closed
117+
// There are still Contexts that are not closed
115118
return
116119
}
117120

internal/noopfree/noopfree.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// This product includes software developed at Datadog (https://www.datadoghq.com/).
44
// Copyright 2016-present Datadog, Inc.
55

6+
// Package noopfree provides a noop-ed free function. A separate package is
7+
// needed to avoid the special go-build case with CGO enabled where it compiles
8+
// .s files with CC instead of the Go assembler that we want here.
69
package noopfree
710

811
import "unsafe"

0 commit comments

Comments
 (0)