api: create dedicated api for cached flows and policies

This commit is contained in:
Jens Langhammer 2020-12-16 22:18:36 +01:00
parent 9e33b49d29
commit cf46ee06b7
4 changed files with 40 additions and 18 deletions

View File

@ -19,8 +19,6 @@ class AdministrationOverviewSerializer(Serializer):
version = SerializerMethodField() version = SerializerMethodField()
version_latest = SerializerMethodField() version_latest = SerializerMethodField()
worker_count = SerializerMethodField() worker_count = SerializerMethodField()
cached_policies = SerializerMethodField()
cached_flows = SerializerMethodField()
def get_version(self, _) -> str: def get_version(self, _) -> str:
"""Get current version""" """Get current version"""
@ -38,14 +36,6 @@ class AdministrationOverviewSerializer(Serializer):
"""Ping workers""" """Ping workers"""
return len(CELERY_APP.control.ping(timeout=0.5)) return len(CELERY_APP.control.ping(timeout=0.5))
def get_cached_policies(self, _) -> int:
"""Get cached policy count"""
return len(cache.keys("policy_*"))
def get_cached_flows(self, _) -> int:
"""Get cached flow count"""
return len(cache.keys("flow_*"))
def create(self, request: Request) -> Response: def create(self, request: Request) -> Response:
raise NotImplementedError raise NotImplementedError
@ -54,12 +44,12 @@ class AdministrationOverviewSerializer(Serializer):
class AdministrationOverviewViewSet(ViewSet): class AdministrationOverviewViewSet(ViewSet):
"""Return single instance of AdministrationOverviewSerializer""" """General overview information about authentik."""
permission_classes = [IsAdminUser] permission_classes = [IsAdminUser]
@swagger_auto_schema(responses={200: AdministrationOverviewSerializer(many=True)}) @swagger_auto_schema(responses={200: AdministrationOverviewSerializer(many=True)})
def list(self, request: Request) -> Response: def list(self, request: Request) -> Response:
"""Return single instance of AdministrationOverviewSerializer""" """General overview information about authentik."""
serializer = AdministrationOverviewSerializer(True) serializer = AdministrationOverviewSerializer(True)
return Response(serializer.data) return Response(serializer.data)

View File

