Build a configurable detection
Validate a small policy file as data before using its threshold.
Configuration is not executable code
A policy file lets an analyst change a threshold without editing the detection. That does not mean it should be sourced as a shell script. source policy.conf would execute substitutions and commands with the script’s permissions. Parse a narrow format and reject everything outside it.
Here the only accepted setting is failure_threshold=N, with N a single digit from 1 through 9. Blank lines and full-line comments are allowed. Unknown keys, duplicate settings, whitespace around the assignment and expressions are rejected. A small explicit grammar is easier to review than permissive guessing.
Validate before producing findings
Check that the policy file exists and that parsing succeeds before writing the output header. Capture the parser’s status as well as its output. A malformed threshold should never silently become zero and select every group.
Pass the validated numeric value to awk through -v, keeping program text constant. Count failed authentication events by user and IP as before. Tests change the threshold and include a command-shaped value; the value must be rejected as data, with no marker file created. This pattern keeps policy flexibility separate from command execution.
printf 'Policy preview:\n'
cat policy.conf
Put it into practice.
Accept an optional first argument naming the policy file, default policy.conf. It must contain exactly one failure_threshold=N with N a single digit 1–9; allow blank lines and lines beginning #, reject all other content, duplicates and whitespace around the assignment. Missing or invalid policy exits nonzero with Invalid policy on stderr and no stdout. Never execute policy text. For validated auth.tsv rows, print user<TAB>ip<TAB>failures for groups reaching the configured FAIL threshold, sorted in LC_ALL=C order.
See an explained solution
export LC_ALL=C
policy=${1:-policy.conf}
[ -f "$policy" ] || { printf 'Invalid policy\n' >&2; exit 1; }
threshold=$(awk -F '=' '
/^[[:space:]]*$/ || /^#/ {next}
{if(NF!=2 || $1!="failure_threshold" || $2!~/^[1-9]$/ || seen++) bad=1;else value=$2}
END {if(bad || !seen) exit 1;print value}
' "$policy") || { printf 'Invalid policy\n' >&2; exit 1; }
printf 'user\tip\tfailures\n'
awk -F '\t' -v threshold="$threshold" 'NR>1 && $4=="FAIL" {k=$2 SUBSEP $3;n[k]++;u[k]=$2;ip[k]=$3} END {for(k in n) if(n[k]>=threshold) printf "%s\t%s\t%d\n",u[k],ip[k],n[k]}' auth.tsv | sort
The parser accepts a single reviewed setting and rejects ambiguous or executable-looking values. Only a validated digit reaches the counting program, so malformed policy cannot become an accidental match-all threshold.