Skip to content

fix: harden shell scripts and fix db_copy loop bug#3159

Open
rhdh-bot wants to merge 4 commits into
mainfrom
fix/shell-script-smells
Open

fix: harden shell scripts and fix db_copy loop bug#3159
rhdh-bot wants to merge 4 commits into
mainfrom
fix/shell-script-smells

Conversation

@rhdh-bot

@rhdh-bot rhdh-bot commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Summary

ShellCheck audit of repository shell scripts found no parse errors (including no "trailing ] outside test" issues), but manual review surfaced one functional bug and several hardening opportunities. This PR addresses all of them.

Critical fix: hack/db_copy.sh

The migration loop contained:

for db in "${allDB[@]}"; do
  db=${allDB["$db"]}   # bug: uses DB name as array index

allDB is a regular indexed array. Using a string like backstage_plugin_auth as a subscript causes bash to fall back to index 0, so every iteration copied backstage_plugin_app instead of the intended six Backstage plugin databases.

Fix: remove the erroneous reassignment; the for loop variable already holds the correct name.

Hardening: config/profile/rhdh/plugin-infra/plugin-infra.sh

  • Enable set -euo pipefail (was set -e only).
  • Replace [ ... == ... ] with [[ ... ]] for bash-native string/boolean comparisons.

Hardening: config/profile/rhdh/plugin-infra/gitops-secret-setup.sh

  • Enable set -euo pipefail.
  • Initialize selected_instance="" so set -u is safe when no ArgoCD instance exists.
  • Replace eval "$cmd" in createBackstageSecret with a command array — avoids shell metacharacter injection if secret values contain special characters.
  • Use ${VAR:-} for optional variables; quote ARGOCD_URL.
  • Adopt name() function style (consistent with other scripts).

Hardening: config/profile/rhdh/plugin-deps/tekton.yaml

Embedded Tekton step scripts updated:

Task step Change
flattener Remove unused ROOT/TARGET vars; add shebang + set -eu; fix [ ... == ... ][ ... = ... ]
build-manifests Add shebang + set -eu
build-gitops Add shebang + set -eu; quote paths/args; cd kustomize || exit

Analysis for reviewers

File Risk before Risk after
hack/db_copy.sh Data loss / wrong DB copied on every run after first DB name Each of 6 plugin DBs migrated correctly
gitops-secret-setup.sh eval could break or inject on unusual secret values Direct array invocation
plugin-infra.sh Unset vars silently expand to empty strings Fail-fast on unset vars
tekton.yaml Unquoted globs, silent cd failure Safer inline shell for CI pipelines

Scope: shell scripts and Tekton inline scripts only — no Go operator logic, CRDs, or bundle changes.

Validation run locally:

  • shellcheck -x -a on all changed .sh files — clean
  • bash -n syntax check — clean

Test plan

  • shellcheck -x -a hack/db_copy.sh config/profile/rhdh/plugin-infra/*.sh
  • Review gitops-secret-setup.sh array-based secret creation (no behavior change for normal values)
  • Confirm Tekton pipeline steps still render valid YAML (no indentation regressions)
  • Optional: dry-run hack/db_copy.sh loop logic — verify six distinct DB names are iterated

Made with Cursor

Correct a logic error in hack/db_copy.sh that caused every database
migration to target backstage_plugin_app, and apply shellcheck-driven
hardening across plugin-infra setup scripts and Tekton inline scripts.

Assisted-by: cursor
Co-authored-by: Cursor <cursoragent@cursor.com>
@rhdh-bot rhdh-bot requested a review from a team as a code owner July 7, 2026 18:34
@openshift-ci openshift-ci Bot requested review from subhashkhileri and zdrapela July 7, 2026 18:35
rhdh-bot and others added 3 commits July 7, 2026 15:44
Address SonarCloud warnings about camelCase function names introduced
during shell script hardening.

Assisted-by: cursor
Co-authored-by: Cursor <cursoragent@cursor.com>
Use snake_case function names and bash [[ ]] conditionals throughout
gitops-secret-setup.sh; align plugin-infra.sh file test with [[ ]].

Assisted-by: cursor
Co-authored-by: Cursor <cursoragent@cursor.com>
@sonarqubecloud

sonarqubecloud Bot commented Jul 7, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant