From 44c89e3516b7b025c8c5154ed05e336ddd4770e8 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Jul 20 2021 09:49:06 +0000 Subject: DBConnectionString/dsn option for db connection Alternative DSN connection string fro connecting to postgres. Allow us to specify e.g. sslmode without adding separate DB* options. Fixes: https://pagure.io/koji/issue/2838 --- diff --git a/hub/hub.conf b/hub/hub.conf index dae14da..7aabe7c 100644 --- a/hub/hub.conf +++ b/hub/hub.conf @@ -13,6 +13,10 @@ DBUser = koji #DBPort = 5432 #Note, that db password is sensitive and this file shouldn't be publicly readable. #DBPass = example_password +# Same can be achieved with supplying whole dsn string (with potential additional options) +# DBConnectionString also takes precedence over all other DB* options +# whose will be deprecated in future +#DBConnectionString = dbname=koji user=koji host=db.example.com port=5432 password=example_password KojiDir = /mnt/koji ## Auth-related options ## diff --git a/hub/kojixmlrpc.py b/hub/kojixmlrpc.py index 25ad98a..468461b 100644 --- a/hub/kojixmlrpc.py +++ b/hub/kojixmlrpc.py @@ -410,6 +410,7 @@ def load_config(environ): ['DBhost', 'string', None], # alias for backwards compatibility ['DBPort', 'integer', None], ['DBPass', 'string', None], + ['DBConnectionString', 'string', None], ['KojiDir', 'string', None], ['AuthPrincipal', 'string', None], @@ -685,11 +686,14 @@ def server_setup(environ): plugins = load_plugins(opts) registry = get_registry(opts, plugins) policy = get_policy(opts, plugins) - koji.db.provideDBopts(database=opts["DBName"], - user=opts["DBUser"], - password=opts.get("DBPass", None), - host=opts.get("DBHost", None), - port=opts.get("DBPort", None)) + if opts.get('DBConnectionString'): + koji.db.provideDBopts(dsn=opts['DBConnectionString']) + else: + koji.db.provideDBopts(database=opts["DBName"], + user=opts["DBUser"], + password=opts.get("DBPass", None), + host=opts.get("DBHost", None), + port=opts.get("DBPort", None)) except Exception: tb_str = ''.join(traceback.format_exception(*sys.exc_info())) logger.error(tb_str) diff --git a/koji/db.py b/koji/db.py index 784c04b..31043be 100644 --- a/koji/db.py +++ b/koji/db.py @@ -188,7 +188,10 @@ def connect(): if opts is None: opts = {} try: - conn = psycopg2.connect(**opts) + if 'dsn' in opts: + conn = psycopg2.connect(dsn=opts['dsn']) + else: + conn = psycopg2.connect(**opts) conn.set_client_encoding('UTF8') except Exception: logger.error(''.join(traceback.format_exception(*sys.exc_info()))) diff --git a/util/koji-sweep-db b/util/koji-sweep-db index a4ecd7f..9841d23 100755 --- a/util/koji-sweep-db +++ b/util/koji-sweep-db @@ -178,6 +178,7 @@ if __name__ == "__main__": ['DBhost', 'string', None], # alias for backwards compatibility ['DBPort', 'integer', None], ['DBPass', 'string', None], + ['DBConnectionString', 'string', None], ] opts = {} @@ -192,14 +193,17 @@ if __name__ == "__main__": opts[name] = config.get(*key) continue opts[name] = default - if opts['DBHost'] is None: - opts['DBHost'] = opts['DBhost'] - - koji.db.provideDBopts(database=opts["DBName"], - user=opts["DBUser"], - password=opts.get("DBPass", None), - host=opts.get("DBHost", None), - port=opts.get("DBPort", None)) + + if opts.get('DBConnectionString'): + koji.db.provideDBopts(dsn=opts['DBConnectionString']) + else: + if opts['DBHost'] is None: + opts['DBHost'] = opts['DBhost'] + koji.db.provideDBopts(database=opts["DBName"], + user=opts["DBUser"], + password=opts.get("DBPass", None), + host=opts.get("DBHost", None), + port=opts.get("DBPort", None)) conn = koji.db.connect() conn.set_session(autocommit=True)