Bot de Telegram para monitoreo de notificaciones judiciales del PJ Paraguay. Multi-tenant con onboarding conversacional, audit log, y polling automático. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
92 lines
2.6 KiB
Python
92 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
scripts/cifrar_credenciales.py
|
|
|
|
Cifra las credenciales del PJ con la Fernet key y las escribe en .env.
|
|
Correr UNA VEZ al configurar el sistema.
|
|
|
|
Uso:
|
|
python scripts/cifrar_credenciales.py
|
|
"""
|
|
import getpass
|
|
import os
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Agregar el directorio raíz al path
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
|
|
def main() -> None:
|
|
env_path = Path(".env")
|
|
|
|
if not env_path.exists():
|
|
print("ERROR: No encontré .env. Copiá .env.example a .env primero.")
|
|
sys.exit(1)
|
|
|
|
# Leer FERNET_KEY del .env
|
|
env_content = env_path.read_text(encoding="utf-8")
|
|
|
|
fernet_key_match = re.search(r"^FERNET_KEY=(.+)$", env_content, re.MULTILINE)
|
|
if not fernet_key_match or not fernet_key_match.group(1).strip():
|
|
print("ERROR: FERNET_KEY no encontrada en .env o está vacía.")
|
|
print("Generá una con:")
|
|
print(" python -c \"from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())\"")
|
|
sys.exit(1)
|
|
|
|
fernet_key = fernet_key_match.group(1).strip()
|
|
|
|
try:
|
|
from cryptography.fernet import Fernet
|
|
f = Fernet(fernet_key.encode())
|
|
except Exception as e:
|
|
print(f"ERROR: FERNET_KEY inválida: {e}")
|
|
sys.exit(1)
|
|
|
|
print("=== Cifrado de credenciales del PJ ===\n")
|
|
print("Estas credenciales son tu usuario y contraseña del sistema del Poder Judicial.")
|
|
print("Se van a cifrar con tu Fernet key y guardar en .env.\n")
|
|
|
|
usuario = input("Cédula de identidad (usuario del PJ): ").strip()
|
|
if not usuario:
|
|
print("ERROR: La cédula no puede estar vacía.")
|
|
sys.exit(1)
|
|
|
|
password = getpass.getpass("Contraseña del PJ (no se muestra): ")
|
|
if not password:
|
|
print("ERROR: La contraseña no puede estar vacía.")
|
|
sys.exit(1)
|
|
|
|
# Cifrar
|
|
usuario_enc = f.encrypt(usuario.encode()).decode()
|
|
password_enc = f.encrypt(password.encode()).decode()
|
|
|
|
# Limpiar memoria
|
|
del password
|
|
del usuario
|
|
|
|
# Actualizar .env
|
|
env_content = re.sub(
|
|
r"^PJ_USUARIO_ENC=.*$",
|
|
f"PJ_USUARIO_ENC={usuario_enc}",
|
|
env_content,
|
|
flags=re.MULTILINE,
|
|
)
|
|
env_content = re.sub(
|
|
r"^PJ_PASSWORD_ENC=.*$",
|
|
f"PJ_PASSWORD_ENC={password_enc}",
|
|
env_content,
|
|
flags=re.MULTILINE,
|
|
)
|
|
|
|
env_path.write_text(env_content, encoding="utf-8")
|
|
|
|
print("\n✅ Credenciales cifradas y guardadas en .env")
|
|
print(" PJ_USUARIO_ENC y PJ_PASSWORD_ENC actualizadas.")
|
|
print("\nPodés arrancar el bot con: python main.py")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|