Skip to content

Commit

Permalink
Storage Service: Add API key support
Browse files Browse the repository at this point in the history
Add an API key auth to the slumber storage service config. Add SS user & api
key to the SS config form.
  • Loading branch information
Hwesta committed May 18, 2016
1 parent 16ffb41 commit 59d4b64
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
17 changes: 16 additions & 1 deletion src/archivematicaCommon/lib/storageService.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import platform
import requests
from requests.auth import AuthBase
import slumber
import sys

Expand All @@ -22,6 +23,16 @@ class BadRequest(Exception):

############# HELPER FUNCTIONS #############

class TastypieApikeyAuth(AuthBase):
def __init__(self, username, apikey):
self.username = username
self.apikey = apikey

def __call__(self, r):
r.headers['Authorization'] = "ApiKey {0}:{1}".format(self.username, self.apikey)
return r


def get_setting(setting, default=''):
try:
return DashboardSetting.objects.get(name=setting).value
Expand All @@ -45,7 +56,9 @@ def _storage_service_url():
def _storage_api():
""" Returns slumber access to storage API. """
storage_service_url = _storage_service_url()
api = slumber.API(storage_service_url)
username = get_setting('storage_service_user', 'test')
api_key = get_setting('storage_service_apikey', None)
api = slumber.API(storage_service_url, auth=TastypieApikeyAuth(username, api_key))
return api

def _storage_relative_from_absolute(location_path, space_path):
Expand Down Expand Up @@ -77,6 +90,7 @@ def create_pipeline(create_default_locations=False, shared_path=None, api_userna
LOGGER.warning("Unable to create Archivematica pipeline in storage service from {} because {}".format(pipeline, e.content))
return False
except slumber.exceptions.HttpServerError as e:
LOGGER.warning("Unable to create Archivematica pipeline in storage service from {} because {}".format(pipeline, e.content), exc_info=True)
if 'column uuid is not unique' in e.content:
pass
else:
Expand All @@ -90,6 +104,7 @@ def _get_pipeline(uuid):
except slumber.exceptions.HttpClientError as e:
if e.response.status_code == 404:
LOGGER.warning("This Archivematica instance is not registered with the storage service or has been disabled.")
LOGGER.warning('Error fetching pipeline', exc_info=True)
pipeline = None
return pipeline

Expand Down
15 changes: 12 additions & 3 deletions src/dashboard/src/components/administration/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,18 @@ def save(self, *args, **kwargs):


class StorageSettingsForm(SettingsForm):
storage_service_url = forms.URLField(required=False,
label="Full URL of the storage service")

storage_service_url = forms.URLField(
label="Storage Service URL",
help_text='Full URL of the storage service. E.g. https://192.168.168.192:8000'
)
storage_service_user = forms.CharField(
label='Storage Service User',
help_text='User in the storage service to authenticate as. E.g. test'
)
storage_service_apikey = forms.CharField(
label='API key',
help_text='API key of the storage service user. E.g. 45f7684483044809b2de045ba59dc876b11b9810'
)

class ArchivistsToolkitConfigForm(ModelForm):
class Meta:
Expand Down
7 changes: 5 additions & 2 deletions src/dashboard/src/installer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,11 @@ def storagesetup(request):
dashboard_uuid = helpers.get_setting('dashboard_uuid', None)
assert dashboard_uuid is not None
# Prefill the storage service URL
inital_data = {'storage_service_url':
helpers.get_setting('storage_service_url', 'http://localhost:8000')}
inital_data = {
'storage_service_url': helpers.get_setting('storage_service_url', 'http://localhost:8000'),
'storage_service_user': helpers.get_setting('storage_service_user', 'test'),
'storage_service_apikey': helpers.get_setting('storage_service_apikey', None)
}
storage_form = StorageSettingsForm(request.POST or None, initial=inital_data)
if storage_form.is_valid():
# Set storage service URL
Expand Down
7 changes: 5 additions & 2 deletions src/dashboard/src/templates/installer/storagesetup.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
.submit {
margin-bottom: 1em;
}
label { width: auto; }
</style>
{% endblock %}

Expand All @@ -19,11 +20,13 @@

<form action="#" method="POST">
<div id="storage" class="well">
<p>Dashboard UUID: {{ dashboard_uuid }}</p>
<p>
<label>Dashboard UUID:</label>
<p><code>{{ dashboard_uuid }}</code></p>
</p>

{{ storage_form.as_p }}

<p>Would you like to used the default transfer source and AIP storage locations?</p>
</div>
<input class="submit" type="submit" name="use_default" value="Register with the storage service &amp; use default configuration." />
<input class="submit" type="submit" name="configure" value="Register with the storage service &amp; set up custom configuration." onclick="window.open($('#id_storage_service_url').val());" />
Expand Down

0 comments on commit 59d4b64

Please sign in to comment.