diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 11856a7..cfa6a8a 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -58,16 +58,13 @@ jobs: # 3. Despliegue con Docker Compose cd /opt/gestion-integral export DB_SA_PASSWORD='${{ secrets.DB_SA_PASSWORD_SECRET }}' - export JWT_KEY='${{ secrets.JWT_KEY }}' - # --- PASO DE DEPURACIÓN --- - echo "Verificando variables de entorno..." - echo "Longitud de DB_SA_PASSWORD: ${#DB_SA_PASSWORD}" - echo "Longitud de JWT_KEY: ${#JWT_KEY}" - # ------------------------- + # Pasa el secreto de Gitea a un Docker Secret + # El comando "tr -d '\n'" elimina cualquier salto de línea final + printf "%s" "${{ secrets.JWT_KEY }}" | tr -d '\n' | docker secret create jwt_secret_key - || docker secret inspect jwt_secret_key > /dev/null echo "Recreando servicios..." - docker compose up -d --force-recreate + docker stack deploy -c /opt/gestion-integral/docker-compose.yml --with-registry-auth gestion-integral-stack # 4. Limpieza echo "Realizando limpieza..." diff --git a/Backend/GestionIntegral.Api/Program.cs b/Backend/GestionIntegral.Api/Program.cs index 30fe4a8..48f9a01 100644 --- a/Backend/GestionIntegral.Api/Program.cs +++ b/Backend/GestionIntegral.Api/Program.cs @@ -112,9 +112,18 @@ builder.Services.AddHealthChecks() // --- Configuración de Autenticación JWT --- var jwtSettings = builder.Configuration.GetSection("Jwt"); +// Lee la RUTA al archivo del secreto desde la variable de entorno +var jwtKeyFilePath = builder.Configuration["JWT_KEY_FILE"]; + +// Si la ruta existe (estamos en Docker Swarm), lee el contenido del archivo. +// Si no, vuelve a buscar la variable de entorno (para desarrollo local). +var jwtKey = !string.IsNullOrEmpty(jwtKeyFilePath) && File.Exists(jwtKeyFilePath) + ? File.ReadAllText(jwtKeyFilePath).Trim() + : builder.Configuration["JWT_KEY"] ?? throw new ArgumentNullException("JWT Key not configured"); + // Le decimos que busque la clave JWT en la raíz de la configuración (donde están las variables de entorno). // Si no la encuentra, como respaldo, busca en la sección "Jwt" del appsettings. -var jwtKey = builder.Configuration["JWT_KEY"] ?? /*jwtSettings["Key"] ??*/ throw new ArgumentNullException("JWT_KEY or Jwt:Key not configured"); +//var jwtKey = builder.Configuration["JWT_KEY"] ?? /*jwtSettings["Key"] ??*/ throw new ArgumentNullException("JWT_KEY or Jwt:Key not configured"); var keyBytes = Encoding.ASCII.GetBytes(jwtKey); diff --git a/Backend/GestionIntegral.Api/appsettings.json b/Backend/GestionIntegral.Api/appsettings.json index b549a03..68c4fbb 100644 --- a/Backend/GestionIntegral.Api/appsettings.json +++ b/Backend/GestionIntegral.Api/appsettings.json @@ -6,7 +6,6 @@ } }, "Jwt": { - "Key": "", "Issuer": "GestionIntegralApi", "Audience": "GestionIntegralClient", "DurationInHours": 8