Skip to content

Commit

Permalink
Merge pull request #4 from bkorty/obscure-password
Browse files Browse the repository at this point in the history
Obscure password
  • Loading branch information
gmr committed Sep 1, 2020
2 parents ce6e2ec + 4bbcf51 commit 69b806f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.0
1.4.1
20 changes: 17 additions & 3 deletions sprockets_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,9 @@ async def _postgres_connect(self) -> bool:
if self._postgres_pool:
self._postgres_pool.close()

LOGGER.debug('Connecting to %s', url)
safe_url = self._obscure_url_password(url)
LOGGER.debug('Connecting to %s', safe_url)

try:
self._postgres_pool = await pool.Pool.from_pool_fill(
url,
Expand Down Expand Up @@ -475,13 +477,25 @@ async def _postgres_connect(self) -> bool:
DEFAULT_POSTGRES_CONNECTION_TTL)))
except (psycopg2.OperationalError,
psycopg2.Error) as error: # pragma: nocover
LOGGER.warning('Error connecting to PostgreSQL on startup: %s',
error)
LOGGER.warning(
'Error connecting to PostgreSQL on startup with %s: %s',
safe_url, error)
return False
self._postgres_connected.set()
LOGGER.debug('Connected to Postgres')
return True

@staticmethod
def _obscure_url_password(url):
"""Generate log safe url with password obscured."""
parsed = parse.urlparse(url)
if parsed.password:
netloc = '{}:*****@{}:{}'.format(parsed.username,
parsed.hostname,
parsed.port)
url = parse.urlunparse(parsed._replace(netloc=netloc))
return url

async def _postgres_on_start(self,
_app: web.Application,
loop: ioloop.IOLoop):
Expand Down
16 changes: 16 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,22 @@ def test_that_stop_is_invoked(self):
obj.stop.assert_called_once()


class ObscurePasswordUrlTestCase(unittest.TestCase):

def test_passwords_obscured(self):
for url, expected in {
'postgresql://server:5432/database':
'postgresql://server:5432/database',
'postgresql://username:password@server:5432/database':
'postgresql://username:*****@server:5432/database',
'postgresql://username@server/database':
'postgresql://username@server/database'
}.items():
result = \
sprockets_postgres.ApplicationMixin._obscure_url_password(url)
self.assertEqual(result, expected)


SRV = collections.namedtuple(
'SRV', ['host', 'port', 'priority', 'weight', 'ttl'])

Expand Down

0 comments on commit 69b806f

Please sign in to comment.