Skip to content

Commit

Permalink
[#642] Make check functions consistent (return bool instead of raisin…
Browse files Browse the repository at this point in the history
…g exceptions)
  • Loading branch information
domoritz committed Mar 28, 2013
1 parent 3824eb2 commit 306f016
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions ckanext/datastore/plugin.py
Expand Up @@ -18,9 +18,6 @@ class DatastoreException(Exception):


class DatastorePlugin(p.SingletonPlugin):
'''
Datastore plugin.
'''
p.implements(p.IConfigurable, inherit=True)
p.implements(p.IActions)
p.implements(p.IAuthFunctions)
Expand Down Expand Up @@ -59,9 +56,11 @@ def configure(self, config):
# Make sure that the right permissions are set
# so that no harmful queries can be made
if not ('debug' in config and config['debug']):
self._check_separate_db()
if self._same_ckan_and_datastore_db():
raise Exception("The write and read-only database "
"connection url are the same.")
if self.legacy_mode:
log.warn("Legacy mode active."
log.warn("Legacy mode active. "
"The sql search will not be available.")
elif not self._read_connection_has_correct_privileges():
if 'debug' in self.config and self.config['debug']:
Expand Down Expand Up @@ -115,6 +114,10 @@ def new_resource_show(context, data_dict):
self._add_is_valid_type_function()

def _is_read_only_database(self):
'''
Returns True if no connection has CREATE privileges on the public
schema. This is the case if replication is enabled.
'''
for url in [self.ckan_url, self.write_url, self.read_url]:
connection = db._get_engine(None,
{'connection_url': url}).connect()
Expand All @@ -124,26 +127,28 @@ def _is_read_only_database(self):
return False
return True

def _check_separate_db(self):
def _same_ckan_and_datastore_db(self):
'''
Make sure the datastore is on a separate db. Otherwise one could access
all internal tables via the api.
Returns True if the CKAN and DataStore db are the same
'''

if not self.legacy_mode:
if self.write_url == self.read_url:
raise Exception("The write and read-only database "
"connection url are the same.")
return True

if self._get_db_from_url(self.ckan_url) == self._get_db_from_url(self.read_url):
raise Exception("The CKAN and datastore database are the same.")
return True
return False

def _get_db_from_url(self, url):
return url[url.rindex("@"):]

def _read_connection_has_correct_privileges(self):
'''
Check whether the right permissions are set for the read only user.
Returns True if the right permissions are set for the read only user.
A table is created by the write user to test the read only user.
'''
write_connection = db._get_engine(None,
Expand All @@ -162,8 +167,6 @@ def _read_connection_has_correct_privileges(self):
have_privilege = read_connection.execute(sql).first()[0]
if have_privilege:
return False
except Exception:
raise
finally:
write_connection.execute("DROP TABLE _foo")
return True
Expand Down

0 comments on commit 306f016

Please sign in to comment.