Commit aaaaa0b0 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Add logging module

parent c7cd871a
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponse, HttpResponseServerError
from django.http import HttpResponse
from django.db.utils import OperationalError
from django.shortcuts import redirect
from django.views import View
......@@ -15,9 +15,11 @@ from .serializers import AuthInfoSerializer
import logging
logger = logging.getLogger(__name__)
idp_cookie_name = 'idp_id'
def idp_id_from_request(request):
id = request.COOKIES.get('idp_id', None)
id = request.COOKIES.get(idp_cookie_name, None)
if id is not None:
return id
else:
......@@ -38,7 +40,6 @@ def get_session(request, key, default):
def set_session(request, key, value):
try:
value = request.session[key] = value
except OperationalError:
......@@ -115,7 +116,10 @@ class AuthCallback(View):
if user is None:
# authentication failed -> 401
logger.error('Login for a user failed')
msg = 'Login for user {} failed'.format(
request.user
)
logger.error(msg)
response = HttpResponse('Unauthorized', status=401)
else:
# redirect back to the frontend
......
......@@ -7,11 +7,15 @@ from rest_framework.response import Response
from rest_framework import status
from . import serializers, models as frontend_models
from .. import models
from .. import models, logging
def _api_error_response():
return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST)
# Response for StateView, LogoutView, and all post requests
def state_response(request):
def _api_state_response(request):
response = {
'logged_in': request.user.is_authenticated()
}
......@@ -39,7 +43,8 @@ class OperationalView(views.APIView):
db_conn.cursor()
# we check if we can access the state
request.session
except OperationalError:
except OperationalError as e:
logging.log_exception(e)
op = False
else:
op = True
......@@ -51,7 +56,7 @@ class StateView(views.APIView):
permission_classes = (AllowAny,)
def get(self, request, format=None):
return state_response(request)
return _api_state_response(request)
class ServiceViewSet(viewsets.ModelViewSet):
......@@ -62,7 +67,7 @@ class ServiceViewSet(viewsets.ModelViewSet):
class SSHPublicKeyView(views.APIView):
def post(self, request, format=None):
if 'type' not in request.data:
return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST)
return _api_error_response()
request_type = request.data['type']
......@@ -77,7 +82,7 @@ class SSHPublicKeyView(views.APIView):
# key state: -> (1), if there are no deployments
# key state: -> (5), if there are deployments
key.delete_key()
return state_response(request)
return _api_state_response(request)
elif request_type == 'add':
if 'key' in request.data:
key = models.SSHPublicKey(
......@@ -87,19 +92,22 @@ class SSHPublicKeyView(views.APIView):
)
# key state: -> (2)
key.save()
return state_response(request)
return _api_state_response(request)
return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST)
return _api_error_response()
class DeploymentView(views.APIView):
def post(self, request, format=None):
if (
'type' not in request.data or
'key' not in request.data or
'service' not in request.data
):
return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST)
logging.logger.error(
'Deployment api got invalid request {}'.format(request.data))
return _api_error_response()
request_type = request.data['type']
request_service = get_object_or_404(
......@@ -113,8 +121,7 @@ class DeploymentView(views.APIView):
deployment = request.user.deployments.get(service=request_service)
except Exception:
if request_type == 'remove':
return Response({'ok': False},
status=status.HTTP_400_BAD_REQUEST)
return _api_error_response()
deployment = models.Deployment(
user=request.user,
......@@ -126,7 +133,7 @@ class DeploymentView(views.APIView):
elif request_type == 'remove':
deployment.withdraw_key(request_key)
else:
return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST)
return _api_error_response()
deployment.save()
return state_response(request)
return _api_state_response(request)
import logging
logger = logging.getLogger(__name__)
def log_exception(exception):
msg = 'Exception occured: {}'.format(exception)
logger.error(msg)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment