Skip to content

Commit

Permalink
Merge pull request #430 from cymed/allow-ssl
Browse files Browse the repository at this point in the history
Allow SSL params for Auth
  • Loading branch information
cymed authored Oct 11, 2024
2 parents d50aa08 + 331fce0 commit d48313e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
40 changes: 30 additions & 10 deletions plugin/teksi_wastewater/interlis/utils/various.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import re
import subprocess
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

Expand Down Expand Up @@ -36,16 +39,33 @@ 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(), 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:
f.write(param + "\n")
args.extend(["--dbparams", '"' + os.path.join(dbparams_path, "dbparams.txt") + '"'])
if not pgconf["user"]:
# allow loading PGUSER from overriden env variables
expression = QgsExpression("@PGUSER")
pguser = expression.evaluate()
args.extend(["--dbusr", f'"{pguser}"'])
return args


Expand Down
12 changes: 2 additions & 10 deletions plugin/teksi_wastewater/utils/database_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d48313e

Please sign in to comment.