Skip to content

Commit 4539024

Browse files
authored
internal/appsec: rework appsec telemetry (#3345)
1 parent db3f35f commit 4539024

File tree

11 files changed

+202
-200
lines changed

11 files changed

+202
-200
lines changed

instrumentation/appsec/emitter/waf/addresses/rasp_rule_type.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,45 @@
66
package addresses
77

88
import (
9+
"math"
10+
911
waf "github.com/DataDog/go-libddwaf/v3"
1012
)
1113

12-
type RASPRuleType string
14+
type RASPRuleType uint8
1315

1416
const (
15-
RASPRuleTypeLFI RASPRuleType = "lfi"
16-
RASPRuleTypeSSRF RASPRuleType = "ssrf"
17-
RASPRuleTypeSQLI RASPRuleType = "sql_injection"
18-
RASPRuleTypeCMDI RASPRuleType = "command_injection"
17+
RASPRuleTypeLFI RASPRuleType = iota
18+
RASPRuleTypeSSRF
19+
RASPRuleTypeSQLI
20+
RASPRuleTypeCMDI
1921
)
2022

21-
func RASPRuleTypes() []RASPRuleType {
22-
return []RASPRuleType{
23-
RASPRuleTypeLFI,
24-
RASPRuleTypeSSRF,
25-
RASPRuleTypeSQLI,
26-
RASPRuleTypeCMDI,
23+
var RASPRuleTypes = [...]RASPRuleType{
24+
RASPRuleTypeLFI,
25+
RASPRuleTypeSSRF,
26+
RASPRuleTypeSQLI,
27+
RASPRuleTypeCMDI,
28+
}
29+
30+
func (r RASPRuleType) String() string {
31+
switch r {
32+
case RASPRuleTypeLFI:
33+
return "lfi"
34+
case RASPRuleTypeSSRF:
35+
return "ssrf"
36+
case RASPRuleTypeSQLI:
37+
return "sql_injection"
38+
case RASPRuleTypeCMDI:
39+
return "command_injection"
2740
}
41+
return "unknown()"
2842
}
2943

3044
// RASPRuleTypeFromAddressSet returns the RASPRuleType for the given address set if it has a RASP address.
3145
func RASPRuleTypeFromAddressSet(addressSet waf.RunAddressData) (RASPRuleType, bool) {
3246
if addressSet.Scope != waf.RASPScope {
33-
return "", false
47+
return math.MaxUint8, false
3448
}
3549

3650
for address := range addressSet.Ephemeral {
@@ -46,5 +60,5 @@ func RASPRuleTypeFromAddressSet(addressSet waf.RunAddressData) (RASPRuleType, bo
4660
}
4761
}
4862

49-
return "", false
63+
return math.MaxUint8, false
5064
}

internal/appsec/appsec.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ func RASPEnabled() bool {
3838
// Start AppSec when enabled is enabled by both using the appsec build tag and
3939
// setting the environment variable DD_APPSEC_ENABLED to true.
4040
func Start(opts ...config.StartOption) {
41-
telemetry := newAppsecTelemetry()
42-
defer telemetry.emit()
43-
4441
startConfig := config.NewStartConfig(opts...)
4542

4643
// AppSec can start either:
@@ -54,19 +51,8 @@ func Start(opts ...config.StartOption) {
5451
return
5552
}
5653

57-
switch modeOrigin {
58-
case config.OriginEnvVar:
59-
telemetry.addEnvConfig("DD_APPSEC_ENABLED", mode == config.ForcedOn)
60-
if mode == config.ForcedOff {
61-
log.Debug("appsec: disabled by the configuration: set the environment variable DD_APPSEC_ENABLED to true to enable it")
62-
return
63-
}
64-
case config.OriginExplicitOption:
65-
telemetry.addCodeConfig("WithEnablementMode", mode)
66-
}
67-
68-
// In any case, if we're forced off, we no longer have any business here...
6954
if mode == config.ForcedOff {
55+
log.Debug("appsec: disabled by the configuration: set the environment variable DD_APPSEC_ENABLED to true to enable it")
7056
return
7157
}
7258

@@ -108,11 +94,17 @@ func Start(opts ...config.StartOption) {
10894
return
10995
}
11096
log.Debug("appsec: awaiting for possible remote activation")
111-
} else if err := appsec.start(telemetry); err != nil { // AppSec is specifically enabled
97+
setActiveAppSec(appsec)
98+
return
99+
}
100+
101+
if err := appsec.start(); err != nil { // AppSec is specifically enabled
112102
logUnexpectedStartError(err)
113103
appsec.stopRC()
114104
return
115105
}
106+
107+
registerAppsecStartTelemetry(mode, modeOrigin)
116108
setActiveAppSec(appsec)
117109
}
118110

@@ -156,7 +148,7 @@ func newAppSec(cfg *config.Config) *appsec {
156148
}
157149

158150
// Start AppSec by registering its security protections according to the configured the security rules.
159-
func (a *appsec) start(telemetry *appsecTelemetry) error {
151+
func (a *appsec) start() error {
160152
// Load the waf to catch early errors if any
161153
if ok, err := waf.Load(); err != nil {
162154
// 1. If there is an error and the loading is not ok: log as an unexpected error case and quit appsec
@@ -183,7 +175,6 @@ func (a *appsec) start(telemetry *appsecTelemetry) error {
183175
// TODO: log the config like the APM tracer does but we first need to define
184176
// an user-friendly string representation of our config and its sources
185177

186-
telemetry.setEnabled()
187178
return nil
188179
}
189180

@@ -192,10 +183,8 @@ func (a *appsec) stop() {
192183
if !a.started {
193184
return
194185
}
195-
telemetry := newAppsecTelemetry()
196-
defer telemetry.emit()
197-
198186
a.started = false
187+
registerAppsecStopTelemetry()
199188
// Disable RC blocking first so that the following is guaranteed not to be concurrent anymore.
200189
a.disableRCBlocking()
201190

internal/appsec/config/config.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type StartConfig struct {
5252
RC *remoteconfig.ClientConfig
5353
// IsEnabled is a function that determines whether AppSec is enabled or not. When unset, the
5454
// default [IsEnabled] function is used.
55-
EnablementMode func() (EnablementMode, Origin, error)
55+
EnablementMode func() (EnablementMode, telemetry.Origin, error)
5656
// MetaStructAvailable is true if meta struct is supported by the trace agent.
5757
MetaStructAvailable bool
5858

@@ -70,30 +70,19 @@ const (
7070
ForcedOn EnablementMode = 1
7171
)
7272

73-
type Origin uint8
74-
75-
const (
76-
// OriginDefault is the origin of configuration values not explicitly set by the user in any way.
77-
OriginDefault Origin = iota
78-
// OriginEnvVar is the origin of configuration values set through environment variables.
79-
OriginEnvVar
80-
// OriginExplicitOption is the origin of configuration values set though explicit options in code.
81-
OriginExplicitOption
82-
)
83-
8473
func NewStartConfig(opts ...StartOption) *StartConfig {
8574
c := &StartConfig{
86-
EnablementMode: func() (mode EnablementMode, origin Origin, err error) {
75+
EnablementMode: func() (mode EnablementMode, origin telemetry.Origin, err error) {
8776
enabled, set, err := IsEnabledByEnvironment()
8877
if set {
89-
origin = OriginEnvVar
78+
origin = telemetry.OriginEnvVar
9079
if enabled {
9180
mode = ForcedOn
9281
} else {
9382
mode = ForcedOff
9483
}
9584
} else {
96-
origin = OriginDefault
85+
origin = telemetry.OriginDefault
9786
mode = RCStandby
9887
}
9988
return mode, origin, err
@@ -109,8 +98,8 @@ func NewStartConfig(opts ...StartOption) *StartConfig {
10998
// implemented by [IsEnabledByEnvironment].
11099
func WithEnablementMode(mode EnablementMode) StartOption {
111100
return func(c *StartConfig) {
112-
c.EnablementMode = func() (EnablementMode, Origin, error) {
113-
return mode, OriginExplicitOption, nil
101+
c.EnablementMode = func() (EnablementMode, telemetry.Origin, error) {
102+
return mode, telemetry.OriginCode, nil
114103
}
115104
}
116105
}

0 commit comments

Comments
 (0)