Fix two audit findings in install/uninstall hook management

- Extract hook comment string to _HOOK_COMMENT variable in both
  install.sh and uninstall.sh; previously the literal string
  'TrueCloud provider patch (S3/B2)' appeared three times across two
  files with no shared constant — a silent mismatch on any divergence
  would cause hook lookup to return empty with no error output
- Wrap midclt update and create calls with if/else error handlers;
  previously a midclt failure under set -euo pipefail silently aborted
  the script at "Updating path and enabling ..." with no diagnostic
  or recovery guidance
This commit is contained in:
2026-06-15 17:43:46 +00:00
parent 6a8ed7fa67
commit d97b87bc5d
2 changed files with 16 additions and 7 deletions
+14 -6
View File
@@ -18,6 +18,7 @@ set -euo pipefail
# The directory containing install.sh is the permanent install location. # The directory containing install.sh is the permanent install location.
PATCH_DIR="$(cd "$(dirname "$0")" && pwd)" PATCH_DIR="$(cd "$(dirname "$0")" && pwd)"
_HOOK_COMMENT='TrueCloud provider patch (S3/B2)'
if [ ! -f "$PATCH_DIR/patch/sitecustomize.py" ]; then if [ ! -f "$PATCH_DIR/patch/sitecustomize.py" ]; then
echo "ERROR: patch files not found at $PATCH_DIR/patch/" >&2 echo "ERROR: patch files not found at $PATCH_DIR/patch/" >&2
@@ -64,19 +65,26 @@ EXISTING_ID=$(midclt call initshutdownscript.query '[]' | \
python3 -c " python3 -c "
import sys, json import sys, json
for s in json.load(sys.stdin): for s in json.load(sys.stdin):
if s.get('comment') == 'TrueCloud provider patch (S3/B2)': if s.get('comment') == '$_HOOK_COMMENT':
print(s['id']) print(s['id'])
break break
" 2>/dev/null || true) " 2>/dev/null || true)
if [ -n "$EXISTING_ID" ]; then if [ -n "$EXISTING_ID" ]; then
echo "Already registered (id=$EXISTING_ID). Updating path and enabling ..." echo "Already registered (id=$EXISTING_ID). Updating path and enabling ..."
midclt call initshutdownscript.update "$EXISTING_ID" \ if ! midclt call initshutdownscript.update "$EXISTING_ID" \
"{\"enabled\": true, \"script\": \"$PATCH_DIR/patch/apply.sh\"}" > /dev/null "{\"enabled\": true, \"script\": \"$PATCH_DIR/patch/apply.sh\"}" > /dev/null; then
echo "ERROR: Failed to update PREINIT hook (id=$EXISTING_ID)." >&2
echo " midclt call initshutdownscript.query '[]'" >&2
exit 1
fi
else else
midclt call initshutdownscript.create \ if ! midclt call initshutdownscript.create \
"{\"type\":\"SCRIPT\",\"script\":\"$PATCH_DIR/patch/apply.sh\",\"when\":\"PREINIT\",\"enabled\":true,\"comment\":\"TrueCloud provider patch (S3/B2)\"}" \ "{\"type\":\"SCRIPT\",\"script\":\"$PATCH_DIR/patch/apply.sh\",\"when\":\"PREINIT\",\"enabled\":true,\"comment\":\"$_HOOK_COMMENT\"}" \
> /dev/null > /dev/null; then
echo "ERROR: Failed to register PREINIT hook." >&2
exit 1
fi
echo "Registered." echo "Registered."
fi fi
echo "" echo ""
+2 -1
View File
@@ -4,6 +4,7 @@
set -euo pipefail set -euo pipefail
PATCH_DIR="$(cd "$(dirname "$0")" && pwd)" PATCH_DIR="$(cd "$(dirname "$0")" && pwd)"
_HOOK_COMMENT='TrueCloud provider patch (S3/B2)'
echo "=== TrueNAS TrueCloud Provider Patch — Uninstall ===" echo "=== TrueNAS TrueCloud Provider Patch — Uninstall ==="
echo "" echo ""
@@ -26,7 +27,7 @@ IDS=$(midclt call initshutdownscript.query '[]' | \
python3 -c " python3 -c "
import sys, json import sys, json
for s in json.load(sys.stdin): for s in json.load(sys.stdin):
if s.get('comment') == 'TrueCloud provider patch (S3/B2)': if s.get('comment') == '$_HOOK_COMMENT':
print(s['id']) print(s['id'])
" 2>/dev/null || true) " 2>/dev/null || true)