-
Notifications
You must be signed in to change notification settings - Fork 439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
internal/appsec/config.go: fix waf timeout configuration #1129
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6543955
internal/appsec/config.go: fix waf timeout config along with exhausti…
Julio-Guerra 8a633da
internal/appsec/config.go: fix
Julio-Guerra f8c29fd
internal/appsec/config_test.go: fix test
Julio-Guerra 9277d2a
internal/appsec/config.go: fix negative condition
Julio-Guerra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016 Datadog, Inc. | ||
|
||
//go:build appsec | ||
// +build appsec | ||
|
||
package appsec | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
expectedDefaultConfig := &config{ | ||
rules: []byte(staticRecommendedRule), | ||
wafTimeout: defaultWAFTimeout, | ||
} | ||
|
||
t.Run("default", func(t *testing.T) { | ||
restoreEnv := cleanEnv() | ||
defer restoreEnv() | ||
cfg, err := newConfig() | ||
require.NoError(t, err) | ||
require.Equal(t, expectedDefaultConfig, cfg) | ||
}) | ||
|
||
t.Run("waf-timeout", func(t *testing.T) { | ||
t.Run("parsable", func(t *testing.T) { | ||
restoreEnv := cleanEnv() | ||
defer restoreEnv() | ||
require.NoError(t, os.Setenv(wafTimeoutEnvVar, "5s")) | ||
cfg, err := newConfig() | ||
require.NoError(t, err) | ||
require.Equal( | ||
t, | ||
&config{ | ||
rules: []byte(staticRecommendedRule), | ||
wafTimeout: 5 * time.Second, | ||
}, | ||
cfg, | ||
) | ||
}) | ||
|
||
t.Run("not-parsable", func(t *testing.T) { | ||
restoreEnv := cleanEnv() | ||
defer restoreEnv() | ||
require.NoError(t, os.Setenv(wafTimeoutEnvVar, "not a duration string")) | ||
cfg, err := newConfig() | ||
require.NoError(t, err) | ||
require.Equal(t, expectedDefaultConfig, cfg) | ||
}) | ||
|
||
t.Run("negative", func(t *testing.T) { | ||
restoreEnv := cleanEnv() | ||
defer restoreEnv() | ||
require.NoError(t, os.Setenv(wafTimeoutEnvVar, "not a duration string")) | ||
Julio-Guerra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cfg, err := newConfig() | ||
require.NoError(t, err) | ||
require.Equal(t, expectedDefaultConfig, cfg) | ||
}) | ||
|
||
t.Run("empty-string", func(t *testing.T) { | ||
restoreEnv := cleanEnv() | ||
defer restoreEnv() | ||
require.NoError(t, os.Setenv(wafTimeoutEnvVar, "")) | ||
cfg, err := newConfig() | ||
require.NoError(t, err) | ||
require.Equal(t, expectedDefaultConfig, cfg) | ||
}) | ||
}) | ||
|
||
t.Run("rules", func(t *testing.T) { | ||
t.Run("empty-string", func(t *testing.T) { | ||
restoreEnv := cleanEnv() | ||
defer restoreEnv() | ||
os.Setenv(rulesEnvVar, "") | ||
cfg, err := newConfig() | ||
require.NoError(t, err) | ||
require.Equal(t, expectedDefaultConfig, cfg) | ||
}) | ||
|
||
t.Run("file-not-found", func(t *testing.T) { | ||
restoreEnv := cleanEnv() | ||
defer restoreEnv() | ||
os.Setenv(rulesEnvVar, "i do not exist") | ||
cfg, err := newConfig() | ||
require.Error(t, err) | ||
require.Nil(t, cfg) | ||
}) | ||
|
||
t.Run("local-file", func(t *testing.T) { | ||
restoreEnv := cleanEnv() | ||
defer restoreEnv() | ||
file, err := ioutil.TempFile("", "example-*") | ||
require.NoError(t, err) | ||
defer func() { | ||
file.Close() | ||
os.Remove(file.Name()) | ||
}() | ||
expectedRules := `custom rule file content` | ||
_, err = file.WriteString(expectedRules) | ||
require.NoError(t, err) | ||
os.Setenv(rulesEnvVar, file.Name()) | ||
cfg, err := newConfig() | ||
require.NoError(t, err) | ||
require.Equal(t, &config{ | ||
rules: []byte(expectedRules), | ||
wafTimeout: defaultWAFTimeout, | ||
}, cfg) | ||
}) | ||
}) | ||
} | ||
|
||
func cleanEnv() func() { | ||
wafTimeout := os.Getenv(wafTimeoutEnvVar) | ||
if err := os.Unsetenv(wafTimeoutEnvVar); err != nil { | ||
panic(err) | ||
} | ||
rules := os.Getenv(rulesEnvVar) | ||
if err := os.Unsetenv(rulesEnvVar); err != nil { | ||
panic(err) | ||
} | ||
return func() { | ||
restoreEnv(wafTimeoutEnvVar, wafTimeout) | ||
restoreEnv(rulesEnvVar, rules) | ||
} | ||
} | ||
|
||
func restoreEnv(key, value string) { | ||
if value != "" { | ||
if err := os.Setenv(key, value); err != nil { | ||
panic(err) | ||
} | ||
} else { | ||
if err := os.Unsetenv(key); err != nil { | ||
panic(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the reason why configuring the waf timeout was broken: this condition was wrong ^^'