mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-03-11 09:04:54 +00:00
Create helper function that uses Augeas Shellvars to parse dbconfig-common files. Signed-off-by: Frederico Gomes fredericojfgomes@gmail.com [sunil: Fix quotes not getting removed from values] [sunil: Add test case] Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
"""Utilities for parsing dbconfig-common files with Augeas."""
|
|
|
|
import pathlib
|
|
|
|
import augeas
|
|
|
|
|
|
def get_credentials(dbconfig_path: str) -> dict[str, str]:
|
|
"""Parse dbconfig-common file with Augeas Shellvars lens."""
|
|
if not pathlib.Path(dbconfig_path).is_file():
|
|
raise FileNotFoundError(f'DB config not found: {dbconfig_path}')
|
|
|
|
aug = _load_augeas(dbconfig_path)
|
|
|
|
required = ['dbc_dbuser', 'dbc_dbpass', 'dbc_dbname']
|
|
credentials = {}
|
|
for key in required + ['dbc_dbserver']:
|
|
credentials[key] = aug.get(key).strip('\'"')
|
|
|
|
if not all(credentials.get(key) for key in required):
|
|
raise ValueError('Missing required dbconfig-common credentials')
|
|
|
|
return {
|
|
'user': credentials['dbc_dbuser'],
|
|
'password': credentials['dbc_dbpass'],
|
|
'database': credentials['dbc_dbname'],
|
|
'host': credentials['dbc_dbserver'] or 'localhost'
|
|
}
|
|
|
|
|
|
def _load_augeas(config_path: str):
|
|
"""Initialize Augeas."""
|
|
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
|
|
augeas.Augeas.NO_MODL_AUTOLOAD)
|
|
pathstr = str(config_path)
|
|
aug.transform('Shellvars', pathstr)
|
|
aug.set('/augeas/context', f'/files{pathstr}')
|
|
aug.load()
|
|
return aug
|