From 5cdd61e9ee6481dac2dd5c72a067c952715f07d5 Mon Sep 17 00:00:00 2001 From: cymed <102035587+cymed@users.noreply.github.com> Date: Fri, 27 Sep 2024 15:36:30 +0200 Subject: [PATCH 1/7] Allow SSL params for Auth --- .../interlis/utils/various.py | 32 +++++++++++++------ .../teksi_wastewater/utils/database_utils.py | 12 ++----- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/plugin/teksi_wastewater/interlis/utils/various.py b/plugin/teksi_wastewater/interlis/utils/various.py index e00ec5bd9..cfac629c8 100644 --- a/plugin/teksi_wastewater/interlis/utils/various.py +++ b/plugin/teksi_wastewater/interlis/utils/various.py @@ -36,16 +36,28 @@ def get_pgconf_as_ili_args() -> List[str]: """Returns the pgconf as a list of ili2db arguments""" pgconf = DatabaseUtils.get_pgconf() args = [] - if pgconf["host"]: - args.extend(["--dbhost", '"' + pgconf["host"] + '"']) - if pgconf["port"]: - args.extend(["--dbport", '"' + pgconf["port"] + '"']) - if pgconf["user"]: - args.extend(["--dbusr", '"' + pgconf["user"] + '"']) - if pgconf["password"]: - args.extend(["--dbpwd", '"' + pgconf["password"] + '"']) - if pgconf["dbname"]: - args.extend(["--dbdatabase", '"' + pgconf["dbname"] + '"']) + dbparams = [] + for key in pgconf: + if key=="host": + args.extend(["--dbhost", '"' + pgconf["host"] + '"']) + elif key=="port": + args.extend(["--dbport", '"' + pgconf["port"] + '"']) + elif key=="user": + args.extend(["--dbusr", '"' + pgconf["user"] + '"']) + elif key=="password": + args.extend(["--dbpwd", '"' + pgconf["password"] + '"']) + elif key=="dbname": + args.extend(["--dbdatabase", '"' + pgconf["dbname"] + '"']) + else: + dbparams.extend([f'{key}={pgconf[key]}']) + if dbparams: + # write into tempfile and add path to args + dbparams_path=os.path.join(tempfile.gettempdir(), "dbparams") + os.makedirs(dbparams_path, exist_ok=True) + with open(os.path.join(dbparams_path,"dbparams.txt"),"w") as f: + for param in dbparams: + f.write(param+"\n") + args.extend(["--dbparams", '"' + os.path.join(dbparams_path,"dbparams.txt") + '"']) return args diff --git a/plugin/teksi_wastewater/utils/database_utils.py b/plugin/teksi_wastewater/utils/database_utils.py index 3f3ee824f..d46283985 100644 --- a/plugin/teksi_wastewater/utils/database_utils.py +++ b/plugin/teksi_wastewater/utils/database_utils.py @@ -132,16 +132,8 @@ def get_pgconf_as_psycopg_dsn() -> List[str]: pgconf = DatabaseUtils.get_pgconf() parts = [] - if pgconf["host"]: - parts.append(f"host={pgconf['host']}") - if pgconf["port"]: - parts.append(f"port={pgconf['port']}") - if pgconf["user"]: - parts.append(f"dbname={pgconf['dbname']}") - if pgconf["password"]: - parts.append(f"user={pgconf['user']}") - if pgconf["dbname"]: - parts.append(f"password={pgconf['password']}") + for key in pgconf: + parts.append(f'{key}={pgconf[key]}') return " ".join(parts) @staticmethod From 9cefc64f3e879f3058d2ac0ae0d5f7f2c2080dd3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 27 Sep 2024 13:40:06 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../interlis/utils/various.py | 20 +++++++++---------- .../teksi_wastewater/utils/database_utils.py | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/plugin/teksi_wastewater/interlis/utils/various.py b/plugin/teksi_wastewater/interlis/utils/various.py index cfac629c8..c22d98788 100644 --- a/plugin/teksi_wastewater/interlis/utils/various.py +++ b/plugin/teksi_wastewater/interlis/utils/various.py @@ -38,26 +38,26 @@ def get_pgconf_as_ili_args() -> List[str]: args = [] dbparams = [] for key in pgconf: - if key=="host": + if key == "host": args.extend(["--dbhost", '"' + pgconf["host"] + '"']) - elif key=="port": + elif key == "port": args.extend(["--dbport", '"' + pgconf["port"] + '"']) - elif key=="user": + elif key == "user": args.extend(["--dbusr", '"' + pgconf["user"] + '"']) - elif key=="password": + elif key == "password": args.extend(["--dbpwd", '"' + pgconf["password"] + '"']) - elif key=="dbname": + elif key == "dbname": args.extend(["--dbdatabase", '"' + pgconf["dbname"] + '"']) else: - dbparams.extend([f'{key}={pgconf[key]}']) + dbparams.extend([f"{key}={pgconf[key]}"]) if dbparams: # write into tempfile and add path to args - dbparams_path=os.path.join(tempfile.gettempdir(), "dbparams") + dbparams_path = os.path.join(tempfile.gettempdir(), "dbparams") os.makedirs(dbparams_path, exist_ok=True) - with open(os.path.join(dbparams_path,"dbparams.txt"),"w") as f: + with open(os.path.join(dbparams_path, "dbparams.txt"), "w") as f: for param in dbparams: - f.write(param+"\n") - args.extend(["--dbparams", '"' + os.path.join(dbparams_path,"dbparams.txt") + '"']) + f.write(param + "\n") + args.extend(["--dbparams", '"' + os.path.join(dbparams_path, "dbparams.txt") + '"']) return args diff --git a/plugin/teksi_wastewater/utils/database_utils.py b/plugin/teksi_wastewater/utils/database_utils.py index d46283985..ae49987b4 100644 --- a/plugin/teksi_wastewater/utils/database_utils.py +++ b/plugin/teksi_wastewater/utils/database_utils.py @@ -133,7 +133,7 @@ def get_pgconf_as_psycopg_dsn() -> List[str]: pgconf = DatabaseUtils.get_pgconf() parts = [] for key in pgconf: - parts.append(f'{key}={pgconf[key]}') + parts.append(f"{key}={pgconf[key]}") return " ".join(parts) @staticmethod From 629491feff17418a32b41d3f48924f7366393fb8 Mon Sep 17 00:00:00 2001 From: cymed <102035587+cymed@users.noreply.github.com> Date: Fri, 27 Sep 2024 17:00:13 +0200 Subject: [PATCH 3/7] add fallback for username --- plugin/teksi_wastewater/interlis/utils/various.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin/teksi_wastewater/interlis/utils/various.py b/plugin/teksi_wastewater/interlis/utils/various.py index c22d98788..f0896d97b 100644 --- a/plugin/teksi_wastewater/interlis/utils/various.py +++ b/plugin/teksi_wastewater/interlis/utils/various.py @@ -58,6 +58,8 @@ def get_pgconf_as_ili_args() -> List[str]: for param in dbparams: f.write(param + "\n") args.extend(["--dbparams", '"' + os.path.join(dbparams_path, "dbparams.txt") + '"']) + if not pgconf["user"]: + args.extend(["--dbusr", '"' + os.getenv("USERNAME") + '"']) return args From 5b96e292e9777f0df58110af4f783725daf15174 Mon Sep 17 00:00:00 2001 From: cymed <102035587+cymed@users.noreply.github.com> Date: Mon, 30 Sep 2024 08:04:25 +0200 Subject: [PATCH 4/7] save in uuid named folder --- plugin/teksi_wastewater/interlis/utils/various.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin/teksi_wastewater/interlis/utils/various.py b/plugin/teksi_wastewater/interlis/utils/various.py index f0896d97b..5d4325825 100644 --- a/plugin/teksi_wastewater/interlis/utils/various.py +++ b/plugin/teksi_wastewater/interlis/utils/various.py @@ -4,6 +4,7 @@ import subprocess import tempfile from typing import List +import uuid from ...utils.database_utils import DatabaseUtils from ...utils.plugin_utils import logger @@ -52,7 +53,7 @@ def get_pgconf_as_ili_args() -> List[str]: dbparams.extend([f"{key}={pgconf[key]}"]) if dbparams: # write into tempfile and add path to args - dbparams_path = os.path.join(tempfile.gettempdir(), "dbparams") + dbparams_path = os.path.join(tempfile.gettempdir(), str(uuid.uuid4())) os.makedirs(dbparams_path, exist_ok=True) with open(os.path.join(dbparams_path, "dbparams.txt"), "w") as f: for param in dbparams: From 5a69a782762d46cc661c79c9285c6dfb10204b8d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 30 Sep 2024 06:04:49 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- plugin/teksi_wastewater/interlis/utils/various.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/teksi_wastewater/interlis/utils/various.py b/plugin/teksi_wastewater/interlis/utils/various.py index 5d4325825..2c33b79b4 100644 --- a/plugin/teksi_wastewater/interlis/utils/various.py +++ b/plugin/teksi_wastewater/interlis/utils/various.py @@ -3,8 +3,8 @@ import re import subprocess import tempfile -from typing import List import uuid +from typing import List from ...utils.database_utils import DatabaseUtils from ...utils.plugin_utils import logger From 1d76895222ce84db7ec238036b0aed858445661c Mon Sep 17 00:00:00 2001 From: cymed <102035587+cymed@users.noreply.github.com> Date: Mon, 30 Sep 2024 13:24:55 +0200 Subject: [PATCH 6/7] allow overriding env variable --- plugin/teksi_wastewater/interlis/utils/various.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugin/teksi_wastewater/interlis/utils/various.py b/plugin/teksi_wastewater/interlis/utils/various.py index 2c33b79b4..b36be0476 100644 --- a/plugin/teksi_wastewater/interlis/utils/various.py +++ b/plugin/teksi_wastewater/interlis/utils/various.py @@ -5,7 +5,7 @@ import tempfile import uuid from typing import List - +from qgis.core import QgsExpression from ...utils.database_utils import DatabaseUtils from ...utils.plugin_utils import logger @@ -60,7 +60,10 @@ def get_pgconf_as_ili_args() -> List[str]: f.write(param + "\n") args.extend(["--dbparams", '"' + os.path.join(dbparams_path, "dbparams.txt") + '"']) if not pgconf["user"]: - args.extend(["--dbusr", '"' + os.getenv("USERNAME") + '"']) + # allow loading PGUSER from overriden env variables + expression = QgsExpression('@PGUSER') + pguser = expression.evaluate() + args.extend(["--dbusr", f'"{pguser}"']) return args From 331fce0e029db398043030594450dc90a8c1b278 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 30 Sep 2024 11:25:20 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- plugin/teksi_wastewater/interlis/utils/various.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugin/teksi_wastewater/interlis/utils/various.py b/plugin/teksi_wastewater/interlis/utils/various.py index b36be0476..c8de43b98 100644 --- a/plugin/teksi_wastewater/interlis/utils/various.py +++ b/plugin/teksi_wastewater/interlis/utils/various.py @@ -5,7 +5,9 @@ import tempfile import uuid from typing import List + from qgis.core import QgsExpression + from ...utils.database_utils import DatabaseUtils from ...utils.plugin_utils import logger @@ -61,7 +63,7 @@ def get_pgconf_as_ili_args() -> List[str]: args.extend(["--dbparams", '"' + os.path.join(dbparams_path, "dbparams.txt") + '"']) if not pgconf["user"]: # allow loading PGUSER from overriden env variables - expression = QgsExpression('@PGUSER') + expression = QgsExpression("@PGUSER") pguser = expression.evaluate() args.extend(["--dbusr", f'"{pguser}"']) return args