Skip to content

Commit

Permalink
fix: use correct names for flags (#420)
Browse files Browse the repository at this point in the history
Fixes #419
  • Loading branch information
theseion authored Dec 14, 2024
1 parent cee89de commit b01715f
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 102 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ Flags:
--fail-fast Fail on first failed test
-f, --file string output file path for ftw tests. Prints to standard output by default.
-h, --help help for run
-i, --include string include only tests matching this Go regular expression (e.g. to include only tests beginning with "91", use "^91.*"). \nIf you want more permanent inclusion, check the 'include' option in the config file."
-i, --include string include only tests matching this Go regular expression (e.g. to include only tests beginning with "91", use "^91.*").
If you want more permanent inclusion, check the 'include' option in the config file.
-T, --include-tags string include tests tagged with labels matching this Go regular expression (e.g. to include all tests being tagged with "cookie", use "^cookie$").
-l, --log-file string path to log file to watch for WAF events
--max-marker-log-lines int maximum number of lines to search for a marker before aborting (default 500)
--max-marker-retries int maximum number of times the search for log markers will be repeated.
Expand All @@ -133,7 +135,6 @@ Flags:
-r, --rate-limit duration Limit the request rate to the server to 1 request per specified duration. 0 is the default, and disables rate limiting.
--read-timeout duration timeout for receiving responses during test execution (default 10s)
--show-failures-only shows only the results of failed tests
-T, --include-tags string include tests tagged with labels matching this Go regular expression (e.g. to include all tests being tagged with "cookie", use "^cookie$").
-t, --time show time spent per test
--wait-delay duration Time to wait between retries for all wait operations. (default 1s)
--wait-for-connection-timeout duration Http connection timeout, The timeout includes connection time, any redirects, and reading the response body. (default 3s)
Expand Down Expand Up @@ -411,7 +412,6 @@ You can configure the name of the HTTP header by setting the `logmarkerheadernam
Sometimes you need to wait for a backend service to be ready before running the tests. For example, you may need to wait for an additional container to be ready before running the tests.
Now you can do that by passing the `--wait-for-host` flag. The value of this option is a URL that will be requested, and you can configure the expected result using the following additional flags:
- `--wait-for-host`: Wait for host to be available before running tests.
- `--wait-delay`: Time to wait between retries for all wait operations. (default 1s)
- `--wait-for-connection-timeout` Http connection timeout, The timeout includes connection time, any redirects, and reading the response body. (default 3s)
- `--wait-for-expect-body-json` Expect response body JSON pattern.
- `--wait-for-expect-body-regex` Expect response body pattern.
Expand Down
77 changes: 46 additions & 31 deletions cmd/quantitative.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ import (
"github.com/coreruleset/go-ftw/output"
)

const (
corpusFlag = "corpus"
corpusLangFlag = "corpus-lang"
corpusLineFlag = "corpus-line"
corpusSizeFlag = "corpus-size"
corpusSourceFlag = "corpus-source"
corpusYearFlag = "corpus-year"
crsPathFlag = "crs-path"
corpusFileFlag = "file"
linesFlag = "lines"
maxConcurrencyFlag = "max-concurrency"
corpusOutputFlag = "output"
paranoiaLevelFlag = "paranoia-level"
payloadFlag = "payload"
ruleFlag = "rule"
)

// NewQuantitativeCmd
// Returns a new cobra command for running quantitative tests
func NewQuantitativeCmd() *cobra.Command {
Expand All @@ -26,45 +43,44 @@ func NewQuantitativeCmd() *cobra.Command {
RunE: runQuantitativeE,
}

runCmd.Flags().IntP("lines", "l", 0, "Number of lines of input to process before stopping.")
runCmd.Flags().IntP("paranoia-level", "P", 1, "Paranoia level used to run the quantitative tests.")
runCmd.Flags().IntP("corpus-line", "n", 0, "Number is the payload line from the corpus to exclusively send.")
runCmd.Flags().StringP("payload", "p", "", "Payload is a string you want to test using quantitative tests. Will not use the corpus.")
runCmd.Flags().IntP("rule", "r", 0, "Rule ID of interest: only show false positives for specified rule ID.")
runCmd.Flags().IntP("max-concurrency", "", 10, "maximum number of goroutines. Defaults to 10, or 1 if log level is debug/trace.")
runCmd.Flags().StringP("corpus", "c", "leipzig", "Corpus to use for the quantitative tests.")
runCmd.Flags().StringP("corpus-lang", "L", "eng", "Corpus language to use for the quantitative tests.")
runCmd.Flags().StringP("corpus-size", "s", "100K", "Corpus size to use for the quantitative tests. Most corpora will have sizes like \"100K\", \"1M\", etc.")
runCmd.Flags().StringP("corpus-year", "y", "2023", "Corpus year to use for the quantitative tests. Most corpus will have a year like \"2023\", \"2022\", etc.")
runCmd.Flags().StringP("corpus-source", "S", "news", "Corpus source to use for the quantitative tests. Most corpus will have a source like \"news\", \"web\", \"wikipedia\", etc.")
runCmd.Flags().StringP("crs-path", "C", ".", "Path to top folder of local CRS installation.")
runCmd.Flags().StringP("file", "f", "", "Output file path for quantitative tests. Prints to standard output by default.")
runCmd.Flags().StringP("output", "o", "normal", "Output type for quantitative tests. \"normal\" is the default.")
runCmd.Flags().IntP(linesFlag, "l", 0, "Number of lines of input to process before stopping.")
runCmd.Flags().IntP(paranoiaLevelFlag, "P", 1, "Paranoia level used to run the quantitative tests.")
runCmd.Flags().IntP(corpusLineFlag, "n", 0, "Number is the payload line from the corpus to exclusively send.")
runCmd.Flags().StringP(payloadFlag, "p", "", "Payload is a string you want to test using quantitative tests. Will not use the corpus.")
runCmd.Flags().IntP(ruleFlag, "r", 0, "Rule ID of interest: only show false positives for specified rule ID.")
runCmd.Flags().IntP(maxConcurrencyFlag, "", 10, "maximum number of goroutines. Defaults to 10, or 1 if log level is debug/trace.")
runCmd.Flags().StringP(corpusFlag, "c", "leipzig", "Corpus to use for the quantitative tests.")
runCmd.Flags().StringP(corpusLangFlag, "L", "eng", "Corpus language to use for the quantitative tests.")
runCmd.Flags().StringP(corpusSizeFlag, "s", "100K", "Corpus size to use for the quantitative tests. Most corpora will have sizes like \"100K\", \"1M\", etc.")
runCmd.Flags().StringP(corpusYearFlag, "y", "2023", "Corpus year to use for the quantitative tests. Most corpus will have a year like \"2023\", \"2022\", etc.")
runCmd.Flags().StringP(corpusSourceFlag, "S", "news", "Corpus source to use for the quantitative tests. Most corpus will have a source like \"news\", \"web\", \"wikipedia\", etc.")
runCmd.Flags().StringP(crsPathFlag, "C", ".", "Path to top folder of local CRS installation.")
runCmd.Flags().StringP(corpusFileFlag, "f", "", "Output file path for quantitative tests. Prints to standard output by default.")
runCmd.Flags().StringP(corpusOutputFlag, "o", "normal", "Output type for quantitative tests. \"normal\" is the default.")

return runCmd
}

func runQuantitativeE(cmd *cobra.Command, _ []string) error {
cmd.SilenceUsage = true

corpusTypeAsString, _ := cmd.Flags().GetString("corpus")
corpusSize, _ := cmd.Flags().GetString("corpus-size")
corpusLang, _ := cmd.Flags().GetString("corpus-lang")
corpusYear, _ := cmd.Flags().GetString("corpus-year")
corpusSource, _ := cmd.Flags().GetString("corpus-source")
directory, _ := cmd.Flags().GetString("crs-path")
fast, _ := cmd.Flags().GetInt("fast")
lines, _ := cmd.Flags().GetInt("lines")
outputFilename, _ := cmd.Flags().GetString("file")
paranoiaLevel, _ := cmd.Flags().GetInt("paranoia-level")
payload, _ := cmd.Flags().GetString("payload")
number, _ := cmd.Flags().GetInt("corpus-line")
rule, _ := cmd.Flags().GetInt("rule")
wantedOutput, _ := cmd.Flags().GetString("output")
maxConcurrency, _ := cmd.Flags().GetInt("max-concurrency")
corpusTypeAsString, _ := cmd.Flags().GetString(corpusFlag)
corpusSize, _ := cmd.Flags().GetString(corpusSizeFlag)
corpusLang, _ := cmd.Flags().GetString(corpusLangFlag)
corpusYear, _ := cmd.Flags().GetString(corpusYearFlag)
corpusSource, _ := cmd.Flags().GetString(corpusSourceFlag)
directory, _ := cmd.Flags().GetString(crsPathFlag)
lines, _ := cmd.Flags().GetInt(linesFlag)
outputFilename, _ := cmd.Flags().GetString(corpusFileFlag)
paranoiaLevel, _ := cmd.Flags().GetInt(paranoiaLevelFlag)
payload, _ := cmd.Flags().GetString(payloadFlag)
number, _ := cmd.Flags().GetInt(corpusLineFlag)
rule, _ := cmd.Flags().GetInt(ruleFlag)
wantedOutput, _ := cmd.Flags().GetString(corpusOutputFlag)
maxConcurrency, _ := cmd.Flags().GetInt(maxConcurrencyFlag)

// --max-concurrency defaults to 1 if debug/trace is enabled, but if set explicitly, it should override this
if !cmd.Flags().Changed("max-concurrency") && zerolog.GlobalLevel() <= zerolog.DebugLevel {
if !cmd.Flags().Changed(maxConcurrencyFlag) && zerolog.GlobalLevel() <= zerolog.DebugLevel {
maxConcurrency = 1
}

Expand Down Expand Up @@ -100,7 +116,6 @@ func runQuantitativeE(cmd *cobra.Command, _ []string) error {
CorpusLang: corpusLang,
CorpusSource: corpusSource,
Directory: directory,
Fast: fast,
Lines: lines,
ParanoiaLevel: paranoiaLevel,
Number: number,
Expand Down
18 changes: 13 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import (
"github.com/coreruleset/go-ftw/config"
)

const (
cloudFlag = "cloud"
configFlag = "config"
debugFlag = "debug"
overridesFlag = "overrides"
traceFlag = "trace"
)

var (
cfgFile string
overridesFile string
Expand All @@ -29,11 +37,11 @@ func NewRootCommand() *cobra.Command {
Use: "ftw run",
Short: "Framework for Testing WAFs - Go Version",
}
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "specify config file (default is $PWD/.ftw.yaml)")
rootCmd.PersistentFlags().StringVar(&overridesFile, "overrides", "", "specify file with platform specific overrides")
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "", false, "debug output")
rootCmd.PersistentFlags().BoolVarP(&trace, "trace", "", false, "trace output: really, really verbose")
rootCmd.PersistentFlags().BoolVarP(&cloud, "cloud", "", false, "cloud mode: rely only on HTTP status codes for determining test success or failure (will not process any logs)")
rootCmd.PersistentFlags().StringVar(&cfgFile, configFlag, "", "specify config file (default is $PWD/.ftw.yaml)")
rootCmd.PersistentFlags().StringVar(&overridesFile, overridesFlag, "", "specify file with platform specific overrides")
rootCmd.PersistentFlags().BoolVarP(&debug, debugFlag, "", false, "debug output")
rootCmd.PersistentFlags().BoolVarP(&trace, traceFlag, "", false, "trace output: really, really verbose")
rootCmd.PersistentFlags().BoolVarP(&cloud, cloudFlag, "", false, "cloud mode: rely only on HTTP status codes for determining test success or failure (will not process any logs)")

return rootCmd
}
Expand Down
Loading

0 comments on commit b01715f

Please sign in to comment.