Skip to content

Commit

Permalink
Merge pull request #35 from Fra-nk/fully_qualified_user
Browse files Browse the repository at this point in the history
Fully qualified user
  • Loading branch information
Fra-nk committed Feb 15, 2019
2 parents f1efd15 + ab8a256 commit 28eb9db
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 40 deletions.
9 changes: 6 additions & 3 deletions CHANGES.rst → CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
Changelog
=========

v2.1.1
======

- Allow user to supply username in the format `user@host` himself.

v2.1.0
==========
======

- init-db now constructs usernames identical to the server. Thus, `user@host` names now work properly.
- Fix log messages of Gunicorn and Flask not ending up in the root logger.`
Expand Down Expand Up @@ -80,5 +85,3 @@ v0.1.0
======

- First running version


45 changes: 22 additions & 23 deletions postgraas_server/management_resources.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import datetime
import json
import logging

import psycopg2
Expand All @@ -26,14 +25,14 @@ class DBInstance(db.Model):
container_id = db.Column(db.String(100))

def __init__(
self,
postgraas_instance_name,
db_name,
username,
password,
hostname,
port,
container_id=None
self,
postgraas_instance_name,
db_name,
username,
password,
hostname,
port,
container_id=None
):
self.postgraas_instance_name = postgraas_instance_name
self.creation_timestamp = datetime.datetime.now()
Expand Down Expand Up @@ -76,24 +75,24 @@ def delete(self, id):

entity = DBInstance.query.get(id)
if not entity:
abort(404, status='failed',
msg='Postgraas instance {} does not exist'.format(id)
)
abort(404, status='failed',
msg='Postgraas instance {} does not exist'.format(id)
)

try:
with psycopg2.connect(
user=entity.username,
password=args['db_pwd'],
host=current_app.postgraas_backend.master_hostname,
port=entity.port,
dbname=entity.db_name
user=entity.username,
password=args['db_pwd'],
host=current_app.postgraas_backend.master_hostname,
port=entity.port,
dbname=entity.db_name
):
pass
except Exception as ex:
return_code = 401 if 'authentication failed' in str(ex) else 500
abort(return_code, status='failed',
msg='Could not connect to postgres instance: {}'.format(str(ex))
)
msg='Could not connect to postgres instance: {}'.format(str(ex))
)

if not current_app.postgraas_backend.exists(entity):
logger.warning(
Expand Down Expand Up @@ -130,9 +129,9 @@ def post(self):
type=str,
help='name of the postgraas instance'
)
parser.add_argument('db_name', required=True, type=str, help='name of the db')
parser.add_argument('db_username', required=True, type=str, help='username of the db')
parser.add_argument('db_pwd', required=True, type=str, help='pass of the db user')
parser.add_argument('db_name', required=True, type=str, help='Database name')
parser.add_argument('db_username', required=True, type=str, help="Username of database user")
parser.add_argument('db_pwd', required=True, type=str, help='Password of the database user')
args = parser.parse_args()

if not args['db_pwd']:
Expand Down Expand Up @@ -187,5 +186,5 @@ def post(self):
db.session.commit()
db_credentials["container_id"] = db_entry.container_id
db_credentials["postgraas_instance_id"] = db_entry.id
db_credentials["db_username"] = username
db_credentials["db_username"] = db_entry.username
return db_credentials, 201
53 changes: 41 additions & 12 deletions tests/test_integration/test_postgraas_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
import os
import uuid

import docker
import os
import pytest
from mock import patch, MagicMock, Mock

Expand All @@ -14,31 +14,27 @@
from .utils import wait_for_postgres_listening

DOCKER_CONFIG = {
"metadb":
{
"metadb": {
"db_name": "postgraas",
"db_username": "postgraas",
"db_pwd": "postgraas12",
"host": "localhost",
"port": "54321"
},
"backend":
{
"backend": {
"type": "docker"
}
}

CLUSTER_CONFIG = {
"metadb":
{
"metadb": {
"db_name": "postgraas",
"db_username": "postgraas",
"db_pwd": "postgraas12",
"host": "localhost",
"port": "54321"
},
"backend":
{
"backend": {
"type": "pg_cluster",
"host": os.environ.get('PGHOST', 'localhost'),
"port": os.environ.get('PGPORT', '5432'),
Expand Down Expand Up @@ -123,7 +119,7 @@ def docker_setup(request, tmpdir):
ctx.pop()


class PostgraasApiTestBase:
class PostgraasApiTestBase(object):
def get_postgraas_by_name(self, name, client):
headers = {'Content-Type': 'application/json'}
instances = client.get('/api/v2/postgraas_instances', headers=headers)
Expand Down Expand Up @@ -164,6 +160,22 @@ def test_create_postgres_instance_api(self):
assert created_db["db_name"] == 'test_create_postgres_instance'
self.delete_instance_by_name(db_credentials, self.app_client)

def test_create_postgres_instance_api_with_fully_qualified_user(self):
db_credentials = {
"postgraas_instance_name": "tests_postgraas_test_create_postgres_instance_api",
"db_name": "test_create_postgres_instance",
"db_username": "db_user@tests_postgraas_test_create_postgres_instance_api",
"db_pwd": "secret"
}
self.delete_instance_by_name(db_credentials, self.app_client)
headers = {'Content-Type': 'application/json'}
result = self.app_client.post(
'/api/v2/postgraas_instances', headers=headers, data=json.dumps(db_credentials)
)
created_db = json.loads(result.get_data(as_text=True))
assert created_db["db_name"] == 'test_create_postgres_instance'
self.delete_instance_by_name(db_credentials, self.app_client)

def test_create_docker_fails(self):
db_credentials = {
"postgraas_instance_name": "tests_postgraas_test_create_postgres_instance_api",
Expand Down Expand Up @@ -246,13 +258,30 @@ def test_create_postgres_instance(self):
"port": pid.get_open_port()
}
mock_c = MagicMock()
mock_c.id = 'fy8rfsufusgsufbvluluivhhvsbr'
mock_c.id = 'EW3uvF3C3tLce9Eo5D76NbQe'
mock_create = Mock(return_value=mock_c)
with patch.object(docker.models.containers.ContainerCollection, 'create', mock_create):
result = pid.create_postgres_instance(
'tests_postgraas_test_instance_name', db_credentials
)
assert result == 'EW3uvF3C3tLce9Eo5D76NbQe'

def test_create_postgres_instance_with_fully_qualified_username(self):
db_credentials = {
"db_name": 'test_db_name',
"db_username": 'test_db_username@{}'.format(pid.get_hostname()),
"db_pwd": 'test_db_pwd',
"host": pid.get_hostname(),
"port": pid.get_open_port()
}
mock_c = MagicMock()
mock_c.id = 'dN4IsDN5eOqeCgli23MlxeTA'
mock_create = Mock(return_value=mock_c)
with patch.object(docker.models.containers.ContainerCollection, 'create', mock_create):
result = pid.create_postgres_instance(
'tests_postgraas_test_instance_name', db_credentials
)
assert result == 'fy8rfsufusgsufbvluluivhhvsbr'
assert result == 'dN4IsDN5eOqeCgli23MlxeTA'

def test_delete_postgres_instance_api(self):
db_credentials = {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_unit/test_backends_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ def test_get_backend_pg_cluster():
def test_get_backend_docker_default():
config = {}
backend_config = get_backend(config)
assert isinstance(backend_config, DockerBackend)
assert isinstance(backend_config, DockerBackend)
3 changes: 2 additions & 1 deletion tests/test_unit/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import postgraas_server.configuration as cf
import pytest


class TestConfiguration:
module_path = os.path.abspath(os.path.dirname(__file__))

Expand Down Expand Up @@ -111,4 +112,4 @@ def test_get_plain_password(self, tmpdir):
}
password_string = cf.get_password(config_dict)
print(password_string)
assert password_string == "v3rys3cur3"
assert password_string == "v3rys3cur3"

0 comments on commit 28eb9db

Please sign in to comment.