authentik.policies.event_matcher.models
Event Matcher models
1"""Event Matcher models""" 2 3from itertools import chain 4 5from django.db import models 6from django.utils.translation import gettext_lazy as _ 7from djangoql.queryset import apply_search 8from rest_framework.serializers import BaseSerializer 9from structlog.stdlib import get_logger 10 11from authentik.api.search.ql import BaseSchema 12from authentik.events.models import Event, event_actions 13from authentik.policies.models import Policy 14from authentik.policies.types import PolicyRequest, PolicyResult 15 16LOGGER = get_logger() 17 18 19class EventMatcherPolicy(Policy): 20 """Passes when Event matches selected criteria.""" 21 22 query = models.TextField( 23 null=True, 24 default=None, 25 ) 26 action = models.TextField( 27 choices=event_actions, 28 null=True, 29 default=None, 30 help_text=_( 31 "Match created events with this action type. " 32 "When left empty, all action types will be matched." 33 ), 34 ) 35 app = models.TextField( 36 null=True, 37 default=None, 38 help_text=_( 39 "Match events created by selected application. " 40 "When left empty, all applications are matched." 41 ), 42 ) 43 model = models.TextField( 44 null=True, 45 default=None, 46 help_text=_( 47 "Match events created by selected model. " 48 "When left empty, all models are matched. When an app is selected, " 49 "all the application's models are matched." 50 ), 51 ) 52 client_ip = models.TextField( 53 null=True, 54 default=None, 55 help_text=_( 56 "Matches Event's Client IP (strict matching, " 57 "for network matching use an Expression Policy)" 58 ), 59 ) 60 61 @property 62 def serializer(self) -> type[BaseSerializer]: 63 from authentik.policies.event_matcher.api import EventMatcherPolicySerializer 64 65 return EventMatcherPolicySerializer 66 67 @property 68 def component(self) -> str: 69 return "ak-policy-event-matcher-form" 70 71 def passes(self, request: PolicyRequest) -> PolicyResult: 72 if "event" not in request.context: 73 return PolicyResult(False) 74 event: Event = request.context["event"] 75 matches: list[PolicyResult] = [] 76 messages = [] 77 checks = [ 78 self.passes_query, 79 self.passes_action, 80 self.passes_client_ip, 81 self.passes_app, 82 self.passes_model, 83 ] 84 for checker in checks: 85 result = checker(request, event) 86 if result is None: 87 continue 88 LOGGER.debug( 89 "Event matcher check result", 90 checker=checker.__name__, 91 result=result, 92 ) 93 # Checks return their "matched" message whether or not they matched 94 if not result.passing: 95 result.messages = () 96 matches.append(result) 97 passing = all(x.passing for x in matches) 98 messages = chain(*[x.messages for x in matches]) 99 result = PolicyResult(passing, *messages) 100 result.source_results = matches 101 return result 102 103 def passes_query(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 104 """Check AKQL query""" 105 if not self.query: 106 return None 107 from authentik.events.api.events import EventViewSet 108 109 class InlineSchema(BaseSchema): 110 def get_fields(self, model): 111 return EventViewSet().get_ql_fields() 112 113 print(Event.objects.filter(pk=event.pk)) 114 qs = apply_search(Event.objects.filter(pk=event.pk), self.query, InlineSchema) 115 return PolicyResult(qs.exists(), str(_("Query matched."))) 116 117 def passes_action(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 118 """Check if `self.action` matches""" 119 if self.action is None: 120 return None 121 return PolicyResult(self.action == event.action, str(_("Action matched."))) 122 123 def passes_client_ip(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 124 """Check if `self.client_ip` matches""" 125 if self.client_ip is None: 126 return None 127 return PolicyResult(self.client_ip == event.client_ip, str(_("Client IP matched."))) 128 129 def passes_app(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 130 """Check if `self.app` matches""" 131 if self.app is None: 132 return None 133 return PolicyResult(self.app == event.app, str(_("App matched."))) 134 135 def passes_model(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 136 """Check if `self.model` is set, and pass if it matches the event's model""" 137 if self.model is None: 138 return None 139 event_model_info = event.context.get("model", {}) 140 event_model = f"{event_model_info.get('app')}.{event_model_info.get('model_name')}" 141 return PolicyResult(event_model == self.model, str(_("Model matched."))) 142 143 class Meta(Policy.PolicyMeta): 144 verbose_name = _("Event Matcher Policy") 145 verbose_name_plural = _("Event Matcher Policies")
20class EventMatcherPolicy(Policy): 21 """Passes when Event matches selected criteria.""" 22 23 query = models.TextField( 24 null=True, 25 default=None, 26 ) 27 action = models.TextField( 28 choices=event_actions, 29 null=True, 30 default=None, 31 help_text=_( 32 "Match created events with this action type. " 33 "When left empty, all action types will be matched." 34 ), 35 ) 36 app = models.TextField( 37 null=True, 38 default=None, 39 help_text=_( 40 "Match events created by selected application. " 41 "When left empty, all applications are matched." 42 ), 43 ) 44 model = models.TextField( 45 null=True, 46 default=None, 47 help_text=_( 48 "Match events created by selected model. " 49 "When left empty, all models are matched. When an app is selected, " 50 "all the application's models are matched." 51 ), 52 ) 53 client_ip = models.TextField( 54 null=True, 55 default=None, 56 help_text=_( 57 "Matches Event's Client IP (strict matching, " 58 "for network matching use an Expression Policy)" 59 ), 60 ) 61 62 @property 63 def serializer(self) -> type[BaseSerializer]: 64 from authentik.policies.event_matcher.api import EventMatcherPolicySerializer 65 66 return EventMatcherPolicySerializer 67 68 @property 69 def component(self) -> str: 70 return "ak-policy-event-matcher-form" 71 72 def passes(self, request: PolicyRequest) -> PolicyResult: 73 if "event" not in request.context: 74 return PolicyResult(False) 75 event: Event = request.context["event"] 76 matches: list[PolicyResult] = [] 77 messages = [] 78 checks = [ 79 self.passes_query, 80 self.passes_action, 81 self.passes_client_ip, 82 self.passes_app, 83 self.passes_model, 84 ] 85 for checker in checks: 86 result = checker(request, event) 87 if result is None: 88 continue 89 LOGGER.debug( 90 "Event matcher check result", 91 checker=checker.__name__, 92 result=result, 93 ) 94 # Checks return their "matched" message whether or not they matched 95 if not result.passing: 96 result.messages = () 97 matches.append(result) 98 passing = all(x.passing for x in matches) 99 messages = chain(*[x.messages for x in matches]) 100 result = PolicyResult(passing, *messages) 101 result.source_results = matches 102 return result 103 104 def passes_query(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 105 """Check AKQL query""" 106 if not self.query: 107 return None 108 from authentik.events.api.events import EventViewSet 109 110 class InlineSchema(BaseSchema): 111 def get_fields(self, model): 112 return EventViewSet().get_ql_fields() 113 114 print(Event.objects.filter(pk=event.pk)) 115 qs = apply_search(Event.objects.filter(pk=event.pk), self.query, InlineSchema) 116 return PolicyResult(qs.exists(), str(_("Query matched."))) 117 118 def passes_action(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 119 """Check if `self.action` matches""" 120 if self.action is None: 121 return None 122 return PolicyResult(self.action == event.action, str(_("Action matched."))) 123 124 def passes_client_ip(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 125 """Check if `self.client_ip` matches""" 126 if self.client_ip is None: 127 return None 128 return PolicyResult(self.client_ip == event.client_ip, str(_("Client IP matched."))) 129 130 def passes_app(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 131 """Check if `self.app` matches""" 132 if self.app is None: 133 return None 134 return PolicyResult(self.app == event.app, str(_("App matched."))) 135 136 def passes_model(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 137 """Check if `self.model` is set, and pass if it matches the event's model""" 138 if self.model is None: 139 return None 140 event_model_info = event.context.get("model", {}) 141 event_model = f"{event_model_info.get('app')}.{event_model_info.get('model_name')}" 142 return PolicyResult(event_model == self.model, str(_("Model matched."))) 143 144 class Meta(Policy.PolicyMeta): 145 verbose_name = _("Event Matcher Policy") 146 verbose_name_plural = _("Event Matcher Policies")
Passes when Event matches selected criteria.
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
62 @property 63 def serializer(self) -> type[BaseSerializer]: 64 from authentik.policies.event_matcher.api import EventMatcherPolicySerializer 65 66 return EventMatcherPolicySerializer
Get serializer for this model
72 def passes(self, request: PolicyRequest) -> PolicyResult: 73 if "event" not in request.context: 74 return PolicyResult(False) 75 event: Event = request.context["event"] 76 matches: list[PolicyResult] = [] 77 messages = [] 78 checks = [ 79 self.passes_query, 80 self.passes_action, 81 self.passes_client_ip, 82 self.passes_app, 83 self.passes_model, 84 ] 85 for checker in checks: 86 result = checker(request, event) 87 if result is None: 88 continue 89 LOGGER.debug( 90 "Event matcher check result", 91 checker=checker.__name__, 92 result=result, 93 ) 94 # Checks return their "matched" message whether or not they matched 95 if not result.passing: 96 result.messages = () 97 matches.append(result) 98 passing = all(x.passing for x in matches) 99 messages = chain(*[x.messages for x in matches]) 100 result = PolicyResult(passing, *messages) 101 result.source_results = matches 102 return result
Check if request passes this policy
104 def passes_query(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 105 """Check AKQL query""" 106 if not self.query: 107 return None 108 from authentik.events.api.events import EventViewSet 109 110 class InlineSchema(BaseSchema): 111 def get_fields(self, model): 112 return EventViewSet().get_ql_fields() 113 114 print(Event.objects.filter(pk=event.pk)) 115 qs = apply_search(Event.objects.filter(pk=event.pk), self.query, InlineSchema) 116 return PolicyResult(qs.exists(), str(_("Query matched.")))
Check AKQL query
118 def passes_action(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 119 """Check if `self.action` matches""" 120 if self.action is None: 121 return None 122 return PolicyResult(self.action == event.action, str(_("Action matched.")))
Check if self.action matches
124 def passes_client_ip(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 125 """Check if `self.client_ip` matches""" 126 if self.client_ip is None: 127 return None 128 return PolicyResult(self.client_ip == event.client_ip, str(_("Client IP matched.")))
Check if self.client_ip matches
130 def passes_app(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 131 """Check if `self.app` matches""" 132 if self.app is None: 133 return None 134 return PolicyResult(self.app == event.app, str(_("App matched.")))
Check if self.app matches
136 def passes_model(self, request: PolicyRequest, event: Event) -> PolicyResult | None: 137 """Check if `self.model` is set, and pass if it matches the event's model""" 138 if self.model is None: 139 return None 140 event_model_info = event.context.get("model", {}) 141 event_model = f"{event_model_info.get('app')}.{event_model_info.get('model_name')}" 142 return PolicyResult(event_model == self.model, str(_("Model matched.")))
Check if self.model is set, and pass if it matches the event's model
Method descriptor with partial application of the given arguments and keywords.
Supports wrapping existing descriptors and handles non-descriptor callables as instance methods.
Accessor to the related object on the forward side of a one-to-one relation.
In the example::
class Restaurant(Model):
place = OneToOneField(Place, related_name='restaurant')
Restaurant.place is a ForwardOneToOneDescriptor instance.
Inherited Members
- authentik.policies.models.Policy
- policy_uuid
- name
- execution_logging
- objects
- PolicyMeta
- created
- last_updated
- get_next_by_created
- get_previous_by_created
- get_next_by_last_updated
- get_previous_by_last_updated
- bindings
- dummypolicy
- eventmatcherpolicy
- passwordexpirypolicy
- reputationpolicy
- expressionpolicy
- geoippolicy
- promptstage_set
- promptstagevalidationpolicy_set
- passwordpolicy
- uniquepasswordpolicy
The requested object does not exist
The query returned multiple objects when only one was expected.