#2958 DBConnectionString/dsn option for db connection
Merged 9 months ago by tkopecek. Opened 10 months ago by tkopecek.
tkopecek/koji issue2838  into  master

file modified
+4
@@ -13,6 +13,10 @@ 

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

file modified
+9 -5
@@ -410,6 +410,7 @@ 

          ['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 @@ 

          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)

file modified
+4 -1
@@ -188,7 +188,10 @@ 

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

file modified
+12 -8
@@ -178,6 +178,7 @@ 

          ['DBhost', 'string', None],   # alias for backwards compatibility

          ['DBPort', 'integer', None],

          ['DBPass', 'string', None],

+         ['DBConnectionString', 'string', None],

      ]

  

      opts = {}
@@ -192,14 +193,17 @@ 

                  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)

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

I tested this today and it works great. Thank you!

I pushed a few changes here: https://pagure.io/fork/ktdreyer/koji/commits/db-connection-string . Want to take these into your branch?

I added a walkthrough for administrators to enable SSL using this new feature. I also think we should just remove the old options from the sample configuration file and steer users towards this new setting.

Metadata Update from @tkopecek:
- Pull-request tagged with: testing-ready

10 months ago

Metadata Update from @jobrauer:
- Pull-request tagged with: testing-done

9 months ago

Commit dfce69f fixes this pull-request

Pull-Request has been merged by tkopecek

9 months ago