Skip to content

Commit 7d307d1

Browse files
committed
sanitize db connect string
fixes #1097
1 parent 437ed1e commit 7d307d1

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

pycsw/core/repository.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def create_engine(clazz, url):
6969
Engines are memoized by url
7070
'''
7171
if url not in clazz._engines:
72-
LOGGER.info('creating new engine: %s', url)
72+
LOGGER.info('creating new engine: %s', util.sanitize_db_connect(url))
7373
engine = create_engine('%s' % url, echo=False, pool_pre_ping=True)
7474

7575
# load SQLite query bindings
@@ -238,7 +238,7 @@ def __init__(self, database, context, app_root=None, table='records', repo_filte
238238
self.queryables['_all'].update(self.context.md_core_model['mappings'])
239239

240240
def ping(self, max_tries=10, wait_seconds=10):
241-
LOGGER.debug(f"Waiting for {self.database}...")
241+
LOGGER.debug(f"Waiting for {util.sanitize_db_connect(self.database)}...")
242242

243243
if self.database.startswith('sqlite'):
244244
sql = 'SELECT sqlite_version();'
@@ -258,12 +258,12 @@ def ping(self, max_tries=10, wait_seconds=10):
258258
sleep(wait_seconds)
259259
else:
260260
raise RuntimeError(
261-
f"Database not responding at {self.database} after {max_tries} tries. ")
261+
f"Database not responding at {util.sanitize_db_connect(self.database)} after {max_tries} tries. ")
262262

263263
def rebuild_db_indexes(self):
264264
"""Rebuild database indexes"""
265265

266-
LOGGER.info('Rebuilding database %s, table %s', self.database, self.table)
266+
LOGGER.info('Rebuilding database %s, table %s', util.sanitize_db_connect(self.database), self.table)
267267
connection = self.engine.connect()
268268
connection.autocommit = True
269269
connection.execute('REINDEX %s' % self.table)
@@ -274,7 +274,7 @@ def optimize_db(self):
274274
"""Optimize database"""
275275
from sqlalchemy.exc import ArgumentError, OperationalError
276276

277-
LOGGER.info('Optimizing database %s', self.database)
277+
LOGGER.info('Optimizing database %s', util.sanitize_db_connect(self.database))
278278
connection = self.engine.connect()
279279
try:
280280
# PostgreSQL
@@ -720,7 +720,7 @@ def setup(database, table, create_sfsql_tables=True, postgis_geometry_column='wk
720720
from sqlalchemy.types import Float
721721
from sqlalchemy.orm import create_session
722722

723-
LOGGER.info('Creating database %s', database)
723+
LOGGER.info('Creating database %s', util.sanitize_db_connect(database))
724724
if database.startswith('sqlite:///'):
725725
_, filepath = database.split('sqlite:///')
726726
dirname = os.path.dirname(filepath)

pycsw/core/util.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,19 @@ def load_custom_repo_mappings(repository_mappings: str) -> typing.Optional[typin
532532
return result
533533

534534

535+
def sanitize_db_connect (url):
536+
"""
537+
helper function to remove user:pw from db connect for logging purposes
538+
539+
:param url: value to be sanitized
540+
541+
:returns: `str` sanitized
542+
"""
543+
if '@' in url:
544+
return url.split('://')[0] + '://***:***@' + url.split('@').pop()
545+
else:
546+
return url
547+
535548
def str2bool(value: typing.Union[bool, str]) -> bool:
536549
"""
537550
helper function to return Python boolean

tests/unittests/test_util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,7 @@ def test_programmatic_import(import_path, expected_attribute):
366366
def test_programmatic_import_with_invalid_path(invalid_import_path):
367367
result = util.programmatic_import(invalid_import_path)
368368
assert result is None
369+
370+
def test_sanitize_url():
371+
result = util.sanitize_db_connect("postgresql://username:password@localhost/pycsw")
372+
assert result == "postgresql://***:***@localhost/pycsw"

0 commit comments

Comments
 (0)