Skip to content

Commit

Permalink
feat(override): adds feature for everriding host and port globally on…
Browse files Browse the repository at this point in the history
… all tests

Signed-off-by: Felipe Zipitria <[email protected]>
  • Loading branch information
fzipi committed Feb 13, 2022
1 parent 24d5a48 commit d41e859
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 46 deletions.
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ type FTWLogType struct {
TimeTruncate time.Duration `koanf:"timetruncate"`
}

// FTWTestOverride holds three lists:
// FTWTestOverride holds four lists:
// Global allows you to override global parameters in tests. An example usage is if you want to change the `dest_addr` of all tests to point to an external IP or host.
// Ignore is for tests you want to ignore. It will still execute the test, but ignore the results. You should add a comment on why you ignore the test
// ForcePass is for tests you want to pass unconditionally. Test will be executed, and pass even when the test fails. You should add a comment on why you force pass the test
// ForceFail is for tests you want to fail unconditionally. Test will be executed, and fail even when the test passes. You should add a comment on why you force fail the test
type FTWTestOverride struct {
Input map[string]string `koanf:"input"`
Ignore map[string]string `koanf:"ignore"`
ForcePass map[string]string `koanf:"forcepass"`
ForceFail map[string]string `koanf:"forcefail"`
Expand Down
19 changes: 15 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
"github.com/fzipi/go-ftw/utils"
)

var yamlConfig = `
---
var yamlConfig = `---
logfile: 'tests/logs/modsec2-apache/apache2/error.log'
logtype:
name: 'apache'
timeregex: '\[([A-Z][a-z]{2} [A-z][a-z]{2} \d{1,2} \d{1,2}\:\d{1,2}\:\d{1,2}\.\d+? \d{4})\]'
timeregex: '\[([A-Z][a-z]{2} [A-z][a-z]{2} \d{1,2} \d{1,2}\:\d{1,2}\:\d{1,2}\.\d+? \d{4})\]'
timeformat: 'ddd MMM DD HH:mm:ss.S YYYY'
testoverride:
input:
dest_addr: 'httpbin.org'
port: '1234'
ignore:
'920400-1': 'This test result must be ignored'
`
Expand All @@ -38,7 +40,6 @@ logtype:
name: nginx
timetruncate: 1s
timeformat: 'ddd MMM DD HH:mm:ss'
`

var jsonConfig = `
Expand Down Expand Up @@ -68,6 +69,10 @@ func TestInitConfig(t *testing.T) {
t.Errorf("Failed! Len must be > 0")
}

if len(FTWConfig.TestOverride.Input) == 0 {
t.Errorf("Failed! Input Len must be > 0")
}

for id, text := range FTWConfig.TestOverride.Ignore {
if !strings.Contains(id, "920400-1") {
t.Errorf("Looks like we could not find item to ignore")
Expand All @@ -77,6 +82,12 @@ func TestInitConfig(t *testing.T) {
}
}

for setting, value := range FTWConfig.TestOverride.Input {
if setting == "dest_addr" && value != "httpbin.org" {
t.Errorf("Looks like we are not overriding destination!")
}
}

}

func TestInitBadConfig(t *testing.T) {
Expand Down
36 changes: 32 additions & 4 deletions runner/run.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package runner

import (
"errors"
"regexp"
"strconv"
"time"

"github.com/fzipi/go-ftw/check"
Expand Down Expand Up @@ -46,7 +48,11 @@ func Run(include string, exclude string, showTime bool, output bool, ftwtests []
printUnlessQuietMode(output, "\trunning %s: ", t.TestTitle)
// Iterate over stages
for _, stage := range t.Stages {
testRequest := stage.Stage.Input
// Apply global overrides initially
testRequest, err := applyInputOverride(stage.Stage.Input)
if err != nil {
log.Debug().Msgf("ftw/run: problem overriding input: %s", err.Error())
}
expectedOutput := stage.Stage.Output

// Check sanity first
Expand All @@ -63,7 +69,7 @@ func Run(include string, exclude string, showTime bool, output bool, ftwtests []
Protocol: testRequest.GetProtocol(),
}

err := client.NewConnection(*dest)
err = client.NewConnection(*dest)

if err != nil && !expectedOutput.ExpectError {
log.Fatal().Msgf("ftw/run: can't connect to destination %+v - unexpected error found. Is your waf running?", dest)
Expand All @@ -74,7 +80,9 @@ func Run(include string, exclude string, showTime bool, output bool, ftwtests []
req = getRequestFromTest(testRequest)

client.StartTrackingTime()

response, err := client.Do(*req)

client.StopTrackingTime()

// Create a new check
Expand Down Expand Up @@ -200,12 +208,10 @@ func checkResult(c *check.FTWCheck, id string, response *http.Response, response
}
// Lastly, check logs
if c.AssertLogContains() {
log.Debug().Msgf("ftw/check: found log with requeste pattern")
result = Success
}
// We assume that the they were already setup, for comparing
if c.AssertNoLogContains() {
log.Debug().Msgf("ftw/check: log does not contain pattern")
result = Success
}
}
Expand Down Expand Up @@ -246,3 +252,25 @@ func printUnlessQuietMode(quiet bool, format string, a ...interface{}) {
emoji.Printf(format, a...)
}
}

// applyInputOverride will check if config had global overrides and write that into the test.
func applyInputOverride(testRequest test.Input) (test.Input, error) {
var retErr error
overrides := config.FTWConfig.TestOverride.Input
for setting, value := range overrides {
switch setting {
case "port":
port, err := strconv.Atoi(value)
if err != nil {
retErr = errors.New("ftw/run: error getting overriden port")
}
testRequest.Port = &port
case "dest_addr":
newValue := value
testRequest.DestAddr = &newValue
default:
retErr = errors.New("ftw/run: override setting not implemented yet")
}
}
return testRequest, retErr
}
Loading

0 comments on commit d41e859

Please sign in to comment.