From 638f9cdff2d0a185708310e9a3d13492a8054aac Mon Sep 17 00:00:00 2001 From: Dannon Baker Date: Fri, 13 Feb 2015 13:05:24 -0500 Subject: [PATCH] Add utility method for masking passwords from urls. --- lib/galaxy/util/__init__.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/galaxy/util/__init__.py b/lib/galaxy/util/__init__.py index dcc840b1f44..26a33f90e08 100644 --- a/lib/galaxy/util/__init__.py +++ b/lib/galaxy/util/__init__.py @@ -21,6 +21,7 @@ import string import sys import tempfile import threading +import urlparse from galaxy.util import json from datetime import datetime @@ -31,8 +32,6 @@ from os.path import relpath from hashlib import md5 from itertools import izip -from urlparse import urlparse - from galaxy import eggs eggs.require( 'docutils' ) @@ -501,6 +500,30 @@ def sanitize_for_filename( text, default=None ): return out +def mask_password_from_url( url ): + """ + Masks out passwords from connection urls like the database connection in galaxy.ini + + >>> mask_password_from_url( 'sqlite+postgresql://user:password@localhost/' ) + 'sqlite+postgresql://user:********@localhost/' + >>> mask_password_from_url( 'amqp://user:amqp@localhost' ) + 'amqp://user:********@localhost' + >>> mask_password_from_url( 'amqp://localhost') + 'amqp://localhost' + """ + split = urlparse.urlsplit(url) + if split.password: + if url.count(split.password) == 1: + url = url.replace(split.password, "********") + else: + # This can manipulate the input other than just masking password, + # so the previous string replace method is preferred when the + # password doesn't appear twice in the url + split._replace(netloc=split.netloc.replace("%s:%s" % (split.username, split.password), '%s:********' % split.username)) + url = urlparse.urlunsplit(split) + return url + + def ready_name_for_url( raw_name ): """ General method to convert a string (i.e. object name) to a URL-ready slug. @@ -856,8 +879,8 @@ class ParamsWithSpecs( collections.defaultdict ): def compare_urls( url1, url2, compare_scheme=True, compare_hostname=True, compare_path=True ): - url1 = urlparse( url1 ) - url2 = urlparse( url2 ) + url1 = urlparse.urlparse( url1 ) + url2 = urlparse.urlparse( url2 ) if compare_scheme and url1.scheme and url2.scheme and url1.scheme != url2.scheme: return False if compare_hostname and url1.hostname and url2.hostname and url1.hostname != url2.hostname: