From f4bd9fc5a58cb224a8ad830b9c806233975cbd8e Mon Sep 17 00:00:00 2001 From: James Wettenhall Date: Sat, 14 Dec 2024 14:27:53 +1100 Subject: [PATCH] Reuse Vault credentials in calls to get_connection_params --- django_informixdb_vault/base.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/django_informixdb_vault/base.py b/django_informixdb_vault/base.py index 4fbc538..863c620 100644 --- a/django_informixdb_vault/base.py +++ b/django_informixdb_vault/base.py @@ -160,30 +160,31 @@ def get_credentials_from_vault(self): def get_connection_params(self): """Returns connection parameters for Informix, with credentials from Vault""" - # django_informixdb expects USER and PASSWORD, so fake them for now - self.settings_dict['USER'] = '' - self.settings_dict['PASSWORD'] = '' + # django_informixdb expects USER and PASSWORD, so fake them if missing + if 'USER' not in self.settings_dict: + self.settings_dict['USER'] = '' + if 'PASSWORD' not in self.settings_dict: + self.settings_dict['PASSWORD'] = '' # parse/get conn_params from django_informixdb conn_params = super().get_connection_params() - # We don't actually use USER and PASSWORD, so delete them. - try: - del self.settings_dict['USER'] - except KeyError: - # Another thread may have already deleted it - pass - try: - del self.settings_dict['PASSWORD'] - except KeyError: - # Another thread may have already deleted it - pass + username = self.settings_dict['USER'] + password = self.settings_dict['PASSWORD'] + + if (username and password): + conn_params['USER'] = username + conn_params['PASSWORD'] = password + + return conn_params username, password = self.get_credentials_from_vault() logger.info( f"Retrieved username ({username}) and password from Vault" f" for database server {self.settings_dict['SERVER']}" ) + self.settings_dict['USER'] = username + self.settings_dict['PASSWORD'] = password conn_params['USER'] = username conn_params['PASSWORD'] = password