#!/bin/bash
# GEP MCP Tool: Configuration Update
#
# Description: Update service configuration files with validation
# GEP Profile: Medium entropy (0.45), write operation, idempotent
# Risk Level: 3 (moderate)
#
# Usage: config_update.sh <service_name> <config_key> <config_value>
set -euo pipefail
SERVICE="${1:-}"
CONFIG_KEY="${2:-}"
CONFIG_VALUE="${3:-}"
# Validation
if [ -z "$SERVICE" ] || [ -z "$CONFIG_KEY" ] || [ -z "$CONFIG_VALUE" ]; then
echo '{"error": "Missing required parameters: service, key, value"}' >&2
exit 1
fi
# Whitelist of allowed services
ALLOWED_SERVICES=("nexus" "nginx" "postgresql" "redis")
if [[ ! " ${ALLOWED_SERVICES[@]} " =~ " ${SERVICE} " ]]; then
echo "{\"error\": \"Service '$SERVICE' not in whitelist\"}" >&2
exit 1
fi
# Config file path (adjust for your system)
CONFIG_DIR="/etc/nexus/conf.d"
CONFIG_FILE="$CONFIG_DIR/${SERVICE}.conf"
# Ensure config directory exists
mkdir -p "$CONFIG_DIR"
# Backup existing config
if [ -f "$CONFIG_FILE" ]; then
BACKUP_FILE="${CONFIG_FILE}.backup.$(date +%Y%m%d_%H%M%S)"
cp "$CONFIG_FILE" "$BACKUP_FILE"
fi
# Create or update config
if [ -f "$CONFIG_FILE" ]; then
# Update existing key or append
if grep -q "^${CONFIG_KEY}=" "$CONFIG_FILE"; then
sed -i "s/^${CONFIG_KEY}=.*/${CONFIG_KEY}=${CONFIG_VALUE}/" "$CONFIG_FILE"
else
echo "${CONFIG_KEY}=${CONFIG_VALUE}" >> "$CONFIG_FILE"
fi
else
# Create new config
echo "${CONFIG_KEY}=${CONFIG_VALUE}" > "$CONFIG_FILE"
fi
# Validate config syntax (service-specific)
case "$SERVICE" in
nginx)
if command -v nginx &> /dev/null; then
nginx -t 2>&1 || {
# Restore backup on validation failure
[ -f "$BACKUP_FILE" ] && mv "$BACKUP_FILE" "$CONFIG_FILE"
echo '{"error": "Config validation failed, restored backup"}' >&2
exit 1
}
fi
;;
postgresql)
# PostgreSQL config validation would go here
;;
esac
# Return success
cat <<EOF
{
"status": "success",
"service": "$SERVICE",
"key": "$CONFIG_KEY",
"value": "$CONFIG_VALUE",
"config_file": "$CONFIG_FILE",
"backup_file": "${BACKUP_FILE:-none}",
"timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
}
EOF