Skip to content

Commit 0cbeaae

Browse files
authored
Public API: Replace Health() by SupportsTarget() (#18)
While integrating go-libddwaf v1.3.0 into dd-trace-go, we figured out that `Load()` was doing too much for the case of remote activation of appsec when in a disabled state, where we don't want to load libddwaf at all until appsec actually starts up remote activation order. For this reason, we decided to make `Health()` useful again by replacing it with `SupportsTarget()` in order to be able to only check if we are on a supported target or not. This allows us to properly check for proper support of libddwaf, apart from the libddwaf loading, to do it early in the appsec startup and be able to properly log the information to the user.
1 parent 2a0301d commit 0cbeaae

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

waf.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,11 @@ func Load() (ok bool, err error) {
112112
return wafLib != nil, wafErr
113113
}
114114

115-
// Health returns an error when this package is not in a usable state describing
116-
// the reason why.
117-
func Health() error {
118-
ok, err := Load()
119-
if !ok {
120-
return err
121-
}
122-
return nil
115+
// SupportsTarget returns true and a nil error when the target host environment
116+
// is supported by this package and can be further used.
117+
// Otherwise, it returns false along with an error detailing why.
118+
func SupportsTarget() (bool, error) {
119+
return supportsTarget()
123120
}
124121

125122
var wafVersion string

waf_dl.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,8 @@ func (waf *wafDl) wafRun(context wafContext, obj *wafObject, result *wafResult,
161161
keepAlive(timeout)
162162
return rc
163163
}
164+
165+
// Implement SupportsTarget()
166+
func supportsTarget() (bool, error) {
167+
return true, nil
168+
}

waf_dl_unsupported.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import (
1515

1616
type wafDl struct{}
1717

18+
var unsupportedTargetErr = &UnsupportedTargetError{fmt.Errorf("the target operating-system %s or architecture %s are not supported", runtime.GOOS, runtime.GOARCH)}
19+
1820
func newWafDl() (dl *wafDl, err error) {
19-
return nil, &UnsupportedTargetError{fmt.Errorf("the target operating-system %s or architecture %s are not supported", runtime.GOOS, runtime.GOARCH)}
21+
return nil, unsupportedTargetErr
2022
}
2123

2224
func (waf *wafDl) wafGetVersion() string {
@@ -50,3 +52,10 @@ func (waf *wafDl) wafResultFree(result *wafResult) {
5052
func (waf *wafDl) wafRun(context wafContext, obj *wafObject, result *wafResult, timeout uint64) wafReturnCode {
5153
return wafErrInternal
5254
}
55+
56+
// Implement SupportsTarget()
57+
func supportsTarget() (bool, error) {
58+
// TODO: provide finer-grained unsupported target error message giving the
59+
// exact reason why
60+
return false, unsupportedTargetErr
61+
}

waf_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
func init() {
2626
if ok, err := Load(); !ok {
27-
panic(err)
27+
panic(err)
2828
}
2929
}
3030

@@ -38,6 +38,12 @@ func TestLoad(t *testing.T) {
3838
require.NoError(t, err)
3939
}
4040

41+
func TestSupportsTarget(t *testing.T) {
42+
supported, err := SupportsTarget()
43+
require.True(t, supported)
44+
require.NoError(t, err)
45+
}
46+
4147
func TestVersion(t *testing.T) {
4248
require.Regexp(t, `[0-9]+\.[0-9]+\.[0-9]+`, Version())
4349
}

waf_unsupported_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ func TestLoad(t *testing.T) {
2424
require.Truef(t, errors.As(err, &expectedErr), "unexpected error of type %[1]T: %[1]v", err)
2525
}
2626

27-
func TestHealth(t *testing.T) {
28-
require.Error(t, waf.Health())
27+
func TestSupportsTarget(t *testing.T) {
28+
supported, err := waf.SupportsTarget()
29+
require.False(t, supported)
30+
require.Error(t, err)
2931
}

0 commit comments

Comments
 (0)