test(hack): check hook scripts under files/ for --request-timeout (#5360)

## What this PR does

Closes #5304

`hack/check-hook-request-timeout.bats` built its candidate list with
`grep -rl 'kubectl' packages/*/*/templates/`. A chart that loads its
hook script with `.Files.Get "files/<name>.sh"` runs that script with
the same in-cluster credentials, but the guard never looked at it.
That's how the flag got through in the first revision of #4915. The
guard also matched only `--request-timeout=<value>`, while kubectl also
accepts `--request-timeout <value>`.

Changes:
- Candidates now include shell scripts under `packages/*/*/files/`,
recognised by a `.sh` suffix or a `sh`/`bash`/`dash` shebang, also
through `env` or `env -S`. Other files there (CAPI component manifests,
YAML defaults) are left out, as the issue suggests. Files that name
`KUBECONFIG` are still skipped.
- The pattern now catches the space form as well:
`--request-timeout(=["']?[^0[:space:]"']|[[:space:]]+["']?[1-9])`, which
also covers quoted values such as `--request-timeout "30s"`. The space
form only counts when a non-zero number follows. Several templates have
comments like "a non-zero --request-timeout on an in-cluster kubectl
call", and a looser pattern would flag them.
- The scan is split into small functions. Seven new cases run it on a
throwaway tree under `$BATS_TEST_TMPDIR`:
- a `files/hook.sh` with `--request-timeout=30s` is reported, which is
the case from the issue;
  - a script without the `.sh` suffix is picked up through its shebang;
  - `--request-timeout 30s` and `--request-timeout=5` are reported;
  - quoted `"30s"` and `'30s'` values are reported;
- an `#!/usr/bin/env -S bash -e` script is collected, and an `env -S
python3` script is not;
- `=0`, ` 0s`, prose in a comment, a script that sets `KUBECONFIG`, and
a non-script YAML under `files/` are not reported.

The existing repository-wide case still passes on main with the wider
scope. `cilium/files/ipam-migration.sh` and both seaweedfs hook scripts
are now checked, and none of them uses the flag.

### How I checked it

`bats hack/check-hook-request-timeout.bats` (bats 1.12.0): 8/8 pass.

### Screenshots

Not a UI change.

### Downstream repositories

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

This is a repository-internal test only.

### Release note

```release-note
NONE
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Tests**
* Expanded automated checks for request-timeout options in chart
templates and eligible shell scripts, covering both
`--request-timeout=value` and `--request-timeout value` formats,
including quoted values.
* Added coverage for zero-valued timeouts, prose mentions, files that
reference `KUBECONFIG`, and non-shell files.
* The check continues to require a minimum number of candidate files and
report files with non-zero timeout values.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Aleksei Sviridkin authored and GitHub committed 2026-11-23 11:58:30 +03:00
commit cac0bb9b28
1 file changed
+102 -16
+102 -16
View File
@@ -11,7 +11,11 @@
# packages before it was found (#4276, #4288), and in one of them the hook
# reported success while doing nothing.
#
# Scope and its limit. A file that names KUBECONFIG is skipped: it hands kubectl
# Scope and its limit. Chart templates are checked, and so are shell scripts
# kept under packages/*/*/files/: a chart that loads its hook script with
# .Files.Get runs it with the same in-cluster credentials. Both the
# --request-timeout=<value> and the --request-timeout <value> forms are caught.
# A file that names KUBECONFIG is skipped: it hands kubectl
# an explicit kubeconfig, so the in-cluster branch is not what serves it and the
# flag is harmless there. That is a per-file judgement, not per-command, which
# is the coarse part of this check and is why it is written down rather than
@@ -26,11 +30,51 @@ load test_helper
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
# A non-zero value in either form, quoted or not. The space form needs a digit
# after it, so prose such as "a non-zero --request-timeout on a call" in a
# comment is not a hit; the = form keeps the rule it always had.
REQUEST_TIMEOUT_RE="--request-timeout(=[\"']?[^0[:space:]\"']|[[:space:]]+[\"']?[1-9])"
# is_shell_script FILE: a .sh file, or one whose first line is a sh/bash
# shebang, directly or through env, `env -S` included.
is_shell_script() {
case "$1" in *.sh) return 0 ;; esac
head -n 1 "$1" | grep -qE '^#!.*/(env([[:space:]]+-S)?[[:space:]]+)?(ba|da)?sh([[:space:]]|$)'
}
# request_timeout_candidates ROOT: non-vendored templates and files/ shell
# scripts under ROOT that invoke kubectl, one path per line.
request_timeout_candidates() {
local root="$1" f
grep -rl 'kubectl' "$root"/packages/*/*/templates/ 2>/dev/null || true
for f in $(grep -rl 'kubectl' "$root"/packages/*/*/files/ 2>/dev/null || true); do
if is_shell_script "$f"; then
echo "$f"
fi
done
}
# request_timeout_offenders FILE...: the files, with matching lines, that pass
# a non-zero --request-timeout without naming KUBECONFIG.
request_timeout_offenders() {
local f hits
for f in "$@"; do
# An explicit kubeconfig means the in-cluster branch is not in play here.
if grep -q 'KUBECONFIG' "$f"; then
continue
fi
hits="$(grep -nE -- "$REQUEST_TIMEOUT_RE" "$f" || true)"
if [ -n "$hits" ]; then
printf '%s\n%s\n' "$f" "$hits"
fi
done
}
@test "no in-cluster kubectl call carries a non-zero --request-timeout" {
cd "$REPO_ROOT"
# Files in scope: non-vendored templates that invoke kubectl.
candidates="$(grep -rl 'kubectl' packages/*/*/templates/ 2>/dev/null || true)"
# Files in scope: non-vendored templates and files/ shell scripts that invoke kubectl.
candidates="$(request_timeout_candidates .)"
[ -n "$candidates" ] || { echo "FAIL: found no templates invoking kubectl; this check is broken, not clean"; false; }
# Guard against a vacuous pass: the tree ships many such templates, so a
@@ -38,19 +82,8 @@ REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
count="$(printf '%s\n' "$candidates" | grep -c . || true)"
[ "$count" -ge 10 ] || { echo "FAIL: only $count template(s) invoke kubectl; expected the tree's usual population"; false; }
offenders=""
for f in $candidates; do
# An explicit kubeconfig means the in-cluster branch is not in play here.
if grep -q 'KUBECONFIG' "$f"; then
continue
fi
hits="$(grep -nE -- '--request-timeout=[^0[:space:]]' "$f" || true)"
if [ -n "$hits" ]; then
offenders="${offenders}${f}
${hits}
"
fi
done
# shellcheck disable=SC2086 # paths in this tree carry no whitespace
offenders="$(request_timeout_offenders $candidates)"
if [ -n "$offenders" ]; then
echo "FAIL: non-zero --request-timeout on a call that depends on in-cluster credentials."
@@ -60,3 +93,56 @@ ${hits}
false
fi
}
# The cases below run the same scan on a throwaway tree, so they pin what it
# catches and what it leaves alone independently of the repository's content.
scan_tree() {
# shellcheck disable=SC2046
request_timeout_offenders $(request_timeout_candidates "$BATS_TEST_TMPDIR/tree")
}
write_tree_file() {
mkdir -p "$(dirname "$BATS_TEST_TMPDIR/tree/$1")"
printf '%s\n' "$2" > "$BATS_TEST_TMPDIR/tree/$1"
}
@test "a hook script under files/ is checked" {
write_tree_file packages/x/y/files/hook.sh 'kubectl get pods --request-timeout=30s'
[ -n "$(scan_tree)" ]
}
@test "a script under files/ without a .sh suffix is checked through its shebang" {
write_tree_file packages/x/y/files/hook "$(printf '#!/bin/sh\nkubectl get pods --request-timeout=30s')"
[ -n "$(scan_tree)" ]
}
@test "the space-separated form is caught" {
write_tree_file packages/x/y/files/hook.sh 'kubectl get pods --request-timeout 30s'
[ -n "$(scan_tree)" ]
write_tree_file packages/x/y/files/hook.sh 'kubectl get pods --request-timeout=5'
[ -n "$(scan_tree)" ]
}
@test "quoted values are caught" {
write_tree_file packages/x/y/files/hook.sh 'kubectl get pods --request-timeout "30s"'
[ -n "$(scan_tree)" ]
write_tree_file packages/x/y/files/hook.sh "kubectl get pods --request-timeout='30s'"
[ -n "$(scan_tree)" ]
}
@test "a script with an env -S shebang is checked" {
write_tree_file packages/x/y/files/hook "$(printf '#!/usr/bin/env -S bash -e\nkubectl get pods --request-timeout=30s')"
[ -n "$(scan_tree)" ]
}
@test "a script for another interpreter is not collected" {
write_tree_file packages/x/y/files/hook "$(printf '#!/usr/bin/env -S python3\n# kubectl get pods --request-timeout=30s')"
[ -z "$(scan_tree)" ]
}
@test "zero values, prose and kubeconfig users are left alone" {
write_tree_file packages/x/y/files/hook.sh "$(printf 'kubectl get pods --request-timeout=0\nkubectl get pods --request-timeout 0s\nkubectl get pods --request-timeout "0"\n# a non-zero --request-timeout on a call is diverted')"
write_tree_file packages/x/y/files/external.sh "$(printf 'export KUBECONFIG=/etc/kubeconfig\nkubectl get pods --request-timeout=30s')"
write_tree_file packages/x/y/files/manifest.yaml 'command: ["kubectl", "get", "pods", "--request-timeout=30s"]'
[ -z "$(scan_tree)" ]
}