Skip to content

Commit

Permalink
On PG on some integrations we are able to connect only with the user …
Browse files Browse the repository at this point in the history
…name (mostly the systems account user name) like e.g. kerberos.

For that we did:
- pass the account name as fall back on every get_uri call except the one to write the layers (since there not needed)
- means in the factories as well in the post_generate_project_validations
- and in the utils we pass the account name always on get_db_connector
- then we allow empty usernames in get_configuration_from_sourceprovider and we set the mode as configuration.tool
  • Loading branch information
signedav committed Oct 29, 2024
1 parent d424cbe commit 17d20f7
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 15 deletions.
5 changes: 4 additions & 1 deletion modelbaker/db_factory/db_command_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ def __init__(self, configuration: Ili2DbCommandConfiguration) -> None:
self.configuration = configuration

@abstractmethod
def get_uri(self, su: bool = False, qgis: bool = False) -> str:
def get_uri(
self, su: bool = False, qgis: bool = False, fallback_user: str = None
) -> str:
"""Gets database uri (connection string) for db connectors (:class:`DBConnector`).
:param bool su: *True* to use super user credentials, *False* otherwise.
:param bool qgis: *True* to use qgis specific credentials (e.g. authcfg), *False* otherwise.
:param str fallback_user: a username as fallback most possibly used when you want to pass your os account name to connect the database
:return: Database uri (connection string).
:rtype str
"""
Expand Down
6 changes: 3 additions & 3 deletions modelbaker/db_factory/db_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ def pre_generate_project(

@abstractmethod
def post_generate_project_validations(
self, configuration: Ili2DbCommandConfiguration
self, configuration: Ili2DbCommandConfiguration, fallback_user: str = None
) -> tuple[bool, str]:
"""This method will be called after an operation of generate project is executed.
:param configuration: Configuration parameters with which were executed the operation of generate project.
:type configuration: :class:`Ili2DbCommandConfiguration`
:param class:`Ili2DbCommandConfiguration` configuration: Configuration parameters with which were executed the operation of generate project.
:param str fallback_user: a username as fallback most possibly used when you want to pass your os account name to connect the database
:return: *True* and an empty message if the called method was succeeded, *False* and a warning message otherwise.
"""

Expand Down
4 changes: 3 additions & 1 deletion modelbaker/db_factory/gpkg_command_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class GpkgCommandConfigManager(DbCommandConfigManager):
def __init__(self, configuration: Ili2DbCommandConfiguration) -> None:
DbCommandConfigManager.__init__(self, configuration)

def get_uri(self, su: bool = False, qgis: bool = False) -> str:
def get_uri(
self, su: bool = False, qgis: bool = False, fallback_user: str = None
) -> str:
return self.configuration.dbfile

def get_db_args(self, hide_password: bool = False, su: bool = False) -> list[str]:
Expand Down
2 changes: 1 addition & 1 deletion modelbaker/db_factory/gpkg_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def pre_generate_project(
return True, ""

def post_generate_project_validations(
self, configuration: Ili2DbCommandConfiguration
self, configuration: Ili2DbCommandConfiguration, fallback_user: str = None
) -> tuple[bool, str]:
return True, ""

Expand Down
4 changes: 3 additions & 1 deletion modelbaker/db_factory/mssql_command_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class MssqlCommandConfigManager(DbCommandConfigManager):
def __init__(self, configuration: Ili2DbCommandConfiguration) -> None:
DbCommandConfigManager.__init__(self, configuration)

def get_uri(self, su: bool = False, qgis: bool = False) -> str:
def get_uri(
self, su: bool = False, qgis: bool = False, fallback_user: str = None
) -> str:
separator = ";"
uri = []
uri += ["DRIVER={{{}}}".format(self.configuration.db_odbc_driver)]
Expand Down
2 changes: 1 addition & 1 deletion modelbaker/db_factory/mssql_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def pre_generate_project(
return True, ""

def post_generate_project_validations(
self, configuration: Ili2DbCommandConfiguration
self, configuration: Ili2DbCommandConfiguration, fallback_user: str = None
) -> tuple[bool, str]:
return True, ""

Expand Down
6 changes: 4 additions & 2 deletions modelbaker/db_factory/pg_command_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class PgCommandConfigManager(DbCommandConfigManager):
def __init__(self, configuration: Ili2DbCommandConfiguration) -> None:
DbCommandConfigManager.__init__(self, configuration)

def get_uri(self, su: bool = False, qgis: bool = False) -> str:
def get_uri(
self, su: bool = False, qgis: bool = False, fallback_user: str = None
) -> str:
uri = []

if su:
Expand Down Expand Up @@ -98,7 +100,7 @@ def get_uri(self, su: bool = False, qgis: bool = False) -> str:
uri += ["password={}".format(authconfig_map.get("password"))]
else:
if not service_config or not service_config.get("user", None):
uri += ["user={}".format(self.configuration.dbusr)]
uri += ["user={}".format(self.configuration.dbusr or fallback_user)]
if not service_config or not service_config.get("password", None):
if self.configuration.dbpwd:
uri += ["password={}".format(self.configuration.dbpwd)]
Expand Down
6 changes: 4 additions & 2 deletions modelbaker/db_factory/pg_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ def pre_generate_project(
return result, message

def post_generate_project_validations(
self, configuration: Ili2DbCommandConfiguration
self, configuration: Ili2DbCommandConfiguration, fallback_user: str = None
) -> tuple[bool, str]:
result = False
message = ""

config_manager = self.get_db_command_config_manager(configuration)
uri = config_manager.get_uri(configuration.db_use_super_login)
uri = config_manager.get_uri(
su=configuration.db_use_super_login, fallback_user=fallback_user
)

connector = self.get_db_connector(uri, configuration.dbschema)

Expand Down
2 changes: 1 addition & 1 deletion modelbaker/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
"""
Creates a new Generator objects.
:param uri: The uri that should be used in the resulting project. If authcfg is used, make sure the mgmt_uri is set as well.
:param mgmt_uri: The uri that should be used to create schemas, tables and query meta information. Does not support authcfg.
:param mgmt_uri: The uri that should be used to create schemas, tables and query meta information. Does not support authcfg but a fallback username.
:consider_basket_handling: Makes the specific handling of basket tables depending if schema is created with createBasketCol.
"""
QObject.__init__(self, parent)
Expand Down
6 changes: 4 additions & 2 deletions modelbaker/utils/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def get_configuration_from_sourceprovider(provider, configuration):
configuration.dbschema = layer_source.schema()
valid = bool(
configuration.dbusr
and configuration.dbpwd
and configuration.dbhost
and configuration.database
and configuration.dbschema
Expand All @@ -113,6 +112,7 @@ def get_configuration_from_sourceprovider(provider, configuration):
and configuration.database
and configuration.dbschema
)
configuration.tool = mode
return valid, mode


Expand All @@ -122,7 +122,9 @@ def get_db_connector(configuration):

db_factory = db_simple_factory.create_factory(configuration.tool)
config_manager = db_factory.get_db_command_config_manager(configuration)
uri_string = config_manager.get_uri(configuration.db_use_super_login)
uri_string = config_manager.get_uri(
configuration.db_use_super_login, QgsApplication.userLoginName()
)

try:
return db_factory.get_db_connector(uri_string, schema)
Expand Down

0 comments on commit 17d20f7

Please sign in to comment.