Skip to content

Commit

Permalink
internal/appsec/config: default the time suffix of DD_APPSEC_WAF_TIME…
Browse files Browse the repository at this point in the history
…OUT to us (#1195)

Co-authored-by: François Mazeau <francois.mazeau@datadoghq.com>
  • Loading branch information
Julio Guerra and Hellzy committed Mar 7, 2022
1 parent 94d830e commit 1dfcbe9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/appsec/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"os"
"strconv"
"time"
"unicode"
"unicode/utf8"

"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
)
Expand Down Expand Up @@ -69,9 +71,18 @@ func readWAFTimeoutConfig() (timeout time.Duration) {
if value == "" {
return
}

// Check if the value ends with a letter, which means the user has
// specified their own time duration unit(s) such as 1s200ms.
// Otherwise, default to microseconds.
if lastRune, _ := utf8.DecodeLastRuneInString(value); !unicode.IsLetter(lastRune) {
value += "us" // Add the default microsecond time-duration suffix
}

parsed, err := time.ParseDuration(value)
if err != nil {
logEnvVarParsingError(wafTimeoutEnvVar, value, err, timeout)
return
}
if parsed <= 0 {
logUnexpectedEnvVarValue(wafTimeoutEnvVar, parsed, "expecting a strictly positive duration", timeout)
Expand Down
17 changes: 17 additions & 0 deletions internal/appsec/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ func TestConfig(t *testing.T) {
)
})

t.Run("parsable-default-microsecond", func(t *testing.T) {
restoreEnv := cleanEnv()
defer restoreEnv()
require.NoError(t, os.Setenv(wafTimeoutEnvVar, "1"))
cfg, err := newConfig()
require.NoError(t, err)
require.Equal(
t,
&config{
rules: []byte(staticRecommendedRule),
wafTimeout: 1 * time.Microsecond,
traceRateLimit: defaultTraceRate,
},
cfg,
)
})

t.Run("not-parsable", func(t *testing.T) {
restoreEnv := cleanEnv()
defer restoreEnv()
Expand Down

0 comments on commit 1dfcbe9

Please sign in to comment.