Skip to content
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

introduce a fail flag controlling when dnspyre exits with non-zero code #241

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ var (
benchmark = dnsbench.Benchmark{
Writer: os.Stdout,
}

failConditions []string
)

const (
ioerrorFailCondition = "ioerror"
negativeFailCondition = "negative"
errorFailCondition = "error"
idmismatchFailCondition = "idmismatch"
)

func init() {
Expand Down Expand Up @@ -130,6 +139,12 @@ func init() {
pApp.Flag("progress", "Controls whether the progress bar is shown. Enabled by default.").
Default("true").BoolVar(&benchmark.ProgressBar)

pApp.Flag("fail", "Controls conditions upon which the dnspyre will exit with a non-zero exit code. Repeatable flag. "+
"Supported options are 'ioerror' (fail if there is at least 1 IO error), 'negative' (fail if there is at least 1 negative DNS answer), "+
"'error' (fail if there is at least 1 error DNS response), 'idmismatch' (fail there is at least 1 ID mismatch between DNS request and response).").
PlaceHolder(ioerrorFailCondition).
EnumsVar(&failConditions, ioerrorFailCondition, negativeFailCondition, errorFailCondition, idmismatchFailCondition)

pApp.Arg("queries", "Queries to issue. It can be a local file referenced using @<file-path>, for example @data/2-domains. "+
"It can also be resource accessible using HTTP, like https://raw.githubusercontent.com/Tantalor93/dnspyre/master/data/1000-domains, in that "+
"case, the file will be downloaded and saved in-memory. "+
Expand Down Expand Up @@ -183,6 +198,30 @@ func Execute() {
}

close(sigsInt)

if len(failConditions) > 0 {
stats := reporter.Merge(&benchmark, res)
for _, f := range failConditions {
switch f {
case ioerrorFailCondition:
if stats.Counters.IOError > 0 {
os.Exit(1)
}
case negativeFailCondition:
if stats.Counters.Negative > 0 {
os.Exit(1)
}
case errorFailCondition:
if stats.Counters.Error > 0 {
os.Exit(1)
}
case idmismatchFailCondition:
if stats.Counters.IDmismatch > 0 {
os.Exit(1)
}
}
}
}
}

func getSupportedDNSTypes() []string {
Expand Down
29 changes: 29 additions & 0 deletions docs/failoncondition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: Fail on condition
layout: default
parent: Examples
---



# Fail on condition
v3.1.0
{: .label .label-yellow }
*dnspyre* by default always returns a zero exit code, but this behaviour can be adjusted by using `--fail <condition>` flag,
which can be used to specify predefined conditions that will cause a *dnspyre* to return a non-zero exit code.

Currently, the dnspyre supports these fail conditions:
* `ioerror` = *dnspyre* exits with a non-zero status code if there is at least 1 IO error (*dnspyre* failed to send DNS request or receive DNS response)
* `negative` = *dnspyre* exits with a non-zero status code if there is at least 1 negative DNS answer (`NXDOMAIN` or `NODATA` response)
* `error` = *dnspyre* exits with a non-zero status code if there is at least 1 error DNS response (`SERVFAIL`, `FORMERR`, `REFUSED`, etc.)
* `idmismatch` = *dnspyre* exits with a non-zero status code if there is at least 1 ID mismatch between DNS request and response

So for example to return a non-zero exit code, when benchmark fails to send request or receive response you would specify `--fail ioerror` flag
```
dnspyre --server 1.2.3.4 google.com --fail ioerror
```

These fail conditions can be combined, this is achieved by repeating the `--flag` flag multiple times with different conditions.
```
dnspyre --server 8.8.8.8 nxdomain.cz --fail ioerror --fail error --fail negative
```
4 changes: 4 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ Flags:
duration, the benchmark is canceled. This option is exclusive with --number option. The duration is
specified in GO duration format e.g. 10s, 15m, 1h.
--[no-]progress Controls whether the progress bar is shown. Enabled by default.
--fail=ioerror ... Controls conditions upon which the dnspyre will exit with a non-zero exit code. Repeatable flag.
Supported options are 'ioerror' (fail if there is at least 1 IO error), 'negative' (fail if there is at
least 1 negative DNS answer), 'error' (fail if there is at least 1 error DNS response), 'idmismatch'
(fail there is at least 1 ID mismatch between DNS request and response).
--[no-]version Show application version.

Args:
Expand Down
Loading