Skip to content

Commit

Permalink
bugfix in username handling in prometheus app
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianneubauer committed Sep 18, 2017
1 parent cd2f790 commit 42005cd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
15 changes: 10 additions & 5 deletions postgraas_server/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ def get_application_config(config):


def get_meta_db_config_path(config):
try:
server = config.get('metadb', 'server')
username = '@'.join([config.get('metadb', 'db_username'), server])
except ConfigParser.NoOptionError:
username = config.get('metadb', 'db_username')
username = get_user(config)

db_path = 'postgresql://{db_username}:{db_pwd}@{host}:{port}/{db_name}'.format(
db_name=config.get('metadb', 'db_name'),
Expand All @@ -71,3 +67,12 @@ def get_meta_db_config_path(config):
port=config.get('metadb', 'port')
)
return db_path


def get_user(config):
try:
server = config.get('metadb', 'server')
username = '@'.join([config.get('metadb', 'db_username'), server])
except ConfigParser.NoOptionError:
username = config.get('metadb', 'db_username')
return username
3 changes: 2 additions & 1 deletion postgraas_server/prometheus_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

@contextmanager
def db_connection(config):
username = cfg.get_user(config)
connection = psycopg2.connect(
database=config.get('metadb', 'db_name'),
user=config.get('metadb', 'db_username'),
user=username,
password=config.get('metadb', 'db_pwd'),
host=config.get('metadb', 'host'),
port=config.get('metadb', 'port')
Expand Down
30 changes: 29 additions & 1 deletion tests/test_unit/test_configuration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os

import ConfigParser
import StringIO
from cryptography.fernet import Fernet

import postgraas_server.configuration as cf
Expand All @@ -19,6 +20,33 @@ def test_get_config(self):
expected = 'postgraas'
assert actual.get('metadb', 'db_name') == expected

def test_get_user(self):
config_string = '''
[metadb]
db_username = postgraas_user
'''

config = ConfigParser.ConfigParser()

config.readfp(StringIO.StringIO(config_string))
username = cf.get_user(config)
expected = 'postgraas_user'

assert username == expected

config_string = '''
[metadb]
server = testserver1
db_username = postgraas_user
'''
config = ConfigParser.ConfigParser()

config.readfp(StringIO.StringIO(config_string))
username = cf.get_user(config)
expected = 'postgraas_user@testserver1'

assert username == expected

def test_secrets(self, tmpdir):
secrets_file = tmpdir.join('secrets')
key = Fernet.generate_key()
Expand Down

0 comments on commit 42005cd

Please sign in to comment.