zoph: Restore database password to old value after restore op.

Fixes: #2346.

Tests:

- Without the patch, install zoph, take a backup. Note the db password in
/etc/zoph.ini. Uninstall zoph. Install it. Note that db password changed.
Restore from previous backup. Note that password has been restored to old value
and zoph is unable to connect to database.

- With the patch, repeat the test and notice zoph works after restore.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2024-03-08 16:57:54 -08:00 committed by James Valleroy
parent d23ceb7050
commit 7d4c5650f4
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -69,8 +69,10 @@ def _get_db_config():
config.read_file(file_handle)
return {
'db_host': config['zoph']['db_host'].strip('"'),
'db_name': config['zoph']['db_name'].strip('"'),
'db_user': config['zoph']['db_user'].strip('"'),
'db_pass': config['zoph']['db_pass'].strip('"'),
}
@ -129,12 +131,19 @@ def restore_database():
"""
with action_utils.service_ensure_running('mysql'):
db_name = _get_db_config()['db_name']
db_user = _get_db_config()['db_user']
db_host = _get_db_config()['db_host']
db_pass = _get_db_config()['db_pass']
subprocess.run(['mysqladmin', '--force', 'drop', db_name], check=False)
subprocess.run(['mysqladmin', 'create', db_name], check=True)
with open(DB_BACKUP_FILE, 'r', encoding='utf-8') as db_restore_file:
subprocess.run(['mysql', db_name], stdin=db_restore_file,
check=True)
# Set the password for user from restored configuration
query = f'ALTER USER {db_user}@{db_host} IDENTIFIED BY "{db_pass}";'
subprocess.run(['mysql'], input=query.encode(), check=True)
@privileged
def uninstall():