Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent creating databases with empty passwords #29

Merged
merged 2 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

v2.0.1 (UNRLEASED)
==================

- prevent creation of databases with an empty password as those cannot be removed.

v2.0.0
======

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

import psycopg2
Expand Down Expand Up @@ -132,6 +133,9 @@ def post(self):
parser.add_argument('db_pwd', required=True, type=str, help='pass of the db user')
args = parser.parse_args()

if not args['db_pwd']:
abort(400, msg='The password may not be empty.')

if DBInstance.query.filter_by(postgraas_instance_name=args['postgraas_instance_name']
).first():
return {
Expand Down
45 changes: 33 additions & 12 deletions tests/test_integration/test_postgras_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,24 @@ def docker_setup(request, tmpdir):
class PostgraasApiTestBase:
def get_postgraas_by_name(self, name, client):
headers = {'Content-Type': 'application/json'}
list = client.get('/api/v2/postgraas_instances', headers=headers)
for instance in json.loads(list.get_data(as_text=True)):
instances = client.get('/api/v2/postgraas_instances', headers=headers)
for instance in json.loads(instances.get_data(as_text=True)):
if instance["postgraas_instance_name"] == name:
return instance["id"]
return None

def delete_instance_by_name(self, db_credentials, client):
id = self.get_postgraas_by_name(db_credentials["postgraas_instance_name"], client)
db_pwd = db_credentials["db_pwd"]
headers = {'Content-Type': 'application/json'}
client.delete(
'/api/v2/postgraas_instances/' + str(id),
data=json.dumps({
'db_pwd': db_pwd
}),
headers=headers
)
instance_id = self.get_postgraas_by_name(db_credentials["postgraas_instance_name"], client)
if instance_id is not None:
db_pwd = db_credentials["db_pwd"]
headers = {'Content-Type': 'application/json'}
client.delete(
'/api/v2/postgraas_instances/' + str(instance_id),
data=json.dumps({
'db_pwd': db_pwd
}),
headers=headers
)


@pytest.mark.usefixtures('docker_setup')
Expand Down Expand Up @@ -381,3 +383,22 @@ def test_return_postgres_instance_api(self):
assert actual_data == expected

self.delete_instance_by_name(db_credentials, self.app_client)

def test_empty_password(self):
instance_name = "test_empty_password"
db_credentials = {
"postgraas_instance_name": instance_name,
"db_name": self.db_name,
"db_username": self.username,
"db_pwd": "",
}
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 result.status_code == 400
print(created_db)
assert 'password may not be empty' in created_db["msg"]