""" Modules to control the encryption of the configuration options """ import json import os import sys import time import hashlib from django.conf import settings from tools.crypto_tools import decrypt as crypto_decrypt, encrypt as crypto_encrypt import getpass from django.conf import settings from tools.envConf import configure from tools.crypto_tools import decrypt as crypto_decrypt, encrypt as crypto_encrypt def _set_conf(): """ Method thath reads the key and encrypt the configuration options """ if settings.DEBUG: if os.path.exists(configure.CONF_PATH): try: with open(configure.CONF_PATH, 'r') as in_file: json.load(in_file) except UnicodeDecodeError: key = hashlib.sha256() key.update(getpass.getpass('Configuration password:').encode()) crypto_key = key.digest()[:16] with open(configure.CONF_PASS_PATH, 'wb') as pass_file: pass_file.write(crypto_key) params_decrypted = crypto_decrypt(configure.CONF_PASS_PATH, configure.CONF_PATH).decode('UTF-8') with open(configure.CONF_PATH, 'w') as in_file: in_file.write(params_decrypted) else: key = hashlib.sha256() key.update(getpass.getpass('Configuration password:').encode()) crypto_key = key.digest()[:16] with open(configure.CONF_PASS_PATH, 'wb') as pass_file: pass_file.write(crypto_key) if os.path.exists(configure.CONF_PATH): try: with open(configure.CONF_PATH, 'rb') as in_file: int(in_file.read(16)) except ValueError: with open(configure.CONF_PATH, 'r') as in_file: parameters = json.load(in_file) crypto_encrypt(parameters, configure.CONF_PASS_PATH, configure.CONF_PATH) configure.main() def check_tty(): """ Method to check the available tty. If a tty is not available it shows in the screen the following steps. """ tty_enabled = os.isatty(sys.stdout.fileno()) if not tty_enabled: print("Run \"python3 manage.py configure\" in a terminal within an available tty. " "When this is done restart " "server. Example:\n docker-compose -> \n" "Run this command to do the configuration directly: \"\n" "\t docker-compose exec gms /bin/bash -c \"cd /var/src/gms/deployed && " "python3 manage.py configure\"\n" "docker ->\n" "docker exec -it gms /bin/bash -c \"cd /home/DjangoServer && " "python3 manage.py configure\"\n" "local -> cd /home/DjangoServer && python3 manage.py configure") while True: time.sleep(1) def config(force=False): """ Main method calling by startup to trigger the configuration instance. Parameters ---------- force: bool Force the overwriting of the configuration """ if force or not os.path.exists(configure.CONF_PATH): check_tty() _set_conf() print('Now run python manage.py runserver') sys.exit() conf = configure.Configure() # settings.ENV_CONF = conf # Comentamos esta línea porque si se mantiene activa es como si inicializamo en settings.ENV_CONF cada vez, por lo que es como si se restableciese la configuración y no coge las nets que se han configurado en el archivo app.py try: getattr(settings, 'ENV_CONF') except AttributeError: settings.ENV_CONF = conf