Skip to content

Commit

Permalink
Merge pull request #2016 from ckan/1976-crash-without-siteurl
Browse files Browse the repository at this point in the history
Make sure CKAN complains if site_url is missing
  • Loading branch information
David Read committed Jun 23, 2015
2 parents 5445af6 + 0fcf6de commit be95a53
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
16 changes: 15 additions & 1 deletion ckan/config/environment.py
Expand Up @@ -249,7 +249,9 @@ def genshi_lookup_attr(cls, obj, key):

def update_config():
''' This code needs to be run when the config is changed to take those
changes into account. '''
changes into account. It is called whenever a plugin is loaded as the
plugin might have changed the config values (for instance it might
change ckan.site_url) '''

for plugin in p.PluginImplementations(p.IConfigurer):
# must do update in place as this does not work:
Expand All @@ -274,6 +276,18 @@ def update_config():
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

site_url = config.get('ckan.site_url', '')
if not site_url:
raise RuntimeError(
'ckan.site_url is not configured and it must have a value.'
' Please amend your .ini file.')
if not site_url.lower().startswith('http'):
raise RuntimeError(
'ckan.site_url should be a full URL, including the schema '
'(http or https)')

# Remove backslash from site_url if present
config['ckan.site_url'] = config['ckan.site_url'].rstrip('/')

ckan_host = config['ckan.host'] = urlparse(site_url).netloc
if config.get('ckan.site_id') is None:
if ':' in ckan_host:
Expand Down
24 changes: 24 additions & 0 deletions ckan/tests/config/test_environment.py
Expand Up @@ -5,6 +5,9 @@

import ckan.tests.helpers as h
import ckan.plugins as p
from ckan.config import environment

from ckan.tests import helpers


class TestUpdateConfig(h.FunctionalTestBase):
Expand Down Expand Up @@ -71,3 +74,24 @@ def test_update_config_db_url_precedence(self):

nosetools.assert_equal(config['sqlalchemy.url'],
'postgresql://mynewsqlurl/')


class TestSiteUrlMandatory(object):

@helpers.change_config('ckan.site_url', '')
def test_missing_siteurl(self):
nosetools.assert_raises(RuntimeError, environment.update_config)

@helpers.change_config('ckan.site_url', 'demo.ckan.org')
def test_siteurl_missing_schema(self):
nosetools.assert_raises(RuntimeError, environment.update_config)

@helpers.change_config('ckan.site_url', 'ftp://demo.ckan.org')
def test_siteurl_wrong_schema(self):
nosetools.assert_raises(RuntimeError, environment.update_config)

@helpers.change_config('ckan.site_url', 'http://demo.ckan.org/')
def test_siteurl_removes_backslash(self):
environment.update_config()
nosetools.assert_equals(config['ckan.site_url'],
'http://demo.ckan.org')
3 changes: 2 additions & 1 deletion contrib/docker/my_init.d/50_configure
Expand Up @@ -25,7 +25,8 @@ write_config () {
"solr_url = ${SOLR_URL}" \
"ckan.storage_path = /var/lib/ckan" \
"email_to = disabled@example.com" \
"error_email_from = ckan@$(hostname -f)"
"error_email_from = ckan@$(hostname -f)" \
"ckan.site_url = http://192.168.0.6"

if [ -n "$ERROR_EMAIL" ]; then
sed -i -e "s&^#email_to.*&email_to = ${ERROR_EMAIL}&" "$CONFIG"
Expand Down
4 changes: 3 additions & 1 deletion doc/maintaining/configuration.rst
Expand Up @@ -217,11 +217,13 @@ Example::

ckan.site_url = http://scotdata.ckan.net

Default value: (none)
Default value: (an explicit value is mandatory)

The URL of your CKAN site. Many CKAN features that need an absolute URL to your
site use this setting.

.. important:: It is mandatory to complete this setting

.. warning::

This setting should not have a trailing / on the end.
Expand Down
7 changes: 7 additions & 0 deletions doc/maintaining/installing/install-from-source.rst
Expand Up @@ -227,6 +227,13 @@ site_id

ckan.site_id = default

site_url
Provide the site's URL (used when putting links to the site into the
FileStore, notification emails etc). For example::

ckan.site_url = http://demo.ckan.org

Do not add a trailing slash to the URL.

.. _setting up solr:

Expand Down

0 comments on commit be95a53

Please sign in to comment.