mirror of
https://github.com/angristan/openvpn-install.git
synced 2025-12-14 16:17:03 +01:00
## Summary - Add comprehensive logging system with color-coded log levels ([INFO], [WARN], [ERROR], [OK]) - Wrap all command executions with `run_cmd()` to capture output and prevent leaks to stdout - Add file logging with timestamps (default: `openvpn-install.log`) - Suppress interactive prompts in auto-install mode for cleaner CI/scripted usage - Show log file location hint on errors for easier debugging ## Changes - **openvpn-install.sh**: New logging functions (`log_info`, `log_warn`, `log_error`, `log_fatal`, `log_success`, `log_prompt`, `log_header`, `log_menu`, `run_cmd`), all `echo` statements converted to use logging functions - **test/validate-output.sh**: New E2E validator that ensures all script output uses proper log formatting (catches raw echo leaks) - **test/server-entrypoint.sh**: Integrates output validation into Docker tests - **test/Dockerfile.server**: Copies validation script into container ## Configuration - `VERBOSE=1` - Show command output in terminal - `LOG_FILE=path` - Customize log location (default: `openvpn-install.log`) - `LOG_FILE=""` - Disable file logging - `FORCE_COLOR=1` - Force colored output in non-TTY environments
88 lines
2.3 KiB
Bash
Executable File
88 lines
2.3 KiB
Bash
Executable File
#!/bin/bash
|
|
# Validates that script output only contains properly formatted log messages
|
|
# All output from openvpn-install.sh should use logging functions
|
|
#
|
|
# Usage: ./validate-output.sh <output_file>
|
|
# Or pipe: some_command | ./validate-output.sh
|
|
|
|
set -euo pipefail
|
|
|
|
INPUT_FILE="${1:-/dev/stdin}"
|
|
|
|
# Valid output patterns:
|
|
# - Lines starting with ANSI escape codes (colored output)
|
|
# - Lines starting with our log prefixes (non-TTY mode)
|
|
# - Lines starting with > (command echo from run_cmd)
|
|
# - Empty lines
|
|
|
|
# ANSI escape code pattern
|
|
ANSI_PATTERN=$'^\033\\['
|
|
|
|
# Log prefix patterns (for non-TTY mode where colors are disabled)
|
|
# These match: [INFO], [WARN], [ERROR], [OK], [DEBUG], or > (command line)
|
|
LOG_PREFIXES='^(\[INFO\]|\[WARN\]|\[ERROR\]|\[OK\]|\[DEBUG\]|> )'
|
|
|
|
# Count issues
|
|
INVALID_LINES=0
|
|
TOTAL_LINES=0
|
|
LINE_NUM=0
|
|
|
|
echo "Validating script output for unformatted lines..."
|
|
echo ""
|
|
|
|
while IFS= read -r line || [[ -n "$line" ]]; do
|
|
LINE_NUM=$((LINE_NUM + 1))
|
|
|
|
# Skip empty lines
|
|
if [[ -z "$line" ]]; then
|
|
continue
|
|
fi
|
|
|
|
TOTAL_LINES=$((TOTAL_LINES + 1))
|
|
|
|
# Check if line starts with ANSI escape code (colored output from log functions)
|
|
if [[ "$line" =~ $ANSI_PATTERN ]]; then
|
|
continue
|
|
fi
|
|
|
|
# Check if line starts with our log prefixes (non-TTY mode)
|
|
if [[ "$line" =~ $LOG_PREFIXES ]]; then
|
|
continue
|
|
fi
|
|
|
|
# If we get here, the line doesn't match expected patterns - it's raw output
|
|
INVALID_LINES=$((INVALID_LINES + 1))
|
|
# Truncate long lines for display
|
|
if [[ ${#line} -gt 100 ]]; then
|
|
DISPLAY_LINE="${line:0:100}..."
|
|
else
|
|
DISPLAY_LINE="$line"
|
|
fi
|
|
echo " [LEAK] Line $LINE_NUM: $DISPLAY_LINE"
|
|
|
|
done <"$INPUT_FILE"
|
|
|
|
echo ""
|
|
echo "----------------------------------------"
|
|
echo "Total lines checked: $TOTAL_LINES"
|
|
echo "Invalid lines found: $INVALID_LINES"
|
|
|
|
if [[ $INVALID_LINES -gt 0 ]]; then
|
|
echo ""
|
|
echo "ERROR: Found $INVALID_LINES line(s) without proper log formatting."
|
|
echo ""
|
|
echo "All user-visible output should use log_* functions:"
|
|
echo " - log_info 'message' -> [INFO] message"
|
|
echo " - log_warn 'message' -> [WARN] message"
|
|
echo " - log_error 'message' -> [ERROR] message"
|
|
echo " - log_success 'message' -> [OK] message"
|
|
echo " - run_cmd 'desc' cmd -> > cmd"
|
|
echo ""
|
|
echo "Raw echo statements or command output should not leak to stdout."
|
|
exit 1
|
|
fi
|
|
|
|
echo ""
|
|
echo "All output is properly formatted!"
|
|
exit 0
|