feat(terraform): agregar permisos sudo para thoth y mejorar seguridad

- 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
This commit is contained in:
Evert Daniel Romero Garrido
2026-05-07 09:44:44 -06:00
parent 2e3627fb66
commit 7e0c764f3f
5 changed files with 210 additions and 22 deletions
+72
View File
@@ -22,6 +22,32 @@ PIPELINE_PUBLIC_KEY="${pipeline_public_key}"
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
# -------------------------------------------------------------------------------
@@ -38,6 +64,52 @@ 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
# -------------------------------------------------------------------------------