Skip to content

Commit

Permalink
[#4878] Allow custom CKAN callback URL for the DataPusher
Browse files Browse the repository at this point in the history
The DataPusher pings back CKAN when performing or finishing
a job (calling the datapusher_hook action), and it does so
via an HTTP request to the host defined in ckan.site_url.

There are a number of scenarios where this does not work, eg:

* CKAN and DataPusher sitting behind a Firewall that doesn't
  allow external requests
* Standard Docker compose setup for development where the
  ckan.site_url is http://localhost:5000 or similar

This change adds a new config option that allows to define an
alternative internal URL that DataPusher can reach.
  • Loading branch information
amercader committed Jun 27, 2019
1 parent f6adcaa commit 02f80e1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ckanext/datapusher/logic/action.py
Expand Up @@ -62,7 +62,14 @@ def datapusher_submit(context, data_dict):
datapusher_url = config.get('ckan.datapusher.url')

site_url = h.url_for('/', qualified=True)
callback_url = h.url_for('/api/3/action/datapusher_hook', qualified=True)

callback_url_base = config.get('ckan.datapusher.callback_url_base')
if callback_url_base:
callback_url = urlparse.urljoin(
callback_url_base.rstrip('/'), '/api/3/action/datapusher_hook')
else:
callback_url = h.url_for(
'/api/3/action/datapusher_hook', qualified=True)

user = p.toolkit.get_action('user_show')(context, {'id': context['user']})

Expand Down
31 changes: 31 additions & 0 deletions ckanext/datapusher/tests/test.py
Expand Up @@ -11,6 +11,7 @@
import ckanext.datastore.backend.postgres as db
import responses
import nose
from nose.tools import assert_equals
import sqlalchemy.orm as orm
from ckan.common import config
from ckanext.datastore.tests.helpers import rebuild_all_dbs, set_url_type
Expand Down Expand Up @@ -232,3 +233,33 @@ def test_datapusher_hook_no_resource_id_in_metadata(self):

self.app.post('/api/action/datapusher_hook', params=postparams,
status=409)

@responses.activate
@helpers.change_config(
'ckan.datapusher.callback_url_base', 'https://ckan.example.com')
@helpers.change_config(
'ckan.datapusher.url', 'http://datapusher.ckan.org')
def test_custom_callback_url_base(self):

package = model.Package.get('annakarenina')
resource = package.resources[0]

responses.add(
responses.POST,
'http://datapusher.ckan.org/job',
content_type='application/json',
body=json.dumps({'job_id': 'foo', 'job_key': 'barloco'})
)
responses.add_passthru(config['solr_url'])

tests.call_action_api(
self.app, 'datapusher_submit', apikey=self.sysadmin_user.apikey,
resource_id=resource.id,
ignore_hash=True
)

data = json.loads(responses.calls[-1].request.body)
assert_equals(
data['result_url'],
'https://ckan.example.com/api/3/action/datapusher_hook'
)
16 changes: 16 additions & 0 deletions doc/maintaining/configuration.rst
Expand Up @@ -1524,6 +1524,22 @@ running on port 8800. If you want to manually install the DataPusher, follow
the installation `instructions <http://docs.ckan.org/projects/datapusher>`_.


.. _ckan.datapusher.callback_url_base:

ckan.datapusher.url
^^^^^^^^^^^^^^^^^^^

Example::

ckan.datapusher.callback_url_base = http://ckan:5000/

Default value: Value of ``ckan.site_url``

Alternative callback URL for DataPusher when performing a request to CKAN. This is
useful on scenarios where the host where DataPusher is running can not access the
public CKAN site URL.


.. _ckan.datapusher.assume_task_stale_after:

ckan.datapusher.assume_task_stale_after
Expand Down

0 comments on commit 02f80e1

Please sign in to comment.