From 306f016d1b1803302c0c9b9999fad3b6e14ba92b Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Wed, 27 Mar 2013 12:51:38 +0100 Subject: [PATCH] [#642] Make check functions consistent (return bool instead of raising exceptions) --- ckanext/datastore/plugin.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/ckanext/datastore/plugin.py b/ckanext/datastore/plugin.py index 4b8ea588a93..10afcfeb192 100644 --- a/ckanext/datastore/plugin.py +++ b/ckanext/datastore/plugin.py @@ -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) @@ -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']: @@ -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() @@ -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, @@ -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