Skip to content

Commit

Permalink
Merge pull request #36 from timohaas/bugfix
Browse files Browse the repository at this point in the history
Prevent users from creating databases with owner `postgres`
  • Loading branch information
Fra-nk committed Nov 6, 2019
2 parents 2d94e02 + 98d9080 commit 96cf19b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
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.1.2
======

- Blacklist postgres as username

v2.1.1
======

Expand Down
6 changes: 6 additions & 0 deletions postgraas_server/management_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ def post(self):
if not args['db_pwd']:
abort(400, msg='The password may not be empty.')

if args['db_username'] == "postgres" or args['db_username'].startswith("postgres@"):
abort(
422,
msg="username {} is backlisted".format(args['db_username'])
)

if DBInstance.query.filter_by(postgraas_instance_name=args['postgraas_instance_name']
).first():
abort(
Expand Down
45 changes: 45 additions & 0 deletions tests/test_integration/test_postgraas_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,51 @@ def test_create_postgres_instance_api_with_fully_qualified_user(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_postgres_as_user(self):
db_credentials = {
"postgraas_instance_name": "tests_postgraas_test_create_postgres_instance_api",
"db_name": "test_create_postgres_instance",
"db_username": "postgres",
"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)
)
assert result.status_code == 422
self.delete_instance_by_name(db_credentials, self.app_client)

def test_create_postgres_instance_api_with_postgres_at_example_com_as_user(self):
db_credentials = {
"postgraas_instance_name": "tests_postgraas_test_create_postgres_instance_api",
"db_name": "test_create_postgres_instance",
"db_username": "postgres@example.com",
"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)
)
assert result.status_code == 422
self.delete_instance_by_name(db_credentials, self.app_client)

def test_create_postgres_instance_api_with_postgres_at_localhost_as_user(self):
db_credentials = {
"postgraas_instance_name": "tests_postgraas_test_create_postgres_instance_api",
"db_name": "test_create_postgres_instance",
"db_username": "postgres@localhost",
"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)
)
assert result.status_code == 422
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

0 comments on commit 96cf19b

Please sign in to comment.