From 61e0e4eea441e5ffd249fee4171b6d42c94d92d5 Mon Sep 17 00:00:00 2001 From: Ryan Banks Date: Wed, 4 Oct 2017 15:43:12 -0500 Subject: [PATCH] Fix base 64 encoding issues with username Signed-off-by: Ryan Banks --- pyformance/reporters/influx.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pyformance/reporters/influx.py b/pyformance/reporters/influx.py index 112cff9..404d29c 100644 --- a/pyformance/reporters/influx.py +++ b/pyformance/reporters/influx.py @@ -20,7 +20,6 @@ DEFAULT_INFLUX_PASSWORD = None DEFAULT_INFLUX_PROTOCOL = "http" - class InfluxReporter(Reporter): """ @@ -51,9 +50,8 @@ def _create_database(self): q = quote("CREATE DATABASE %s" % self.database) request = Request(url + "?q=" + q) if self.username: - auth = base64.encodestring( - '%s:%s' % (self.username, self.password))[:-1] - request.add_header("Authorization", "Basic %s" % auth) + auth = _encode_username(self.username, self.password) + request.add_header("Authorization", "Basic %s" % auth.decode('utf-8')) try: response = urlopen(request) _result = response.read() @@ -84,12 +82,16 @@ def report_now(self, registry=None, timestamp=None): url = "%s://%s:%s%s" % (self.protocol, self.server, self.port, path) request = Request(url, post_data.encode("utf-8")) if self.username: - auth = base64.encodestring( - '%s:%s' % (self.username, self.password))[:-1] - request.add_header("Authorization", "Basic %s" % auth) + auth = _encode_username(self.username, self.password) + request.add_header("Authorization", "Basic %s" % auth.decode('utf-8')) try: response = urlopen(request) - _result = response.read() + response.read() except URLError as err: LOG.warning("Cannot write to %s: %s", self.server, err.reason) + + +def _encode_username(username, password): + auth_string = ('%s:%s' % (username, password)).encode() + return base64.b64encode(auth_string)