@ -19,13 +19,13 @@ from authentik.core.api.sources import SourceViewSet
from authentik.core.api.tokens import TokenViewSet from authentik.core.api.tokens import TokenViewSet
from authentik.core.api.users import UserViewSet from authentik.core.api.users import UserViewSet
from authentik.crypto.api import CertificateKeyPairViewSet from authentik.crypto.api import CertificateKeyPairViewSet
from authentik.flows.api import FlowStageBindingViewSet, FlowViewSet, StageViewSet from authentik.flows.api import FlowStageBindingViewSet, FlowViewSet, StageViewSet, FlowCacheViewSet
from authentik.outposts.api import ( from authentik.outposts.api import (
DockerServiceConnectionViewSet, DockerServiceConnectionViewSet,
KubernetesServiceConnectionViewSet, KubernetesServiceConnectionViewSet,
OutpostViewSet, OutpostViewSet,
) )
from authentik.policies.api import PolicyBindingViewSet, PolicyViewSet from authentik.policies.api import PolicyBindingViewSet, PolicyViewSet, PolicyCacheViewSet
from authentik.policies.dummy.api import DummyPolicyViewSet from authentik.policies.dummy.api import DummyPolicyViewSet
from authentik.policies.expiry.api import PasswordExpiryPolicyViewSet from authentik.policies.expiry.api import PasswordExpiryPolicyViewSet
from authentik.policies.expression.api import ExpressionPolicyViewSet from authentik.policies.expression.api import ExpressionPolicyViewSet
@ -82,6 +82,7 @@ router.register(
router.register("outposts/proxy", ProxyOutpostConfigViewSet) router.register("outposts/proxy", ProxyOutpostConfigViewSet)
router.register("flows/instances", FlowViewSet) router.register("flows/instances", FlowViewSet)
router.register("flows/cached", FlowCacheViewSet, basename="flows_cache")
router.register("flows/bindings", FlowStageBindingViewSet) router.register("flows/bindings", FlowStageBindingViewSet)
router.register("crypto/certificatekeypairs", CertificateKeyPairViewSet) router.register("crypto/certificatekeypairs", CertificateKeyPairViewSet)
@ -94,6 +95,7 @@ router.register("sources/saml", SAMLSourceViewSet)
router.register("sources/oauth", OAuthSourceViewSet) router.register("sources/oauth", OAuthSourceViewSet)
router.register("policies/all", PolicyViewSet) router.register("policies/all", PolicyViewSet)
router.register("policies/cached", PolicyCacheViewSet, basename="policies_cache")
router.register("policies/bindings", PolicyBindingViewSet) router.register("policies/bindings", PolicyBindingViewSet)
router.register("policies/expression", ExpressionPolicyViewSet) router.register("policies/expression", ExpressionPolicyViewSet)
router.register("policies/group_membership", GroupMembershipPolicyViewSet) router.register("policies/group_membership", GroupMembershipPolicyViewSet)

View File

@ -1,7 +1,10 @@
"""Flow API Views""" """Flow API Views"""
from django.core.cache import cache from django.core.cache import cache
from rest_framework.serializers import ModelSerializer, SerializerMethodField from rest_framework.mixins import ListModelMixin
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.serializers import ModelSerializer, Serializer, SerializerMethodField
from rest_framework.viewsets import GenericViewSet, ModelViewSet, ReadOnlyModelViewSet
from authentik.flows.models import Flow, FlowStageBinding, Stage from authentik.flows.models import Flow, FlowStageBinding, Stage
from authentik.flows.planner import cache_key from authentik.flows.planner import cache_key
@ -98,3 +101,14 @@ class FlowStageBindingViewSet(ModelViewSet):
queryset = FlowStageBinding.objects.all() queryset = FlowStageBinding.objects.all()
serializer_class = FlowStageBindingSerializer serializer_class = FlowStageBindingSerializer
filterset_fields = "__all__" filterset_fields = "__all__"
class FlowCacheViewSet(ListModelMixin, GenericViewSet):
"""Info about cached flows"""
queryset = Flow.objects.none()
serializer_class = Serializer
def list(self, request: Request) -> Response:
"""Info about cached flows"""
return Response(data={"pagination": {"count": len(cache.keys("flow_*"))}})

View File

@ -1,11 +1,16 @@
"""policy API Views""" """policy API Views"""
from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from drf_yasg2.utils import swagger_auto_schema
from rest_framework.mixins import ListModelMixin
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.serializers import ( from rest_framework.serializers import (
ModelSerializer, ModelSerializer,
PrimaryKeyRelatedField, PrimaryKeyRelatedField, Serializer,
SerializerMethodField, SerializerMethodField,
) )
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet from rest_framework.viewsets import GenericViewSet, ModelViewSet, ReadOnlyModelViewSet
from authentik.policies.forms import GENERAL_FIELDS from authentik.policies.forms import GENERAL_FIELDS
from authentik.policies.models import Policy, PolicyBinding, PolicyBindingModel from authentik.policies.models import Policy, PolicyBinding, PolicyBindingModel
@ -102,3 +107,14 @@ class PolicyBindingViewSet(ModelViewSet):
serializer_class = PolicyBindingSerializer serializer_class = PolicyBindingSerializer
filterset_fields = ["policy", "target", "enabled", "order", "timeout"] filterset_fields = ["policy", "target", "enabled", "order", "timeout"]
search_fields = ["policy__name"] search_fields = ["policy__name"]
class PolicyCacheViewSet(ListModelMixin, GenericViewSet):
"""Info about cached policies"""
queryset = Policy.objects.none()
serializer_class = Serializer
def list(self, request: Request) -> Response:
"""Info about cached policies"""
return Response(data={"pagination": {"count": len(cache.keys("policy_*"))}})