7e0c764f3f
- Configurar permisos sudo completos para usuario thoth: * Editar /etc/sacc4/sacc4.env * Gestionar servicios api-sacc4-*.service * Editar archivos systemd * Control total de /opt/sacc4 - Eliminar acceso SSH abierto (0.0.0.0/0) - Agregar soporte AWS Systems Manager Session Manager - Actualizar llave SSH a sacc-prod-key-2026 - Preservar tags de scheduling (AutoStart/AutoStop) en EC2 y RDS - Agregar variable allowed_ssh_cidrs para acceso de emergencia BREAKING CHANGE: SSH restringido, usar Session Manager como acceso principal
195 lines
7.8 KiB
Bash
Executable File
195 lines
7.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# ===============================================================================================================
|
|
# user-data.sh - Script de inicialización de la EC2 para proyectosacc
|
|
# Descripción:
|
|
# Configura la instancia EC2 al primer boot: instala dependencias,
|
|
# crea usuarios, configura Nginx como proxy de API, y prepara
|
|
# directorios de despliegue.
|
|
#
|
|
# Autor: Área de Tecnología y Desarrollo - CCsoft
|
|
# ===============================================================================================================
|
|
|
|
set -euo pipefail
|
|
|
|
# -------------------------------------------------------------------------------
|
|
# Variables
|
|
# -------------------------------------------------------------------------------
|
|
PIPELINE_PUBLIC_KEY="${pipeline_public_key}"
|
|
|
|
# -------------------------------------------------------------------------------
|
|
# Actualizar sistema e instalar dependencias
|
|
# -------------------------------------------------------------------------------
|
|
apt-get update -y
|
|
apt-get install -y nginx openjdk-21-jdk awscli curl jq
|
|
|
|
# -------------------------------------------------------------------------------
|
|
# Instalar y verificar AWS Systems Manager Agent
|
|
# -------------------------------------------------------------------------------
|
|
# SSM Agent permite acceso seguro sin abrir puertos SSH (0.0.0.0/0)
|
|
# Pre-instalado en Amazon Linux; en Ubuntu puede requerir instalación manual
|
|
apt-get install -y amazon-ssm-agent 2>/dev/null || true
|
|
|
|
# Si el paquete no está disponible en repositorios, descargar desde AWS
|
|
if ! command -v amazon-ssm-agent &> /dev/null; then
|
|
echo "Descargando SSM Agent desde AWS..."
|
|
curl -fsSL -o /tmp/amazon-ssm-agent.deb https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_amd64/amazon-ssm-agent.deb
|
|
dpkg -i /tmp/amazon-ssm-agent.deb || true
|
|
rm -f /tmp/amazon-ssm-agent.deb
|
|
fi
|
|
|
|
# Asegurar que el servicio esté habilitado y corriendo
|
|
systemctl enable amazon-ssm-agent || true
|
|
systemctl restart amazon-ssm-agent || true
|
|
|
|
# Verificar estado del agente
|
|
if systemctl is-active --quiet amazon-ssm-agent; then
|
|
echo "SSM Agent instalado y activo correctamente"
|
|
else
|
|
echo "ADVERTENCIA: No se pudo iniciar SSM Agent. Verificar conectividad a AWS y permisos IAM."
|
|
fi
|
|
|
|
# -------------------------------------------------------------------------------
|
|
# Crear usuarios del sistema
|
|
# -------------------------------------------------------------------------------
|
|
useradd -m -s /bin/bash thoth || true
|
|
useradd -m -s /bin/bash osiris || true
|
|
|
|
# -------------------------------------------------------------------------------
|
|
# Configurar SSH para el pipeline (usuario thoth)
|
|
# -------------------------------------------------------------------------------
|
|
mkdir -p /home/thoth/.ssh
|
|
chmod 700 /home/thoth/.ssh
|
|
|
|
echo "$PIPELINE_PUBLIC_KEY" > /home/thoth/.ssh/authorized_keys
|
|
chmod 600 /home/thoth/.ssh/authorized_keys
|
|
chown -R thoth:thoth /home/thoth/.ssh
|
|
|
|
# -------------------------------------------------------------------------------
|
|
# Configurar permisos sudo para usuario thoth (SACC4 Application Management)
|
|
# -------------------------------------------------------------------------------
|
|
cat > /etc/sudoers.d/thoth <<'SUDOERS_EOF'
|
|
# SACC4 Application Management Permissions
|
|
# User: thoth
|
|
# Generated: $(date)
|
|
|
|
# 1. Editar archivo de configuracion de la aplicacion
|
|
thoth ALL=(ALL) NOPASSWD: /usr/bin/nano /etc/sacc4/sacc4.env
|
|
thoth ALL=(ALL) NOPASSWD: /usr/bin/vim /etc/sacc4/sacc4.env
|
|
thoth ALL=(ALL) NOPASSWD: /usr/bin/vi /etc/sacc4/sacc4.env
|
|
|
|
# 2. Gestionar servicios systemd
|
|
thoth ALL=(ALL) NOPASSWD: /usr/bin/systemctl status api-sacc4-*.service
|
|
thoth ALL=(ALL) NOPASSWD: /usr/bin/systemctl start api-sacc4-*.service
|
|
thoth ALL=(ALL) NOPASSWD: /usr/bin/systemctl stop api-sacc4-*.service
|
|
thoth ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart api-sacc4-*.service
|
|
thoth ALL=(ALL) NOPASSWD: /usr/bin/systemctl enable api-sacc4-*.service
|
|
thoth ALL=(ALL) NOPASSWD: /usr/bin/systemctl disable api-sacc4-*.service
|
|
thoth ALL=(ALL) NOPASSWD: /usr/bin/journalctl -u api-sacc4-*.service
|
|
|
|
# 3. Editar archivos de servicios systemd
|
|
thoth ALL=(ALL) NOPASSWD: /usr/bin/nano /etc/systemd/system/api-sacc4-*.service
|
|
thoth ALL=(ALL) NOPASSWD: /usr/bin/vim /etc/systemd/system/api-sacc4-*.service
|
|
thoth ALL=(ALL) NOPASSWD: /usr/bin/vi /etc/systemd/system/api-sacc4-*.service
|
|
|
|
# 4. Recargar systemd daemon
|
|
thoth ALL=(ALL) NOPASSWD: /usr/bin/systemctl daemon-reload
|
|
|
|
# 5. Control total del directorio /opt/sacc4
|
|
thoth ALL=(ALL) NOPASSWD: /bin/ls -la /opt/sacc4/*
|
|
thoth ALL=(ALL) NOPASSWD: /bin/chown -R thoth\:thoth /opt/sacc4/*
|
|
thoth ALL=(ALL) NOPASSWD: /bin/chmod -R [0-7][0-7][0-7] /opt/sacc4/*
|
|
thoth ALL=(ALL) NOPASSWD: /bin/mkdir -p /opt/sacc4/*
|
|
thoth ALL=(ALL) NOPASSWD: /bin/rm -rf /opt/sacc4/*
|
|
thoth ALL=(ALL) NOPASSWD: /bin/cp -r * /opt/sacc4/*
|
|
thoth ALL=(ALL) NOPASSWD: /bin/mv * /opt/sacc4/*
|
|
SUDOERS_EOF
|
|
|
|
chmod 440 /etc/sudoers.d/thoth
|
|
chown root:root /etc/sudoers.d/thoth
|
|
|
|
# Validar sintaxis del archivo sudoers
|
|
visudo -c || echo "ADVERTENCIA: Error en sintaxis de /etc/sudoers.d/thoth"
|
|
|
|
# -------------------------------------------------------------------------------
|
|
# Crear estructura de directorios de despliegue
|
|
# -------------------------------------------------------------------------------
|
|
mkdir -p /home/thoth/deploy/artifacts/{backup,current,logs,pids}
|
|
mkdir -p /home/thoth/deploy/{scripts,setup}
|
|
chown -R thoth:thoth /home/thoth/deploy
|
|
|
|
mkdir -p /var/log/proyectosacc/proyectosacc-app
|
|
chown -R osiris:osiris /var/log/proyectosacc
|
|
|
|
# -------------------------------------------------------------------------------
|
|
# Configurar Nginx como proxy inverso SOLO para la API
|
|
# -------------------------------------------------------------------------------
|
|
cat > /etc/nginx/sites-available/proyectosacc-api <<'NGINX_EOF'
|
|
server {
|
|
listen 80 default_server;
|
|
listen [::]:80 default_server;
|
|
server_name _;
|
|
|
|
location /api/ {
|
|
proxy_pass http://localhost:8080/;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_connect_timeout 60s;
|
|
proxy_send_timeout 60s;
|
|
proxy_read_timeout 60s;
|
|
}
|
|
|
|
location / {
|
|
return 404;
|
|
}
|
|
|
|
error_page 500 502 503 504 /50x.html;
|
|
location = /50x.html {
|
|
root /usr/share/nginx/html;
|
|
}
|
|
}
|
|
NGINX_EOF
|
|
|
|
rm -f /etc/nginx/sites-enabled/default
|
|
ln -sf /etc/nginx/sites-available/proyectosacc-api /etc/nginx/sites-enabled/proyectosacc-api
|
|
|
|
nginx -t
|
|
systemctl enable nginx
|
|
systemctl restart nginx
|
|
|
|
# -------------------------------------------------------------------------------
|
|
# Crear servicio systemd template para la API (será sobrescrito por deploy.sh)
|
|
# -------------------------------------------------------------------------------
|
|
cat > /etc/systemd/system/proyectosacc-app.service <<'SYSTEMD_EOF'
|
|
[Unit]
|
|
Description=Proyecto SACC App Service
|
|
After=network.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
User=osiris
|
|
Group=osiris
|
|
WorkingDirectory=/home/thoth/deploy/artifacts/current
|
|
ExecStart=/usr/bin/java -jar /home/thoth/deploy/artifacts/current/proyectosacc-app.jar
|
|
SuccessExitStatus=143
|
|
Restart=on-failure
|
|
RestartSec=10
|
|
StandardOutput=append:/var/log/proyectosacc/proyectosacc-app/proyectosacc-app-service.log
|
|
StandardError=append:/var/log/proyectosacc/proyectosacc-app/proyectosacc-app-service.log
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
SYSTEMD_EOF
|
|
|
|
systemctl daemon-reload
|
|
systemctl enable proyectosacc-app.service || true
|
|
|
|
# -------------------------------------------------------------------------------
|
|
# Ajustar permisos finales
|
|
# -------------------------------------------------------------------------------
|
|
usermod -aG osiris thoth || true
|
|
chown -R osiris:osiris /home/thoth/deploy/artifacts
|
|
chmod 750 /home/thoth/deploy/artifacts
|