2017-02-20 11:23:00 +01:00
|
|
|
# Petale - Simple App as Key/Value Storage Interface
|
|
|
|
# Copyright (C) 2017 Entr'ouvert
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Affero General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-04-06 12:14:01 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-06-07 05:54:14 +02:00
|
|
|
import logging
|
2020-04-06 12:14:01 +02:00
|
|
|
try:
|
|
|
|
from functools import reduce
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2017-06-07 05:54:14 +02:00
|
|
|
|
2017-10-18 16:53:51 +02:00
|
|
|
import requests
|
|
|
|
|
2020-04-06 12:14:01 +02:00
|
|
|
from django.utils.six.moves.urllib import parse as urlparse
|
|
|
|
|
2017-03-29 17:13:54 +02:00
|
|
|
from django.db.models.query import Q, F
|
|
|
|
from django.http import StreamingHttpResponse, HttpResponse
|
2017-06-07 05:54:14 +02:00
|
|
|
from django.conf import settings
|
2017-10-11 18:14:02 +02:00
|
|
|
from django.db.transaction import atomic
|
2017-03-01 15:00:30 +01:00
|
|
|
|
|
|
|
from rest_framework import status
|
2017-02-20 11:23:00 +01:00
|
|
|
from rest_framework.views import APIView
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2017-03-29 20:24:10 +02:00
|
|
|
from .models import CUT, Petal, Partner, AccessControlList
|
2017-03-29 20:57:33 +02:00
|
|
|
from .utils import logit, StreamingHash
|
2017-03-29 17:13:54 +02:00
|
|
|
from .exceptions import (PartnerNotFound, CutNotFound, KeyNotFound, NotFound, MissingContentType,
|
|
|
|
ConcurrentAccess, PreconditionException)
|
2017-02-28 10:57:16 +01:00
|
|
|
|
2017-02-20 11:23:00 +01:00
|
|
|
|
2017-07-21 14:16:25 +02:00
|
|
|
def cut_exists(request, cut_uuid):
|
2017-06-07 05:54:14 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-01-26 18:42:27 +01:00
|
|
|
if not getattr(settings, 'PETALE_CHECK_CUT_UUID', True):
|
|
|
|
CUT.objects.get_or_create(uuid=cut_uuid)
|
|
|
|
return True
|
|
|
|
|
2017-06-07 05:54:14 +02:00
|
|
|
authentic_url = getattr(settings, 'PETALE_AUTHENTIC_URL', None)
|
|
|
|
if not authentic_url:
|
|
|
|
logger.warning(u'PETALE_AUTHENTIC SETTINGS improperly defined')
|
|
|
|
return False
|
|
|
|
|
|
|
|
url = urlparse.urljoin(authentic_url, 'api/users/synchronization/')
|
|
|
|
try:
|
2017-07-21 14:16:25 +02:00
|
|
|
response = requests.post(url, json={"known_uuids": [cut_uuid]},
|
2017-07-21 14:57:05 +02:00
|
|
|
auth=request.user.credentials, verify=False)
|
2017-06-07 05:54:14 +02:00
|
|
|
response.raise_for_status()
|
|
|
|
except requests.RequestException as e:
|
|
|
|
logger.warning(u'authentic synchro API failed: %s', e)
|
|
|
|
return False
|
|
|
|
try:
|
|
|
|
data = response.json()
|
|
|
|
except ValueError as e:
|
|
|
|
logger.warning(u'authentic synchro API failed to decode response: %s', e)
|
|
|
|
return False
|
|
|
|
|
|
|
|
if data.get("unknown_uuids"):
|
2017-07-21 14:56:45 +02:00
|
|
|
logger.warning(u'unknown uuid : %s %s', request.user.credentials[0], data)
|
2017-06-07 05:54:14 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
CUT.objects.get_or_create(uuid=cut_uuid)
|
2017-03-29 21:05:27 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2017-03-09 11:29:53 +01:00
|
|
|
class PetalAPIKeysView(APIView):
|
2017-07-13 17:28:51 +02:00
|
|
|
http_method_names = ['get', 'delete']
|
2017-03-15 12:23:34 +01:00
|
|
|
|
2017-03-09 19:51:14 +01:00
|
|
|
@logit
|
2017-03-29 20:34:16 +02:00
|
|
|
def get(self, request, partner_name, cut_uuid):
|
2017-03-29 17:13:54 +02:00
|
|
|
try:
|
2017-03-29 20:34:16 +02:00
|
|
|
partner = Partner.objects.get(name=partner_name)
|
2017-03-30 08:52:40 +02:00
|
|
|
except Partner.DoesNotExist:
|
2017-03-29 17:13:54 +02:00
|
|
|
raise PartnerNotFound
|
|
|
|
|
2017-03-29 20:34:16 +02:00
|
|
|
qs = Petal.objects.filter(partner=partner, cut_id__uuid=cut_uuid)
|
2017-03-20 10:56:37 +01:00
|
|
|
|
2017-07-26 00:08:47 +02:00
|
|
|
if not qs.exists():
|
|
|
|
if not CUT.objects.filter(uuid=cut_uuid).exists():
|
2017-07-26 16:07:52 +02:00
|
|
|
if not cut_exists(request, cut_uuid):
|
2017-07-26 00:08:47 +02:00
|
|
|
raise CutNotFound
|
2017-03-20 10:56:37 +01:00
|
|
|
|
|
|
|
key_filter = None
|
|
|
|
|
|
|
|
# filter by param
|
2017-03-20 10:35:59 +01:00
|
|
|
if request.query_params.get('prefix'):
|
2017-03-20 10:56:37 +01:00
|
|
|
key_filter = Q(name__startswith=request.query_params['prefix'])
|
|
|
|
|
|
|
|
# filter by acls
|
|
|
|
acls = AccessControlList.objects.all()
|
|
|
|
acls = acls.filter(partner=partner, user=request.user)
|
|
|
|
prefixes = []
|
|
|
|
for acl in acls:
|
|
|
|
if acl.key == '*':
|
|
|
|
prefixes = []
|
|
|
|
break
|
|
|
|
prefixes.append(acl.key.strip('*'))
|
|
|
|
if prefixes:
|
|
|
|
acls_filter = reduce(Q.__or__, (Q(name__startswith=prefix) for prefix in prefixes))
|
|
|
|
if key_filter:
|
|
|
|
key_filter &= acls_filter
|
|
|
|
else:
|
|
|
|
key_filter = acls_filter
|
|
|
|
|
|
|
|
if key_filter:
|
2017-03-29 17:13:54 +02:00
|
|
|
qs = qs.filter(key_filter)
|
2017-03-09 11:29:53 +01:00
|
|
|
|
2017-03-29 17:13:54 +02:00
|
|
|
return Response({
|
|
|
|
'keys': [petal.name for petal in qs]
|
|
|
|
})
|
2017-03-09 11:29:53 +01:00
|
|
|
|
2017-07-13 17:28:51 +02:00
|
|
|
@logit
|
|
|
|
def delete(self, request, partner_name, cut_uuid):
|
|
|
|
try:
|
|
|
|
partner = Partner.objects.get(name=partner_name)
|
|
|
|
except Partner.DoesNotExist:
|
|
|
|
raise PartnerNotFound
|
|
|
|
|
|
|
|
Petal.objects.filter(partner=partner, cut_id__uuid=cut_uuid).delete()
|
|
|
|
|
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
|
|
|
|
2017-03-09 11:29:53 +01:00
|
|
|
|
2017-02-20 11:23:00 +01:00
|
|
|
class PetalAPIView(APIView):
|
2017-03-29 17:13:54 +02:00
|
|
|
http_method_names = ['get', 'head', 'put', 'delete']
|
2017-02-20 11:23:00 +01:00
|
|
|
|
2017-03-29 20:34:16 +02:00
|
|
|
def get_petal(self, partner_name, cut_uuid, petal_name, if_match=None, if_none_match=None):
|
2020-04-06 12:14:01 +02:00
|
|
|
if_match = if_match and [x.strip() for x in if_match.split(',')]
|
|
|
|
if_none_match = if_none_match and [x.strip() for x in if_none_match.split(',')]
|
2017-03-01 15:00:30 +01:00
|
|
|
|
2017-03-29 17:13:54 +02:00
|
|
|
try:
|
|
|
|
qs = Petal.objects.filter(
|
2017-03-29 20:34:16 +02:00
|
|
|
name=petal_name,
|
|
|
|
partner_id__name=partner_name,
|
|
|
|
cut_id__uuid=cut_uuid
|
2017-03-29 17:13:54 +02:00
|
|
|
)
|
2017-03-29 20:24:10 +02:00
|
|
|
qs = qs.select_related('partner', 'cut')
|
2017-03-29 17:13:54 +02:00
|
|
|
petal = qs.get()
|
|
|
|
except Petal.DoesNotExist:
|
|
|
|
if if_match:
|
|
|
|
raise PreconditionException
|
2017-06-07 05:54:14 +02:00
|
|
|
# Only check cut uuid on IDP during creation
|
2017-07-26 16:07:52 +02:00
|
|
|
if not CUT.objects.filter(uuid=cut_uuid).exists():
|
|
|
|
if not cut_exists(self.request, cut_uuid):
|
|
|
|
raise CutNotFound
|
2017-03-29 17:13:54 +02:00
|
|
|
raise KeyNotFound
|
|
|
|
|
|
|
|
if if_match:
|
|
|
|
if if_match == ['*']:
|
|
|
|
pass
|
|
|
|
elif petal.etag not in if_match:
|
|
|
|
raise PreconditionException
|
2017-02-20 11:23:00 +01:00
|
|
|
|
2017-03-29 17:13:54 +02:00
|
|
|
if if_none_match:
|
|
|
|
if if_none_match == ['*']:
|
|
|
|
raise PreconditionException
|
|
|
|
if petal.etag in if_none_match:
|
|
|
|
raise PreconditionException
|
|
|
|
return petal
|
2017-02-20 11:23:00 +01:00
|
|
|
|
2017-03-10 10:13:36 +01:00
|
|
|
@logit
|
2017-03-29 20:34:16 +02:00
|
|
|
def head(self, request, partner_name, cut_uuid, petal_name):
|
|
|
|
petal = self.get_petal(partner_name, cut_uuid, petal_name)
|
2017-03-29 17:13:54 +02:00
|
|
|
response = HttpResponse(content_type=petal.content_type)
|
|
|
|
response['Content-Length'] = petal.size
|
|
|
|
response['ETag'] = petal.etag
|
|
|
|
return response
|
2017-03-10 10:13:36 +01:00
|
|
|
|
2017-03-09 19:51:14 +01:00
|
|
|
@logit
|
2017-03-29 20:34:16 +02:00
|
|
|
def get(self, request, partner_name, cut_uuid, petal_name):
|
2017-09-27 17:10:04 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
2017-03-29 17:13:54 +02:00
|
|
|
if_none_match = request.META.get('HTTP_IF_NONE_MATCH')
|
2020-04-06 12:14:01 +02:00
|
|
|
if_none_match = if_none_match and [x.strip() for x in if_none_match.split(',')]
|
2017-03-29 17:13:54 +02:00
|
|
|
|
2017-03-29 20:34:16 +02:00
|
|
|
petal = self.get_petal(partner_name, cut_uuid, petal_name)
|
2017-03-29 17:13:54 +02:00
|
|
|
if if_none_match:
|
|
|
|
if if_none_match == ['*'] or petal.etag in if_none_match:
|
|
|
|
return Response(
|
|
|
|
status=status.HTTP_304_NOT_MODIFIED,
|
|
|
|
headers={
|
|
|
|
'ETag': petal.etag
|
|
|
|
}
|
|
|
|
)
|
2017-09-27 17:10:04 +02:00
|
|
|
# verify file exists before creating a StreamingHttpResponse
|
|
|
|
# as StreamingHttpResponse generate its content after the Django global try/catch
|
|
|
|
try:
|
|
|
|
petal.data.open()
|
|
|
|
except IOError as e:
|
|
|
|
logger.error('file not found "%s": %s', petal.data.path, e)
|
|
|
|
return HttpResponse('missing file', status=500)
|
2017-03-29 17:13:54 +02:00
|
|
|
response = StreamingHttpResponse(
|
|
|
|
petal.data.chunks(),
|
|
|
|
content_type=petal.content_type,
|
|
|
|
)
|
|
|
|
response['ETag'] = petal.etag
|
|
|
|
return response
|
2017-02-20 11:23:00 +01:00
|
|
|
|
2017-03-09 19:51:14 +01:00
|
|
|
@logit
|
2017-10-11 18:14:02 +02:00
|
|
|
@atomic
|
2017-03-29 20:34:16 +02:00
|
|
|
def put(self, request, partner_name, cut_uuid, petal_name):
|
2017-10-18 16:53:51 +02:00
|
|
|
# pylint: disable=too-many-locals
|
2017-03-29 17:13:54 +02:00
|
|
|
if_match = request.META.get('HTTP_IF_MATCH')
|
|
|
|
if_none_match = request.META.get('HTTP_IF_NONE_MATCH')
|
|
|
|
content_type = request.META.get('CONTENT_TYPE')
|
2017-03-03 16:28:41 +01:00
|
|
|
|
2017-03-13 14:13:19 +01:00
|
|
|
# check that content-type is set
|
2017-03-29 17:13:54 +02:00
|
|
|
if not content_type:
|
|
|
|
raise MissingContentType
|
|
|
|
|
|
|
|
try:
|
2017-03-29 20:34:16 +02:00
|
|
|
petal = self.get_petal(partner_name, cut_uuid, petal_name,
|
2017-03-29 17:13:54 +02:00
|
|
|
if_match=if_match,
|
|
|
|
if_none_match=if_none_match)
|
2017-10-11 18:14:02 +02:00
|
|
|
created = False
|
2017-03-29 17:13:54 +02:00
|
|
|
except PreconditionException:
|
|
|
|
raise ConcurrentAccess
|
|
|
|
except NotFound:
|
|
|
|
try:
|
2017-03-29 20:34:16 +02:00
|
|
|
partner = Partner.objects.get(name=partner_name)
|
2017-03-30 08:52:40 +02:00
|
|
|
except Partner.DoesNotExist:
|
2017-03-29 17:13:54 +02:00
|
|
|
raise PartnerNotFound
|
2017-06-07 05:54:14 +02:00
|
|
|
try:
|
|
|
|
cut = CUT.objects.get(uuid=cut_uuid)
|
|
|
|
except CUT.DoesNotExist:
|
|
|
|
raise CutNotFound
|
2017-03-29 17:13:54 +02:00
|
|
|
|
2017-10-11 18:14:02 +02:00
|
|
|
petal, created = Petal.objects.get_or_create(
|
|
|
|
name=petal_name, partner=partner, cut=cut,
|
|
|
|
defaults={'size': 0})
|
|
|
|
if not created and if_none_match:
|
|
|
|
raise ConcurrentAccess
|
2017-03-29 17:13:54 +02:00
|
|
|
else:
|
|
|
|
if if_none_match:
|
|
|
|
raise ConcurrentAccess
|
|
|
|
|
|
|
|
status_code = status.HTTP_200_OK
|
2017-03-29 20:57:33 +02:00
|
|
|
try:
|
|
|
|
content_length = int(request.META.get('CONTENT_LENGTH'))
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
content_length = 0
|
2017-03-30 00:15:13 +02:00
|
|
|
size_delta = content_length - petal.size
|
|
|
|
|
|
|
|
petal.check_limits(content_length)
|
2017-03-29 17:13:54 +02:00
|
|
|
|
2017-10-11 18:14:02 +02:00
|
|
|
if created:
|
2017-03-29 17:13:54 +02:00
|
|
|
status_code = status.HTTP_201_CREATED
|
2017-03-29 20:57:33 +02:00
|
|
|
old_name = None
|
2017-03-29 17:13:54 +02:00
|
|
|
else:
|
2017-03-29 20:57:33 +02:00
|
|
|
old_name = petal.data.name
|
|
|
|
|
|
|
|
streaming_digest = StreamingHash(request)
|
|
|
|
petal.data.save(petal_name, streaming_digest, save=False)
|
2017-03-29 17:13:54 +02:00
|
|
|
|
|
|
|
# update metadata
|
|
|
|
petal.content_type = content_type
|
2017-03-29 20:57:33 +02:00
|
|
|
petal.etag = streaming_digest.etag()
|
|
|
|
petal.size = content_length
|
2017-03-29 17:13:54 +02:00
|
|
|
|
|
|
|
# update partner size
|
2017-03-29 20:24:10 +02:00
|
|
|
Partner.objects.filter(id=petal.partner.id).update(size=F('size') + size_delta)
|
2017-03-29 17:13:54 +02:00
|
|
|
|
2017-03-01 16:52:12 +01:00
|
|
|
petal.save()
|
2017-03-29 20:57:33 +02:00
|
|
|
if old_name:
|
|
|
|
petal.data.storage.delete(old_name)
|
2017-03-29 17:13:54 +02:00
|
|
|
return Response(
|
|
|
|
{},
|
|
|
|
status=status_code,
|
|
|
|
headers={
|
|
|
|
'ETag': petal.etag
|
|
|
|
})
|
2017-02-20 11:23:00 +01:00
|
|
|
|
2017-03-09 19:51:14 +01:00
|
|
|
@logit
|
2017-03-29 20:34:16 +02:00
|
|
|
def delete(self, request, partner_name, cut_uuid, petal_name):
|
2017-04-07 10:14:00 +02:00
|
|
|
if_match = request.META.get('HTTP_IF_MATCH')
|
2017-03-29 17:13:54 +02:00
|
|
|
if_none_match = request.META.get('HTTP_IF_NONE_MATCH')
|
|
|
|
|
2017-04-07 09:52:51 +02:00
|
|
|
try:
|
|
|
|
petal = self.get_petal(
|
|
|
|
partner_name, cut_uuid, petal_name,
|
|
|
|
if_match=if_match,
|
|
|
|
if_none_match=if_none_match)
|
|
|
|
except PreconditionException:
|
|
|
|
raise ConcurrentAccess
|
2017-03-01 15:00:30 +01:00
|
|
|
petal.delete()
|
2017-03-29 17:13:54 +02:00
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|