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
This commit is contained in:
Tomas Kopecek 2021-07-20 11:49:06 +02:00
parent efe1aa0416
commit 44c89e3516
4 changed files with 28 additions and 13 deletions

View file

@ -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 ##

View file

@ -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)

View file

@ -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())))

View file

@ -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)