Hi Everyone i am working on django framework my credential store in config.ini file but when i encrypt my db password and run server getting 500 error in log showing password authentication failed for user this will happen when i encrypt db password and store in same config.ini file bt i store new config.ini file it will work but i need to store db password encryption in existing config.ini file please help me out.
settings.py
from cryptography.fernet import Fernet
key = Fernet.generate_key()
with open('key.key', 'wb') as key_file:
key_file.write(key)
# Define encrypt
def encrypt(value):
with open('key.key', 'rb') as key_file:
key = key_file.read()
f = Fernet(key)
return f.encrypt(value.encode()).decode()
#Define decrypt functions
def decrypt(value):
with open('key.key', 'rb') as key_file:
key = key_file.read()
f = Fernet(key)
return f.decrypt(value.encode()).decode()
# Read config file:------------------------------------------------------------
config = RawConfigParser()
config.read(os.path.join(config_path, 'config.ini'))
DB_PASSWORD = config['db_setting']['DB_PASSWORD']
encrypt_db_password=encrypt(DB_PASSWORD)
# update config.ini file with encrypt_db_password
config.set('db_setting','DB_PASSWORD', encrypt_db_password)
with open('utility\config.ini', 'w') as config_file:
config.write(config_file)
#decrypt db_password
decrypted_db_password = decrypt(encrypt_db_password)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db',
'USER': DB_USER,
'PASSWORD': decrypted_db_password,
'HOST': DB_HOST,
'PORT': DB_PORT,
'CONN_MAX_AGE': MAX_CONNECTION_AGE,
'OPTIONS': {'options': '-c search_path=' + SCHEMA + ',public'}
}
try to encrypt decrypt db password i will work when db credential store new config.ini file but i need to store my credential with encyption in existing config.ini file. as per me this line generate issue
1- this will not work
update config.ini file with encrypt_db_password
config.set('db_setting','DB_PASSWORD', encrypt_db_password)
with open('utility\config.ini', 'w') as config_file:
config.write(config_file)
**2- this will work **
creating config.ini file with encrypt_db_password
config.set('db_setting','DB_PASSWORD', encrypt_db_password)
with open('config.ini', 'w') as config_file:
config.write(config_file)