The Seven Veils: Chain of Responsibility Audit Pipeline in Bash
A robust CI/CD and security audit script in pure Bash where an artifact passes through an extensible sequential tribunal of verification handlersβfailing fast or escalating telemetry without tight coupling.
#!/usr/bin/env bash
set -Eeuo pipefail
readonly HANDLERS=(veil_syntax veil_permissions veil_secrets veil_checksum)
run_pipeline() {
local target="$1"
for handler in "${HANDLERS[@]}"; do
echo "[TRIBUNAL] Invoking ${handler}..." >&2
"${handler}" "${target}" || { echo "[VEIL CONDEMNATION] Failed at ${handler}" >&2; return 1; }
done
echo "[TRIBUNAL ABSOLUTION] All veils traversed."
}
The Lineage to the Gang of Four Chain of Responsibility
In 1994, the Gang of Four defined the Chain of Responsibility Pattern:
βAvoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.β β Design Patterns, p. 223
In DevOps, pre-flight gatekeepers and artifact security scanners often degenerate into massive, monolithic scripts stuffed with brittle, nested if-else blocks:
# β οΈ THE MONOLITHIC SPAGHETTI ANTI-PATTERN
if check_permissions; then
if check_secrets; then
if check_syntax; then
...
fi
fi
fi
Adding a new verification step requires modifying the innermost core, increasing the blast radius of regressions.
The Seven Veils Tribunal structures security evaluation as a discrete chain of independent handler functions. Each handler:
- Inspects the passed target payload or file path.
- Performs its localized validation.
- Either passes control to the successor handler with an exit status of
0, or terminates the request early with rich diagnostic telemetry.
The Complete Bash Script
Save this script as /usr/local/bin/seven-veils-audit.sh and make it executable:
#!/usr/bin/env bash
# ==============================================================================
# SCRIPT: seven-veils-audit.sh
# PATTERN: Chain of Responsibility Pattern (Gang of Four Behavioral)
# ARCANUM: Divination // The Seven Veils Audit Tribunal
# DESCRIPTION: Sequential pipeline of decoupled security verification handlers.
# ==============================================================================
set -Eeuo pipefail
# ------------------------------------------------------------------------------
# 1. DISCRETE HANDLERS (THE VEILS)
# Each handler accepts: target_file -> returns 0 (Pass/Delegate) or 1+ (Halt)
# ------------------------------------------------------------------------------
veil_existence() {
local target="$1"
echo " [VEIL 1: EXISTENCE] Inspecting target reality..."
if [[ ! -e "${target}" ]]; then
echo " β ERROR: Target '${target}' does not exist in physical filesystem!" >&2
return 101
fi
return 0
}
veil_permissions() {
local target="$1"
echo " [VEIL 2: PERMISSIONS] Verifying octave mode permissions..."
local perms
perms=$(stat -c "%a" "${target}")
# Enforce no world-writable permissions (e.g. 777 or 666)
if [[ "${perms: -1}" =~ [2367] ]]; then
echo " β CRITICAL: World-writable bit detected: ${perms}" >&2
echo " Fix with: chmod o-w '${target}'" >&2
return 102
fi
return 0
}
veil_secrets() {
local target="$1"
echo " [VEIL 3: SECRETS] Scrying for exposed private keys and API tokens..."
if grep -Eq "(BEGIN PRIVATE KEY|AKIA[0-9A-Z]{16}|ghp_[a-zA-Z0-9]{36})" "${target}" 2>/dev/null; then
echo " β BREACH DETECTED: Plaintext cryptographic material or API secret found!" >&2
return 103
fi
return 0
}
veil_shebang_syntax() {
local target="$1"
echo " [VEIL 4: SYNTAX] Examining shebang header & parsing tokens..."
if head -n 1 "${target}" | grep -q "^#!\s*/bin/bash"; then
echo " β οΈ WARN: Hardcoded '/bin/bash' detected. Prefer '#!/usr/bin/env bash'." >&2
fi
# If it is a shell script, run syntax dry-run
if [[ "${target}" == *.sh ]]; then
bash -n "${target}" || {
echo " β SYNTAX ERROR: Bash AST parsing failed!" >&2
return 104
}
fi
return 0
}
veil_integrity_hash() {
local target="$1"
echo " [VEIL 5: INTEGRITY] Computing cryptographic fingerprint..."
local hash
hash=$(sha256sum "${target}" | awk '{print $1}')
echo " β SHA256: ${hash}"
return 0
}
# ------------------------------------------------------------------------------
# 2. THE CHAIN DISPATCH TABLE
# Handlers can be reordered, appended, or conditionally enabled at runtime.
# ------------------------------------------------------------------------------
declare -a AUDIT_CHAIN=(
veil_existence
veil_permissions
veil_secrets
veil_shebang_syntax
veil_integrity_hash
)
# ------------------------------------------------------------------------------
# 3. PIPELINE DISPATCH ORCHESTRATOR
# ------------------------------------------------------------------------------
execute_audit_chain() {
local target="$1"
echo "=========================================================="
echo "[TRIBUNAL] Commencing Seven Veils Audit for: ${target}"
echo "=========================================================="
for handler in "${AUDIT_CHAIN[@]}"; do
if ! "${handler}" "${target}"; then
local status=$?
echo "=========================================================="
echo "[CONDEMNATION] Pipeline halted by: ${handler} (Code ${status})"
echo "=========================================================="
return "${status}"
fi
done
echo "=========================================================="
echo "[ABSOLUTION] All ${#AUDIT_CHAIN[@]} Veils successfully traversed."
echo "[STATUS] Target verified for production deployment."
echo "=========================================================="
return 0
}
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <artifact_file>" >&2
exit 1
fi
execute_audit_chain "$1"
Sequence Execution Flow
Target Artifact
β
βΌ
ββββββββββββββββββββ
β veil_existence β ββ(Fails)βββΆ [Halt 101]
ββββββββββ¬ββββββββββ
β (Pass)
βΌ
ββββββββββββββββββββ
β veil_permissions β ββ(Fails)βββΆ [Halt 102]
ββββββββββ¬ββββββββββ
β (Pass)
βΌ
ββββββββββββββββββββ
β veil_secrets β ββ(Fails)βββΆ [Halt 103]
ββββββββββ¬ββββββββββ
β (Pass)
βΌ
ββββββββββββββββββββ
β veil_syntax β ββ(Fails)βββΆ [Halt 104]
ββββββββββ¬ββββββββββ
β (Pass)
βΌ
ββββββββββββββββββββ
β veil_integrity β
ββββββββββ¬ββββββββββ
β (Pass)
βΌ
β¨ [Absolution Granted]