From 47fe867803ec7e87a49e57a9fbd6874ee6f87944 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 27 Nov 2020 18:37:45 +0100 Subject: [PATCH] root: add more API methods --- passbook/core/api/applications.py | 14 ++++++++++++++ passbook/flows/api.py | 20 +++++++++++++++++++- passbook/policies/api.py | 2 ++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/passbook/core/api/applications.py b/passbook/core/api/applications.py index 52d6a41d3..79a207bf8 100644 --- a/passbook/core/api/applications.py +++ b/passbook/core/api/applications.py @@ -1,11 +1,14 @@ """Application API Views""" from django.db.models import QuerySet +from rest_framework.decorators import action from rest_framework.request import Request from rest_framework.response import Response from rest_framework.serializers import ModelSerializer from rest_framework.viewsets import ModelViewSet from rest_framework_guardian.filters import ObjectPermissionsFilter +from passbook.admin.api.overview_metrics import get_events_per_1h +from passbook.audit.models import EventAction from passbook.core.models import Application from passbook.policies.engine import PolicyEngine @@ -59,3 +62,14 @@ class ApplicationViewSet(ModelViewSet): allowed_applications.append(application) serializer = self.get_serializer(allowed_applications, many=True) return self.get_paginated_response(serializer.data) + + @action(detail=True) + def metrics(self, request: Request, slug: str): + # TODO: Check app read and audit read perms + app = Application.objects.get(slug=slug) + return Response( + get_events_per_1h( + action=EventAction.AUTHORIZE_APPLICATION, + context__authorized_application__pk=app.pk.hex, + ) + ) diff --git a/passbook/flows/api.py b/passbook/flows/api.py index 93e6f6d67..d1aaa71fd 100644 --- a/passbook/flows/api.py +++ b/passbook/flows/api.py @@ -1,17 +1,35 @@ """Flow API Views""" +from django.core.cache import cache from rest_framework.serializers import ModelSerializer, SerializerMethodField from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet from passbook.flows.models import Flow, FlowStageBinding, Stage +from passbook.flows.planner import cache_key class FlowSerializer(ModelSerializer): """Flow Serializer""" + cache_count = SerializerMethodField() + + # pylint: disable=invalid-name + def get_cache_count(self, flow: Flow): + """Get count of cached flows""" + return len(cache.keys(f"{cache_key(flow)}*")) + class Meta: model = Flow - fields = ["pk", "name", "slug", "title", "designation", "stages", "policies"] + fields = [ + "pk", + "name", + "slug", + "title", + "designation", + "stages", + "policies", + "cache_count", + ] class FlowViewSet(ModelViewSet): diff --git a/passbook/policies/api.py b/passbook/policies/api.py index 6a47cd8de..3a67ec42d 100644 --- a/passbook/policies/api.py +++ b/passbook/policies/api.py @@ -1,4 +1,5 @@ """policy API Views""" +import django_filters.rest_framework from django.core.exceptions import ObjectDoesNotExist from rest_framework.serializers import ( ModelSerializer, @@ -62,6 +63,7 @@ class PolicyBindingViewSet(ModelViewSet): queryset = PolicyBinding.objects.all() serializer_class = PolicyBindingSerializer + filterset_fields = ["policy", "target", "enabled", "order", "timeout"] class PolicySerializer(ModelSerializer):